image.c 121 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725
  1. /*
  2. * ***** BEGIN GPL LICENSE BLOCK *****
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License
  6. * as published by the Free Software Foundation; either version 2
  7. * of the License, or (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software Foundation,
  16. * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  17. *
  18. * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
  19. * All rights reserved.
  20. *
  21. * Contributor(s): Blender Foundation, 2006, full recode
  22. *
  23. * ***** END GPL LICENSE BLOCK *****
  24. */
  25. /** \file blender/blenkernel/intern/image.c
  26. * \ingroup bke
  27. */
  28. #include <stdio.h>
  29. #include <string.h>
  30. #include <fcntl.h>
  31. #include <math.h>
  32. #ifndef WIN32
  33. # include <unistd.h>
  34. #else
  35. # include <io.h>
  36. #endif
  37. #include <time.h>
  38. #include "MEM_guardedalloc.h"
  39. #include "IMB_colormanagement.h"
  40. #include "IMB_imbuf_types.h"
  41. #include "IMB_imbuf.h"
  42. #include "IMB_moviecache.h"
  43. #ifdef WITH_OPENEXR
  44. # include "intern/openexr/openexr_multi.h"
  45. #endif
  46. #include "DNA_packedFile_types.h"
  47. #include "DNA_scene_types.h"
  48. #include "DNA_object_types.h"
  49. #include "DNA_camera_types.h"
  50. #include "DNA_sequence_types.h"
  51. #include "DNA_brush_types.h"
  52. #include "DNA_mesh_types.h"
  53. #include "DNA_meshdata_types.h"
  54. #include "BLI_blenlib.h"
  55. #include "BLI_math_vector.h"
  56. #include "BLI_mempool.h"
  57. #include "BLI_threads.h"
  58. #include "BLI_timecode.h" /* for stamp timecode format */
  59. #include "BLI_utildefines.h"
  60. #include "BKE_bmfont.h"
  61. #include "BKE_colortools.h"
  62. #include "BKE_global.h"
  63. #include "BKE_icons.h"
  64. #include "BKE_image.h"
  65. #include "BKE_library.h"
  66. #include "BKE_library_query.h"
  67. #include "BKE_library_remap.h"
  68. #include "BKE_main.h"
  69. #include "BKE_packedFile.h"
  70. #include "BKE_report.h"
  71. #include "BKE_scene.h"
  72. #include "BKE_node.h"
  73. #include "BKE_sequencer.h" /* seq_foreground_frame_get() */
  74. #include "BLF_api.h"
  75. #include "PIL_time.h"
  76. #include "RE_pipeline.h"
  77. #include "GPU_draw.h"
  78. #include "BLI_sys_types.h" // for intptr_t support
  79. /* for image user iteration */
  80. #include "DNA_node_types.h"
  81. #include "DNA_space_types.h"
  82. #include "DNA_screen_types.h"
  83. #include "DNA_view3d_types.h"
  84. static SpinLock image_spin;
  85. /* prototypes */
  86. static int image_num_files(struct Image *ima);
  87. static ImBuf *image_acquire_ibuf(Image *ima, ImageUser *iuser, void **r_lock);
  88. static void image_update_views_format(Image *ima, ImageUser *iuser);
  89. static void image_add_view(Image *ima, const char *viewname, const char *filepath);
  90. /* max int, to indicate we don't store sequences in ibuf */
  91. #define IMA_NO_INDEX 0x7FEFEFEF
  92. /* quick lookup: supports 1 million frames, thousand passes */
  93. #define IMA_MAKE_INDEX(frame, index) (((frame) << 10) + (index))
  94. #define IMA_INDEX_FRAME(index) ((index) >> 10)
  95. /*
  96. #define IMA_INDEX_PASS(index) (index & ~1023)
  97. */
  98. /* ******** IMAGE CACHE ************* */
  99. typedef struct ImageCacheKey {
  100. int index;
  101. } ImageCacheKey;
  102. static unsigned int imagecache_hashhash(const void *key_v)
  103. {
  104. const ImageCacheKey *key = key_v;
  105. return key->index;
  106. }
  107. static bool imagecache_hashcmp(const void *a_v, const void *b_v)
  108. {
  109. const ImageCacheKey *a = a_v;
  110. const ImageCacheKey *b = b_v;
  111. return (a->index != b->index);
  112. }
  113. static void imagecache_keydata(void *userkey, int *framenr, int *proxy, int *render_flags)
  114. {
  115. ImageCacheKey *key = userkey;
  116. *framenr = IMA_INDEX_FRAME(key->index);
  117. *proxy = IMB_PROXY_NONE;
  118. *render_flags = 0;
  119. }
  120. static void imagecache_put(Image *image, int index, ImBuf *ibuf)
  121. {
  122. ImageCacheKey key;
  123. if (image->cache == NULL) {
  124. // char cache_name[64];
  125. // BLI_snprintf(cache_name, sizeof(cache_name), "Image Datablock %s", image->id.name);
  126. image->cache = IMB_moviecache_create("Image Datablock Cache", sizeof(ImageCacheKey),
  127. imagecache_hashhash, imagecache_hashcmp);
  128. IMB_moviecache_set_getdata_callback(image->cache, imagecache_keydata);
  129. }
  130. key.index = index;
  131. IMB_moviecache_put(image->cache, &key, ibuf);
  132. }
  133. static struct ImBuf *imagecache_get(Image *image, int index)
  134. {
  135. if (image->cache) {
  136. ImageCacheKey key;
  137. key.index = index;
  138. return IMB_moviecache_get(image->cache, &key);
  139. }
  140. return NULL;
  141. }
  142. void BKE_images_init(void)
  143. {
  144. BLI_spin_init(&image_spin);
  145. }
  146. void BKE_images_exit(void)
  147. {
  148. BLI_spin_end(&image_spin);
  149. }
  150. /* ******** IMAGE PROCESSING ************* */
  151. static void de_interlace_ng(struct ImBuf *ibuf) /* neogeo fields */
  152. {
  153. struct ImBuf *tbuf1, *tbuf2;
  154. if (ibuf == NULL) return;
  155. if (ibuf->flags & IB_fields) return;
  156. ibuf->flags |= IB_fields;
  157. if (ibuf->rect) {
  158. /* make copies */
  159. tbuf1 = IMB_allocImBuf(ibuf->x, (ibuf->y >> 1), (unsigned char)32, (int)IB_rect);
  160. tbuf2 = IMB_allocImBuf(ibuf->x, (ibuf->y >> 1), (unsigned char)32, (int)IB_rect);
  161. ibuf->x *= 2;
  162. IMB_rectcpy(tbuf1, ibuf, 0, 0, 0, 0, ibuf->x, ibuf->y);
  163. IMB_rectcpy(tbuf2, ibuf, 0, 0, tbuf2->x, 0, ibuf->x, ibuf->y);
  164. ibuf->x /= 2;
  165. IMB_rectcpy(ibuf, tbuf1, 0, 0, 0, 0, tbuf1->x, tbuf1->y);
  166. IMB_rectcpy(ibuf, tbuf2, 0, tbuf2->y, 0, 0, tbuf2->x, tbuf2->y);
  167. IMB_freeImBuf(tbuf1);
  168. IMB_freeImBuf(tbuf2);
  169. }
  170. ibuf->y /= 2;
  171. }
  172. static void de_interlace_st(struct ImBuf *ibuf) /* standard fields */
  173. {
  174. struct ImBuf *tbuf1, *tbuf2;
  175. if (ibuf == NULL) return;
  176. if (ibuf->flags & IB_fields) return;
  177. ibuf->flags |= IB_fields;
  178. if (ibuf->rect) {
  179. /* make copies */
  180. tbuf1 = IMB_allocImBuf(ibuf->x, (ibuf->y >> 1), (unsigned char)32, IB_rect);
  181. tbuf2 = IMB_allocImBuf(ibuf->x, (ibuf->y >> 1), (unsigned char)32, IB_rect);
  182. ibuf->x *= 2;
  183. IMB_rectcpy(tbuf1, ibuf, 0, 0, 0, 0, ibuf->x, ibuf->y);
  184. IMB_rectcpy(tbuf2, ibuf, 0, 0, tbuf2->x, 0, ibuf->x, ibuf->y);
  185. ibuf->x /= 2;
  186. IMB_rectcpy(ibuf, tbuf2, 0, 0, 0, 0, tbuf2->x, tbuf2->y);
  187. IMB_rectcpy(ibuf, tbuf1, 0, tbuf2->y, 0, 0, tbuf1->x, tbuf1->y);
  188. IMB_freeImBuf(tbuf1);
  189. IMB_freeImBuf(tbuf2);
  190. }
  191. ibuf->y /= 2;
  192. }
  193. void BKE_image_de_interlace(Image *ima, int odd)
  194. {
  195. ImBuf *ibuf = BKE_image_acquire_ibuf(ima, NULL, NULL);
  196. if (ibuf) {
  197. if (odd)
  198. de_interlace_st(ibuf);
  199. else
  200. de_interlace_ng(ibuf);
  201. }
  202. BKE_image_release_ibuf(ima, ibuf, NULL);
  203. }
  204. /* ***************** ALLOC & FREE, DATA MANAGING *************** */
  205. static void image_free_cached_frames(Image *image)
  206. {
  207. if (image->cache) {
  208. IMB_moviecache_free(image->cache);
  209. image->cache = NULL;
  210. }
  211. }
  212. static void image_free_packedfiles(Image *ima)
  213. {
  214. while (ima->packedfiles.last) {
  215. ImagePackedFile *imapf = ima->packedfiles.last;
  216. if (imapf->packedfile) {
  217. freePackedFile(imapf->packedfile);
  218. }
  219. BLI_remlink(&ima->packedfiles, imapf);
  220. MEM_freeN(imapf);
  221. }
  222. }
  223. void BKE_image_free_packedfiles(Image *ima)
  224. {
  225. image_free_packedfiles(ima);
  226. }
  227. void BKE_image_free_views(Image *image)
  228. {
  229. BLI_freelistN(&image->views);
  230. }
  231. static void image_free_anims(Image *ima)
  232. {
  233. while (ima->anims.last) {
  234. ImageAnim *ia = ima->anims.last;
  235. if (ia->anim) {
  236. IMB_free_anim(ia->anim);
  237. ia->anim = NULL;
  238. }
  239. BLI_remlink(&ima->anims, ia);
  240. MEM_freeN(ia);
  241. }
  242. }
  243. /**
  244. * Simply free the image data from memory,
  245. * on display the image can load again (except for render buffers).
  246. */
  247. void BKE_image_free_buffers_ex(Image *ima, bool do_lock)
  248. {
  249. if (do_lock) {
  250. BLI_spin_lock(&image_spin);
  251. }
  252. image_free_cached_frames(ima);
  253. image_free_anims(ima);
  254. if (ima->rr) {
  255. RE_FreeRenderResult(ima->rr);
  256. ima->rr = NULL;
  257. }
  258. if (!G.background) {
  259. /* Background mode doesn't use opnegl,
  260. * so we can avoid freeing GPU images and save some
  261. * time by skipping mutex lock.
  262. */
  263. GPU_free_image(ima);
  264. }
  265. ima->ok = IMA_OK;
  266. if (do_lock) {
  267. BLI_spin_unlock(&image_spin);
  268. }
  269. }
  270. void BKE_image_free_buffers(Image *ima)
  271. {
  272. BKE_image_free_buffers_ex(ima, false);
  273. }
  274. /** Free (or release) any data used by this image (does not free the image itself). */
  275. void BKE_image_free(Image *ima)
  276. {
  277. int a;
  278. /* Also frees animdata. */
  279. BKE_image_free_buffers(ima);
  280. image_free_packedfiles(ima);
  281. for (a = 0; a < IMA_MAX_RENDER_SLOT; a++) {
  282. if (ima->renders[a]) {
  283. RE_FreeRenderResult(ima->renders[a]);
  284. ima->renders[a] = NULL;
  285. }
  286. }
  287. BKE_image_free_views(ima);
  288. MEM_SAFE_FREE(ima->stereo3d_format);
  289. BKE_icon_id_delete(&ima->id);
  290. BKE_previewimg_free(&ima->preview);
  291. }
  292. /* only image block itself */
  293. static void image_init(Image *ima, short source, short type)
  294. {
  295. BLI_assert(MEMCMP_STRUCT_OFS_IS_ZERO(ima, id));
  296. ima->ok = IMA_OK;
  297. ima->xrep = ima->yrep = 1;
  298. ima->aspx = ima->aspy = 1.0;
  299. ima->gen_x = 1024; ima->gen_y = 1024;
  300. ima->gen_type = IMA_GENTYPE_GRID;
  301. ima->source = source;
  302. ima->type = type;
  303. if (source == IMA_SRC_VIEWER)
  304. ima->flag |= IMA_VIEW_AS_RENDER;
  305. BKE_color_managed_colorspace_settings_init(&ima->colorspace_settings);
  306. ima->stereo3d_format = MEM_callocN(sizeof(Stereo3dFormat), "Image Stereo Format");
  307. }
  308. void BKE_image_init(struct Image *image)
  309. {
  310. if (image) {
  311. image_init(image, IMA_SRC_GENERATED, IMA_TYPE_UV_TEST);
  312. }
  313. }
  314. static Image *image_alloc(Main *bmain, const char *name, short source, short type)
  315. {
  316. Image *ima;
  317. ima = BKE_libblock_alloc(bmain, ID_IM, name);
  318. if (ima) {
  319. image_init(ima, source, type);
  320. }
  321. return ima;
  322. }
  323. /* Get the ibuf from an image cache by it's index and frame.
  324. * Local use here only.
  325. *
  326. * Returns referenced image buffer if it exists, callee is to
  327. * call IMB_freeImBuf to de-reference the image buffer after
  328. * it's done handling it.
  329. */
  330. static ImBuf *image_get_cached_ibuf_for_index_frame(Image *ima, int index, int frame)
  331. {
  332. if (index != IMA_NO_INDEX) {
  333. index = IMA_MAKE_INDEX(frame, index);
  334. }
  335. return imagecache_get(ima, index);
  336. }
  337. /* no ima->ibuf anymore, but listbase */
  338. static void image_assign_ibuf(Image *ima, ImBuf *ibuf, int index, int frame)
  339. {
  340. if (ibuf) {
  341. if (index != IMA_NO_INDEX)
  342. index = IMA_MAKE_INDEX(frame, index);
  343. imagecache_put(ima, index, ibuf);
  344. }
  345. }
  346. static void copy_image_packedfiles(ListBase *lb_dst, const ListBase *lb_src)
  347. {
  348. const ImagePackedFile *imapf_src;
  349. BLI_listbase_clear(lb_dst);
  350. for (imapf_src = lb_src->first; imapf_src; imapf_src = imapf_src->next) {
  351. ImagePackedFile *imapf_dst = MEM_mallocN(sizeof(ImagePackedFile), "Image Packed Files (copy)");
  352. BLI_strncpy(imapf_dst->filepath, imapf_src->filepath, sizeof(imapf_dst->filepath));
  353. if (imapf_src->packedfile)
  354. imapf_dst->packedfile = dupPackedFile(imapf_src->packedfile);
  355. BLI_addtail(lb_dst, imapf_dst);
  356. }
  357. }
  358. /* empty image block, of similar type and filename */
  359. Image *BKE_image_copy(Main *bmain, const Image *ima)
  360. {
  361. Image *nima = image_alloc(bmain, ima->id.name + 2, ima->source, ima->type);
  362. BLI_strncpy(nima->name, ima->name, sizeof(ima->name));
  363. nima->flag = ima->flag;
  364. nima->tpageflag = ima->tpageflag;
  365. nima->gen_x = ima->gen_x;
  366. nima->gen_y = ima->gen_y;
  367. nima->gen_type = ima->gen_type;
  368. copy_v4_v4(nima->gen_color, ima->gen_color);
  369. nima->animspeed = ima->animspeed;
  370. nima->aspx = ima->aspx;
  371. nima->aspy = ima->aspy;
  372. BKE_color_managed_colorspace_settings_copy(&nima->colorspace_settings, &ima->colorspace_settings);
  373. copy_image_packedfiles(&nima->packedfiles, &ima->packedfiles);
  374. /* nima->stere3d_format is already allocated by image_alloc... */
  375. *nima->stereo3d_format = *ima->stereo3d_format;
  376. BLI_duplicatelist(&nima->views, &ima->views);
  377. BKE_previewimg_id_copy(&nima->id, &ima->id);
  378. BKE_id_copy_ensure_local(bmain, &ima->id, &nima->id);
  379. return nima;
  380. }
  381. void BKE_image_make_local(Main *bmain, Image *ima, const bool lib_local)
  382. {
  383. BKE_id_make_local_generic(bmain, &ima->id, true, lib_local);
  384. }
  385. void BKE_image_merge(Image *dest, Image *source)
  386. {
  387. /* sanity check */
  388. if (dest && source && dest != source) {
  389. BLI_spin_lock(&image_spin);
  390. if (source->cache != NULL) {
  391. struct MovieCacheIter *iter;
  392. iter = IMB_moviecacheIter_new(source->cache);
  393. while (!IMB_moviecacheIter_done(iter)) {
  394. ImBuf *ibuf = IMB_moviecacheIter_getImBuf(iter);
  395. ImageCacheKey *key = IMB_moviecacheIter_getUserKey(iter);
  396. imagecache_put(dest, key->index, ibuf);
  397. IMB_moviecacheIter_step(iter);
  398. }
  399. IMB_moviecacheIter_free(iter);
  400. }
  401. BLI_spin_unlock(&image_spin);
  402. BKE_libblock_free(G.main, source);
  403. }
  404. }
  405. /* note, we could be clever and scale all imbuf's but since some are mipmaps its not so simple */
  406. bool BKE_image_scale(Image *image, int width, int height)
  407. {
  408. ImBuf *ibuf;
  409. void *lock;
  410. ibuf = BKE_image_acquire_ibuf(image, NULL, &lock);
  411. if (ibuf) {
  412. IMB_scaleImBuf(ibuf, width, height);
  413. ibuf->userflags |= IB_BITMAPDIRTY;
  414. }
  415. BKE_image_release_ibuf(image, ibuf, lock);
  416. return (ibuf != NULL);
  417. }
  418. bool BKE_image_has_bindcode(Image *ima)
  419. {
  420. bool has_bindcode = false;
  421. for (int i = 0; i < TEXTARGET_COUNT; i++) {
  422. if (ima->bindcode[i]) {
  423. has_bindcode = true;
  424. break;
  425. }
  426. }
  427. return has_bindcode;
  428. }
  429. static void image_init_color_management(Image *ima)
  430. {
  431. ImBuf *ibuf;
  432. char name[FILE_MAX];
  433. BKE_image_user_file_path(NULL, ima, name);
  434. /* will set input color space to image format default's */
  435. ibuf = IMB_loadiffname(name, IB_test | IB_alphamode_detect, ima->colorspace_settings.name);
  436. if (ibuf) {
  437. if (ibuf->flags & IB_alphamode_premul)
  438. ima->alpha_mode = IMA_ALPHA_PREMUL;
  439. else
  440. ima->alpha_mode = IMA_ALPHA_STRAIGHT;
  441. IMB_freeImBuf(ibuf);
  442. }
  443. }
  444. char BKE_image_alpha_mode_from_extension_ex(const char *filepath)
  445. {
  446. if (BLI_testextensie_n(filepath, ".exr", ".cin", ".dpx", ".hdr", NULL)) {
  447. return IMA_ALPHA_PREMUL;
  448. }
  449. else {
  450. return IMA_ALPHA_STRAIGHT;
  451. }
  452. }
  453. void BKE_image_alpha_mode_from_extension(Image *image)
  454. {
  455. image->alpha_mode = BKE_image_alpha_mode_from_extension_ex(image->name);
  456. }
  457. Image *BKE_image_load(Main *bmain, const char *filepath)
  458. {
  459. Image *ima;
  460. int file;
  461. char str[FILE_MAX];
  462. BLI_strncpy(str, filepath, sizeof(str));
  463. BLI_path_abs(str, bmain->name);
  464. /* exists? */
  465. file = BLI_open(str, O_BINARY | O_RDONLY, 0);
  466. if (file == -1)
  467. return NULL;
  468. close(file);
  469. ima = image_alloc(bmain, BLI_path_basename(filepath), IMA_SRC_FILE, IMA_TYPE_IMAGE);
  470. BLI_strncpy(ima->name, filepath, sizeof(ima->name));
  471. if (BLI_testextensie_array(filepath, imb_ext_movie))
  472. ima->source = IMA_SRC_MOVIE;
  473. image_init_color_management(ima);
  474. return ima;
  475. }
  476. /* checks if image was already loaded, then returns same image */
  477. /* otherwise creates new. */
  478. /* does not load ibuf itself */
  479. /* pass on optional frame for #name images */
  480. Image *BKE_image_load_exists_ex(const char *filepath, bool *r_exists)
  481. {
  482. Image *ima;
  483. char str[FILE_MAX], strtest[FILE_MAX];
  484. BLI_strncpy(str, filepath, sizeof(str));
  485. BLI_path_abs(str, G.main->name);
  486. /* first search an identical filepath */
  487. for (ima = G.main->image.first; ima; ima = ima->id.next) {
  488. if (ima->source != IMA_SRC_VIEWER && ima->source != IMA_SRC_GENERATED) {
  489. BLI_strncpy(strtest, ima->name, sizeof(ima->name));
  490. BLI_path_abs(strtest, ID_BLEND_PATH(G.main, &ima->id));
  491. if (BLI_path_cmp(strtest, str) == 0) {
  492. if ((BKE_image_has_anim(ima) == false) ||
  493. (ima->id.us == 0))
  494. {
  495. id_us_plus(&ima->id); /* officially should not, it doesn't link here! */
  496. if (ima->ok == 0)
  497. ima->ok = IMA_OK;
  498. if (r_exists)
  499. *r_exists = true;
  500. return ima;
  501. }
  502. }
  503. }
  504. }
  505. if (r_exists)
  506. *r_exists = false;
  507. return BKE_image_load(G.main, filepath);
  508. }
  509. Image *BKE_image_load_exists(const char *filepath)
  510. {
  511. return BKE_image_load_exists_ex(filepath, NULL);
  512. }
  513. static ImBuf *add_ibuf_size(unsigned int width, unsigned int height, const char *name, int depth, int floatbuf, short gen_type,
  514. const float color[4], ColorManagedColorspaceSettings *colorspace_settings)
  515. {
  516. ImBuf *ibuf;
  517. unsigned char *rect = NULL;
  518. float *rect_float = NULL;
  519. if (floatbuf) {
  520. ibuf = IMB_allocImBuf(width, height, depth, IB_rectfloat);
  521. if (colorspace_settings->name[0] == '\0') {
  522. const char *colorspace = IMB_colormanagement_role_colorspace_name_get(COLOR_ROLE_DEFAULT_FLOAT);
  523. BLI_strncpy(colorspace_settings->name, colorspace, sizeof(colorspace_settings->name));
  524. }
  525. if (ibuf != NULL) {
  526. rect_float = ibuf->rect_float;
  527. IMB_colormanagement_check_is_data(ibuf, colorspace_settings->name);
  528. }
  529. }
  530. else {
  531. ibuf = IMB_allocImBuf(width, height, depth, IB_rect);
  532. if (colorspace_settings->name[0] == '\0') {
  533. const char *colorspace = IMB_colormanagement_role_colorspace_name_get(COLOR_ROLE_DEFAULT_BYTE);
  534. BLI_strncpy(colorspace_settings->name, colorspace, sizeof(colorspace_settings->name));
  535. }
  536. if (ibuf != NULL) {
  537. rect = (unsigned char *)ibuf->rect;
  538. IMB_colormanagement_assign_rect_colorspace(ibuf, colorspace_settings->name);
  539. }
  540. }
  541. if (!ibuf) {
  542. return NULL;
  543. }
  544. BLI_strncpy(ibuf->name, name, sizeof(ibuf->name));
  545. ibuf->userflags |= IB_BITMAPDIRTY;
  546. switch (gen_type) {
  547. case IMA_GENTYPE_GRID:
  548. BKE_image_buf_fill_checker(rect, rect_float, width, height);
  549. break;
  550. case IMA_GENTYPE_GRID_COLOR:
  551. BKE_image_buf_fill_checker_color(rect, rect_float, width, height);
  552. break;
  553. default:
  554. BKE_image_buf_fill_color(rect, rect_float, width, height, color);
  555. break;
  556. }
  557. return ibuf;
  558. }
  559. /* adds new image block, creates ImBuf and initializes color */
  560. Image *BKE_image_add_generated(
  561. Main *bmain, unsigned int width, unsigned int height, const char *name,
  562. int depth, int floatbuf, short gen_type, const float color[4], const bool stereo3d)
  563. {
  564. /* on save, type is changed to FILE in editsima.c */
  565. Image *ima = image_alloc(bmain, name, IMA_SRC_GENERATED, IMA_TYPE_UV_TEST);
  566. if (ima) {
  567. int view_id;
  568. const char *names[2] = {STEREO_LEFT_NAME, STEREO_RIGHT_NAME};
  569. /* BLI_strncpy(ima->name, name, FILE_MAX); */ /* don't do this, this writes in ain invalid filepath! */
  570. ima->gen_x = width;
  571. ima->gen_y = height;
  572. ima->gen_type = gen_type;
  573. ima->gen_flag |= (floatbuf ? IMA_GEN_FLOAT : 0);
  574. ima->gen_depth = depth;
  575. copy_v4_v4(ima->gen_color, color);
  576. for (view_id = 0; view_id < 2; view_id++) {
  577. ImBuf *ibuf;
  578. ibuf = add_ibuf_size(width, height, ima->name, depth, floatbuf, gen_type, color, &ima->colorspace_settings);
  579. image_assign_ibuf(ima, ibuf, stereo3d ? view_id : IMA_NO_INDEX, 0);
  580. /* image_assign_ibuf puts buffer to the cache, which increments user counter. */
  581. IMB_freeImBuf(ibuf);
  582. if (!stereo3d) break;
  583. image_add_view(ima, names[view_id], "");
  584. }
  585. ima->ok = IMA_OK_LOADED;
  586. }
  587. return ima;
  588. }
  589. /* Create an image image from ibuf. The refcount of ibuf is increased,
  590. * caller should take care to drop its reference by calling
  591. * IMB_freeImBuf if needed. */
  592. Image *BKE_image_add_from_imbuf(ImBuf *ibuf, const char *name)
  593. {
  594. /* on save, type is changed to FILE in editsima.c */
  595. Image *ima;
  596. if (name == NULL) {
  597. name = BLI_path_basename(ibuf->name);
  598. }
  599. ima = image_alloc(G.main, name, IMA_SRC_FILE, IMA_TYPE_IMAGE);
  600. if (ima) {
  601. BLI_strncpy(ima->name, ibuf->name, FILE_MAX);
  602. image_assign_ibuf(ima, ibuf, IMA_NO_INDEX, 0);
  603. ima->ok = IMA_OK_LOADED;
  604. }
  605. return ima;
  606. }
  607. /* packs rects from memory as PNG
  608. * convert multiview images to R_IMF_VIEWS_INDIVIDUAL
  609. */
  610. static void image_memorypack_multiview(Image *ima)
  611. {
  612. ImageView *iv;
  613. int i;
  614. image_free_packedfiles(ima);
  615. for (i = 0, iv = ima->views.first; iv; iv = iv->next, i++) {
  616. ImBuf *ibuf = image_get_cached_ibuf_for_index_frame(ima, i, 0);
  617. ibuf->ftype = IMB_FTYPE_PNG;
  618. ibuf->planes = R_IMF_PLANES_RGBA;
  619. /* if the image was a R_IMF_VIEWS_STEREO_3D we force _L, _R suffices */
  620. if (ima->views_format == R_IMF_VIEWS_STEREO_3D) {
  621. const char *suffix[2] = {STEREO_LEFT_SUFFIX, STEREO_RIGHT_SUFFIX};
  622. BLI_path_suffix(iv->filepath, FILE_MAX, suffix[i], "");
  623. }
  624. IMB_saveiff(ibuf, iv->filepath, IB_rect | IB_mem);
  625. if (ibuf->encodedbuffer == NULL) {
  626. printf("memory save for pack error\n");
  627. IMB_freeImBuf(ibuf);
  628. image_free_packedfiles(ima);
  629. return;
  630. }
  631. else {
  632. ImagePackedFile *imapf;
  633. PackedFile *pf = MEM_callocN(sizeof(*pf), "PackedFile");
  634. pf->data = ibuf->encodedbuffer;
  635. pf->size = ibuf->encodedsize;
  636. imapf = MEM_mallocN(sizeof(ImagePackedFile), "Image PackedFile");
  637. BLI_strncpy(imapf->filepath, iv->filepath, sizeof(imapf->filepath));
  638. imapf->packedfile = pf;
  639. BLI_addtail(&ima->packedfiles, imapf);
  640. ibuf->encodedbuffer = NULL;
  641. ibuf->encodedsize = 0;
  642. ibuf->userflags &= ~IB_BITMAPDIRTY;
  643. }
  644. IMB_freeImBuf(ibuf);
  645. }
  646. if (ima->source == IMA_SRC_GENERATED) {
  647. ima->source = IMA_SRC_FILE;
  648. ima->type = IMA_TYPE_IMAGE;
  649. }
  650. ima->views_format = R_IMF_VIEWS_INDIVIDUAL;
  651. }
  652. /* packs rect from memory as PNG */
  653. void BKE_image_memorypack(Image *ima)
  654. {
  655. ImBuf *ibuf;
  656. if (BKE_image_is_multiview(ima)) {
  657. image_memorypack_multiview(ima);
  658. return;
  659. }
  660. ibuf = image_get_cached_ibuf_for_index_frame(ima, IMA_NO_INDEX, 0);
  661. if (ibuf == NULL)
  662. return;
  663. image_free_packedfiles(ima);
  664. ibuf->ftype = IMB_FTYPE_PNG;
  665. ibuf->planes = R_IMF_PLANES_RGBA;
  666. IMB_saveiff(ibuf, ibuf->name, IB_rect | IB_mem);
  667. if (ibuf->encodedbuffer == NULL) {
  668. printf("memory save for pack error\n");
  669. }
  670. else {
  671. ImagePackedFile *imapf;
  672. PackedFile *pf = MEM_callocN(sizeof(*pf), "PackedFile");
  673. pf->data = ibuf->encodedbuffer;
  674. pf->size = ibuf->encodedsize;
  675. imapf = MEM_mallocN(sizeof(ImagePackedFile), "Image PackedFile");
  676. BLI_strncpy(imapf->filepath, ima->name, sizeof(imapf->filepath));
  677. imapf->packedfile = pf;
  678. BLI_addtail(&ima->packedfiles, imapf);
  679. ibuf->encodedbuffer = NULL;
  680. ibuf->encodedsize = 0;
  681. ibuf->userflags &= ~IB_BITMAPDIRTY;
  682. if (ima->source == IMA_SRC_GENERATED) {
  683. ima->source = IMA_SRC_FILE;
  684. ima->type = IMA_TYPE_IMAGE;
  685. }
  686. }
  687. IMB_freeImBuf(ibuf);
  688. }
  689. void BKE_image_packfiles(ReportList *reports, Image *ima, const char *basepath)
  690. {
  691. const int totfiles = image_num_files(ima);
  692. if (totfiles == 1) {
  693. ImagePackedFile *imapf = MEM_mallocN(sizeof(ImagePackedFile), "Image packed file");
  694. BLI_addtail(&ima->packedfiles, imapf);
  695. imapf->packedfile = newPackedFile(reports, ima->name, basepath);
  696. if (imapf->packedfile) {
  697. BLI_strncpy(imapf->filepath, ima->name, sizeof(imapf->filepath));
  698. }
  699. else {
  700. BLI_freelinkN(&ima->packedfiles, imapf);
  701. }
  702. }
  703. else {
  704. ImageView *iv;
  705. for (iv = ima->views.first; iv; iv = iv->next) {
  706. ImagePackedFile *imapf = MEM_mallocN(sizeof(ImagePackedFile), "Image packed file");
  707. BLI_addtail(&ima->packedfiles, imapf);
  708. imapf->packedfile = newPackedFile(reports, iv->filepath, basepath);
  709. if (imapf->packedfile) {
  710. BLI_strncpy(imapf->filepath, iv->filepath, sizeof(imapf->filepath));
  711. }
  712. else {
  713. BLI_freelinkN(&ima->packedfiles, imapf);
  714. }
  715. }
  716. }
  717. }
  718. void BKE_image_packfiles_from_mem(ReportList *reports, Image *ima, char *data, const size_t data_len)
  719. {
  720. const int totfiles = image_num_files(ima);
  721. if (totfiles != 1) {
  722. BKE_report(reports, RPT_ERROR, "Cannot pack multiview images from raw data currently...");
  723. }
  724. else {
  725. ImagePackedFile *imapf = MEM_mallocN(sizeof(ImagePackedFile), __func__);
  726. BLI_addtail(&ima->packedfiles, imapf);
  727. imapf->packedfile = newPackedFileMemory(data, data_len);
  728. BLI_strncpy(imapf->filepath, ima->name, sizeof(imapf->filepath));
  729. }
  730. }
  731. void BKE_image_tag_time(Image *ima)
  732. {
  733. ima->lastused = PIL_check_seconds_timer_i();
  734. }
  735. #if 0
  736. static void tag_all_images_time()
  737. {
  738. Image *ima;
  739. int ctime = PIL_check_seconds_timer_i();
  740. ima = G.main->image.first;
  741. while (ima) {
  742. if (ima->bindcode || ima->repbind || ima->ibufs.first) {
  743. ima->lastused = ctime;
  744. }
  745. }
  746. }
  747. #endif
  748. static uintptr_t image_mem_size(Image *image)
  749. {
  750. uintptr_t size = 0;
  751. /* viewers have memory depending on other rules, has no valid rect pointer */
  752. if (image->source == IMA_SRC_VIEWER)
  753. return 0;
  754. BLI_spin_lock(&image_spin);
  755. if (image->cache != NULL) {
  756. struct MovieCacheIter *iter = IMB_moviecacheIter_new(image->cache);
  757. while (!IMB_moviecacheIter_done(iter)) {
  758. ImBuf *ibuf = IMB_moviecacheIter_getImBuf(iter);
  759. ImBuf *ibufm;
  760. int level;
  761. if (ibuf->rect) {
  762. size += MEM_allocN_len(ibuf->rect);
  763. }
  764. if (ibuf->rect_float) {
  765. size += MEM_allocN_len(ibuf->rect_float);
  766. }
  767. for (level = 0; level < IMB_MIPMAP_LEVELS; level++) {
  768. ibufm = ibuf->mipmap[level];
  769. if (ibufm) {
  770. if (ibufm->rect) {
  771. size += MEM_allocN_len(ibufm->rect);
  772. }
  773. if (ibufm->rect_float) {
  774. size += MEM_allocN_len(ibufm->rect_float);
  775. }
  776. }
  777. }
  778. IMB_moviecacheIter_step(iter);
  779. }
  780. IMB_moviecacheIter_free(iter);
  781. }
  782. BLI_spin_unlock(&image_spin);
  783. return size;
  784. }
  785. void BKE_image_print_memlist(void)
  786. {
  787. Image *ima;
  788. uintptr_t size, totsize = 0;
  789. for (ima = G.main->image.first; ima; ima = ima->id.next)
  790. totsize += image_mem_size(ima);
  791. printf("\ntotal image memory len: %.3f MB\n", (double)totsize / (double)(1024 * 1024));
  792. for (ima = G.main->image.first; ima; ima = ima->id.next) {
  793. size = image_mem_size(ima);
  794. if (size)
  795. printf("%s len: %.3f MB\n", ima->id.name + 2, (double)size / (double)(1024 * 1024));
  796. }
  797. }
  798. static bool imagecache_check_dirty(ImBuf *ibuf, void *UNUSED(userkey), void *UNUSED(userdata))
  799. {
  800. return (ibuf->userflags & IB_BITMAPDIRTY) == 0;
  801. }
  802. void BKE_image_free_all_textures(void)
  803. {
  804. #undef CHECK_FREED_SIZE
  805. Tex *tex;
  806. Image *ima;
  807. #ifdef CHECK_FREED_SIZE
  808. uintptr_t tot_freed_size = 0;
  809. #endif
  810. for (ima = G.main->image.first; ima; ima = ima->id.next)
  811. ima->id.tag &= ~LIB_TAG_DOIT;
  812. for (tex = G.main->tex.first; tex; tex = tex->id.next)
  813. if (tex->ima)
  814. tex->ima->id.tag |= LIB_TAG_DOIT;
  815. for (ima = G.main->image.first; ima; ima = ima->id.next) {
  816. if (ima->cache && (ima->id.tag & LIB_TAG_DOIT)) {
  817. #ifdef CHECK_FREED_SIZE
  818. uintptr_t old_size = image_mem_size(ima);
  819. #endif
  820. IMB_moviecache_cleanup(ima->cache, imagecache_check_dirty, NULL);
  821. #ifdef CHECK_FREED_SIZE
  822. tot_freed_size += old_size - image_mem_size(ima);
  823. #endif
  824. }
  825. }
  826. #ifdef CHECK_FREED_SIZE
  827. printf("%s: freed total %lu MB\n", __func__, tot_freed_size / (1024 * 1024));
  828. #endif
  829. }
  830. static bool imagecache_check_free_anim(ImBuf *ibuf, void *UNUSED(userkey), void *userdata)
  831. {
  832. int except_frame = *(int *)userdata;
  833. return (ibuf->userflags & IB_BITMAPDIRTY) == 0 &&
  834. (ibuf->index != IMA_NO_INDEX) &&
  835. (except_frame != IMA_INDEX_FRAME(ibuf->index));
  836. }
  837. /* except_frame is weak, only works for seqs without offset... */
  838. void BKE_image_free_anim_ibufs(Image *ima, int except_frame)
  839. {
  840. BLI_spin_lock(&image_spin);
  841. if (ima->cache != NULL) {
  842. IMB_moviecache_cleanup(ima->cache, imagecache_check_free_anim, &except_frame);
  843. }
  844. BLI_spin_unlock(&image_spin);
  845. }
  846. void BKE_image_all_free_anim_ibufs(int cfra)
  847. {
  848. Image *ima;
  849. for (ima = G.main->image.first; ima; ima = ima->id.next)
  850. if (BKE_image_is_animated(ima))
  851. BKE_image_free_anim_ibufs(ima, cfra);
  852. }
  853. /* *********** READ AND WRITE ************** */
  854. int BKE_image_imtype_to_ftype(const char imtype, ImbFormatOptions *r_options)
  855. {
  856. memset(r_options, 0, sizeof(*r_options));
  857. if (imtype == R_IMF_IMTYPE_TARGA) {
  858. return IMB_FTYPE_TGA;
  859. }
  860. else if (imtype == R_IMF_IMTYPE_RAWTGA) {
  861. r_options->flag = RAWTGA;
  862. return IMB_FTYPE_TGA;
  863. }
  864. else if (imtype == R_IMF_IMTYPE_IRIS) {
  865. return IMB_FTYPE_IMAGIC;
  866. }
  867. #ifdef WITH_HDR
  868. else if (imtype == R_IMF_IMTYPE_RADHDR) {
  869. return IMB_FTYPE_RADHDR;
  870. }
  871. #endif
  872. else if (imtype == R_IMF_IMTYPE_PNG) {
  873. r_options->quality = 15;
  874. return IMB_FTYPE_PNG;
  875. }
  876. #ifdef WITH_DDS
  877. else if (imtype == R_IMF_IMTYPE_DDS) {
  878. return IMB_FTYPE_DDS;
  879. }
  880. #endif
  881. else if (imtype == R_IMF_IMTYPE_BMP) {
  882. return IMB_FTYPE_BMP;
  883. }
  884. #ifdef WITH_TIFF
  885. else if (imtype == R_IMF_IMTYPE_TIFF) {
  886. return IMB_FTYPE_TIF;
  887. }
  888. #endif
  889. else if (imtype == R_IMF_IMTYPE_OPENEXR || imtype == R_IMF_IMTYPE_MULTILAYER) {
  890. return IMB_FTYPE_OPENEXR;
  891. }
  892. #ifdef WITH_CINEON
  893. else if (imtype == R_IMF_IMTYPE_CINEON) {
  894. return IMB_FTYPE_CINEON;
  895. }
  896. else if (imtype == R_IMF_IMTYPE_DPX) {
  897. return IMB_FTYPE_DPX;
  898. }
  899. #endif
  900. #ifdef WITH_OPENJPEG
  901. else if (imtype == R_IMF_IMTYPE_JP2) {
  902. r_options->flag |= JP2_JP2;
  903. r_options->quality = 90;
  904. return IMB_FTYPE_JP2;
  905. }
  906. #endif
  907. else {
  908. r_options->quality = 90;
  909. return IMB_FTYPE_JPG;
  910. }
  911. }
  912. char BKE_image_ftype_to_imtype(const int ftype, const ImbFormatOptions *options)
  913. {
  914. if (ftype == 0) {
  915. return R_IMF_IMTYPE_TARGA;
  916. }
  917. else if (ftype == IMB_FTYPE_IMAGIC) {
  918. return R_IMF_IMTYPE_IRIS;
  919. }
  920. #ifdef WITH_HDR
  921. else if (ftype == IMB_FTYPE_RADHDR) {
  922. return R_IMF_IMTYPE_RADHDR;
  923. }
  924. #endif
  925. else if (ftype == IMB_FTYPE_PNG) {
  926. return R_IMF_IMTYPE_PNG;
  927. }
  928. #ifdef WITH_DDS
  929. else if (ftype == IMB_FTYPE_DDS) {
  930. return R_IMF_IMTYPE_DDS;
  931. }
  932. #endif
  933. else if (ftype == IMB_FTYPE_BMP) {
  934. return R_IMF_IMTYPE_BMP;
  935. }
  936. #ifdef WITH_TIFF
  937. else if (ftype == IMB_FTYPE_TIF) {
  938. return R_IMF_IMTYPE_TIFF;
  939. }
  940. #endif
  941. else if (ftype == IMB_FTYPE_OPENEXR) {
  942. return R_IMF_IMTYPE_OPENEXR;
  943. }
  944. #ifdef WITH_CINEON
  945. else if (ftype == IMB_FTYPE_CINEON) {
  946. return R_IMF_IMTYPE_CINEON;
  947. }
  948. else if (ftype == IMB_FTYPE_DPX) {
  949. return R_IMF_IMTYPE_DPX;
  950. }
  951. #endif
  952. else if (ftype == IMB_FTYPE_TGA) {
  953. if (options && (options->flag & RAWTGA)) {
  954. return R_IMF_IMTYPE_RAWTGA;
  955. }
  956. else {
  957. return R_IMF_IMTYPE_TARGA;
  958. }
  959. }
  960. #ifdef WITH_OPENJPEG
  961. else if (ftype == IMB_FTYPE_JP2) {
  962. return R_IMF_IMTYPE_JP2;
  963. }
  964. #endif
  965. else {
  966. return R_IMF_IMTYPE_JPEG90;
  967. }
  968. }
  969. bool BKE_imtype_is_movie(const char imtype)
  970. {
  971. switch (imtype) {
  972. case R_IMF_IMTYPE_AVIRAW:
  973. case R_IMF_IMTYPE_AVIJPEG:
  974. case R_IMF_IMTYPE_QUICKTIME:
  975. case R_IMF_IMTYPE_FFMPEG:
  976. case R_IMF_IMTYPE_H264:
  977. case R_IMF_IMTYPE_THEORA:
  978. case R_IMF_IMTYPE_XVID:
  979. case R_IMF_IMTYPE_FRAMESERVER:
  980. return true;
  981. }
  982. return false;
  983. }
  984. int BKE_imtype_supports_zbuf(const char imtype)
  985. {
  986. switch (imtype) {
  987. case R_IMF_IMTYPE_IRIZ:
  988. case R_IMF_IMTYPE_OPENEXR: /* but not R_IMF_IMTYPE_MULTILAYER */
  989. return 1;
  990. }
  991. return 0;
  992. }
  993. int BKE_imtype_supports_compress(const char imtype)
  994. {
  995. switch (imtype) {
  996. case R_IMF_IMTYPE_PNG:
  997. return 1;
  998. }
  999. return 0;
  1000. }
  1001. int BKE_imtype_supports_quality(const char imtype)
  1002. {
  1003. switch (imtype) {
  1004. case R_IMF_IMTYPE_JPEG90:
  1005. case R_IMF_IMTYPE_JP2:
  1006. case R_IMF_IMTYPE_AVIJPEG:
  1007. return 1;
  1008. }
  1009. return 0;
  1010. }
  1011. int BKE_imtype_requires_linear_float(const char imtype)
  1012. {
  1013. switch (imtype) {
  1014. case R_IMF_IMTYPE_CINEON:
  1015. case R_IMF_IMTYPE_DPX:
  1016. case R_IMF_IMTYPE_RADHDR:
  1017. case R_IMF_IMTYPE_OPENEXR:
  1018. case R_IMF_IMTYPE_MULTILAYER:
  1019. return true;
  1020. }
  1021. return 0;
  1022. }
  1023. char BKE_imtype_valid_channels(const char imtype, bool write_file)
  1024. {
  1025. char chan_flag = IMA_CHAN_FLAG_RGB; /* assume all support rgb */
  1026. /* alpha */
  1027. switch (imtype) {
  1028. case R_IMF_IMTYPE_BMP:
  1029. if (write_file) break;
  1030. ATTR_FALLTHROUGH;
  1031. case R_IMF_IMTYPE_TARGA:
  1032. case R_IMF_IMTYPE_RAWTGA:
  1033. case R_IMF_IMTYPE_IRIS:
  1034. case R_IMF_IMTYPE_PNG:
  1035. case R_IMF_IMTYPE_TIFF:
  1036. case R_IMF_IMTYPE_OPENEXR:
  1037. case R_IMF_IMTYPE_MULTILAYER:
  1038. case R_IMF_IMTYPE_DDS:
  1039. case R_IMF_IMTYPE_JP2:
  1040. case R_IMF_IMTYPE_QUICKTIME:
  1041. case R_IMF_IMTYPE_DPX:
  1042. chan_flag |= IMA_CHAN_FLAG_ALPHA;
  1043. break;
  1044. }
  1045. /* bw */
  1046. switch (imtype) {
  1047. case R_IMF_IMTYPE_PNG:
  1048. case R_IMF_IMTYPE_JPEG90:
  1049. case R_IMF_IMTYPE_TARGA:
  1050. case R_IMF_IMTYPE_RAWTGA:
  1051. case R_IMF_IMTYPE_TIFF:
  1052. case R_IMF_IMTYPE_IRIS:
  1053. chan_flag |= IMA_CHAN_FLAG_BW;
  1054. break;
  1055. }
  1056. return chan_flag;
  1057. }
  1058. char BKE_imtype_valid_depths(const char imtype)
  1059. {
  1060. switch (imtype) {
  1061. case R_IMF_IMTYPE_RADHDR:
  1062. return R_IMF_CHAN_DEPTH_32;
  1063. case R_IMF_IMTYPE_TIFF:
  1064. return R_IMF_CHAN_DEPTH_8 | R_IMF_CHAN_DEPTH_16;
  1065. case R_IMF_IMTYPE_OPENEXR:
  1066. return R_IMF_CHAN_DEPTH_16 | R_IMF_CHAN_DEPTH_32;
  1067. case R_IMF_IMTYPE_MULTILAYER:
  1068. return R_IMF_CHAN_DEPTH_16 | R_IMF_CHAN_DEPTH_32;
  1069. /* eeh, cineon does some strange 10bits per channel */
  1070. case R_IMF_IMTYPE_DPX:
  1071. return R_IMF_CHAN_DEPTH_8 | R_IMF_CHAN_DEPTH_10 | R_IMF_CHAN_DEPTH_12 | R_IMF_CHAN_DEPTH_16;
  1072. case R_IMF_IMTYPE_CINEON:
  1073. return R_IMF_CHAN_DEPTH_10;
  1074. case R_IMF_IMTYPE_JP2:
  1075. return R_IMF_CHAN_DEPTH_8 | R_IMF_CHAN_DEPTH_12 | R_IMF_CHAN_DEPTH_16;
  1076. case R_IMF_IMTYPE_PNG:
  1077. return R_IMF_CHAN_DEPTH_8 | R_IMF_CHAN_DEPTH_16;
  1078. /* most formats are 8bit only */
  1079. default:
  1080. return R_IMF_CHAN_DEPTH_8;
  1081. }
  1082. }
  1083. /* string is from command line --render-format arg, keep in sync with
  1084. * creator_args.c help info */
  1085. char BKE_imtype_from_arg(const char *imtype_arg)
  1086. {
  1087. if (STREQ(imtype_arg, "TGA")) return R_IMF_IMTYPE_TARGA;
  1088. else if (STREQ(imtype_arg, "IRIS")) return R_IMF_IMTYPE_IRIS;
  1089. #ifdef WITH_DDS
  1090. else if (STREQ(imtype_arg, "DDS")) return R_IMF_IMTYPE_DDS;
  1091. #endif
  1092. else if (STREQ(imtype_arg, "JPEG")) return R_IMF_IMTYPE_JPEG90;
  1093. else if (STREQ(imtype_arg, "IRIZ")) return R_IMF_IMTYPE_IRIZ;
  1094. else if (STREQ(imtype_arg, "RAWTGA")) return R_IMF_IMTYPE_RAWTGA;
  1095. else if (STREQ(imtype_arg, "AVIRAW")) return R_IMF_IMTYPE_AVIRAW;
  1096. else if (STREQ(imtype_arg, "AVIJPEG")) return R_IMF_IMTYPE_AVIJPEG;
  1097. else if (STREQ(imtype_arg, "PNG")) return R_IMF_IMTYPE_PNG;
  1098. else if (STREQ(imtype_arg, "QUICKTIME")) return R_IMF_IMTYPE_QUICKTIME;
  1099. else if (STREQ(imtype_arg, "BMP")) return R_IMF_IMTYPE_BMP;
  1100. #ifdef WITH_HDR
  1101. else if (STREQ(imtype_arg, "HDR")) return R_IMF_IMTYPE_RADHDR;
  1102. #endif
  1103. #ifdef WITH_TIFF
  1104. else if (STREQ(imtype_arg, "TIFF")) return R_IMF_IMTYPE_TIFF;
  1105. #endif
  1106. #ifdef WITH_OPENEXR
  1107. else if (STREQ(imtype_arg, "EXR")) return R_IMF_IMTYPE_OPENEXR;
  1108. else if (STREQ(imtype_arg, "MULTILAYER")) return R_IMF_IMTYPE_MULTILAYER;
  1109. #endif
  1110. else if (STREQ(imtype_arg, "FFMPEG")) return R_IMF_IMTYPE_FFMPEG;
  1111. else if (STREQ(imtype_arg, "FRAMESERVER")) return R_IMF_IMTYPE_FRAMESERVER;
  1112. #ifdef WITH_CINEON
  1113. else if (STREQ(imtype_arg, "CINEON")) return R_IMF_IMTYPE_CINEON;
  1114. else if (STREQ(imtype_arg, "DPX")) return R_IMF_IMTYPE_DPX;
  1115. #endif
  1116. #ifdef WITH_OPENJPEG
  1117. else if (STREQ(imtype_arg, "JP2")) return R_IMF_IMTYPE_JP2;
  1118. #endif
  1119. else return R_IMF_IMTYPE_INVALID;
  1120. }
  1121. static bool do_add_image_extension(char *string, const char imtype, const ImageFormatData *im_format)
  1122. {
  1123. const char *extension = NULL;
  1124. const char *extension_test;
  1125. (void)im_format; /* may be unused, depends on build options */
  1126. if (imtype == R_IMF_IMTYPE_IRIS) {
  1127. if (!BLI_testextensie(string, extension_test = ".rgb"))
  1128. extension = extension_test;
  1129. }
  1130. else if (imtype == R_IMF_IMTYPE_IRIZ) {
  1131. if (!BLI_testextensie(string, extension_test = ".rgb"))
  1132. extension = extension_test;
  1133. }
  1134. #ifdef WITH_HDR
  1135. else if (imtype == R_IMF_IMTYPE_RADHDR) {
  1136. if (!BLI_testextensie(string, extension_test = ".hdr"))
  1137. extension = extension_test;
  1138. }
  1139. #endif
  1140. else if (ELEM(imtype, R_IMF_IMTYPE_PNG, R_IMF_IMTYPE_FFMPEG, R_IMF_IMTYPE_H264, R_IMF_IMTYPE_THEORA, R_IMF_IMTYPE_XVID)) {
  1141. if (!BLI_testextensie(string, extension_test = ".png"))
  1142. extension = extension_test;
  1143. }
  1144. #ifdef WITH_DDS
  1145. else if (imtype == R_IMF_IMTYPE_DDS) {
  1146. if (!BLI_testextensie(string, extension_test = ".dds"))
  1147. extension = extension_test;
  1148. }
  1149. #endif
  1150. else if (ELEM(imtype, R_IMF_IMTYPE_TARGA, R_IMF_IMTYPE_RAWTGA)) {
  1151. if (!BLI_testextensie(string, extension_test = ".tga"))
  1152. extension = extension_test;
  1153. }
  1154. else if (imtype == R_IMF_IMTYPE_BMP) {
  1155. if (!BLI_testextensie(string, extension_test = ".bmp"))
  1156. extension = extension_test;
  1157. }
  1158. #ifdef WITH_TIFF
  1159. else if (imtype == R_IMF_IMTYPE_TIFF) {
  1160. if (!BLI_testextensie_n(string, extension_test = ".tif", ".tiff", NULL)) {
  1161. extension = extension_test;
  1162. }
  1163. }
  1164. #endif
  1165. #ifdef WITH_OPENIMAGEIO
  1166. else if (imtype == R_IMF_IMTYPE_PSD) {
  1167. if (!BLI_testextensie(string, extension_test = ".psd"))
  1168. extension = extension_test;
  1169. }
  1170. #endif
  1171. #ifdef WITH_OPENEXR
  1172. else if (imtype == R_IMF_IMTYPE_OPENEXR || imtype == R_IMF_IMTYPE_MULTILAYER) {
  1173. if (!BLI_testextensie(string, extension_test = ".exr"))
  1174. extension = extension_test;
  1175. }
  1176. #endif
  1177. #ifdef WITH_CINEON
  1178. else if (imtype == R_IMF_IMTYPE_CINEON) {
  1179. if (!BLI_testextensie(string, extension_test = ".cin"))
  1180. extension = extension_test;
  1181. }
  1182. else if (imtype == R_IMF_IMTYPE_DPX) {
  1183. if (!BLI_testextensie(string, extension_test = ".dpx"))
  1184. extension = extension_test;
  1185. }
  1186. #endif
  1187. #ifdef WITH_OPENJPEG
  1188. else if (imtype == R_IMF_IMTYPE_JP2) {
  1189. if (im_format) {
  1190. if (im_format->jp2_codec == R_IMF_JP2_CODEC_JP2) {
  1191. if (!BLI_testextensie(string, extension_test = ".jp2"))
  1192. extension = extension_test;
  1193. }
  1194. else if (im_format->jp2_codec == R_IMF_JP2_CODEC_J2K) {
  1195. if (!BLI_testextensie(string, extension_test = ".j2c"))
  1196. extension = extension_test;
  1197. }
  1198. else
  1199. BLI_assert(!"Unsupported jp2 codec was specified in im_format->jp2_codec");
  1200. }
  1201. else {
  1202. if (!BLI_testextensie(string, extension_test = ".jp2"))
  1203. extension = extension_test;
  1204. }
  1205. }
  1206. #endif
  1207. else { // R_IMF_IMTYPE_AVIRAW, R_IMF_IMTYPE_AVIJPEG, R_IMF_IMTYPE_JPEG90, R_IMF_IMTYPE_QUICKTIME etc
  1208. if (!(BLI_testextensie_n(string, extension_test = ".jpg", ".jpeg", NULL)))
  1209. extension = extension_test;
  1210. }
  1211. if (extension) {
  1212. /* prefer this in many cases to avoid .png.tga, but in certain cases it breaks */
  1213. /* remove any other known image extension */
  1214. if (BLI_testextensie_array(string, imb_ext_image) ||
  1215. (G.have_quicktime && BLI_testextensie_array(string, imb_ext_image_qt)))
  1216. {
  1217. return BLI_replace_extension(string, FILE_MAX, extension);
  1218. }
  1219. else {
  1220. return BLI_ensure_extension(string, FILE_MAX, extension);
  1221. }
  1222. }
  1223. else {
  1224. return false;
  1225. }
  1226. }
  1227. int BKE_image_path_ensure_ext_from_imformat(char *string, const ImageFormatData *im_format)
  1228. {
  1229. return do_add_image_extension(string, im_format->imtype, im_format);
  1230. }
  1231. int BKE_image_path_ensure_ext_from_imtype(char *string, const char imtype)
  1232. {
  1233. return do_add_image_extension(string, imtype, NULL);
  1234. }
  1235. void BKE_imformat_defaults(ImageFormatData *im_format)
  1236. {
  1237. memset(im_format, 0, sizeof(*im_format));
  1238. im_format->planes = R_IMF_PLANES_RGBA;
  1239. im_format->imtype = R_IMF_IMTYPE_PNG;
  1240. im_format->depth = R_IMF_CHAN_DEPTH_8;
  1241. im_format->quality = 90;
  1242. im_format->compress = 15;
  1243. BKE_color_managed_display_settings_init(&im_format->display_settings);
  1244. BKE_color_managed_view_settings_init(&im_format->view_settings);
  1245. }
  1246. void BKE_imbuf_to_image_format(struct ImageFormatData *im_format, const ImBuf *imbuf)
  1247. {
  1248. int ftype = imbuf->ftype;
  1249. int custom_flags = imbuf->foptions.flag;
  1250. char quality = imbuf->foptions.quality;
  1251. BKE_imformat_defaults(im_format);
  1252. /* file type */
  1253. if (ftype == IMB_FTYPE_IMAGIC)
  1254. im_format->imtype = R_IMF_IMTYPE_IRIS;
  1255. #ifdef WITH_HDR
  1256. else if (ftype == IMB_FTYPE_RADHDR)
  1257. im_format->imtype = R_IMF_IMTYPE_RADHDR;
  1258. #endif
  1259. else if (ftype == IMB_FTYPE_PNG) {
  1260. im_format->imtype = R_IMF_IMTYPE_PNG;
  1261. if (custom_flags & PNG_16BIT)
  1262. im_format->depth = R_IMF_CHAN_DEPTH_16;
  1263. im_format->compress = quality;
  1264. }
  1265. #ifdef WITH_DDS
  1266. else if (ftype == IMB_FTYPE_DDS)
  1267. im_format->imtype = R_IMF_IMTYPE_DDS;
  1268. #endif
  1269. else if (ftype == IMB_FTYPE_BMP)
  1270. im_format->imtype = R_IMF_IMTYPE_BMP;
  1271. #ifdef WITH_TIFF
  1272. else if (ftype == IMB_FTYPE_TIF) {
  1273. im_format->imtype = R_IMF_IMTYPE_TIFF;
  1274. if (custom_flags & TIF_16BIT)
  1275. im_format->depth = R_IMF_CHAN_DEPTH_16;
  1276. if (custom_flags & TIF_COMPRESS_NONE)
  1277. im_format->tiff_codec = R_IMF_TIFF_CODEC_NONE;
  1278. if (custom_flags & TIF_COMPRESS_DEFLATE)
  1279. im_format->tiff_codec = R_IMF_TIFF_CODEC_DEFLATE;
  1280. if (custom_flags & TIF_COMPRESS_LZW)
  1281. im_format->tiff_codec = R_IMF_TIFF_CODEC_LZW;
  1282. if (custom_flags & TIF_COMPRESS_PACKBITS)
  1283. im_format->tiff_codec = R_IMF_TIFF_CODEC_PACKBITS;
  1284. }
  1285. #endif
  1286. #ifdef WITH_OPENEXR
  1287. else if (ftype == IMB_FTYPE_OPENEXR) {
  1288. im_format->imtype = R_IMF_IMTYPE_OPENEXR;
  1289. if (custom_flags & OPENEXR_HALF)
  1290. im_format->depth = R_IMF_CHAN_DEPTH_16;
  1291. if (custom_flags & OPENEXR_COMPRESS)
  1292. im_format->exr_codec = R_IMF_EXR_CODEC_ZIP; // Can't determine compression
  1293. if (imbuf->zbuf_float)
  1294. im_format->flag |= R_IMF_FLAG_ZBUF;
  1295. }
  1296. #endif
  1297. #ifdef WITH_CINEON
  1298. else if (ftype == IMB_FTYPE_CINEON)
  1299. im_format->imtype = R_IMF_IMTYPE_CINEON;
  1300. else if (ftype == IMB_FTYPE_DPX)
  1301. im_format->imtype = R_IMF_IMTYPE_DPX;
  1302. #endif
  1303. else if (ftype == IMB_FTYPE_TGA) {
  1304. if (custom_flags & RAWTGA)
  1305. im_format->imtype = R_IMF_IMTYPE_RAWTGA;
  1306. else
  1307. im_format->imtype = R_IMF_IMTYPE_TARGA;
  1308. }
  1309. #ifdef WITH_OPENJPEG
  1310. else if (ftype == IMB_FTYPE_JP2) {
  1311. im_format->imtype = R_IMF_IMTYPE_JP2;
  1312. im_format->quality = quality;
  1313. if (custom_flags & JP2_16BIT)
  1314. im_format->depth = R_IMF_CHAN_DEPTH_16;
  1315. else if (custom_flags & JP2_12BIT)
  1316. im_format->depth = R_IMF_CHAN_DEPTH_12;
  1317. if (custom_flags & JP2_YCC)
  1318. im_format->jp2_flag |= R_IMF_JP2_FLAG_YCC;
  1319. if (custom_flags & JP2_CINE) {
  1320. im_format->jp2_flag |= R_IMF_JP2_FLAG_CINE_PRESET;
  1321. if (custom_flags & JP2_CINE_48FPS)
  1322. im_format->jp2_flag |= R_IMF_JP2_FLAG_CINE_48;
  1323. }
  1324. if (custom_flags & JP2_JP2)
  1325. im_format->jp2_codec = R_IMF_JP2_CODEC_JP2;
  1326. else if (custom_flags & JP2_J2K)
  1327. im_format->jp2_codec = R_IMF_JP2_CODEC_J2K;
  1328. else
  1329. BLI_assert(!"Unsupported jp2 codec was specified in file type");
  1330. }
  1331. #endif
  1332. else {
  1333. im_format->imtype = R_IMF_IMTYPE_JPEG90;
  1334. im_format->quality = quality;
  1335. }
  1336. /* planes */
  1337. im_format->planes = imbuf->planes;
  1338. }
  1339. #define STAMP_NAME_SIZE ((MAX_ID_NAME - 2) + 16)
  1340. /* could allow access externally - 512 is for long names,
  1341. * STAMP_NAME_SIZE is for id names, allowing them some room for description */
  1342. typedef struct StampData {
  1343. char file[512];
  1344. char note[512];
  1345. char date[512];
  1346. char marker[512];
  1347. char time[512];
  1348. char frame[512];
  1349. char camera[STAMP_NAME_SIZE];
  1350. char cameralens[STAMP_NAME_SIZE];
  1351. char scene[STAMP_NAME_SIZE];
  1352. char strip[STAMP_NAME_SIZE];
  1353. char rendertime[STAMP_NAME_SIZE];
  1354. char memory[STAMP_NAME_SIZE];
  1355. } StampData;
  1356. #undef STAMP_NAME_SIZE
  1357. static void stampdata(Scene *scene, Object *camera, StampData *stamp_data, int do_prefix)
  1358. {
  1359. char text[256];
  1360. struct tm *tl;
  1361. time_t t;
  1362. if (scene->r.stamp & R_STAMP_FILENAME) {
  1363. BLI_snprintf(stamp_data->file, sizeof(stamp_data->file), do_prefix ? "File %s" : "%s", G.relbase_valid ? G.main->name : "<untitled>");
  1364. }
  1365. else {
  1366. stamp_data->file[0] = '\0';
  1367. }
  1368. if (scene->r.stamp & R_STAMP_NOTE) {
  1369. /* Never do prefix for Note */
  1370. BLI_snprintf(stamp_data->note, sizeof(stamp_data->note), "%s", scene->r.stamp_udata);
  1371. }
  1372. else {
  1373. stamp_data->note[0] = '\0';
  1374. }
  1375. if (scene->r.stamp & R_STAMP_DATE) {
  1376. t = time(NULL);
  1377. tl = localtime(&t);
  1378. BLI_snprintf(text, sizeof(text), "%04d/%02d/%02d %02d:%02d:%02d", tl->tm_year + 1900, tl->tm_mon + 1, tl->tm_mday, tl->tm_hour, tl->tm_min, tl->tm_sec);
  1379. BLI_snprintf(stamp_data->date, sizeof(stamp_data->date), do_prefix ? "Date %s" : "%s", text);
  1380. }
  1381. else {
  1382. stamp_data->date[0] = '\0';
  1383. }
  1384. if (scene->r.stamp & R_STAMP_MARKER) {
  1385. const char *name = BKE_scene_find_last_marker_name(scene, CFRA);
  1386. if (name) BLI_strncpy(text, name, sizeof(text));
  1387. else BLI_strncpy(text, "<none>", sizeof(text));
  1388. BLI_snprintf(stamp_data->marker, sizeof(stamp_data->marker), do_prefix ? "Marker %s" : "%s", text);
  1389. }
  1390. else {
  1391. stamp_data->marker[0] = '\0';
  1392. }
  1393. if (scene->r.stamp & R_STAMP_TIME) {
  1394. const short timecode_style = USER_TIMECODE_SMPTE_FULL;
  1395. BLI_timecode_string_from_time(text, sizeof(text), 0, FRA2TIME(scene->r.cfra), FPS, timecode_style);
  1396. BLI_snprintf(stamp_data->time, sizeof(stamp_data->time), do_prefix ? "Timecode %s" : "%s", text);
  1397. }
  1398. else {
  1399. stamp_data->time[0] = '\0';
  1400. }
  1401. if (scene->r.stamp & R_STAMP_FRAME) {
  1402. char fmtstr[32];
  1403. int digits = 1;
  1404. if (scene->r.efra > 9)
  1405. digits = 1 + (int) log10(scene->r.efra);
  1406. BLI_snprintf(fmtstr, sizeof(fmtstr), do_prefix ? "Frame %%0%di" : "%%0%di", digits);
  1407. BLI_snprintf(stamp_data->frame, sizeof(stamp_data->frame), fmtstr, scene->r.cfra);
  1408. }
  1409. else {
  1410. stamp_data->frame[0] = '\0';
  1411. }
  1412. if (scene->r.stamp & R_STAMP_CAMERA) {
  1413. BLI_snprintf(stamp_data->camera, sizeof(stamp_data->camera), do_prefix ? "Camera %s" : "%s", camera ? camera->id.name + 2 : "<none>");
  1414. }
  1415. else {
  1416. stamp_data->camera[0] = '\0';
  1417. }
  1418. if (scene->r.stamp & R_STAMP_CAMERALENS) {
  1419. if (camera && camera->type == OB_CAMERA) {
  1420. BLI_snprintf(text, sizeof(text), "%.2f", ((Camera *)camera->data)->lens);
  1421. }
  1422. else {
  1423. BLI_strncpy(text, "<none>", sizeof(text));
  1424. }
  1425. BLI_snprintf(stamp_data->cameralens, sizeof(stamp_data->cameralens), do_prefix ? "Lens %s" : "%s", text);
  1426. }
  1427. else {
  1428. stamp_data->cameralens[0] = '\0';
  1429. }
  1430. if (scene->r.stamp & R_STAMP_SCENE) {
  1431. BLI_snprintf(stamp_data->scene, sizeof(stamp_data->scene), do_prefix ? "Scene %s" : "%s", scene->id.name + 2);
  1432. }
  1433. else {
  1434. stamp_data->scene[0] = '\0';
  1435. }
  1436. if (scene->r.stamp & R_STAMP_SEQSTRIP) {
  1437. Sequence *seq = BKE_sequencer_foreground_frame_get(scene, scene->r.cfra);
  1438. if (seq) BLI_strncpy(text, seq->name + 2, sizeof(text));
  1439. else BLI_strncpy(text, "<none>", sizeof(text));
  1440. BLI_snprintf(stamp_data->strip, sizeof(stamp_data->strip), do_prefix ? "Strip %s" : "%s", text);
  1441. }
  1442. else {
  1443. stamp_data->strip[0] = '\0';
  1444. }
  1445. {
  1446. Render *re = RE_GetRender(scene->id.name);
  1447. RenderStats *stats = re ? RE_GetStats(re) : NULL;
  1448. if (stats && (scene->r.stamp & R_STAMP_RENDERTIME)) {
  1449. BLI_timecode_string_from_time_simple(text, sizeof(text), stats->lastframetime);
  1450. BLI_snprintf(stamp_data->rendertime, sizeof(stamp_data->rendertime), do_prefix ? "RenderTime %s" : "%s", text);
  1451. }
  1452. else {
  1453. stamp_data->rendertime[0] = '\0';
  1454. }
  1455. if (stats && (scene->r.stamp & R_STAMP_MEMORY)) {
  1456. BLI_snprintf(stamp_data->memory, sizeof(stamp_data->memory), do_prefix ? "Peak Memory %.2fM" : "%.2fM", stats->mem_peak);
  1457. }
  1458. else {
  1459. stamp_data->memory[0] = '\0';
  1460. }
  1461. }
  1462. }
  1463. /* Will always add prefix. */
  1464. static void stampdata_from_template(StampData *stamp_data,
  1465. const Scene *scene,
  1466. const StampData *stamp_data_template)
  1467. {
  1468. if (scene->r.stamp & R_STAMP_FILENAME) {
  1469. BLI_snprintf(stamp_data->file, sizeof(stamp_data->file), "File %s", stamp_data_template->file);
  1470. }
  1471. else {
  1472. stamp_data->file[0] = '\0';
  1473. }
  1474. if (scene->r.stamp & R_STAMP_NOTE) {
  1475. BLI_snprintf(stamp_data->note, sizeof(stamp_data->note), "%s", stamp_data_template->note);
  1476. }
  1477. else {
  1478. stamp_data->note[0] = '\0';
  1479. }
  1480. if (scene->r.stamp & R_STAMP_DATE) {
  1481. BLI_snprintf(stamp_data->date, sizeof(stamp_data->date), "Date %s", stamp_data_template->date);
  1482. }
  1483. else {
  1484. stamp_data->date[0] = '\0';
  1485. }
  1486. if (scene->r.stamp & R_STAMP_MARKER) {
  1487. BLI_snprintf(stamp_data->marker, sizeof(stamp_data->marker), "Marker %s", stamp_data_template->marker);
  1488. }
  1489. else {
  1490. stamp_data->marker[0] = '\0';
  1491. }
  1492. if (scene->r.stamp & R_STAMP_TIME) {
  1493. BLI_snprintf(stamp_data->time, sizeof(stamp_data->time), "Timecode %s", stamp_data_template->time);
  1494. }
  1495. else {
  1496. stamp_data->time[0] = '\0';
  1497. }
  1498. if (scene->r.stamp & R_STAMP_FRAME) {
  1499. BLI_snprintf(stamp_data->frame, sizeof(stamp_data->frame), "Frame %s", stamp_data_template->frame);
  1500. }
  1501. else {
  1502. stamp_data->frame[0] = '\0';
  1503. }
  1504. if (scene->r.stamp & R_STAMP_CAMERA) {
  1505. BLI_snprintf(stamp_data->camera, sizeof(stamp_data->camera), "Camera %s", stamp_data_template->camera);
  1506. }
  1507. else {
  1508. stamp_data->camera[0] = '\0';
  1509. }
  1510. if (scene->r.stamp & R_STAMP_CAMERALENS) {
  1511. BLI_snprintf(stamp_data->cameralens, sizeof(stamp_data->cameralens), "Lens %s", stamp_data_template->cameralens);
  1512. }
  1513. else {
  1514. stamp_data->cameralens[0] = '\0';
  1515. }
  1516. if (scene->r.stamp & R_STAMP_SCENE) {
  1517. BLI_snprintf(stamp_data->scene, sizeof(stamp_data->scene), "Scene %s", stamp_data_template->scene);
  1518. }
  1519. else {
  1520. stamp_data->scene[0] = '\0';
  1521. }
  1522. if (scene->r.stamp & R_STAMP_SEQSTRIP) {
  1523. BLI_snprintf(stamp_data->strip, sizeof(stamp_data->strip), "Strip %s", stamp_data_template->strip);
  1524. }
  1525. else {
  1526. stamp_data->strip[0] = '\0';
  1527. }
  1528. if (scene->r.stamp & R_STAMP_RENDERTIME) {
  1529. BLI_snprintf(stamp_data->rendertime, sizeof(stamp_data->rendertime), "RenderTime %s", stamp_data_template->rendertime);
  1530. }
  1531. else {
  1532. stamp_data->rendertime[0] = '\0';
  1533. }
  1534. if (scene->r.stamp & R_STAMP_MEMORY) {
  1535. BLI_snprintf(stamp_data->memory, sizeof(stamp_data->memory), "Peak Memory %s", stamp_data_template->memory);
  1536. }
  1537. else {
  1538. stamp_data->memory[0] = '\0';
  1539. }
  1540. }
  1541. void BKE_image_stamp_buf(
  1542. Scene *scene, Object *camera, const StampData *stamp_data_template,
  1543. unsigned char *rect, float *rectf, int width, int height, int channels)
  1544. {
  1545. struct StampData stamp_data;
  1546. float w, h, pad;
  1547. int x, y, y_ofs;
  1548. float h_fixed;
  1549. const int mono = blf_mono_font_render; // XXX
  1550. struct ColorManagedDisplay *display;
  1551. const char *display_device;
  1552. /* vars for calculating wordwrap */
  1553. struct {
  1554. struct ResultBLF info;
  1555. rctf rect;
  1556. } wrap;
  1557. /* this could be an argument if we want to operate on non linear float imbuf's
  1558. * for now though this is only used for renders which use scene settings */
  1559. #define TEXT_SIZE_CHECK(str, w, h) \
  1560. ((str[0]) && ((void)(h = h_fixed), (w = BLF_width(mono, str, sizeof(str)))))
  1561. /* must enable BLF_WORD_WRAP before using */
  1562. #define TEXT_SIZE_CHECK_WORD_WRAP(str, w, h) \
  1563. ((str[0]) && (BLF_boundbox_ex(mono, str, sizeof(str), &wrap.rect, &wrap.info), \
  1564. (void)(h = h_fixed * wrap.info.lines), (w = BLI_rctf_size_x(&wrap.rect))))
  1565. #define BUFF_MARGIN_X 2
  1566. #define BUFF_MARGIN_Y 1
  1567. if (!rect && !rectf)
  1568. return;
  1569. display_device = scene->display_settings.display_device;
  1570. display = IMB_colormanagement_display_get_named(display_device);
  1571. if (stamp_data_template == NULL) {
  1572. stampdata(scene, camera, &stamp_data, (scene->r.stamp & R_STAMP_HIDE_LABELS) == 0);
  1573. }
  1574. else {
  1575. stampdata_from_template(&stamp_data, scene, stamp_data_template);
  1576. }
  1577. /* TODO, do_versions */
  1578. if (scene->r.stamp_font_id < 8)
  1579. scene->r.stamp_font_id = 12;
  1580. /* set before return */
  1581. BLF_size(mono, scene->r.stamp_font_id, 72);
  1582. BLF_wordwrap(mono, width - (BUFF_MARGIN_X * 2));
  1583. BLF_buffer(mono, rectf, rect, width, height, channels, display);
  1584. BLF_buffer_col(mono, scene->r.fg_stamp);
  1585. pad = BLF_width_max(mono);
  1586. /* use 'h_fixed' rather than 'h', aligns better */
  1587. h_fixed = BLF_height_max(mono);
  1588. y_ofs = -BLF_descender(mono);
  1589. x = 0;
  1590. y = height;
  1591. if (TEXT_SIZE_CHECK(stamp_data.file, w, h)) {
  1592. /* Top left corner */
  1593. y -= h;
  1594. /* also a little of space to the background. */
  1595. buf_rectfill_area(rect, rectf, width, height, scene->r.bg_stamp, display,
  1596. x - BUFF_MARGIN_X, y - BUFF_MARGIN_Y, w + BUFF_MARGIN_X, y + h + BUFF_MARGIN_Y);
  1597. /* and draw the text. */
  1598. BLF_position(mono, x, y + y_ofs, 0.0);
  1599. BLF_draw_buffer(mono, stamp_data.file, BLF_DRAW_STR_DUMMY_MAX);
  1600. /* the extra pixel for background. */
  1601. y -= BUFF_MARGIN_Y * 2;
  1602. }
  1603. /* Top left corner, below File */
  1604. if (TEXT_SIZE_CHECK(stamp_data.date, w, h)) {
  1605. y -= h;
  1606. /* and space for background. */
  1607. buf_rectfill_area(rect, rectf, width, height, scene->r.bg_stamp, display,
  1608. 0, y - BUFF_MARGIN_Y, w + BUFF_MARGIN_X, y + h + BUFF_MARGIN_Y);
  1609. BLF_position(mono, x, y + y_ofs, 0.0);
  1610. BLF_draw_buffer(mono, stamp_data.date, BLF_DRAW_STR_DUMMY_MAX);
  1611. /* the extra pixel for background. */
  1612. y -= BUFF_MARGIN_Y * 2;
  1613. }
  1614. /* Top left corner, below File, Date */
  1615. if (TEXT_SIZE_CHECK(stamp_data.rendertime, w, h)) {
  1616. y -= h;
  1617. /* and space for background. */
  1618. buf_rectfill_area(rect, rectf, width, height, scene->r.bg_stamp, display,
  1619. 0, y - BUFF_MARGIN_Y, w + BUFF_MARGIN_X, y + h + BUFF_MARGIN_Y);
  1620. BLF_position(mono, x, y + y_ofs, 0.0);
  1621. BLF_draw_buffer(mono, stamp_data.rendertime, BLF_DRAW_STR_DUMMY_MAX);
  1622. /* the extra pixel for background. */
  1623. y -= BUFF_MARGIN_Y * 2;
  1624. }
  1625. /* Top left corner, below File, Date, Rendertime */
  1626. if (TEXT_SIZE_CHECK(stamp_data.memory, w, h)) {
  1627. y -= h;
  1628. /* and space for background. */
  1629. buf_rectfill_area(rect, rectf, width, height, scene->r.bg_stamp, display,
  1630. 0, y - BUFF_MARGIN_Y, w + BUFF_MARGIN_X, y + h + BUFF_MARGIN_Y);
  1631. BLF_position(mono, x, y + y_ofs, 0.0);
  1632. BLF_draw_buffer(mono, stamp_data.memory, BLF_DRAW_STR_DUMMY_MAX);
  1633. /* the extra pixel for background. */
  1634. y -= BUFF_MARGIN_Y * 2;
  1635. }
  1636. /* Top left corner, below File, Date, Memory, Rendertime */
  1637. BLF_enable(mono, BLF_WORD_WRAP);
  1638. if (TEXT_SIZE_CHECK_WORD_WRAP(stamp_data.note, w, h)) {
  1639. y -= h;
  1640. /* and space for background. */
  1641. buf_rectfill_area(rect, rectf, width, height, scene->r.bg_stamp, display,
  1642. 0, y - BUFF_MARGIN_Y, w + BUFF_MARGIN_X, y + h + BUFF_MARGIN_Y);
  1643. BLF_position(mono, x, y + y_ofs + (h - h_fixed), 0.0);
  1644. BLF_draw_buffer(mono, stamp_data.note, BLF_DRAW_STR_DUMMY_MAX);
  1645. }
  1646. BLF_disable(mono, BLF_WORD_WRAP);
  1647. x = 0;
  1648. y = 0;
  1649. /* Bottom left corner, leaving space for timing */
  1650. if (TEXT_SIZE_CHECK(stamp_data.marker, w, h)) {
  1651. /* extra space for background. */
  1652. buf_rectfill_area(rect, rectf, width, height, scene->r.bg_stamp, display,
  1653. x - BUFF_MARGIN_X, y - BUFF_MARGIN_Y, w + BUFF_MARGIN_X, y + h + BUFF_MARGIN_Y);
  1654. /* and pad the text. */
  1655. BLF_position(mono, x, y + y_ofs, 0.0);
  1656. BLF_draw_buffer(mono, stamp_data.marker, BLF_DRAW_STR_DUMMY_MAX);
  1657. /* space width. */
  1658. x += w + pad;
  1659. }
  1660. /* Left bottom corner */
  1661. if (TEXT_SIZE_CHECK(stamp_data.time, w, h)) {
  1662. /* extra space for background */
  1663. buf_rectfill_area(rect, rectf, width, height, scene->r.bg_stamp, display,
  1664. x - BUFF_MARGIN_X, y, x + w + BUFF_MARGIN_X, y + h + BUFF_MARGIN_Y);
  1665. /* and pad the text. */
  1666. BLF_position(mono, x, y + y_ofs, 0.0);
  1667. BLF_draw_buffer(mono, stamp_data.time, BLF_DRAW_STR_DUMMY_MAX);
  1668. /* space width. */
  1669. x += w + pad;
  1670. }
  1671. if (TEXT_SIZE_CHECK(stamp_data.frame, w, h)) {
  1672. /* extra space for background. */
  1673. buf_rectfill_area(rect, rectf, width, height, scene->r.bg_stamp, display,
  1674. x - BUFF_MARGIN_X, y - BUFF_MARGIN_Y, x + w + BUFF_MARGIN_X, y + h + BUFF_MARGIN_Y);
  1675. /* and pad the text. */
  1676. BLF_position(mono, x, y + y_ofs, 0.0);
  1677. BLF_draw_buffer(mono, stamp_data.frame, BLF_DRAW_STR_DUMMY_MAX);
  1678. /* space width. */
  1679. x += w + pad;
  1680. }
  1681. if (TEXT_SIZE_CHECK(stamp_data.camera, w, h)) {
  1682. /* extra space for background. */
  1683. buf_rectfill_area(rect, rectf, width, height, scene->r.bg_stamp, display,
  1684. x - BUFF_MARGIN_X, y - BUFF_MARGIN_Y, x + w + BUFF_MARGIN_X, y + h + BUFF_MARGIN_Y);
  1685. BLF_position(mono, x, y + y_ofs, 0.0);
  1686. BLF_draw_buffer(mono, stamp_data.camera, BLF_DRAW_STR_DUMMY_MAX);
  1687. /* space width. */
  1688. x += w + pad;
  1689. }
  1690. if (TEXT_SIZE_CHECK(stamp_data.cameralens, w, h)) {
  1691. /* extra space for background. */
  1692. buf_rectfill_area(rect, rectf, width, height, scene->r.bg_stamp, display,
  1693. x - BUFF_MARGIN_X, y - BUFF_MARGIN_Y, x + w + BUFF_MARGIN_X, y + h + BUFF_MARGIN_Y);
  1694. BLF_position(mono, x, y + y_ofs, 0.0);
  1695. BLF_draw_buffer(mono, stamp_data.cameralens, BLF_DRAW_STR_DUMMY_MAX);
  1696. }
  1697. if (TEXT_SIZE_CHECK(stamp_data.scene, w, h)) {
  1698. /* Bottom right corner, with an extra space because blenfont is too strict! */
  1699. x = width - w - 2;
  1700. /* extra space for background. */
  1701. buf_rectfill_area(rect, rectf, width, height, scene->r.bg_stamp, display,
  1702. x - BUFF_MARGIN_X, y - BUFF_MARGIN_Y, x + w + BUFF_MARGIN_X, y + h + BUFF_MARGIN_Y);
  1703. /* and pad the text. */
  1704. BLF_position(mono, x, y + y_ofs, 0.0);
  1705. BLF_draw_buffer(mono, stamp_data.scene, BLF_DRAW_STR_DUMMY_MAX);
  1706. }
  1707. if (TEXT_SIZE_CHECK(stamp_data.strip, w, h)) {
  1708. /* Top right corner, with an extra space because blenfont is too strict! */
  1709. x = width - w - pad;
  1710. y = height - h;
  1711. /* extra space for background. */
  1712. buf_rectfill_area(rect, rectf, width, height, scene->r.bg_stamp, display,
  1713. x - BUFF_MARGIN_X, y - BUFF_MARGIN_Y, x + w + BUFF_MARGIN_X, y + h + BUFF_MARGIN_Y);
  1714. BLF_position(mono, x, y + y_ofs, 0.0);
  1715. BLF_draw_buffer(mono, stamp_data.strip, BLF_DRAW_STR_DUMMY_MAX);
  1716. }
  1717. /* cleanup the buffer. */
  1718. BLF_buffer(mono, NULL, NULL, 0, 0, 0, NULL);
  1719. BLF_wordwrap(mono, 0);
  1720. #undef TEXT_SIZE_CHECK
  1721. #undef TEXT_SIZE_CHECK_WORD_WRAP
  1722. #undef BUFF_MARGIN_X
  1723. #undef BUFF_MARGIN_Y
  1724. }
  1725. void BKE_render_result_stamp_info(Scene *scene, Object *camera, struct RenderResult *rr, bool allocate_only)
  1726. {
  1727. struct StampData *stamp_data;
  1728. if (!(scene && (scene->r.stamp & R_STAMP_ALL)) && !allocate_only)
  1729. return;
  1730. if (!rr->stamp_data) {
  1731. stamp_data = MEM_callocN(sizeof(StampData), "RenderResult.stamp_data");
  1732. }
  1733. else {
  1734. stamp_data = rr->stamp_data;
  1735. }
  1736. if (!allocate_only)
  1737. stampdata(scene, camera, stamp_data, 0);
  1738. if (!rr->stamp_data) {
  1739. rr->stamp_data = stamp_data;
  1740. }
  1741. }
  1742. void BKE_stamp_info_callback(void *data, struct StampData *stamp_data, StampCallback callback, bool noskip)
  1743. {
  1744. if ((callback == NULL) || (stamp_data == NULL)) {
  1745. return;
  1746. }
  1747. #define CALL(member, value_str) \
  1748. if (noskip || stamp_data->member[0]) { \
  1749. callback(data, value_str, stamp_data->member, sizeof(stamp_data->member)); \
  1750. } ((void)0)
  1751. CALL(file, "File");
  1752. CALL(note, "Note");
  1753. CALL(date, "Date");
  1754. CALL(marker, "Marker");
  1755. CALL(time, "Time");
  1756. CALL(frame, "Frame");
  1757. CALL(camera, "Camera");
  1758. CALL(cameralens, "Lens");
  1759. CALL(scene, "Scene");
  1760. CALL(strip, "Strip");
  1761. CALL(rendertime, "RenderTime");
  1762. CALL(memory, "Memory");
  1763. #undef CALL
  1764. }
  1765. /* wrap for callback only */
  1766. static void metadata_change_field(void *data, const char *propname, char *propvalue, int UNUSED(len))
  1767. {
  1768. IMB_metadata_change_field(data, propname, propvalue);
  1769. }
  1770. static void metadata_get_field(void *data, const char *propname, char *propvalue, int len)
  1771. {
  1772. IMB_metadata_get_field(data, propname, propvalue, len);
  1773. }
  1774. void BKE_imbuf_stamp_info(RenderResult *rr, struct ImBuf *ibuf)
  1775. {
  1776. struct StampData *stamp_data = rr->stamp_data;
  1777. BKE_stamp_info_callback(ibuf, stamp_data, metadata_change_field, false);
  1778. }
  1779. void BKE_stamp_info_from_imbuf(RenderResult *rr, struct ImBuf *ibuf)
  1780. {
  1781. struct StampData *stamp_data = rr->stamp_data;
  1782. BKE_stamp_info_callback(ibuf, stamp_data, metadata_get_field, true);
  1783. }
  1784. bool BKE_imbuf_alpha_test(ImBuf *ibuf)
  1785. {
  1786. int tot;
  1787. if (ibuf->rect_float) {
  1788. const float *buf = ibuf->rect_float;
  1789. for (tot = ibuf->x * ibuf->y; tot--; buf += 4) {
  1790. if (buf[3] < 1.0f) {
  1791. return true;
  1792. }
  1793. }
  1794. }
  1795. else if (ibuf->rect) {
  1796. unsigned char *buf = (unsigned char *)ibuf->rect;
  1797. for (tot = ibuf->x * ibuf->y; tot--; buf += 4) {
  1798. if (buf[3] != 255) {
  1799. return true;
  1800. }
  1801. }
  1802. }
  1803. return false;
  1804. }
  1805. /* note: imf->planes is ignored here, its assumed the image channels
  1806. * are already set */
  1807. void BKE_imbuf_write_prepare(ImBuf *ibuf, const ImageFormatData *imf)
  1808. {
  1809. char imtype = imf->imtype;
  1810. char compress = imf->compress;
  1811. char quality = imf->quality;
  1812. /* initialize all from image format */
  1813. ibuf->foptions.flag = 0;
  1814. if (imtype == R_IMF_IMTYPE_IRIS) {
  1815. ibuf->ftype = IMB_FTYPE_IMAGIC;
  1816. }
  1817. #ifdef WITH_HDR
  1818. else if (imtype == R_IMF_IMTYPE_RADHDR) {
  1819. ibuf->ftype = IMB_FTYPE_RADHDR;
  1820. }
  1821. #endif
  1822. else if (ELEM(imtype, R_IMF_IMTYPE_PNG, R_IMF_IMTYPE_FFMPEG, R_IMF_IMTYPE_H264, R_IMF_IMTYPE_THEORA, R_IMF_IMTYPE_XVID)) {
  1823. ibuf->ftype = IMB_FTYPE_PNG;
  1824. if (imtype == R_IMF_IMTYPE_PNG) {
  1825. if (imf->depth == R_IMF_CHAN_DEPTH_16)
  1826. ibuf->foptions.flag |= PNG_16BIT;
  1827. ibuf->foptions.quality = compress;
  1828. }
  1829. }
  1830. #ifdef WITH_DDS
  1831. else if (imtype == R_IMF_IMTYPE_DDS) {
  1832. ibuf->ftype = IMB_FTYPE_DDS;
  1833. }
  1834. #endif
  1835. else if (imtype == R_IMF_IMTYPE_BMP) {
  1836. ibuf->ftype = IMB_FTYPE_BMP;
  1837. }
  1838. #ifdef WITH_TIFF
  1839. else if (imtype == R_IMF_IMTYPE_TIFF) {
  1840. ibuf->ftype = IMB_FTYPE_TIF;
  1841. if (imf->depth == R_IMF_CHAN_DEPTH_16) {
  1842. ibuf->foptions.flag |= TIF_16BIT;
  1843. }
  1844. if (imf->tiff_codec == R_IMF_TIFF_CODEC_NONE) {
  1845. ibuf->foptions.flag |= TIF_COMPRESS_NONE;
  1846. }
  1847. else if (imf->tiff_codec == R_IMF_TIFF_CODEC_DEFLATE) {
  1848. ibuf->foptions.flag |= TIF_COMPRESS_DEFLATE;
  1849. }
  1850. else if (imf->tiff_codec == R_IMF_TIFF_CODEC_LZW) {
  1851. ibuf->foptions.flag |= TIF_COMPRESS_LZW;
  1852. }
  1853. else if (imf->tiff_codec == R_IMF_TIFF_CODEC_PACKBITS) {
  1854. ibuf->foptions.flag |= TIF_COMPRESS_PACKBITS;
  1855. }
  1856. }
  1857. #endif
  1858. #ifdef WITH_OPENEXR
  1859. else if (ELEM(imtype, R_IMF_IMTYPE_OPENEXR, R_IMF_IMTYPE_MULTILAYER)) {
  1860. ibuf->ftype = IMB_FTYPE_OPENEXR;
  1861. if (imf->depth == R_IMF_CHAN_DEPTH_16)
  1862. ibuf->foptions.flag |= OPENEXR_HALF;
  1863. ibuf->foptions.flag |= (imf->exr_codec & OPENEXR_COMPRESS);
  1864. if (!(imf->flag & R_IMF_FLAG_ZBUF)) {
  1865. /* Signal for exr saving. */
  1866. IMB_freezbuffloatImBuf(ibuf);
  1867. }
  1868. }
  1869. #endif
  1870. #ifdef WITH_CINEON
  1871. else if (imtype == R_IMF_IMTYPE_CINEON) {
  1872. ibuf->ftype = IMB_FTYPE_CINEON;
  1873. if (imf->cineon_flag & R_IMF_CINEON_FLAG_LOG) {
  1874. ibuf->foptions.flag |= CINEON_LOG;
  1875. }
  1876. if (imf->depth == R_IMF_CHAN_DEPTH_16) {
  1877. ibuf->foptions.flag |= CINEON_16BIT;
  1878. }
  1879. else if (imf->depth == R_IMF_CHAN_DEPTH_12) {
  1880. ibuf->foptions.flag |= CINEON_12BIT;
  1881. }
  1882. else if (imf->depth == R_IMF_CHAN_DEPTH_10) {
  1883. ibuf->foptions.flag |= CINEON_10BIT;
  1884. }
  1885. }
  1886. else if (imtype == R_IMF_IMTYPE_DPX) {
  1887. ibuf->ftype = IMB_FTYPE_DPX;
  1888. if (imf->cineon_flag & R_IMF_CINEON_FLAG_LOG) {
  1889. ibuf->foptions.flag |= CINEON_LOG;
  1890. }
  1891. if (imf->depth == R_IMF_CHAN_DEPTH_16) {
  1892. ibuf->foptions.flag |= CINEON_16BIT;
  1893. }
  1894. else if (imf->depth == R_IMF_CHAN_DEPTH_12) {
  1895. ibuf->foptions.flag |= CINEON_12BIT;
  1896. }
  1897. else if (imf->depth == R_IMF_CHAN_DEPTH_10) {
  1898. ibuf->foptions.flag |= CINEON_10BIT;
  1899. }
  1900. }
  1901. #endif
  1902. else if (imtype == R_IMF_IMTYPE_TARGA) {
  1903. ibuf->ftype = IMB_FTYPE_TGA;
  1904. }
  1905. else if (imtype == R_IMF_IMTYPE_RAWTGA) {
  1906. ibuf->ftype = IMB_FTYPE_TGA;
  1907. ibuf->foptions.flag = RAWTGA;
  1908. }
  1909. #ifdef WITH_OPENJPEG
  1910. else if (imtype == R_IMF_IMTYPE_JP2) {
  1911. if (quality < 10) quality = 90;
  1912. ibuf->ftype = IMB_FTYPE_JP2;
  1913. ibuf->foptions.quality = quality;
  1914. if (imf->depth == R_IMF_CHAN_DEPTH_16) {
  1915. ibuf->foptions.flag |= JP2_16BIT;
  1916. }
  1917. else if (imf->depth == R_IMF_CHAN_DEPTH_12) {
  1918. ibuf->foptions.flag |= JP2_12BIT;
  1919. }
  1920. if (imf->jp2_flag & R_IMF_JP2_FLAG_YCC) {
  1921. ibuf->foptions.flag |= JP2_YCC;
  1922. }
  1923. if (imf->jp2_flag & R_IMF_JP2_FLAG_CINE_PRESET) {
  1924. ibuf->foptions.flag |= JP2_CINE;
  1925. if (imf->jp2_flag & R_IMF_JP2_FLAG_CINE_48)
  1926. ibuf->foptions.flag |= JP2_CINE_48FPS;
  1927. }
  1928. if (imf->jp2_codec == R_IMF_JP2_CODEC_JP2)
  1929. ibuf->foptions.flag |= JP2_JP2;
  1930. else if (imf->jp2_codec == R_IMF_JP2_CODEC_J2K)
  1931. ibuf->foptions.flag |= JP2_J2K;
  1932. else
  1933. BLI_assert(!"Unsupported jp2 codec was specified in im_format->jp2_codec");
  1934. }
  1935. #endif
  1936. else {
  1937. /* R_IMF_IMTYPE_JPEG90, etc. default we save jpegs */
  1938. if (quality < 10) quality = 90;
  1939. ibuf->ftype = IMB_FTYPE_JPG;
  1940. ibuf->foptions.quality = quality;
  1941. }
  1942. }
  1943. int BKE_imbuf_write(ImBuf *ibuf, const char *name, const ImageFormatData *imf)
  1944. {
  1945. int ok;
  1946. BKE_imbuf_write_prepare(ibuf, imf);
  1947. BLI_make_existing_file(name);
  1948. ok = IMB_saveiff(ibuf, name, IB_rect | IB_zbuf | IB_zbuffloat);
  1949. if (ok == 0) {
  1950. perror(name);
  1951. }
  1952. return(ok);
  1953. }
  1954. /* same as BKE_imbuf_write() but crappy workaround not to permanently modify
  1955. * _some_, values in the imbuf */
  1956. int BKE_imbuf_write_as(ImBuf *ibuf, const char *name, ImageFormatData *imf,
  1957. const bool save_copy)
  1958. {
  1959. ImBuf ibuf_back = *ibuf;
  1960. int ok;
  1961. /* all data is rgba anyway,
  1962. * this just controls how to save for some formats */
  1963. ibuf->planes = imf->planes;
  1964. ok = BKE_imbuf_write(ibuf, name, imf);
  1965. if (save_copy) {
  1966. /* note that we are not restoring _all_ settings */
  1967. ibuf->planes = ibuf_back.planes;
  1968. ibuf->ftype = ibuf_back.ftype;
  1969. ibuf->foptions = ibuf_back.foptions;
  1970. }
  1971. return ok;
  1972. }
  1973. int BKE_imbuf_write_stamp(
  1974. Scene *scene, struct RenderResult *rr, ImBuf *ibuf, const char *name,
  1975. const struct ImageFormatData *imf)
  1976. {
  1977. if (scene && scene->r.stamp & R_STAMP_ALL)
  1978. BKE_imbuf_stamp_info(rr, ibuf);
  1979. return BKE_imbuf_write(ibuf, name, imf);
  1980. }
  1981. static void do_makepicstring(
  1982. char *string, const char *base, const char *relbase, int frame, const char imtype,
  1983. const ImageFormatData *im_format, const short use_ext, const short use_frames,
  1984. const char *suffix)
  1985. {
  1986. if (string == NULL) return;
  1987. BLI_strncpy(string, base, FILE_MAX - 10); /* weak assumption */
  1988. BLI_path_abs(string, relbase);
  1989. if (use_frames)
  1990. BLI_path_frame(string, frame, 4);
  1991. if (suffix)
  1992. BLI_path_suffix(string, FILE_MAX, suffix, "");
  1993. if (use_ext)
  1994. do_add_image_extension(string, imtype, im_format);
  1995. }
  1996. void BKE_image_path_from_imformat(
  1997. char *string, const char *base, const char *relbase, int frame,
  1998. const ImageFormatData *im_format, const bool use_ext, const bool use_frames, const char *suffix)
  1999. {
  2000. do_makepicstring(string, base, relbase, frame, im_format->imtype, im_format, use_ext, use_frames, suffix);
  2001. }
  2002. void BKE_image_path_from_imtype(
  2003. char *string, const char *base, const char *relbase, int frame,
  2004. const char imtype, const bool use_ext, const bool use_frames, const char *view)
  2005. {
  2006. do_makepicstring(string, base, relbase, frame, imtype, NULL, use_ext, use_frames, view);
  2007. }
  2008. struct anim *openanim_noload(const char *name, int flags, int streamindex, char colorspace[IMA_MAX_SPACE])
  2009. {
  2010. struct anim *anim;
  2011. anim = IMB_open_anim(name, flags, streamindex, colorspace);
  2012. return anim;
  2013. }
  2014. /* used by sequencer too */
  2015. struct anim *openanim(const char *name, int flags, int streamindex, char colorspace[IMA_MAX_SPACE])
  2016. {
  2017. struct anim *anim;
  2018. struct ImBuf *ibuf;
  2019. anim = IMB_open_anim(name, flags, streamindex, colorspace);
  2020. if (anim == NULL) return NULL;
  2021. ibuf = IMB_anim_absolute(anim, 0, IMB_TC_NONE, IMB_PROXY_NONE);
  2022. if (ibuf == NULL) {
  2023. if (BLI_exists(name))
  2024. printf("not an anim: %s\n", name);
  2025. else
  2026. printf("anim file doesn't exist: %s\n", name);
  2027. IMB_free_anim(anim);
  2028. return NULL;
  2029. }
  2030. IMB_freeImBuf(ibuf);
  2031. return(anim);
  2032. }
  2033. /* ************************* New Image API *************** */
  2034. /* Notes about Image storage
  2035. * - packedfile
  2036. * -> written in .blend
  2037. * - filename
  2038. * -> written in .blend
  2039. * - movie
  2040. * -> comes from packedfile or filename
  2041. * - renderresult
  2042. * -> comes from packedfile or filename
  2043. * - listbase
  2044. * -> ibufs from exrhandle
  2045. * - flipbook array
  2046. * -> ibufs come from movie, temporary renderresult or sequence
  2047. * - ibuf
  2048. * -> comes from packedfile or filename or generated
  2049. */
  2050. /* forces existence of 1 Image for renderout or nodes, returns Image */
  2051. /* name is only for default, when making new one */
  2052. Image *BKE_image_verify_viewer(int type, const char *name)
  2053. {
  2054. Image *ima;
  2055. for (ima = G.main->image.first; ima; ima = ima->id.next)
  2056. if (ima->source == IMA_SRC_VIEWER)
  2057. if (ima->type == type)
  2058. break;
  2059. if (ima == NULL)
  2060. ima = image_alloc(G.main, name, IMA_SRC_VIEWER, type);
  2061. /* happens on reload, imagewindow cannot be image user when hidden*/
  2062. if (ima->id.us == 0)
  2063. id_us_plus(&ima->id);
  2064. return ima;
  2065. }
  2066. static void image_viewer_create_views(const RenderData *rd, Image *ima)
  2067. {
  2068. if ((rd->scemode & R_MULTIVIEW) == 0) {
  2069. image_add_view(ima, "", "");
  2070. }
  2071. else {
  2072. SceneRenderView *srv;
  2073. for (srv = rd->views.first; srv; srv = srv->next) {
  2074. if (BKE_scene_multiview_is_render_view_active(rd, srv) == false)
  2075. continue;
  2076. image_add_view(ima, srv->name, "");
  2077. }
  2078. }
  2079. }
  2080. /* Reset the image cache and views when the Viewer Nodes views don't match the scene views */
  2081. void BKE_image_verify_viewer_views(const RenderData *rd, Image *ima, ImageUser *iuser)
  2082. {
  2083. bool do_reset;
  2084. const bool is_multiview = (rd->scemode & R_MULTIVIEW) != 0;
  2085. BLI_lock_thread(LOCK_DRAW_IMAGE);
  2086. if (!BKE_scene_multiview_is_stereo3d(rd))
  2087. iuser->flag &= ~IMA_SHOW_STEREO;
  2088. /* see if all scene render views are in the image view list */
  2089. do_reset = (BKE_scene_multiview_num_views_get(rd) != BLI_listbase_count(&ima->views));
  2090. /* multiview also needs to be sure all the views are synced */
  2091. if (is_multiview && !do_reset) {
  2092. SceneRenderView *srv;
  2093. ImageView *iv;
  2094. for (iv = ima->views.first; iv; iv = iv->next) {
  2095. srv = BLI_findstring(&rd->views, iv->name, offsetof(SceneRenderView, name));
  2096. if ((srv == NULL) || (BKE_scene_multiview_is_render_view_active(rd, srv) == false)) {
  2097. do_reset = true;
  2098. break;
  2099. }
  2100. }
  2101. }
  2102. if (do_reset) {
  2103. BLI_spin_lock(&image_spin);
  2104. image_free_cached_frames(ima);
  2105. BKE_image_free_views(ima);
  2106. /* add new views */
  2107. image_viewer_create_views(rd, ima);
  2108. BLI_spin_unlock(&image_spin);
  2109. }
  2110. BLI_unlock_thread(LOCK_DRAW_IMAGE);
  2111. }
  2112. void BKE_image_walk_all_users(const Main *mainp, void *customdata,
  2113. void callback(Image *ima, ImageUser *iuser, void *customdata))
  2114. {
  2115. wmWindowManager *wm;
  2116. wmWindow *win;
  2117. Tex *tex;
  2118. /* texture users */
  2119. for (tex = mainp->tex.first; tex; tex = tex->id.next) {
  2120. if (tex->type == TEX_IMAGE && tex->ima) {
  2121. callback(tex->ima, &tex->iuser, customdata);
  2122. }
  2123. if (tex->nodetree) {
  2124. bNode *node;
  2125. for (node = tex->nodetree->nodes.first; node; node = node->next) {
  2126. if (node->id && node->type == TEX_NODE_IMAGE) {
  2127. Image *ima = (Image *)node->id;
  2128. ImageUser *iuser = node->storage;
  2129. callback(ima, iuser, customdata);
  2130. }
  2131. }
  2132. }
  2133. }
  2134. /* image window, compo node users */
  2135. for (wm = mainp->wm.first; wm; wm = wm->id.next) { /* only 1 wm */
  2136. for (win = wm->windows.first; win; win = win->next) {
  2137. ScrArea *sa;
  2138. for (sa = win->screen->areabase.first; sa; sa = sa->next) {
  2139. if (sa->spacetype == SPACE_VIEW3D) {
  2140. View3D *v3d = sa->spacedata.first;
  2141. BGpic *bgpic;
  2142. for (bgpic = v3d->bgpicbase.first; bgpic; bgpic = bgpic->next) {
  2143. callback(bgpic->ima, &bgpic->iuser, customdata);
  2144. }
  2145. }
  2146. else if (sa->spacetype == SPACE_IMAGE) {
  2147. SpaceImage *sima = sa->spacedata.first;
  2148. callback(sima->image, &sima->iuser, customdata);
  2149. }
  2150. else if (sa->spacetype == SPACE_NODE) {
  2151. SpaceNode *snode = sa->spacedata.first;
  2152. if (snode->nodetree && snode->nodetree->type == NTREE_COMPOSIT) {
  2153. bNode *node;
  2154. for (node = snode->nodetree->nodes.first; node; node = node->next) {
  2155. if (node->id && node->type == CMP_NODE_IMAGE) {
  2156. Image *ima = (Image *)node->id;
  2157. ImageUser *iuser = node->storage;
  2158. callback(ima, iuser, customdata);
  2159. }
  2160. }
  2161. }
  2162. }
  2163. }
  2164. }
  2165. }
  2166. }
  2167. static void image_tag_frame_recalc(Image *ima, ImageUser *iuser, void *customdata)
  2168. {
  2169. Image *changed_image = customdata;
  2170. if (ima == changed_image && BKE_image_is_animated(ima)) {
  2171. iuser->flag |= IMA_NEED_FRAME_RECALC;
  2172. }
  2173. }
  2174. static void image_init_imageuser(Image *ima, ImageUser *iuser)
  2175. {
  2176. RenderResult *rr = ima->rr;
  2177. iuser->multi_index = 0;
  2178. iuser->layer = iuser->pass = iuser->view = 0;
  2179. if (rr)
  2180. BKE_image_multilayer_index(rr, iuser);
  2181. }
  2182. void BKE_image_init_imageuser(Image *ima, ImageUser *iuser)
  2183. {
  2184. image_init_imageuser(ima, iuser);
  2185. }
  2186. void BKE_image_signal(Image *ima, ImageUser *iuser, int signal)
  2187. {
  2188. if (ima == NULL)
  2189. return;
  2190. BLI_spin_lock(&image_spin);
  2191. switch (signal) {
  2192. case IMA_SIGNAL_FREE:
  2193. BKE_image_free_buffers(ima);
  2194. if (iuser) {
  2195. iuser->ok = 1;
  2196. if (iuser->scene) {
  2197. image_update_views_format(ima, iuser);
  2198. }
  2199. }
  2200. break;
  2201. case IMA_SIGNAL_SRC_CHANGE:
  2202. if (ima->type == IMA_TYPE_UV_TEST)
  2203. if (ima->source != IMA_SRC_GENERATED)
  2204. ima->type = IMA_TYPE_IMAGE;
  2205. if (ima->source == IMA_SRC_GENERATED) {
  2206. if (ima->gen_x == 0 || ima->gen_y == 0) {
  2207. ImBuf *ibuf = image_get_cached_ibuf_for_index_frame(ima, IMA_NO_INDEX, 0);
  2208. if (ibuf) {
  2209. ima->gen_x = ibuf->x;
  2210. ima->gen_y = ibuf->y;
  2211. IMB_freeImBuf(ibuf);
  2212. }
  2213. }
  2214. /* Changing source type to generated will likely change file format
  2215. * used by generated image buffer. Saving different file format to
  2216. * the old name might confuse other applications.
  2217. *
  2218. * Here we ensure original image path wouldn't be used when saving
  2219. * generated image.
  2220. */
  2221. ima->name[0] = '\0';
  2222. }
  2223. #if 0
  2224. /* force reload on first use, but not for multilayer, that makes nodes and buttons in ui drawing fail */
  2225. if (ima->type != IMA_TYPE_MULTILAYER)
  2226. BKE_image_free_buffers(ima);
  2227. #else
  2228. /* image buffers for non-sequence multilayer will share buffers with RenderResult,
  2229. * however sequence multilayer will own buffers. Such logic makes switching from
  2230. * single multilayer file to sequence completely unstable
  2231. * since changes in nodes seems this workaround isn't needed anymore, all sockets
  2232. * are nicely detecting anyway, but freeing buffers always here makes multilayer
  2233. * sequences behave stable
  2234. */
  2235. BKE_image_free_buffers(ima);
  2236. #endif
  2237. ima->ok = 1;
  2238. if (iuser)
  2239. iuser->ok = 1;
  2240. BKE_image_walk_all_users(G.main, ima, image_tag_frame_recalc);
  2241. break;
  2242. case IMA_SIGNAL_RELOAD:
  2243. /* try to repack file */
  2244. if (BKE_image_has_packedfile(ima)) {
  2245. const int totfiles = image_num_files(ima);
  2246. if (totfiles != BLI_listbase_count_ex(&ima->packedfiles, totfiles + 1)) {
  2247. /* in case there are new available files to be loaded */
  2248. image_free_packedfiles(ima);
  2249. BKE_image_packfiles(NULL, ima, ID_BLEND_PATH(G.main, &ima->id));
  2250. }
  2251. else {
  2252. ImagePackedFile *imapf;
  2253. for (imapf = ima->packedfiles.first; imapf; imapf = imapf->next) {
  2254. PackedFile *pf;
  2255. pf = newPackedFile(NULL, imapf->filepath, ID_BLEND_PATH(G.main, &ima->id));
  2256. if (pf) {
  2257. freePackedFile(imapf->packedfile);
  2258. imapf->packedfile = pf;
  2259. }
  2260. else {
  2261. printf("ERROR: Image \"%s\" not available. Keeping packed image\n", imapf->filepath);
  2262. }
  2263. }
  2264. }
  2265. if (BKE_image_has_packedfile(ima))
  2266. BKE_image_free_buffers(ima);
  2267. }
  2268. else
  2269. BKE_image_free_buffers(ima);
  2270. if (iuser) {
  2271. iuser->ok = 1;
  2272. if (iuser->scene) {
  2273. image_update_views_format(ima, iuser);
  2274. }
  2275. }
  2276. break;
  2277. case IMA_SIGNAL_USER_NEW_IMAGE:
  2278. if (iuser) {
  2279. iuser->ok = 1;
  2280. if (ima->source == IMA_SRC_FILE || ima->source == IMA_SRC_SEQUENCE) {
  2281. if (ima->type == IMA_TYPE_MULTILAYER) {
  2282. image_init_imageuser(ima, iuser);
  2283. }
  2284. }
  2285. }
  2286. break;
  2287. case IMA_SIGNAL_COLORMANAGE:
  2288. BKE_image_free_buffers(ima);
  2289. ima->ok = 1;
  2290. if (iuser)
  2291. iuser->ok = 1;
  2292. break;
  2293. }
  2294. BLI_spin_unlock(&image_spin);
  2295. /* don't use notifiers because they are not 100% sure to succeeded
  2296. * this also makes sure all scenes are accounted for. */
  2297. {
  2298. Scene *scene;
  2299. for (scene = G.main->scene.first; scene; scene = scene->id.next) {
  2300. if (scene->nodetree) {
  2301. nodeUpdateID(scene->nodetree, &ima->id);
  2302. }
  2303. }
  2304. }
  2305. }
  2306. /* return renderpass for a given pass index and active view */
  2307. /* fallback to available if there are missing passes for active view */
  2308. static RenderPass *image_render_pass_get(RenderLayer *rl, const int pass, const int view, int *r_passindex)
  2309. {
  2310. RenderPass *rpass_ret = NULL;
  2311. RenderPass *rpass;
  2312. int rp_index = 0;
  2313. const char *rp_name = "";
  2314. for (rpass = rl->passes.first; rpass; rpass = rpass->next, rp_index++) {
  2315. if (rp_index == pass) {
  2316. rpass_ret = rpass;
  2317. if (view == 0) {
  2318. /* no multiview or left eye */
  2319. break;
  2320. }
  2321. else {
  2322. rp_name = rpass->name;
  2323. }
  2324. }
  2325. /* multiview */
  2326. else if (rp_name[0] &&
  2327. STREQ(rpass->name, rp_name) &&
  2328. (rpass->view_id == view))
  2329. {
  2330. rpass_ret = rpass;
  2331. break;
  2332. }
  2333. }
  2334. /* fallback to the first pass in the layer */
  2335. if (rpass_ret == NULL) {
  2336. rp_index = 0;
  2337. rpass_ret = rl->passes.first;
  2338. }
  2339. if (r_passindex) {
  2340. *r_passindex = (rpass == rpass_ret ? rp_index : pass);
  2341. }
  2342. return rpass_ret;
  2343. }
  2344. /* if layer or pass changes, we need an index for the imbufs list */
  2345. /* note it is called for rendered results, but it doesnt use the index! */
  2346. /* and because rendered results use fake layer/passes, don't correct for wrong indices here */
  2347. RenderPass *BKE_image_multilayer_index(RenderResult *rr, ImageUser *iuser)
  2348. {
  2349. RenderLayer *rl;
  2350. RenderPass *rpass = NULL;
  2351. if (rr == NULL)
  2352. return NULL;
  2353. if (iuser) {
  2354. short index = 0, rv_index, rl_index = 0;
  2355. bool is_stereo = (iuser->flag & IMA_SHOW_STEREO) && RE_RenderResult_is_stereo(rr);
  2356. rv_index = is_stereo ? iuser->multiview_eye : iuser->view;
  2357. if (RE_HasFakeLayer(rr)) rl_index += 1;
  2358. for (rl = rr->layers.first; rl; rl = rl->next, rl_index++) {
  2359. if (iuser->layer == rl_index) {
  2360. int rp_index;
  2361. rpass = image_render_pass_get(rl, iuser->pass, rv_index, &rp_index);
  2362. iuser->multi_index = index + rp_index;
  2363. break;
  2364. }
  2365. else {
  2366. index += BLI_listbase_count(&rl->passes);
  2367. }
  2368. }
  2369. }
  2370. return rpass;
  2371. }
  2372. void BKE_image_multiview_index(Image *ima, ImageUser *iuser)
  2373. {
  2374. if (iuser) {
  2375. bool is_stereo = BKE_image_is_stereo(ima) && (iuser->flag & IMA_SHOW_STEREO);
  2376. if (is_stereo) {
  2377. iuser->multi_index = iuser->multiview_eye;
  2378. }
  2379. else {
  2380. if ((iuser->view < 0) || (iuser->view >= BLI_listbase_count_ex(&ima->views, iuser->view + 1))) {
  2381. iuser->multi_index = iuser->view = 0;
  2382. }
  2383. else {
  2384. iuser->multi_index = iuser->view;
  2385. }
  2386. }
  2387. }
  2388. }
  2389. /* if layer or pass changes, we need an index for the imbufs list */
  2390. /* note it is called for rendered results, but it doesnt use the index! */
  2391. /* and because rendered results use fake layer/passes, don't correct for wrong indices here */
  2392. bool BKE_image_is_multilayer(Image *ima)
  2393. {
  2394. if (ELEM(ima->source, IMA_SRC_FILE, IMA_SRC_SEQUENCE)) {
  2395. if (ima->type == IMA_TYPE_MULTILAYER) {
  2396. return true;
  2397. }
  2398. }
  2399. else if (ima->source == IMA_SRC_VIEWER) {
  2400. if (ima->type == IMA_TYPE_R_RESULT) {
  2401. return true;
  2402. }
  2403. }
  2404. return false;
  2405. }
  2406. bool BKE_image_is_multiview(Image *ima)
  2407. {
  2408. return (BLI_listbase_count_ex(&ima->views, 2) > 1);
  2409. }
  2410. bool BKE_image_is_stereo(Image *ima)
  2411. {
  2412. return BKE_image_is_multiview(ima) &&
  2413. (BLI_findstring(&ima->views, STEREO_LEFT_NAME, offsetof(ImageView, name)) &&
  2414. BLI_findstring(&ima->views, STEREO_RIGHT_NAME, offsetof(ImageView, name)));
  2415. }
  2416. static void image_init_multilayer_multiview(Image *ima, RenderResult *rr)
  2417. {
  2418. /* update image views from render views, but only if they actually changed,
  2419. * to avoid invalid memory access during render. ideally these should always
  2420. * be acquired with a mutex along with the render result, but there are still
  2421. * some places with just an image pointer that need to access views */
  2422. if (rr && BLI_listbase_count(&ima->views) == BLI_listbase_count(&rr->views)) {
  2423. ImageView *iv = ima->views.first;
  2424. RenderView *rv = rr->views.first;
  2425. bool modified = false;
  2426. for (; rv; rv = rv->next, iv = iv->next) {
  2427. modified |= !STREQ(rv->name, iv->name);
  2428. }
  2429. if (!modified)
  2430. return;
  2431. }
  2432. BKE_image_free_views(ima);
  2433. if (rr) {
  2434. for (RenderView *rv = rr->views.first; rv; rv = rv->next) {
  2435. ImageView *iv = MEM_callocN(sizeof(ImageView), "Viewer Image View");
  2436. BLI_strncpy(iv->name, rv->name, sizeof(iv->name));
  2437. BLI_addtail(&ima->views, iv);
  2438. }
  2439. }
  2440. }
  2441. RenderResult *BKE_image_acquire_renderresult(Scene *scene, Image *ima)
  2442. {
  2443. RenderResult *rr = NULL;
  2444. if (ima->rr) {
  2445. rr = ima->rr;
  2446. }
  2447. else if (ima->type == IMA_TYPE_R_RESULT) {
  2448. if (ima->render_slot == ima->last_render_slot)
  2449. rr = RE_AcquireResultRead(RE_GetRender(scene->id.name));
  2450. else
  2451. rr = ima->renders[ima->render_slot];
  2452. /* set proper views */
  2453. image_init_multilayer_multiview(ima, rr);
  2454. }
  2455. return rr;
  2456. }
  2457. void BKE_image_release_renderresult(Scene *scene, Image *ima)
  2458. {
  2459. if (ima->rr) {
  2460. /* pass */
  2461. }
  2462. else if (ima->type == IMA_TYPE_R_RESULT) {
  2463. if (ima->render_slot == ima->last_render_slot)
  2464. RE_ReleaseResult(RE_GetRender(scene->id.name));
  2465. }
  2466. }
  2467. bool BKE_image_is_openexr(struct Image *ima)
  2468. {
  2469. #ifdef WITH_OPENEXR
  2470. if (ELEM(ima->source, IMA_SRC_FILE, IMA_SRC_SEQUENCE)) {
  2471. return BLI_testextensie(ima->name, ".exr");
  2472. }
  2473. #else
  2474. UNUSED_VARS(ima);
  2475. #endif
  2476. return false;
  2477. }
  2478. void BKE_image_backup_render(Scene *scene, Image *ima, bool free_current_slot)
  2479. {
  2480. /* called right before rendering, ima->renders contains render
  2481. * result pointers for everything but the current render */
  2482. Render *re = RE_GetRender(scene->id.name);
  2483. int slot = ima->render_slot, last = ima->last_render_slot;
  2484. if (slot != last) {
  2485. ima->renders[last] = NULL;
  2486. RE_SwapResult(re, &ima->renders[last]);
  2487. if (ima->renders[slot]) {
  2488. if (free_current_slot) {
  2489. RE_FreeRenderResult(ima->renders[slot]);
  2490. ima->renders[slot] = NULL;
  2491. }
  2492. else {
  2493. RE_SwapResult(re, &ima->renders[slot]);
  2494. }
  2495. }
  2496. }
  2497. ima->last_render_slot = slot;
  2498. }
  2499. /**************************** multiview save openexr *********************************/
  2500. #ifdef WITH_OPENEXR
  2501. static const char *image_get_view_cb(void *base, const int view_id)
  2502. {
  2503. Image *ima = base;
  2504. ImageView *iv = BLI_findlink(&ima->views, view_id);
  2505. return iv ? iv->name : "";
  2506. }
  2507. #endif /* WITH_OPENEXR */
  2508. #ifdef WITH_OPENEXR
  2509. static ImBuf *image_get_buffer_cb(void *base, const int view_id)
  2510. {
  2511. Image *ima = base;
  2512. ImageUser iuser = {0};
  2513. iuser.view = view_id;
  2514. iuser.ok = 1;
  2515. BKE_image_multiview_index(ima, &iuser);
  2516. return image_acquire_ibuf(ima, &iuser, NULL);
  2517. }
  2518. #endif /* WITH_OPENEXR */
  2519. bool BKE_image_save_openexr_multiview(Image *ima, ImBuf *ibuf, const char *filepath, const int flags)
  2520. {
  2521. #ifdef WITH_OPENEXR
  2522. char name[FILE_MAX];
  2523. bool ok;
  2524. BLI_strncpy(name, filepath, sizeof(name));
  2525. BLI_path_abs(name, G.main->name);
  2526. ibuf->userdata = ima;
  2527. ok = IMB_exr_multiview_save(ibuf, name, flags, BLI_listbase_count(&ima->views), image_get_view_cb, image_get_buffer_cb);
  2528. ibuf->userdata = NULL;
  2529. return ok;
  2530. #else
  2531. UNUSED_VARS(ima, ibuf, filepath, flags);
  2532. return false;
  2533. #endif
  2534. }
  2535. /**************************** multiview load openexr *********************************/
  2536. static void image_add_view(Image *ima, const char *viewname, const char *filepath)
  2537. {
  2538. ImageView *iv;
  2539. iv = MEM_mallocN(sizeof(ImageView), "Viewer Image View");
  2540. BLI_strncpy(iv->name, viewname, sizeof(iv->name));
  2541. BLI_strncpy(iv->filepath, filepath, sizeof(iv->filepath));
  2542. /* For stereo drawing we need to ensure:
  2543. * STEREO_LEFT_NAME == STEREO_LEFT_ID and
  2544. * STEREO_RIGHT_NAME == STEREO_RIGHT_ID */
  2545. if (STREQ(viewname, STEREO_LEFT_NAME)) {
  2546. BLI_addhead(&ima->views, iv);
  2547. }
  2548. else if (STREQ(viewname, STEREO_RIGHT_NAME)) {
  2549. ImageView *left_iv = BLI_findstring(&ima->views, STEREO_LEFT_NAME, offsetof(ImageView, name));
  2550. if (left_iv == NULL) {
  2551. BLI_addhead(&ima->views, iv);
  2552. }
  2553. else {
  2554. BLI_insertlinkafter(&ima->views, left_iv, iv);
  2555. }
  2556. }
  2557. else {
  2558. BLI_addtail(&ima->views, iv);
  2559. }
  2560. }
  2561. #ifdef WITH_OPENEXR
  2562. static void image_add_view_cb(void *base, const char *str)
  2563. {
  2564. Image *ima = base;
  2565. image_add_view(ima, str, ima->name);
  2566. }
  2567. static void image_add_buffer_cb(void *base, const char *str, ImBuf *ibuf, const int frame)
  2568. {
  2569. Image *ima = base;
  2570. int id;
  2571. bool predivide = (ima->alpha_mode == IMA_ALPHA_PREMUL);
  2572. const char *colorspace = ima->colorspace_settings.name;
  2573. const char *to_colorspace = IMB_colormanagement_role_colorspace_name_get(COLOR_ROLE_SCENE_LINEAR);
  2574. if (ibuf == NULL)
  2575. return;
  2576. id = BLI_findstringindex(&ima->views, str, offsetof(ImageView, name));
  2577. if (id == -1)
  2578. return;
  2579. if (ibuf->channels >= 3)
  2580. IMB_colormanagement_transform(ibuf->rect_float, ibuf->x, ibuf->y, ibuf->channels,
  2581. colorspace, to_colorspace, predivide);
  2582. image_assign_ibuf(ima, ibuf, id, frame);
  2583. IMB_freeImBuf(ibuf);
  2584. }
  2585. #endif /* WITH_OPENEXR */
  2586. /* after imbuf load, openexr type can return with a exrhandle open */
  2587. /* in that case we have to build a render-result */
  2588. #ifdef WITH_OPENEXR
  2589. static void image_create_multiview(Image *ima, ImBuf *ibuf, const int frame)
  2590. {
  2591. BKE_image_free_views(ima);
  2592. IMB_exr_multiview_convert(ibuf->userdata, ima, image_add_view_cb, image_add_buffer_cb, frame);
  2593. IMB_exr_close(ibuf->userdata);
  2594. }
  2595. #endif /* WITH_OPENEXR */
  2596. /* after imbuf load, openexr type can return with a exrhandle open */
  2597. /* in that case we have to build a render-result */
  2598. #ifdef WITH_OPENEXR
  2599. static void image_create_multilayer(Image *ima, ImBuf *ibuf, int framenr)
  2600. {
  2601. const char *colorspace = ima->colorspace_settings.name;
  2602. bool predivide = (ima->alpha_mode == IMA_ALPHA_PREMUL);
  2603. /* only load rr once for multiview */
  2604. if (!ima->rr)
  2605. ima->rr = RE_MultilayerConvert(ibuf->userdata, colorspace, predivide, ibuf->x, ibuf->y);
  2606. IMB_exr_close(ibuf->userdata);
  2607. ibuf->userdata = NULL;
  2608. if (ima->rr)
  2609. ima->rr->framenr = framenr;
  2610. /* set proper views */
  2611. image_init_multilayer_multiview(ima, ima->rr);
  2612. }
  2613. #endif /* WITH_OPENEXR */
  2614. /* common stuff to do with images after loading */
  2615. static void image_initialize_after_load(Image *ima, ImBuf *ibuf)
  2616. {
  2617. /* preview is NULL when it has never been used as an icon before */
  2618. if (G.background == 0 && ima->preview == NULL)
  2619. BKE_icon_changed(BKE_icon_id_ensure(&ima->id));
  2620. /* fields */
  2621. if (ima->flag & IMA_FIELDS) {
  2622. if (ima->flag & IMA_STD_FIELD) de_interlace_st(ibuf);
  2623. else de_interlace_ng(ibuf);
  2624. }
  2625. /* timer */
  2626. BKE_image_tag_time(ima);
  2627. ima->ok = IMA_OK_LOADED;
  2628. }
  2629. static int imbuf_alpha_flags_for_image(Image *ima)
  2630. {
  2631. int flag = 0;
  2632. if (ima->flag & IMA_IGNORE_ALPHA)
  2633. flag |= IB_ignore_alpha;
  2634. else if (ima->alpha_mode == IMA_ALPHA_PREMUL)
  2635. flag |= IB_alphamode_premul;
  2636. return flag;
  2637. }
  2638. /* the number of files will vary according to the stereo format */
  2639. static int image_num_files(Image *ima)
  2640. {
  2641. const bool is_multiview = BKE_image_is_multiview(ima);
  2642. if (!is_multiview) {
  2643. return 1;
  2644. }
  2645. else if (ima->views_format == R_IMF_VIEWS_STEREO_3D) {
  2646. return 1;
  2647. }
  2648. /* R_IMF_VIEWS_INDIVIDUAL */
  2649. else {
  2650. return BLI_listbase_count(&ima->views);
  2651. }
  2652. }
  2653. static ImBuf *load_sequence_single(Image *ima, ImageUser *iuser, int frame, const int view_id, bool *r_assign)
  2654. {
  2655. struct ImBuf *ibuf;
  2656. char name[FILE_MAX];
  2657. int flag;
  2658. ImageUser iuser_t = {0};
  2659. /* XXX temp stuff? */
  2660. if (ima->lastframe != frame)
  2661. ima->tpageflag |= IMA_TPAGE_REFRESH;
  2662. ima->lastframe = frame;
  2663. if (iuser) {
  2664. iuser_t = *iuser;
  2665. }
  2666. else {
  2667. /* TODO(sergey): Do we need to initialize something here? */
  2668. }
  2669. iuser_t.view = view_id;
  2670. BKE_image_user_file_path(&iuser_t, ima, name);
  2671. flag = IB_rect | IB_multilayer;
  2672. flag |= imbuf_alpha_flags_for_image(ima);
  2673. /* read ibuf */
  2674. ibuf = IMB_loadiffname(name, flag, ima->colorspace_settings.name);
  2675. #if 0
  2676. if (ibuf) {
  2677. printf(AT " loaded %s\n", name);
  2678. }
  2679. else {
  2680. printf(AT " missed %s\n", name);
  2681. }
  2682. #endif
  2683. if (ibuf) {
  2684. #ifdef WITH_OPENEXR
  2685. /* handle multilayer case, don't assign ibuf. will be handled in BKE_image_acquire_ibuf */
  2686. if (ibuf->ftype == IMB_FTYPE_OPENEXR && ibuf->userdata) {
  2687. /* handle singlelayer multiview case assign ibuf based on available views */
  2688. if (IMB_exr_has_singlelayer_multiview(ibuf->userdata)) {
  2689. image_create_multiview(ima, ibuf, frame);
  2690. IMB_freeImBuf(ibuf);
  2691. ibuf = NULL;
  2692. }
  2693. else if (IMB_exr_has_multilayer(ibuf->userdata)) {
  2694. /* handle multilayer case, don't assign ibuf. will be handled in BKE_image_acquire_ibuf */
  2695. image_create_multilayer(ima, ibuf, frame);
  2696. ima->type = IMA_TYPE_MULTILAYER;
  2697. IMB_freeImBuf(ibuf);
  2698. ibuf = NULL;
  2699. }
  2700. }
  2701. else {
  2702. image_initialize_after_load(ima, ibuf);
  2703. *r_assign = true;
  2704. }
  2705. #else
  2706. image_initialize_after_load(ima, ibuf);
  2707. *r_assign = true;
  2708. #endif
  2709. }
  2710. return ibuf;
  2711. }
  2712. static ImBuf *image_load_sequence_file(Image *ima, ImageUser *iuser, int frame)
  2713. {
  2714. struct ImBuf *ibuf = NULL;
  2715. const bool is_multiview = BKE_image_is_multiview(ima);
  2716. const int totfiles = image_num_files(ima);
  2717. bool assign = false;
  2718. if (!is_multiview) {
  2719. ibuf = load_sequence_single(ima, iuser, frame, 0, &assign);
  2720. if (assign) {
  2721. image_assign_ibuf(ima, ibuf, 0, frame);
  2722. }
  2723. }
  2724. else {
  2725. const int totviews = BLI_listbase_count(&ima->views);
  2726. int i;
  2727. struct ImBuf **ibuf_arr;
  2728. ibuf_arr = MEM_mallocN(sizeof(ImBuf *) * totviews, "Image Views Imbufs");
  2729. for (i = 0; i < totfiles; i++)
  2730. ibuf_arr[i] = load_sequence_single(ima, iuser, frame, i, &assign);
  2731. if (BKE_image_is_stereo(ima) && ima->views_format == R_IMF_VIEWS_STEREO_3D)
  2732. IMB_ImBufFromStereo3d(ima->stereo3d_format, ibuf_arr[0], &ibuf_arr[0], &ibuf_arr[1]);
  2733. /* return the original requested ImBuf */
  2734. ibuf = ibuf_arr[(iuser ? iuser->multi_index : 0)];
  2735. if (assign) {
  2736. for (i = 0; i < totviews; i++) {
  2737. image_assign_ibuf(ima, ibuf_arr[i], i, frame);
  2738. }
  2739. }
  2740. /* "remove" the others (decrease their refcount) */
  2741. for (i = 0; i < totviews; i++) {
  2742. if (ibuf_arr[i] != ibuf) {
  2743. IMB_freeImBuf(ibuf_arr[i]);
  2744. }
  2745. }
  2746. /* cleanup */
  2747. MEM_freeN(ibuf_arr);
  2748. }
  2749. return ibuf;
  2750. }
  2751. static ImBuf *image_load_sequence_multilayer(Image *ima, ImageUser *iuser, int frame)
  2752. {
  2753. struct ImBuf *ibuf = NULL;
  2754. /* either we load from RenderResult, or we have to load a new one */
  2755. /* check for new RenderResult */
  2756. if (ima->rr == NULL || frame != ima->rr->framenr) {
  2757. if (ima->rr) {
  2758. /* Cached image buffers shares pointers with render result,
  2759. * need to ensure there's no image buffers are hanging around
  2760. * with dead links after freeing the render result.
  2761. */
  2762. image_free_cached_frames(ima);
  2763. RE_FreeRenderResult(ima->rr);
  2764. ima->rr = NULL;
  2765. }
  2766. ibuf = image_load_sequence_file(ima, iuser, frame);
  2767. if (ibuf) { /* actually an error */
  2768. ima->type = IMA_TYPE_IMAGE;
  2769. printf("error, multi is normal image\n");
  2770. }
  2771. }
  2772. if (ima->rr) {
  2773. RenderPass *rpass = BKE_image_multilayer_index(ima->rr, iuser);
  2774. if (rpass) {
  2775. // printf("load from pass %s\n", rpass->name);
  2776. /* since we free render results, we copy the rect */
  2777. ibuf = IMB_allocImBuf(ima->rr->rectx, ima->rr->recty, 32, 0);
  2778. ibuf->rect_float = MEM_dupallocN(rpass->rect);
  2779. ibuf->flags |= IB_rectfloat;
  2780. ibuf->mall = IB_rectfloat;
  2781. ibuf->channels = rpass->channels;
  2782. image_initialize_after_load(ima, ibuf);
  2783. image_assign_ibuf(ima, ibuf, iuser ? iuser->multi_index : 0, frame);
  2784. }
  2785. // else printf("pass not found\n");
  2786. }
  2787. else
  2788. ima->ok = 0;
  2789. if (iuser)
  2790. iuser->ok = ima->ok;
  2791. return ibuf;
  2792. }
  2793. static ImBuf *load_movie_single(Image *ima, ImageUser *iuser, int frame, const int view_id)
  2794. {
  2795. struct ImBuf *ibuf = NULL;
  2796. ImageAnim *ia;
  2797. ia = BLI_findlink(&ima->anims, view_id);
  2798. if (ia->anim == NULL) {
  2799. char str[FILE_MAX];
  2800. int flags = IB_rect;
  2801. ImageUser iuser_t;
  2802. if (ima->flag & IMA_DEINTERLACE) {
  2803. flags |= IB_animdeinterlace;
  2804. }
  2805. if (iuser)
  2806. iuser_t = *iuser;
  2807. iuser_t.view = view_id;
  2808. BKE_image_user_file_path(&iuser_t, ima, str);
  2809. /* FIXME: make several stream accessible in image editor, too*/
  2810. ia->anim = openanim(str, flags, 0, ima->colorspace_settings.name);
  2811. /* let's initialize this user */
  2812. if (ia->anim && iuser && iuser->frames == 0)
  2813. iuser->frames = IMB_anim_get_duration(ia->anim,
  2814. IMB_TC_RECORD_RUN);
  2815. }
  2816. if (ia->anim) {
  2817. int dur = IMB_anim_get_duration(ia->anim,
  2818. IMB_TC_RECORD_RUN);
  2819. int fra = frame - 1;
  2820. if (fra < 0) fra = 0;
  2821. if (fra > (dur - 1)) fra = dur - 1;
  2822. ibuf = IMB_makeSingleUser(
  2823. IMB_anim_absolute(ia->anim, fra,
  2824. IMB_TC_RECORD_RUN,
  2825. IMB_PROXY_NONE));
  2826. if (ibuf) {
  2827. image_initialize_after_load(ima, ibuf);
  2828. }
  2829. else
  2830. ima->ok = 0;
  2831. }
  2832. else
  2833. ima->ok = 0;
  2834. return ibuf;
  2835. }
  2836. static ImBuf *image_load_movie_file(Image *ima, ImageUser *iuser, int frame)
  2837. {
  2838. struct ImBuf *ibuf = NULL;
  2839. const bool is_multiview = BKE_image_is_multiview(ima);
  2840. const int totfiles = image_num_files(ima);
  2841. int i;
  2842. if (totfiles != BLI_listbase_count_ex(&ima->anims, totfiles + 1)) {
  2843. image_free_anims(ima);
  2844. for (i = 0; i < totfiles; i++) {
  2845. /* allocate the ImageAnim */
  2846. ImageAnim *ia = MEM_callocN(sizeof(ImageAnim), "Image Anim");
  2847. BLI_addtail(&ima->anims, ia);
  2848. }
  2849. }
  2850. if (!is_multiview) {
  2851. ibuf = load_movie_single(ima, iuser, frame, 0);
  2852. image_assign_ibuf(ima, ibuf, 0, frame);
  2853. }
  2854. else {
  2855. struct ImBuf **ibuf_arr;
  2856. const int totviews = BLI_listbase_count(&ima->views);
  2857. ibuf_arr = MEM_mallocN(sizeof(ImBuf *) * totviews, "Image Views (movie) Imbufs");
  2858. for (i = 0; i < totfiles; i++) {
  2859. ibuf_arr[i] = load_movie_single(ima, iuser, frame, i);
  2860. }
  2861. if (BKE_image_is_stereo(ima) && ima->views_format == R_IMF_VIEWS_STEREO_3D)
  2862. IMB_ImBufFromStereo3d(ima->stereo3d_format, ibuf_arr[0], &ibuf_arr[0], &ibuf_arr[1]);
  2863. for (i = 0; i < totviews; i++) {
  2864. if (ibuf_arr[i]) {
  2865. image_assign_ibuf(ima, ibuf_arr[i], i, frame);
  2866. }
  2867. else {
  2868. ima->ok = 0;
  2869. }
  2870. }
  2871. /* return the original requested ImBuf */
  2872. ibuf = ibuf_arr[(iuser ? iuser->multi_index : 0)];
  2873. /* "remove" the others (decrease their refcount) */
  2874. for (i = 0; i < totviews; i++) {
  2875. if (ibuf_arr[i] != ibuf) {
  2876. IMB_freeImBuf(ibuf_arr[i]);
  2877. }
  2878. }
  2879. /* cleanup */
  2880. MEM_freeN(ibuf_arr);
  2881. }
  2882. if (iuser)
  2883. iuser->ok = ima->ok;
  2884. return ibuf;
  2885. }
  2886. static ImBuf *load_image_single(
  2887. Image *ima, ImageUser *iuser, int cfra,
  2888. const int view_id,
  2889. const bool has_packed,
  2890. bool *r_assign)
  2891. {
  2892. char filepath[FILE_MAX];
  2893. struct ImBuf *ibuf = NULL;
  2894. int flag;
  2895. /* is there a PackedFile with this image ? */
  2896. if (has_packed) {
  2897. ImagePackedFile *imapf;
  2898. flag = IB_rect | IB_multilayer;
  2899. flag |= imbuf_alpha_flags_for_image(ima);
  2900. imapf = BLI_findlink(&ima->packedfiles, view_id);
  2901. if (imapf->packedfile) {
  2902. ibuf = IMB_ibImageFromMemory(
  2903. (unsigned char *)imapf->packedfile->data, imapf->packedfile->size, flag,
  2904. ima->colorspace_settings.name, "<packed data>");
  2905. }
  2906. }
  2907. else {
  2908. ImageUser iuser_t;
  2909. flag = IB_rect | IB_multilayer | IB_metadata;
  2910. flag |= imbuf_alpha_flags_for_image(ima);
  2911. /* get the correct filepath */
  2912. BKE_image_user_frame_calc(iuser, cfra, 0);
  2913. if (iuser)
  2914. iuser_t = *iuser;
  2915. else
  2916. iuser_t.framenr = ima->lastframe;
  2917. iuser_t.view = view_id;
  2918. BKE_image_user_file_path(&iuser_t, ima, filepath);
  2919. /* read ibuf */
  2920. ibuf = IMB_loadiffname(filepath, flag, ima->colorspace_settings.name);
  2921. }
  2922. if (ibuf) {
  2923. #ifdef WITH_OPENEXR
  2924. if (ibuf->ftype == IMB_FTYPE_OPENEXR && ibuf->userdata) {
  2925. if (IMB_exr_has_singlelayer_multiview(ibuf->userdata)) {
  2926. /* handle singlelayer multiview case assign ibuf based on available views */
  2927. image_create_multiview(ima, ibuf, cfra);
  2928. IMB_freeImBuf(ibuf);
  2929. ibuf = NULL;
  2930. }
  2931. else if (IMB_exr_has_multilayer(ibuf->userdata)) {
  2932. /* handle multilayer case, don't assign ibuf. will be handled in BKE_image_acquire_ibuf */
  2933. image_create_multilayer(ima, ibuf, cfra);
  2934. ima->type = IMA_TYPE_MULTILAYER;
  2935. IMB_freeImBuf(ibuf);
  2936. ibuf = NULL;
  2937. }
  2938. }
  2939. else
  2940. #endif
  2941. {
  2942. image_initialize_after_load(ima, ibuf);
  2943. *r_assign = true;
  2944. /* check if the image is a font image... */
  2945. detectBitmapFont(ibuf);
  2946. /* make packed file for autopack */
  2947. if ((has_packed == false) && (G.fileflags & G_AUTOPACK)) {
  2948. ImagePackedFile *imapf = MEM_mallocN(sizeof(ImagePackedFile), "Image Packefile");
  2949. BLI_addtail(&ima->packedfiles, imapf);
  2950. BLI_strncpy(imapf->filepath, filepath, sizeof(imapf->filepath));
  2951. imapf->packedfile = newPackedFile(NULL, filepath, ID_BLEND_PATH(G.main, &ima->id));
  2952. }
  2953. }
  2954. }
  2955. else {
  2956. ima->ok = 0;
  2957. }
  2958. return ibuf;
  2959. }
  2960. /* warning, 'iuser' can be NULL
  2961. * note: Image->views was already populated (in image_update_views_format)
  2962. */
  2963. static ImBuf *image_load_image_file(Image *ima, ImageUser *iuser, int cfra)
  2964. {
  2965. struct ImBuf *ibuf = NULL;
  2966. bool assign = false;
  2967. const bool is_multiview = BKE_image_is_multiview(ima);
  2968. const int totfiles = image_num_files(ima);
  2969. bool has_packed = BKE_image_has_packedfile(ima);
  2970. /* always ensure clean ima */
  2971. BKE_image_free_buffers(ima);
  2972. /* this should never happen, but just playing safe */
  2973. if (has_packed) {
  2974. if (totfiles != BLI_listbase_count_ex(&ima->packedfiles, totfiles + 1)) {
  2975. image_free_packedfiles(ima);
  2976. has_packed = false;
  2977. }
  2978. }
  2979. if (!is_multiview) {
  2980. ibuf = load_image_single(ima, iuser, cfra, 0, has_packed, &assign);
  2981. if (assign) {
  2982. image_assign_ibuf(ima, ibuf, IMA_NO_INDEX, 0);
  2983. }
  2984. }
  2985. else {
  2986. struct ImBuf **ibuf_arr;
  2987. const int totviews = BLI_listbase_count(&ima->views);
  2988. int i;
  2989. BLI_assert(totviews > 0);
  2990. ibuf_arr = MEM_callocN(sizeof(ImBuf *) * totviews, "Image Views Imbufs");
  2991. for (i = 0; i < totfiles; i++)
  2992. ibuf_arr[i] = load_image_single(ima, iuser, cfra, i, has_packed, &assign);
  2993. /* multi-views/multi-layers OpenEXR files directly populate ima, and return NULL ibuf... */
  2994. if (BKE_image_is_stereo(ima) && ima->views_format == R_IMF_VIEWS_STEREO_3D &&
  2995. ibuf_arr[0] && totfiles == 1 && totviews >= 2)
  2996. {
  2997. IMB_ImBufFromStereo3d(ima->stereo3d_format, ibuf_arr[0], &ibuf_arr[0], &ibuf_arr[1]);
  2998. }
  2999. /* return the original requested ImBuf */
  3000. i = (iuser && iuser->multi_index < totviews) ? iuser->multi_index : 0;
  3001. ibuf = ibuf_arr[i];
  3002. if (assign) {
  3003. for (i = 0; i < totviews; i++) {
  3004. image_assign_ibuf(ima, ibuf_arr[i], i, 0);
  3005. }
  3006. }
  3007. /* "remove" the others (decrease their refcount) */
  3008. for (i = 0; i < totviews; i++) {
  3009. if (ibuf_arr[i] != ibuf) {
  3010. IMB_freeImBuf(ibuf_arr[i]);
  3011. }
  3012. }
  3013. /* cleanup */
  3014. MEM_freeN(ibuf_arr);
  3015. }
  3016. if (iuser)
  3017. iuser->ok = ima->ok;
  3018. return ibuf;
  3019. }
  3020. static ImBuf *image_get_ibuf_multilayer(Image *ima, ImageUser *iuser)
  3021. {
  3022. ImBuf *ibuf = NULL;
  3023. if (ima->rr == NULL) {
  3024. ibuf = image_load_image_file(ima, iuser, 0);
  3025. if (ibuf) { /* actually an error */
  3026. ima->type = IMA_TYPE_IMAGE;
  3027. return ibuf;
  3028. }
  3029. }
  3030. if (ima->rr) {
  3031. RenderPass *rpass = BKE_image_multilayer_index(ima->rr, iuser);
  3032. if (rpass) {
  3033. ibuf = IMB_allocImBuf(ima->rr->rectx, ima->rr->recty, 32, 0);
  3034. image_initialize_after_load(ima, ibuf);
  3035. ibuf->rect_float = rpass->rect;
  3036. ibuf->flags |= IB_rectfloat;
  3037. ibuf->channels = rpass->channels;
  3038. image_assign_ibuf(ima, ibuf, iuser ? iuser->multi_index : IMA_NO_INDEX, 0);
  3039. }
  3040. }
  3041. if (ibuf == NULL)
  3042. ima->ok = 0;
  3043. if (iuser)
  3044. iuser->ok = ima->ok;
  3045. return ibuf;
  3046. }
  3047. /* showing RGBA result itself (from compo/sequence) or
  3048. * like exr, using layers etc */
  3049. /* always returns a single ibuf, also during render progress */
  3050. static ImBuf *image_get_render_result(Image *ima, ImageUser *iuser, void **r_lock)
  3051. {
  3052. Render *re;
  3053. RenderResult rres;
  3054. RenderView *rv;
  3055. float *rectf, *rectz;
  3056. unsigned int *rect;
  3057. float dither;
  3058. int channels, layer, pass;
  3059. ImBuf *ibuf;
  3060. int from_render = (ima->render_slot == ima->last_render_slot);
  3061. int actview;
  3062. bool byte_buffer_in_display_space = false;
  3063. if (!(iuser && iuser->scene))
  3064. return NULL;
  3065. /* if we the caller is not going to release the lock, don't give the image */
  3066. if (!r_lock)
  3067. return NULL;
  3068. re = RE_GetRender(iuser->scene->id.name);
  3069. channels = 4;
  3070. layer = iuser->layer;
  3071. pass = iuser->pass;
  3072. actview = iuser->view;
  3073. if (BKE_image_is_stereo(ima) && (iuser->flag & IMA_SHOW_STEREO))
  3074. actview = iuser->multiview_eye;
  3075. if (from_render) {
  3076. RE_AcquireResultImage(re, &rres, actview);
  3077. }
  3078. else if (ima->renders[ima->render_slot]) {
  3079. rres = *(ima->renders[ima->render_slot]);
  3080. rres.have_combined = ((RenderView *)rres.views.first)->rectf != NULL;
  3081. }
  3082. else
  3083. memset(&rres, 0, sizeof(RenderResult));
  3084. if (!(rres.rectx > 0 && rres.recty > 0)) {
  3085. if (from_render)
  3086. RE_ReleaseResultImage(re);
  3087. return NULL;
  3088. }
  3089. /* release is done in BKE_image_release_ibuf using r_lock */
  3090. if (from_render) {
  3091. BLI_lock_thread(LOCK_VIEWER);
  3092. *r_lock = re;
  3093. rv = NULL;
  3094. }
  3095. else {
  3096. rv = BLI_findlink(&rres.views, actview);
  3097. if (rv == NULL)
  3098. rv = rres.views.first;
  3099. }
  3100. /* this gives active layer, composite or sequence result */
  3101. if (rv == NULL) {
  3102. rect = (unsigned int *)rres.rect32;
  3103. rectf = rres.rectf;
  3104. rectz = rres.rectz;
  3105. }
  3106. else {
  3107. rect = (unsigned int *)rv->rect32;
  3108. rectf = rv->rectf;
  3109. rectz = rv->rectz;
  3110. }
  3111. dither = iuser->scene->r.dither_intensity;
  3112. /* combined layer gets added as first layer */
  3113. if (rres.have_combined && layer == 0) {
  3114. /* pass */
  3115. }
  3116. else if (rect && layer == 0) {
  3117. /* rect32 is set when there's a Sequence pass, this pass seems
  3118. * to have layer=0 (this is from image_buttons.c)
  3119. * in this case we ignore float buffer, because it could have
  3120. * hung from previous pass which was float
  3121. */
  3122. rectf = NULL;
  3123. }
  3124. else if (rres.layers.first) {
  3125. RenderLayer *rl = BLI_findlink(&rres.layers, layer - (rres.have_combined ? 1 : 0));
  3126. if (rl) {
  3127. RenderPass *rpass = image_render_pass_get(rl, pass, actview, NULL);
  3128. if (rpass) {
  3129. rectf = rpass->rect;
  3130. if (pass == 0) {
  3131. if (rectf == NULL) {
  3132. /* Happens when Save Buffers is enabled.
  3133. * Use display buffer stored in the render layer.
  3134. */
  3135. rect = (unsigned int *) rl->display_buffer;
  3136. byte_buffer_in_display_space = true;
  3137. }
  3138. }
  3139. else {
  3140. channels = rpass->channels;
  3141. dither = 0.0f; /* don't dither passes */
  3142. }
  3143. }
  3144. for (rpass = rl->passes.first; rpass; rpass = rpass->next)
  3145. if (STREQ(rpass->name, RE_PASSNAME_Z) && rpass->view_id == actview)
  3146. rectz = rpass->rect;
  3147. }
  3148. }
  3149. ibuf = image_get_cached_ibuf_for_index_frame(ima, IMA_NO_INDEX, 0);
  3150. /* make ibuf if needed, and initialize it */
  3151. if (ibuf == NULL) {
  3152. ibuf = IMB_allocImBuf(rres.rectx, rres.recty, 32, 0);
  3153. image_assign_ibuf(ima, ibuf, IMA_NO_INDEX, 0);
  3154. }
  3155. /* Set color space settings for a byte buffer.
  3156. *
  3157. * This is mainly to make it so color management treats byte buffer
  3158. * from render result with Save Buffers enabled as final display buffer
  3159. * and doesnt' apply any color management on it.
  3160. *
  3161. * For other cases we need to be sure it stays to default byte buffer space.
  3162. */
  3163. if (ibuf->rect != rect) {
  3164. if (byte_buffer_in_display_space) {
  3165. const char *colorspace =
  3166. IMB_colormanagement_get_display_colorspace_name(&iuser->scene->view_settings,
  3167. &iuser->scene->display_settings);
  3168. IMB_colormanagement_assign_rect_colorspace(ibuf, colorspace);
  3169. }
  3170. else {
  3171. const char *colorspace = IMB_colormanagement_role_colorspace_name_get(COLOR_ROLE_DEFAULT_BYTE);
  3172. IMB_colormanagement_assign_rect_colorspace(ibuf, colorspace);
  3173. }
  3174. }
  3175. /* invalidate color managed buffers if render result changed */
  3176. BLI_lock_thread(LOCK_COLORMANAGE);
  3177. if (ibuf->x != rres.rectx || ibuf->y != rres.recty || ibuf->rect_float != rectf) {
  3178. ibuf->userflags |= IB_DISPLAY_BUFFER_INVALID;
  3179. }
  3180. ibuf->x = rres.rectx;
  3181. ibuf->y = rres.recty;
  3182. if (rect) {
  3183. imb_freerectImBuf(ibuf);
  3184. ibuf->rect = rect;
  3185. }
  3186. else {
  3187. /* byte buffer of render result has been freed, make sure image buffers
  3188. * does not reference to this buffer anymore
  3189. * need check for whether byte buffer was allocated and owned by image itself
  3190. * or if it's reusing buffer from render result
  3191. */
  3192. if ((ibuf->mall & IB_rect) == 0)
  3193. ibuf->rect = NULL;
  3194. }
  3195. if (rectf) {
  3196. ibuf->rect_float = rectf;
  3197. ibuf->flags |= IB_rectfloat;
  3198. ibuf->channels = channels;
  3199. }
  3200. else {
  3201. ibuf->rect_float = NULL;
  3202. ibuf->flags &= ~IB_rectfloat;
  3203. }
  3204. if (rectz) {
  3205. ibuf->zbuf_float = rectz;
  3206. ibuf->flags |= IB_zbuffloat;
  3207. }
  3208. else {
  3209. ibuf->zbuf_float = NULL;
  3210. ibuf->flags &= ~IB_zbuffloat;
  3211. }
  3212. BLI_unlock_thread(LOCK_COLORMANAGE);
  3213. ibuf->dither = dither;
  3214. ima->ok = IMA_OK_LOADED;
  3215. return ibuf;
  3216. }
  3217. static int image_get_multiview_index(Image *ima, ImageUser *iuser)
  3218. {
  3219. const bool is_multilayer = BKE_image_is_multilayer(ima);
  3220. const bool is_backdrop = (ima->source == IMA_SRC_VIEWER) && (ima->type == IMA_TYPE_COMPOSITE) && (iuser == NULL);
  3221. int index = BKE_image_is_animated(ima) ? 0 : IMA_NO_INDEX;
  3222. if (is_multilayer) {
  3223. return iuser ? iuser->multi_index : index;
  3224. }
  3225. else if (is_backdrop) {
  3226. if (BKE_image_is_stereo(ima)) {
  3227. /* backdrop hackaround (since there is no iuser */
  3228. return ima->eye;
  3229. }
  3230. }
  3231. else if (BKE_image_is_multiview(ima)) {
  3232. return iuser ? iuser->multi_index : index;
  3233. }
  3234. return index;
  3235. }
  3236. static void image_get_frame_and_index(Image *ima, ImageUser *iuser, int *r_frame, int *r_index)
  3237. {
  3238. int frame = 0, index = image_get_multiview_index(ima, iuser);
  3239. /* see if we already have an appropriate ibuf, with image source and type */
  3240. if (ima->source == IMA_SRC_MOVIE) {
  3241. frame = iuser ? iuser->framenr : ima->lastframe;
  3242. }
  3243. else if (ima->source == IMA_SRC_SEQUENCE) {
  3244. if (ima->type == IMA_TYPE_IMAGE) {
  3245. frame = iuser ? iuser->framenr : ima->lastframe;
  3246. }
  3247. else if (ima->type == IMA_TYPE_MULTILAYER) {
  3248. frame = iuser ? iuser->framenr : ima->lastframe;
  3249. }
  3250. }
  3251. *r_frame = frame;
  3252. *r_index = index;
  3253. }
  3254. /* Get the ibuf from an image cache for a given image user.
  3255. *
  3256. * Returns referenced image buffer if it exists, callee is to
  3257. * call IMB_freeImBuf to de-reference the image buffer after
  3258. * it's done handling it.
  3259. */
  3260. static ImBuf *image_get_cached_ibuf(Image *ima, ImageUser *iuser, int *r_frame, int *r_index)
  3261. {
  3262. ImBuf *ibuf = NULL;
  3263. int frame = 0, index = image_get_multiview_index(ima, iuser);
  3264. /* see if we already have an appropriate ibuf, with image source and type */
  3265. if (ima->source == IMA_SRC_MOVIE) {
  3266. frame = iuser ? iuser->framenr : ima->lastframe;
  3267. ibuf = image_get_cached_ibuf_for_index_frame(ima, index, frame);
  3268. /* XXX temp stuff? */
  3269. if (ima->lastframe != frame)
  3270. ima->tpageflag |= IMA_TPAGE_REFRESH;
  3271. ima->lastframe = frame;
  3272. }
  3273. else if (ima->source == IMA_SRC_SEQUENCE) {
  3274. if (ima->type == IMA_TYPE_IMAGE) {
  3275. frame = iuser ? iuser->framenr : ima->lastframe;
  3276. ibuf = image_get_cached_ibuf_for_index_frame(ima, index, frame);
  3277. /* XXX temp stuff? */
  3278. if (ima->lastframe != frame) {
  3279. ima->tpageflag |= IMA_TPAGE_REFRESH;
  3280. }
  3281. ima->lastframe = frame;
  3282. /* counter the fact that image is set as invalid when loading a frame
  3283. * that is not in the cache (through image_acquire_ibuf for instance),
  3284. * yet we have valid frames in the cache loaded */
  3285. if (ibuf) {
  3286. ima->ok = IMA_OK_LOADED;
  3287. if (iuser)
  3288. iuser->ok = ima->ok;
  3289. }
  3290. }
  3291. else if (ima->type == IMA_TYPE_MULTILAYER) {
  3292. frame = iuser ? iuser->framenr : ima->lastframe;
  3293. ibuf = image_get_cached_ibuf_for_index_frame(ima, index, frame);
  3294. }
  3295. }
  3296. else if (ima->source == IMA_SRC_FILE) {
  3297. if (ima->type == IMA_TYPE_IMAGE)
  3298. ibuf = image_get_cached_ibuf_for_index_frame(ima, index, 0);
  3299. else if (ima->type == IMA_TYPE_MULTILAYER)
  3300. ibuf = image_get_cached_ibuf_for_index_frame(ima, index, 0);
  3301. }
  3302. else if (ima->source == IMA_SRC_GENERATED) {
  3303. ibuf = image_get_cached_ibuf_for_index_frame(ima, index, 0);
  3304. }
  3305. else if (ima->source == IMA_SRC_VIEWER) {
  3306. /* always verify entirely, not that this shouldn't happen
  3307. * as part of texture sampling in rendering anyway, so not
  3308. * a big bottleneck */
  3309. }
  3310. if (r_frame)
  3311. *r_frame = frame;
  3312. if (r_index)
  3313. *r_index = index;
  3314. return ibuf;
  3315. }
  3316. BLI_INLINE bool image_quick_test(Image *ima, ImageUser *iuser)
  3317. {
  3318. if (ima == NULL)
  3319. return false;
  3320. if (iuser) {
  3321. if (iuser->ok == 0)
  3322. return false;
  3323. }
  3324. else if (ima->ok == 0)
  3325. return false;
  3326. return true;
  3327. }
  3328. /* Checks optional ImageUser and verifies/creates ImBuf.
  3329. *
  3330. * not thread-safe, so callee should worry about thread locks
  3331. */
  3332. static ImBuf *image_acquire_ibuf(Image *ima, ImageUser *iuser, void **r_lock)
  3333. {
  3334. ImBuf *ibuf = NULL;
  3335. int frame = 0, index = 0;
  3336. if (r_lock)
  3337. *r_lock = NULL;
  3338. /* quick reject tests */
  3339. if (!image_quick_test(ima, iuser))
  3340. return NULL;
  3341. ibuf = image_get_cached_ibuf(ima, iuser, &frame, &index);
  3342. if (ibuf == NULL) {
  3343. /* we are sure we have to load the ibuf, using source and type */
  3344. if (ima->source == IMA_SRC_MOVIE) {
  3345. /* source is from single file, use flipbook to store ibuf */
  3346. ibuf = image_load_movie_file(ima, iuser, frame);
  3347. }
  3348. else if (ima->source == IMA_SRC_SEQUENCE) {
  3349. if (ima->type == IMA_TYPE_IMAGE) {
  3350. /* regular files, ibufs in flipbook, allows saving */
  3351. ibuf = image_load_sequence_file(ima, iuser, frame);
  3352. }
  3353. /* no else; on load the ima type can change */
  3354. if (ima->type == IMA_TYPE_MULTILAYER) {
  3355. /* only 1 layer/pass stored in imbufs, no exrhandle anim storage, no saving */
  3356. ibuf = image_load_sequence_multilayer(ima, iuser, frame);
  3357. }
  3358. }
  3359. else if (ima->source == IMA_SRC_FILE) {
  3360. if (ima->type == IMA_TYPE_IMAGE)
  3361. ibuf = image_load_image_file(ima, iuser, frame); /* cfra only for '#', this global is OK */
  3362. /* no else; on load the ima type can change */
  3363. if (ima->type == IMA_TYPE_MULTILAYER)
  3364. /* keeps render result, stores ibufs in listbase, allows saving */
  3365. ibuf = image_get_ibuf_multilayer(ima, iuser);
  3366. }
  3367. else if (ima->source == IMA_SRC_GENERATED) {
  3368. /* generated is: ibuf is allocated dynamically */
  3369. /* UV testgrid or black or solid etc */
  3370. if (ima->gen_x == 0) ima->gen_x = 1024;
  3371. if (ima->gen_y == 0) ima->gen_y = 1024;
  3372. if (ima->gen_depth == 0) ima->gen_depth = 24;
  3373. ibuf = add_ibuf_size(ima->gen_x, ima->gen_y, ima->name, ima->gen_depth, (ima->gen_flag & IMA_GEN_FLOAT) != 0, ima->gen_type,
  3374. ima->gen_color, &ima->colorspace_settings);
  3375. image_assign_ibuf(ima, ibuf, index, 0);
  3376. ima->ok = IMA_OK_LOADED;
  3377. }
  3378. else if (ima->source == IMA_SRC_VIEWER) {
  3379. if (ima->type == IMA_TYPE_R_RESULT) {
  3380. /* always verify entirely, and potentially
  3381. * returns pointer to release later */
  3382. ibuf = image_get_render_result(ima, iuser, r_lock);
  3383. }
  3384. else if (ima->type == IMA_TYPE_COMPOSITE) {
  3385. /* requires lock/unlock, otherwise don't return image */
  3386. if (r_lock) {
  3387. /* unlock in BKE_image_release_ibuf */
  3388. BLI_lock_thread(LOCK_VIEWER);
  3389. *r_lock = ima;
  3390. /* XXX anim play for viewer nodes not yet supported */
  3391. frame = 0; // XXX iuser ? iuser->framenr : 0;
  3392. ibuf = image_get_cached_ibuf_for_index_frame(ima, index, frame);
  3393. if (!ibuf) {
  3394. /* Composite Viewer, all handled in compositor */
  3395. /* fake ibuf, will be filled in compositor */
  3396. ibuf = IMB_allocImBuf(256, 256, 32, IB_rect | IB_rectfloat);
  3397. image_assign_ibuf(ima, ibuf, index, frame);
  3398. }
  3399. }
  3400. }
  3401. }
  3402. /* We only want movies and sequences to be memory limited. */
  3403. if (ibuf != NULL && !ELEM(ima->source, IMA_SRC_MOVIE, IMA_SRC_SEQUENCE)) {
  3404. ibuf->userflags |= IB_PERSISTENT;
  3405. }
  3406. }
  3407. BKE_image_tag_time(ima);
  3408. return ibuf;
  3409. }
  3410. /* return image buffer for given image and user
  3411. *
  3412. * - will lock render result if image type is render result and lock is not NULL
  3413. * - will return NULL if image type if render or composite result and lock is NULL
  3414. *
  3415. * references the result, BKE_image_release_ibuf should be used to de-reference
  3416. */
  3417. ImBuf *BKE_image_acquire_ibuf(Image *ima, ImageUser *iuser, void **r_lock)
  3418. {
  3419. ImBuf *ibuf;
  3420. BLI_spin_lock(&image_spin);
  3421. ibuf = image_acquire_ibuf(ima, iuser, r_lock);
  3422. BLI_spin_unlock(&image_spin);
  3423. return ibuf;
  3424. }
  3425. void BKE_image_release_ibuf(Image *ima, ImBuf *ibuf, void *lock)
  3426. {
  3427. if (lock) {
  3428. /* for getting image during threaded render / compositing, need to release */
  3429. if (lock == ima) {
  3430. BLI_unlock_thread(LOCK_VIEWER); /* viewer image */
  3431. }
  3432. else if (lock) {
  3433. RE_ReleaseResultImage(lock); /* render result */
  3434. BLI_unlock_thread(LOCK_VIEWER); /* view image imbuf */
  3435. }
  3436. }
  3437. if (ibuf) {
  3438. BLI_spin_lock(&image_spin);
  3439. IMB_freeImBuf(ibuf);
  3440. BLI_spin_unlock(&image_spin);
  3441. }
  3442. }
  3443. /* checks whether there's an image buffer for given image and user */
  3444. bool BKE_image_has_ibuf(Image *ima, ImageUser *iuser)
  3445. {
  3446. ImBuf *ibuf;
  3447. /* quick reject tests */
  3448. if (!image_quick_test(ima, iuser))
  3449. return false;
  3450. BLI_spin_lock(&image_spin);
  3451. ibuf = image_get_cached_ibuf(ima, iuser, NULL, NULL);
  3452. if (!ibuf)
  3453. ibuf = image_acquire_ibuf(ima, iuser, NULL);
  3454. BLI_spin_unlock(&image_spin);
  3455. IMB_freeImBuf(ibuf);
  3456. return ibuf != NULL;
  3457. }
  3458. /* ******** Pool for image buffers ******** */
  3459. typedef struct ImagePoolEntry {
  3460. struct ImagePoolEntry *next, *prev;
  3461. Image *image;
  3462. ImBuf *ibuf;
  3463. int index;
  3464. int frame;
  3465. } ImagePoolEntry;
  3466. typedef struct ImagePool {
  3467. ListBase image_buffers;
  3468. BLI_mempool *memory_pool;
  3469. } ImagePool;
  3470. ImagePool *BKE_image_pool_new(void)
  3471. {
  3472. ImagePool *pool = MEM_callocN(sizeof(ImagePool), "Image Pool");
  3473. pool->memory_pool = BLI_mempool_create(sizeof(ImagePoolEntry), 0, 128, BLI_MEMPOOL_NOP);
  3474. return pool;
  3475. }
  3476. void BKE_image_pool_free(ImagePool *pool)
  3477. {
  3478. /* Use single lock to dereference all the image buffers. */
  3479. BLI_spin_lock(&image_spin);
  3480. for (ImagePoolEntry *entry = pool->image_buffers.first;
  3481. entry != NULL;
  3482. entry = entry->next)
  3483. {
  3484. if (entry->ibuf) {
  3485. IMB_freeImBuf(entry->ibuf);
  3486. }
  3487. }
  3488. BLI_spin_unlock(&image_spin);
  3489. BLI_mempool_destroy(pool->memory_pool);
  3490. MEM_freeN(pool);
  3491. }
  3492. BLI_INLINE ImBuf *image_pool_find_entry(ImagePool *pool, Image *image, int frame, int index, bool *found)
  3493. {
  3494. ImagePoolEntry *entry;
  3495. *found = false;
  3496. for (entry = pool->image_buffers.first; entry; entry = entry->next) {
  3497. if (entry->image == image && entry->frame == frame && entry->index == index) {
  3498. *found = true;
  3499. return entry->ibuf;
  3500. }
  3501. }
  3502. return NULL;
  3503. }
  3504. ImBuf *BKE_image_pool_acquire_ibuf(Image *ima, ImageUser *iuser, ImagePool *pool)
  3505. {
  3506. ImBuf *ibuf;
  3507. int index, frame;
  3508. bool found;
  3509. if (!image_quick_test(ima, iuser))
  3510. return NULL;
  3511. if (pool == NULL) {
  3512. /* pool could be NULL, in this case use general acquire function */
  3513. return BKE_image_acquire_ibuf(ima, iuser, NULL);
  3514. }
  3515. image_get_frame_and_index(ima, iuser, &frame, &index);
  3516. ibuf = image_pool_find_entry(pool, ima, frame, index, &found);
  3517. if (found)
  3518. return ibuf;
  3519. BLI_spin_lock(&image_spin);
  3520. ibuf = image_pool_find_entry(pool, ima, frame, index, &found);
  3521. /* will also create entry even in cases image buffer failed to load,
  3522. * prevents trying to load the same buggy file multiple times
  3523. */
  3524. if (!found) {
  3525. ImagePoolEntry *entry;
  3526. ibuf = image_acquire_ibuf(ima, iuser, NULL);
  3527. entry = BLI_mempool_alloc(pool->memory_pool);
  3528. entry->image = ima;
  3529. entry->frame = frame;
  3530. entry->index = index;
  3531. entry->ibuf = ibuf;
  3532. BLI_addtail(&pool->image_buffers, entry);
  3533. }
  3534. BLI_spin_unlock(&image_spin);
  3535. return ibuf;
  3536. }
  3537. void BKE_image_pool_release_ibuf(Image *ima, ImBuf *ibuf, ImagePool *pool)
  3538. {
  3539. /* if pool wasn't actually used, use general release stuff,
  3540. * for pools image buffers will be dereferenced on pool free
  3541. */
  3542. if (pool == NULL) {
  3543. BKE_image_release_ibuf(ima, ibuf, NULL);
  3544. }
  3545. }
  3546. int BKE_image_user_frame_get(const ImageUser *iuser, int cfra, int fieldnr, bool *r_is_in_range)
  3547. {
  3548. const int len = (iuser->fie_ima * iuser->frames) / 2;
  3549. if (r_is_in_range) {
  3550. *r_is_in_range = false;
  3551. }
  3552. if (len == 0) {
  3553. return 0;
  3554. }
  3555. else {
  3556. int framenr;
  3557. cfra = cfra - iuser->sfra + 1;
  3558. /* cyclic */
  3559. if (iuser->cycl) {
  3560. cfra = ((cfra) % len);
  3561. if (cfra < 0) cfra += len;
  3562. if (cfra == 0) cfra = len;
  3563. if (r_is_in_range) {
  3564. *r_is_in_range = true;
  3565. }
  3566. }
  3567. if (cfra < 0) {
  3568. cfra = 0;
  3569. }
  3570. else if (cfra > len) {
  3571. cfra = len;
  3572. }
  3573. else {
  3574. if (r_is_in_range) {
  3575. *r_is_in_range = true;
  3576. }
  3577. }
  3578. /* convert current frame to current field */
  3579. cfra = 2 * (cfra);
  3580. if (fieldnr) cfra++;
  3581. /* transform to images space */
  3582. framenr = (cfra + iuser->fie_ima - 2) / iuser->fie_ima;
  3583. if (framenr > iuser->frames) framenr = iuser->frames;
  3584. if (iuser->cycl) {
  3585. framenr = ((framenr) % len);
  3586. while (framenr < 0) framenr += len;
  3587. if (framenr == 0) framenr = len;
  3588. }
  3589. /* important to apply after else we cant loop on frames 100 - 110 for eg. */
  3590. framenr += iuser->offset;
  3591. return framenr;
  3592. }
  3593. }
  3594. void BKE_image_user_frame_calc(ImageUser *iuser, int cfra, int fieldnr)
  3595. {
  3596. if (iuser) {
  3597. bool is_in_range;
  3598. const int framenr = BKE_image_user_frame_get(iuser, cfra, fieldnr, &is_in_range);
  3599. if (is_in_range) {
  3600. iuser->flag |= IMA_USER_FRAME_IN_RANGE;
  3601. }
  3602. else {
  3603. iuser->flag &= ~IMA_USER_FRAME_IN_RANGE;
  3604. }
  3605. /* allows image users to handle redraws */
  3606. if (iuser->flag & IMA_ANIM_ALWAYS)
  3607. if (framenr != iuser->framenr)
  3608. iuser->flag |= IMA_ANIM_REFRESHED;
  3609. iuser->framenr = framenr;
  3610. if (iuser->ok == 0) iuser->ok = 1;
  3611. }
  3612. }
  3613. void BKE_image_user_check_frame_calc(ImageUser *iuser, int cfra, int fieldnr)
  3614. {
  3615. if ((iuser->flag & IMA_ANIM_ALWAYS) || (iuser->flag & IMA_NEED_FRAME_RECALC)) {
  3616. BKE_image_user_frame_calc(iuser, cfra, fieldnr);
  3617. iuser->flag &= ~IMA_NEED_FRAME_RECALC;
  3618. }
  3619. }
  3620. /* goes over all ImageUsers, and sets frame numbers if auto-refresh is set */
  3621. static void image_update_frame(struct Image *UNUSED(ima), struct ImageUser *iuser, void *customdata)
  3622. {
  3623. int cfra = *(int *)customdata;
  3624. BKE_image_user_check_frame_calc(iuser, cfra, 0);
  3625. }
  3626. void BKE_image_update_frame(const Main *bmain, int cfra)
  3627. {
  3628. BKE_image_walk_all_users(bmain, &cfra, image_update_frame);
  3629. }
  3630. void BKE_image_user_file_path(ImageUser *iuser, Image *ima, char *filepath)
  3631. {
  3632. if (BKE_image_is_multiview(ima) && (ima->rr == NULL)) {
  3633. ImageView *iv = BLI_findlink(&ima->views, iuser->view);
  3634. if (iv->filepath[0])
  3635. BLI_strncpy(filepath, iv->filepath, FILE_MAX);
  3636. else
  3637. BLI_strncpy(filepath, ima->name, FILE_MAX);
  3638. }
  3639. else {
  3640. BLI_strncpy(filepath, ima->name, FILE_MAX);
  3641. }
  3642. if (ima->source == IMA_SRC_SEQUENCE) {
  3643. char head[FILE_MAX], tail[FILE_MAX];
  3644. unsigned short numlen;
  3645. int frame = iuser ? iuser->framenr : ima->lastframe;
  3646. BLI_stringdec(filepath, head, tail, &numlen);
  3647. BLI_stringenc(filepath, head, tail, numlen, frame);
  3648. }
  3649. BLI_path_abs(filepath, ID_BLEND_PATH(G.main, &ima->id));
  3650. }
  3651. bool BKE_image_has_alpha(struct Image *image)
  3652. {
  3653. ImBuf *ibuf;
  3654. void *lock;
  3655. int planes;
  3656. ibuf = BKE_image_acquire_ibuf(image, NULL, &lock);
  3657. planes = (ibuf ? ibuf->planes : 0);
  3658. BKE_image_release_ibuf(image, ibuf, lock);
  3659. if (planes == 32)
  3660. return true;
  3661. else
  3662. return false;
  3663. }
  3664. void BKE_image_get_size(Image *image, ImageUser *iuser, int *width, int *height)
  3665. {
  3666. ImBuf *ibuf = NULL;
  3667. void *lock;
  3668. if (image != NULL) {
  3669. ibuf = BKE_image_acquire_ibuf(image, iuser, &lock);
  3670. }
  3671. if (ibuf && ibuf->x > 0 && ibuf->y > 0) {
  3672. *width = ibuf->x;
  3673. *height = ibuf->y;
  3674. }
  3675. else if (image != NULL && image->type == IMA_TYPE_R_RESULT &&
  3676. iuser != NULL && iuser->scene != NULL)
  3677. {
  3678. Scene *scene = iuser->scene;
  3679. *width = (scene->r.xsch * scene->r.size) / 100;
  3680. *height = (scene->r.ysch * scene->r.size) / 100;
  3681. if ((scene->r.mode & R_BORDER) && (scene->r.mode & R_CROP)) {
  3682. *width *= BLI_rctf_size_x(&scene->r.border);
  3683. *height *= BLI_rctf_size_y(&scene->r.border);
  3684. }
  3685. }
  3686. else {
  3687. *width = IMG_SIZE_FALLBACK;
  3688. *height = IMG_SIZE_FALLBACK;
  3689. }
  3690. if (image != NULL) {
  3691. BKE_image_release_ibuf(image, ibuf, lock);
  3692. }
  3693. }
  3694. void BKE_image_get_size_fl(Image *image, ImageUser *iuser, float size[2])
  3695. {
  3696. int width, height;
  3697. BKE_image_get_size(image, iuser, &width, &height);
  3698. size[0] = (float)width;
  3699. size[1] = (float)height;
  3700. }
  3701. void BKE_image_get_aspect(Image *image, float *aspx, float *aspy)
  3702. {
  3703. *aspx = 1.0;
  3704. /* x is always 1 */
  3705. if (image)
  3706. *aspy = image->aspy / image->aspx;
  3707. else
  3708. *aspy = 1.0f;
  3709. }
  3710. unsigned char *BKE_image_get_pixels_for_frame(struct Image *image, int frame)
  3711. {
  3712. ImageUser iuser = {NULL};
  3713. void *lock;
  3714. ImBuf *ibuf;
  3715. unsigned char *pixels = NULL;
  3716. iuser.framenr = frame;
  3717. iuser.ok = true;
  3718. ibuf = BKE_image_acquire_ibuf(image, &iuser, &lock);
  3719. if (ibuf) {
  3720. pixels = (unsigned char *) ibuf->rect;
  3721. if (pixels)
  3722. pixels = MEM_dupallocN(pixels);
  3723. BKE_image_release_ibuf(image, ibuf, lock);
  3724. }
  3725. if (!pixels)
  3726. return NULL;
  3727. return pixels;
  3728. }
  3729. float *BKE_image_get_float_pixels_for_frame(struct Image *image, int frame)
  3730. {
  3731. ImageUser iuser = {NULL};
  3732. void *lock;
  3733. ImBuf *ibuf;
  3734. float *pixels = NULL;
  3735. iuser.framenr = frame;
  3736. iuser.ok = true;
  3737. ibuf = BKE_image_acquire_ibuf(image, &iuser, &lock);
  3738. if (ibuf) {
  3739. pixels = ibuf->rect_float;
  3740. if (pixels)
  3741. pixels = MEM_dupallocN(pixels);
  3742. BKE_image_release_ibuf(image, ibuf, lock);
  3743. }
  3744. if (!pixels)
  3745. return NULL;
  3746. return pixels;
  3747. }
  3748. int BKE_image_sequence_guess_offset(Image *image)
  3749. {
  3750. return BLI_stringdec(image->name, NULL, NULL, NULL);
  3751. }
  3752. bool BKE_image_has_anim(Image *ima)
  3753. {
  3754. return (BLI_listbase_is_empty(&ima->anims) == false);
  3755. }
  3756. bool BKE_image_has_packedfile(Image *ima)
  3757. {
  3758. return (BLI_listbase_is_empty(&ima->packedfiles) == false);
  3759. }
  3760. /**
  3761. * Checks the image buffer changes (not keyframed values)
  3762. *
  3763. * to see if we need to call #BKE_image_user_check_frame_calc
  3764. */
  3765. bool BKE_image_is_animated(Image *image)
  3766. {
  3767. return ELEM(image->source, IMA_SRC_MOVIE, IMA_SRC_SEQUENCE);
  3768. }
  3769. bool BKE_image_is_dirty(Image *image)
  3770. {
  3771. bool is_dirty = false;
  3772. BLI_spin_lock(&image_spin);
  3773. if (image->cache != NULL) {
  3774. struct MovieCacheIter *iter = IMB_moviecacheIter_new(image->cache);
  3775. while (!IMB_moviecacheIter_done(iter)) {
  3776. ImBuf *ibuf = IMB_moviecacheIter_getImBuf(iter);
  3777. if (ibuf->userflags & IB_BITMAPDIRTY) {
  3778. is_dirty = true;
  3779. break;
  3780. }
  3781. IMB_moviecacheIter_step(iter);
  3782. }
  3783. IMB_moviecacheIter_free(iter);
  3784. }
  3785. BLI_spin_unlock(&image_spin);
  3786. return is_dirty;
  3787. }
  3788. void BKE_image_file_format_set(Image *image, int ftype, const ImbFormatOptions *options)
  3789. {
  3790. #if 0
  3791. ImBuf *ibuf = BKE_image_acquire_ibuf(image, NULL, NULL);
  3792. if (ibuf) {
  3793. ibuf->ftype = ftype;
  3794. ibuf->foptions = options;
  3795. }
  3796. BKE_image_release_ibuf(image, ibuf, NULL);
  3797. #endif
  3798. BLI_spin_lock(&image_spin);
  3799. if (image->cache != NULL) {
  3800. struct MovieCacheIter *iter = IMB_moviecacheIter_new(image->cache);
  3801. while (!IMB_moviecacheIter_done(iter)) {
  3802. ImBuf *ibuf = IMB_moviecacheIter_getImBuf(iter);
  3803. ibuf->ftype = ftype;
  3804. ibuf->foptions = *options;
  3805. IMB_moviecacheIter_step(iter);
  3806. }
  3807. IMB_moviecacheIter_free(iter);
  3808. }
  3809. BLI_spin_unlock(&image_spin);
  3810. }
  3811. bool BKE_image_has_loaded_ibuf(Image *image)
  3812. {
  3813. bool has_loaded_ibuf = false;
  3814. BLI_spin_lock(&image_spin);
  3815. if (image->cache != NULL) {
  3816. struct MovieCacheIter *iter = IMB_moviecacheIter_new(image->cache);
  3817. while (!IMB_moviecacheIter_done(iter)) {
  3818. has_loaded_ibuf = true;
  3819. break;
  3820. }
  3821. IMB_moviecacheIter_free(iter);
  3822. }
  3823. BLI_spin_unlock(&image_spin);
  3824. return has_loaded_ibuf;
  3825. }
  3826. /* References the result, BKE_image_release_ibuf is to be called to de-reference.
  3827. * Use lock=NULL when calling BKE_image_release_ibuf().
  3828. */
  3829. ImBuf *BKE_image_get_ibuf_with_name(Image *image, const char *name)
  3830. {
  3831. ImBuf *ibuf = NULL;
  3832. BLI_spin_lock(&image_spin);
  3833. if (image->cache != NULL) {
  3834. struct MovieCacheIter *iter = IMB_moviecacheIter_new(image->cache);
  3835. while (!IMB_moviecacheIter_done(iter)) {
  3836. ImBuf *current_ibuf = IMB_moviecacheIter_getImBuf(iter);
  3837. if (STREQ(current_ibuf->name, name)) {
  3838. ibuf = current_ibuf;
  3839. IMB_refImBuf(ibuf);
  3840. break;
  3841. }
  3842. IMB_moviecacheIter_step(iter);
  3843. }
  3844. IMB_moviecacheIter_free(iter);
  3845. }
  3846. BLI_spin_unlock(&image_spin);
  3847. return ibuf;
  3848. }
  3849. /* References the result, BKE_image_release_ibuf is to be called to de-reference.
  3850. * Use lock=NULL when calling BKE_image_release_ibuf().
  3851. *
  3852. * TODO(sergey): This is actually "get first entry from the cache", which is
  3853. * not so much predictable. But using first loaded image buffer
  3854. * was also malicious logic and all the areas which uses this
  3855. * function are to be re-considered.
  3856. */
  3857. ImBuf *BKE_image_get_first_ibuf(Image *image)
  3858. {
  3859. ImBuf *ibuf = NULL;
  3860. BLI_spin_lock(&image_spin);
  3861. if (image->cache != NULL) {
  3862. struct MovieCacheIter *iter = IMB_moviecacheIter_new(image->cache);
  3863. while (!IMB_moviecacheIter_done(iter)) {
  3864. ibuf = IMB_moviecacheIter_getImBuf(iter);
  3865. IMB_refImBuf(ibuf);
  3866. break;
  3867. }
  3868. IMB_moviecacheIter_free(iter);
  3869. }
  3870. BLI_spin_unlock(&image_spin);
  3871. return ibuf;
  3872. }
  3873. static void image_update_views_format(Image *ima, ImageUser *iuser)
  3874. {
  3875. SceneRenderView *srv;
  3876. ImageView *iv;
  3877. Scene *scene = iuser->scene;
  3878. const bool is_multiview = ((scene->r.scemode & R_MULTIVIEW) != 0) &&
  3879. ((ima->flag & IMA_USE_VIEWS) != 0);
  3880. /* reset the image views */
  3881. BKE_image_free_views(ima);
  3882. if (!is_multiview) {
  3883. /* nothing to do */
  3884. }
  3885. else if (ima->views_format == R_IMF_VIEWS_STEREO_3D) {
  3886. int i;
  3887. const char *names[2] = {STEREO_LEFT_NAME, STEREO_RIGHT_NAME};
  3888. for (i = 0; i < 2; i++) {
  3889. image_add_view(ima, names[i], ima->name);
  3890. }
  3891. return;
  3892. }
  3893. else {
  3894. /* R_IMF_VIEWS_INDIVIDUAL */
  3895. char prefix[FILE_MAX] = {'\0'};
  3896. char *name = ima->name;
  3897. const char *ext = NULL;
  3898. BKE_scene_multiview_view_prefix_get(scene, name, prefix, &ext);
  3899. if (prefix[0] == '\0') {
  3900. BKE_image_free_views(ima);
  3901. return;
  3902. }
  3903. /* create all the image views */
  3904. for (srv = scene->r.views.first; srv; srv = srv->next) {
  3905. if (BKE_scene_multiview_is_render_view_active(&scene->r, srv)) {
  3906. char filepath[FILE_MAX];
  3907. BLI_snprintf(filepath, sizeof(filepath), "%s%s%s", prefix, srv->suffix, ext);
  3908. image_add_view(ima, srv->name, filepath);
  3909. }
  3910. }
  3911. /* check if the files are all available */
  3912. iv = ima->views.last;
  3913. while (iv) {
  3914. int file;
  3915. char str[FILE_MAX];
  3916. BLI_strncpy(str, iv->filepath, sizeof(str));
  3917. BLI_path_abs(str, G.main->name);
  3918. /* exists? */
  3919. file = BLI_open(str, O_BINARY | O_RDONLY, 0);
  3920. if (file == -1) {
  3921. ImageView *iv_del = iv;
  3922. iv = iv->prev;
  3923. BLI_remlink(&ima->views, iv_del);
  3924. MEM_freeN(iv_del);
  3925. }
  3926. else {
  3927. iv = iv->prev;
  3928. close(file);
  3929. }
  3930. }
  3931. /* all good */
  3932. if (!BKE_image_is_multiview(ima)) {
  3933. BKE_image_free_views(ima);
  3934. }
  3935. }
  3936. }