|
Blender
V2.59
|
00001 /* image.c 00002 * 00003 * $Id: image.c 38551 2011-07-21 00:41:00Z campbellbarton $ 00004 * 00005 * ***** BEGIN GPL LICENSE BLOCK ***** 00006 * 00007 * This program is free software; you can redistribute it and/or 00008 * modify it under the terms of the GNU General Public License 00009 * as published by the Free Software Foundation; either version 2 00010 * of the License, or (at your option) any later version. 00011 * 00012 * This program is distributed in the hope that it will be useful, 00013 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00015 * GNU General Public License for more details. 00016 * 00017 * You should have received a copy of the GNU General Public License 00018 * along with this program; if not, write to the Free Software Foundation, 00019 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. 00020 * 00021 * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV. 00022 * All rights reserved. 00023 * 00024 * Contributor(s): Blender Foundation, 2006, full recode 00025 * 00026 * ***** END GPL LICENSE BLOCK ***** 00027 */ 00028 00034 #include <stdio.h> 00035 #include <string.h> 00036 #include <fcntl.h> 00037 #include <math.h> 00038 #ifndef WIN32 00039 #include <unistd.h> 00040 #else 00041 #include <io.h> 00042 #endif 00043 00044 #include <time.h> 00045 00046 #ifdef _WIN32 00047 #define open _open 00048 #define close _close 00049 #endif 00050 00051 #include "MEM_guardedalloc.h" 00052 00053 #include "IMB_imbuf_types.h" 00054 #include "IMB_imbuf.h" 00055 00056 #ifdef WITH_OPENEXR 00057 #include "intern/openexr/openexr_multi.h" 00058 #endif 00059 00060 #include "DNA_packedFile_types.h" 00061 #include "DNA_scene_types.h" 00062 #include "DNA_object_types.h" 00063 #include "DNA_camera_types.h" 00064 #include "DNA_sequence_types.h" 00065 #include "DNA_userdef_types.h" 00066 00067 #include "BLI_blenlib.h" 00068 #include "BLI_threads.h" 00069 #include "BLI_utildefines.h" 00070 00071 #include "BKE_bmfont.h" 00072 #include "BKE_global.h" 00073 #include "BKE_icons.h" 00074 #include "BKE_image.h" 00075 #include "BKE_library.h" 00076 #include "BKE_main.h" 00077 #include "BKE_packedFile.h" 00078 #include "BKE_scene.h" 00079 #include "BKE_node.h" 00080 #include "BKE_sequencer.h" /* seq_foreground_frame_get() */ 00081 #include "BKE_utildefines.h" 00082 00083 #include "BLF_api.h" 00084 00085 #include "PIL_time.h" 00086 00087 #include "RE_pipeline.h" 00088 00089 #include "GPU_draw.h" 00090 00091 #include "BLO_sys_types.h" // for intptr_t support 00092 00093 /* max int, to indicate we don't store sequences in ibuf */ 00094 #define IMA_NO_INDEX 0x7FEFEFEF 00095 00096 /* quick lookup: supports 1 million frames, thousand passes */ 00097 #define IMA_MAKE_INDEX(frame, index) ((frame)<<10)+index 00098 #define IMA_INDEX_FRAME(index) (index>>10) 00099 #define IMA_INDEX_PASS(index) (index & ~1023) 00100 00101 /* ******** IMAGE PROCESSING ************* */ 00102 00103 static void de_interlace_ng(struct ImBuf *ibuf) /* neogeo fields */ 00104 { 00105 struct ImBuf * tbuf1, * tbuf2; 00106 00107 if (ibuf == NULL) return; 00108 if (ibuf->flags & IB_fields) return; 00109 ibuf->flags |= IB_fields; 00110 00111 if (ibuf->rect) { 00112 /* make copies */ 00113 tbuf1 = IMB_allocImBuf(ibuf->x, (short)(ibuf->y >> 1), (unsigned char)32, (int)IB_rect); 00114 tbuf2 = IMB_allocImBuf(ibuf->x, (short)(ibuf->y >> 1), (unsigned char)32, (int)IB_rect); 00115 00116 ibuf->x *= 2; 00117 00118 IMB_rectcpy(tbuf1, ibuf, 0, 0, 0, 0, ibuf->x, ibuf->y); 00119 IMB_rectcpy(tbuf2, ibuf, 0, 0, tbuf2->x, 0, ibuf->x, ibuf->y); 00120 00121 ibuf->x /= 2; 00122 IMB_rectcpy(ibuf, tbuf1, 0, 0, 0, 0, tbuf1->x, tbuf1->y); 00123 IMB_rectcpy(ibuf, tbuf2, 0, tbuf2->y, 0, 0, tbuf2->x, tbuf2->y); 00124 00125 IMB_freeImBuf(tbuf1); 00126 IMB_freeImBuf(tbuf2); 00127 } 00128 ibuf->y /= 2; 00129 } 00130 00131 static void de_interlace_st(struct ImBuf *ibuf) /* standard fields */ 00132 { 00133 struct ImBuf * tbuf1, * tbuf2; 00134 00135 if (ibuf == NULL) return; 00136 if (ibuf->flags & IB_fields) return; 00137 ibuf->flags |= IB_fields; 00138 00139 if (ibuf->rect) { 00140 /* make copies */ 00141 tbuf1 = IMB_allocImBuf(ibuf->x, (short)(ibuf->y >> 1), (unsigned char)32, IB_rect); 00142 tbuf2 = IMB_allocImBuf(ibuf->x, (short)(ibuf->y >> 1), (unsigned char)32, IB_rect); 00143 00144 ibuf->x *= 2; 00145 00146 IMB_rectcpy(tbuf1, ibuf, 0, 0, 0, 0, ibuf->x, ibuf->y); 00147 IMB_rectcpy(tbuf2, ibuf, 0, 0, tbuf2->x, 0, ibuf->x, ibuf->y); 00148 00149 ibuf->x /= 2; 00150 IMB_rectcpy(ibuf, tbuf2, 0, 0, 0, 0, tbuf2->x, tbuf2->y); 00151 IMB_rectcpy(ibuf, tbuf1, 0, tbuf2->y, 0, 0, tbuf1->x, tbuf1->y); 00152 00153 IMB_freeImBuf(tbuf1); 00154 IMB_freeImBuf(tbuf2); 00155 } 00156 ibuf->y /= 2; 00157 } 00158 00159 void image_de_interlace(Image *ima, int odd) 00160 { 00161 ImBuf *ibuf= BKE_image_get_ibuf(ima, NULL); 00162 if(ibuf) { 00163 if(odd) 00164 de_interlace_st(ibuf); 00165 else 00166 de_interlace_ng(ibuf); 00167 } 00168 } 00169 00170 /* ***************** ALLOC & FREE, DATA MANAGING *************** */ 00171 00172 static void image_free_buffers(Image *ima) 00173 { 00174 ImBuf *ibuf; 00175 00176 while((ibuf = ima->ibufs.first)) { 00177 BLI_remlink(&ima->ibufs, ibuf); 00178 00179 if (ibuf->userdata) { 00180 MEM_freeN(ibuf->userdata); 00181 ibuf->userdata = NULL; 00182 } 00183 IMB_freeImBuf(ibuf); 00184 } 00185 00186 if(ima->anim) IMB_free_anim(ima->anim); 00187 ima->anim= NULL; 00188 00189 if(ima->rr) { 00190 RE_FreeRenderResult(ima->rr); 00191 ima->rr= NULL; 00192 } 00193 00194 GPU_free_image(ima); 00195 00196 ima->ok= IMA_OK; 00197 } 00198 00199 /* called by library too, do not free ima itself */ 00200 void free_image(Image *ima) 00201 { 00202 int a; 00203 00204 image_free_buffers(ima); 00205 if (ima->packedfile) { 00206 freePackedFile(ima->packedfile); 00207 ima->packedfile = NULL; 00208 } 00209 BKE_icon_delete(&ima->id); 00210 ima->id.icon_id = 0; 00211 00212 BKE_previewimg_free(&ima->preview); 00213 00214 for(a=0; a<IMA_MAX_RENDER_SLOT; a++) { 00215 if(ima->renders[a]) { 00216 RE_FreeRenderResult(ima->renders[a]); 00217 ima->renders[a]= NULL; 00218 } 00219 } 00220 } 00221 00222 /* only image block itself */ 00223 static Image *image_alloc(const char *name, short source, short type) 00224 { 00225 Image *ima; 00226 00227 ima= alloc_libblock(&G.main->image, ID_IM, name); 00228 if(ima) { 00229 ima->ok= IMA_OK; 00230 00231 ima->xrep= ima->yrep= 1; 00232 ima->aspx= ima->aspy= 1.0; 00233 ima->gen_x= 1024; ima->gen_y= 1024; 00234 ima->gen_type= 1; /* no defines yet? */ 00235 00236 ima->source= source; 00237 ima->type= type; 00238 } 00239 return ima; 00240 } 00241 00242 /* get the ibuf from an image cache, local use here only */ 00243 static ImBuf *image_get_ibuf(Image *ima, int index, int frame) 00244 { 00245 /* this function is intended to be thread safe. with IMA_NO_INDEX this 00246 * should be OK, but when iterating over the list this is more tricky 00247 * */ 00248 if(index==IMA_NO_INDEX) 00249 return ima->ibufs.first; 00250 else { 00251 ImBuf *ibuf; 00252 00253 index= IMA_MAKE_INDEX(frame, index); 00254 for(ibuf= ima->ibufs.first; ibuf; ibuf= ibuf->next) 00255 if(ibuf->index==index) 00256 return ibuf; 00257 00258 return NULL; 00259 } 00260 } 00261 00262 /* no ima->ibuf anymore, but listbase */ 00263 static void image_remove_ibuf(Image *ima, ImBuf *ibuf) 00264 { 00265 if(ibuf) { 00266 BLI_remlink(&ima->ibufs, ibuf); 00267 IMB_freeImBuf(ibuf); 00268 } 00269 } 00270 00271 00272 /* no ima->ibuf anymore, but listbase */ 00273 static void image_assign_ibuf(Image *ima, ImBuf *ibuf, int index, int frame) 00274 { 00275 if(ibuf) { 00276 ImBuf *link; 00277 00278 if(index!=IMA_NO_INDEX) 00279 index= IMA_MAKE_INDEX(frame, index); 00280 00281 /* insert based on index */ 00282 for(link= ima->ibufs.first; link; link= link->next) 00283 if(link->index>=index) 00284 break; 00285 00286 ibuf->index= index; 00287 00288 /* this function accepts link==NULL */ 00289 BLI_insertlinkbefore(&ima->ibufs, link, ibuf); 00290 00291 /* now we don't want copies? */ 00292 if(link && ibuf->index==link->index) 00293 image_remove_ibuf(ima, link); 00294 } 00295 } 00296 00297 /* empty image block, of similar type and filename */ 00298 Image *copy_image(Image *ima) 00299 { 00300 Image *nima= image_alloc(ima->id.name+2, ima->source, ima->type); 00301 00302 BLI_strncpy(nima->name, ima->name, sizeof(ima->name)); 00303 00304 nima->flag= ima->flag; 00305 nima->tpageflag= ima->tpageflag; 00306 00307 nima->gen_x= ima->gen_x; 00308 nima->gen_y= ima->gen_y; 00309 nima->gen_type= ima->gen_type; 00310 00311 nima->animspeed= ima->animspeed; 00312 00313 nima->aspx= ima->aspx; 00314 nima->aspy= ima->aspy; 00315 00316 return nima; 00317 } 00318 00319 void BKE_image_merge(Image *dest, Image *source) 00320 { 00321 ImBuf *ibuf; 00322 00323 /* sanity check */ 00324 if(dest && source && dest!=source) { 00325 00326 while((ibuf= source->ibufs.first)) { 00327 BLI_remlink(&source->ibufs, ibuf); 00328 image_assign_ibuf(dest, ibuf, IMA_INDEX_PASS(ibuf->index), IMA_INDEX_FRAME(ibuf->index)); 00329 } 00330 00331 free_libblock(&G.main->image, source); 00332 } 00333 } 00334 00335 00336 /* checks if image was already loaded, then returns same image */ 00337 /* otherwise creates new. */ 00338 /* does not load ibuf itself */ 00339 /* pass on optional frame for #name images */ 00340 Image *BKE_add_image_file(const char *name) 00341 { 00342 Image *ima; 00343 int file, len; 00344 const char *libname; 00345 char str[FILE_MAX], strtest[FILE_MAX]; 00346 00347 BLI_strncpy(str, name, sizeof(str)); 00348 BLI_path_abs(str, G.main->name); 00349 00350 /* exists? */ 00351 file= open(str, O_BINARY|O_RDONLY); 00352 if(file== -1) return NULL; 00353 close(file); 00354 00355 /* first search an identical image */ 00356 for(ima= G.main->image.first; ima; ima= ima->id.next) { 00357 if(ima->source!=IMA_SRC_VIEWER && ima->source!=IMA_SRC_GENERATED) { 00358 BLI_strncpy(strtest, ima->name, sizeof(ima->name)); 00359 BLI_path_abs(strtest, G.main->name); 00360 00361 if( strcmp(strtest, str)==0 ) { 00362 if(ima->anim==NULL || ima->id.us==0) { 00363 BLI_strncpy(ima->name, name, sizeof(ima->name)); /* for stringcode */ 00364 ima->id.us++; /* officially should not, it doesn't link here! */ 00365 if(ima->ok==0) 00366 ima->ok= IMA_OK; 00367 /* RETURN! */ 00368 return ima; 00369 } 00370 } 00371 } 00372 } 00373 /* add new image */ 00374 00375 /* create a short library name */ 00376 len= strlen(name); 00377 00378 while (len > 0 && name[len - 1] != '/' && name[len - 1] != '\\') len--; 00379 libname= name+len; 00380 00381 ima= image_alloc(libname, IMA_SRC_FILE, IMA_TYPE_IMAGE); 00382 BLI_strncpy(ima->name, name, sizeof(ima->name)); 00383 00384 if(BLI_testextensie_array(name, imb_ext_movie)) 00385 ima->source= IMA_SRC_MOVIE; 00386 00387 return ima; 00388 } 00389 00390 static ImBuf *add_ibuf_size(unsigned int width, unsigned int height, const char *name, int depth, int floatbuf, short uvtestgrid, float color[4]) 00391 { 00392 ImBuf *ibuf; 00393 unsigned char *rect= NULL; 00394 float *rect_float= NULL; 00395 00396 if (floatbuf) { 00397 ibuf= IMB_allocImBuf(width, height, depth, IB_rectfloat); 00398 rect_float= (float*)ibuf->rect_float; 00399 } 00400 else { 00401 ibuf= IMB_allocImBuf(width, height, depth, IB_rect); 00402 rect= (unsigned char*)ibuf->rect; 00403 } 00404 00405 BLI_strncpy(ibuf->name, name, sizeof(ibuf->name)); 00406 ibuf->userflags |= IB_BITMAPDIRTY; 00407 00408 switch(uvtestgrid) { 00409 case 1: 00410 BKE_image_buf_fill_checker(rect, rect_float, width, height); 00411 break; 00412 case 2: 00413 BKE_image_buf_fill_checker_color(rect, rect_float, width, height); 00414 break; 00415 default: 00416 BKE_image_buf_fill_color(rect, rect_float, width, height, color); 00417 } 00418 00419 return ibuf; 00420 } 00421 00422 /* adds new image block, creates ImBuf and initializes color */ 00423 Image *BKE_add_image_size(unsigned int width, unsigned int height, const char *name, int depth, int floatbuf, short uvtestgrid, float color[4]) 00424 { 00425 /* on save, type is changed to FILE in editsima.c */ 00426 Image *ima= image_alloc(name, IMA_SRC_GENERATED, IMA_TYPE_UV_TEST); 00427 00428 if (ima) { 00429 ImBuf *ibuf; 00430 00431 BLI_strncpy(ima->name, name, FILE_MAX); 00432 ima->gen_x= width; 00433 ima->gen_y= height; 00434 ima->gen_type= uvtestgrid; 00435 ima->gen_flag |= (floatbuf ? IMA_GEN_FLOAT : 0); 00436 00437 ibuf= add_ibuf_size(width, height, name, depth, floatbuf, uvtestgrid, color); 00438 image_assign_ibuf(ima, ibuf, IMA_NO_INDEX, 0); 00439 00440 ima->ok= IMA_OK_LOADED; 00441 } 00442 00443 return ima; 00444 } 00445 00446 /* creates an image image owns the imbuf passed */ 00447 Image *BKE_add_image_imbuf(ImBuf *ibuf) 00448 { 00449 /* on save, type is changed to FILE in editsima.c */ 00450 Image *ima; 00451 00452 ima= image_alloc(BLI_path_basename(ibuf->name), IMA_SRC_FILE, IMA_TYPE_IMAGE); 00453 00454 if (ima) { 00455 BLI_strncpy(ima->name, ibuf->name, FILE_MAX); 00456 image_assign_ibuf(ima, ibuf, IMA_NO_INDEX, 0); 00457 ima->ok= IMA_OK_LOADED; 00458 } 00459 00460 return ima; 00461 } 00462 00463 /* packs rect from memory as PNG */ 00464 void BKE_image_memorypack(Image *ima) 00465 { 00466 ImBuf *ibuf= image_get_ibuf(ima, IMA_NO_INDEX, 0); 00467 00468 if(ibuf==NULL) 00469 return; 00470 if (ima->packedfile) { 00471 freePackedFile(ima->packedfile); 00472 ima->packedfile = NULL; 00473 } 00474 00475 ibuf->ftype= PNG; 00476 ibuf->depth= 32; 00477 00478 IMB_saveiff(ibuf, ibuf->name, IB_rect | IB_mem); 00479 if(ibuf->encodedbuffer==NULL) { 00480 printf("memory save for pack error\n"); 00481 } 00482 else { 00483 PackedFile *pf = MEM_callocN(sizeof(*pf), "PackedFile"); 00484 00485 pf->data = ibuf->encodedbuffer; 00486 pf->size = ibuf->encodedsize; 00487 ima->packedfile= pf; 00488 ibuf->encodedbuffer= NULL; 00489 ibuf->encodedsize= 0; 00490 ibuf->userflags &= ~IB_BITMAPDIRTY; 00491 00492 if(ima->source==IMA_SRC_GENERATED) { 00493 ima->source= IMA_SRC_FILE; 00494 ima->type= IMA_TYPE_IMAGE; 00495 } 00496 } 00497 } 00498 00499 void tag_image_time(Image *ima) 00500 { 00501 if (ima) 00502 ima->lastused = (int)PIL_check_seconds_timer(); 00503 } 00504 00505 #if 0 00506 static void tag_all_images_time() 00507 { 00508 Image *ima; 00509 int ctime = (int)PIL_check_seconds_timer(); 00510 00511 ima= G.main->image.first; 00512 while(ima) { 00513 if(ima->bindcode || ima->repbind || ima->ibufs.first) { 00514 ima->lastused = ctime; 00515 } 00516 } 00517 } 00518 #endif 00519 00520 void free_old_images(void) 00521 { 00522 Image *ima; 00523 static int lasttime = 0; 00524 int ctime = (int)PIL_check_seconds_timer(); 00525 00526 /* 00527 Run garbage collector once for every collecting period of time 00528 if textimeout is 0, that's the option to NOT run the collector 00529 */ 00530 if (U.textimeout == 0 || ctime % U.texcollectrate || ctime == lasttime) 00531 return; 00532 00533 /* of course not! */ 00534 if (G.rendering) 00535 return; 00536 00537 lasttime = ctime; 00538 00539 ima= G.main->image.first; 00540 while(ima) { 00541 if((ima->flag & IMA_NOCOLLECT)==0 && ctime - ima->lastused > U.textimeout) { 00542 /* 00543 If it's in GL memory, deallocate and set time tag to current time 00544 This gives textures a "second chance" to be used before dying. 00545 */ 00546 if(ima->bindcode || ima->repbind) { 00547 GPU_free_image(ima); 00548 ima->lastused = ctime; 00549 } 00550 /* Otherwise, just kill the buffers */ 00551 else if (ima->ibufs.first) { 00552 image_free_buffers(ima); 00553 } 00554 } 00555 ima = ima->id.next; 00556 } 00557 } 00558 00559 static uintptr_t image_mem_size(Image *ima) 00560 { 00561 ImBuf *ibuf, *ibufm; 00562 int level; 00563 uintptr_t size = 0; 00564 00565 size= 0; 00566 00567 /* viewers have memory depending on other rules, has no valid rect pointer */ 00568 if(ima->source==IMA_SRC_VIEWER) 00569 return 0; 00570 00571 for(ibuf= ima->ibufs.first; ibuf; ibuf= ibuf->next) { 00572 if(ibuf->rect) size += MEM_allocN_len(ibuf->rect); 00573 else if(ibuf->rect_float) size += MEM_allocN_len(ibuf->rect_float); 00574 00575 for(level=0; level<IB_MIPMAP_LEVELS; level++) { 00576 ibufm= ibuf->mipmap[level]; 00577 if(ibufm) { 00578 if(ibufm->rect) size += MEM_allocN_len(ibufm->rect); 00579 else if(ibufm->rect_float) size += MEM_allocN_len(ibufm->rect_float); 00580 } 00581 } 00582 } 00583 00584 return size; 00585 } 00586 00587 void BKE_image_print_memlist(void) 00588 { 00589 Image *ima; 00590 uintptr_t size, totsize= 0; 00591 00592 for(ima= G.main->image.first; ima; ima= ima->id.next) 00593 totsize += image_mem_size(ima); 00594 00595 printf("\ntotal image memory len: %.3lf MB\n", (double)totsize/(double)(1024*1024)); 00596 00597 for(ima= G.main->image.first; ima; ima= ima->id.next) { 00598 size= image_mem_size(ima); 00599 00600 if(size) 00601 printf("%s len: %.3f MB\n", ima->id.name+2, (double)size/(double)(1024*1024)); 00602 } 00603 } 00604 00605 void BKE_image_free_all_textures(void) 00606 { 00607 Tex *tex; 00608 Image *ima; 00609 /* unsigned int totsize= 0; */ 00610 00611 for(ima= G.main->image.first; ima; ima= ima->id.next) 00612 ima->id.flag &= ~LIB_DOIT; 00613 00614 for(tex= G.main->tex.first; tex; tex= tex->id.next) 00615 if(tex->ima) 00616 tex->ima->id.flag |= LIB_DOIT; 00617 00618 for(ima= G.main->image.first; ima; ima= ima->id.next) { 00619 if(ima->ibufs.first && (ima->id.flag & LIB_DOIT)) { 00620 ImBuf *ibuf; 00621 00622 for(ibuf= ima->ibufs.first; ibuf; ibuf= ibuf->next) { 00623 /* escape when image is painted on */ 00624 if(ibuf->userflags & IB_BITMAPDIRTY) 00625 break; 00626 00627 /* if(ibuf->mipmap[0]) 00628 totsize+= 1.33*ibuf->x*ibuf->y*4; 00629 else 00630 totsize+= ibuf->x*ibuf->y*4;*/ 00631 00632 } 00633 if(ibuf==NULL) 00634 image_free_buffers(ima); 00635 } 00636 } 00637 /* printf("freed total %d MB\n", totsize/(1024*1024)); */ 00638 } 00639 00640 /* except_frame is weak, only works for seqs without offset... */ 00641 void BKE_image_free_anim_ibufs(Image *ima, int except_frame) 00642 { 00643 ImBuf *ibuf, *nbuf; 00644 00645 for(ibuf= ima->ibufs.first; ibuf; ibuf= nbuf) { 00646 nbuf= ibuf->next; 00647 if(ibuf->userflags & IB_BITMAPDIRTY) 00648 continue; 00649 if(ibuf->index==IMA_NO_INDEX) 00650 continue; 00651 if(except_frame!=IMA_INDEX_FRAME(ibuf->index)) { 00652 BLI_remlink(&ima->ibufs, ibuf); 00653 00654 if (ibuf->userdata) { 00655 MEM_freeN(ibuf->userdata); 00656 ibuf->userdata = NULL; 00657 } 00658 IMB_freeImBuf(ibuf); 00659 } 00660 } 00661 } 00662 00663 void BKE_image_all_free_anim_ibufs(int cfra) 00664 { 00665 Image *ima; 00666 00667 for(ima= G.main->image.first; ima; ima= ima->id.next) 00668 if(ELEM(ima->source, IMA_SRC_SEQUENCE, IMA_SRC_MOVIE)) 00669 BKE_image_free_anim_ibufs(ima, cfra); 00670 } 00671 00672 00673 /* *********** READ AND WRITE ************** */ 00674 00675 int BKE_imtype_to_ftype(int imtype) 00676 { 00677 if(imtype==R_TARGA) 00678 return TGA; 00679 else if(imtype==R_RAWTGA) 00680 return RAWTGA; 00681 else if(imtype== R_IRIS) 00682 return IMAGIC; 00683 #ifdef WITH_HDR 00684 else if (imtype==R_RADHDR) 00685 return RADHDR; 00686 #endif 00687 else if (imtype==R_PNG) 00688 return PNG; 00689 #ifdef WITH_DDS 00690 else if (imtype==R_DDS) 00691 return DDS; 00692 #endif 00693 else if (imtype==R_BMP) 00694 return BMP; 00695 #ifdef WITH_TIFF 00696 else if (imtype==R_TIFF) 00697 return TIF; 00698 #endif 00699 else if (imtype==R_OPENEXR || imtype==R_MULTILAYER) 00700 return OPENEXR; 00701 #ifdef WITH_CINEON 00702 else if (imtype==R_CINEON) 00703 return CINEON; 00704 else if (imtype==R_DPX) 00705 return DPX; 00706 #endif 00707 #ifdef WITH_OPENJPEG 00708 else if(imtype==R_JP2) 00709 return JP2; 00710 #endif 00711 else 00712 return JPG|90; 00713 } 00714 00715 int BKE_ftype_to_imtype(int ftype) 00716 { 00717 if(ftype==0) 00718 return TGA; 00719 else if(ftype == IMAGIC) 00720 return R_IRIS; 00721 #ifdef WITH_HDR 00722 else if (ftype & RADHDR) 00723 return R_RADHDR; 00724 #endif 00725 else if (ftype & PNG) 00726 return R_PNG; 00727 #ifdef WITH_DDS 00728 else if (ftype & DDS) 00729 return R_DDS; 00730 #endif 00731 else if (ftype & BMP) 00732 return R_BMP; 00733 #ifdef WITH_TIFF 00734 else if (ftype & TIF) 00735 return R_TIFF; 00736 #endif 00737 else if (ftype & OPENEXR) 00738 return R_OPENEXR; 00739 #ifdef WITH_CINEON 00740 else if (ftype & CINEON) 00741 return R_CINEON; 00742 else if (ftype & DPX) 00743 return R_DPX; 00744 #endif 00745 else if (ftype & TGA) 00746 return R_TARGA; 00747 else if(ftype & RAWTGA) 00748 return R_RAWTGA; 00749 #ifdef WITH_OPENJPEG 00750 else if(ftype & JP2) 00751 return R_JP2; 00752 #endif 00753 else 00754 return R_JPEG90; 00755 } 00756 00757 00758 int BKE_imtype_is_movie(int imtype) 00759 { 00760 switch(imtype) { 00761 case R_AVIRAW: 00762 case R_AVIJPEG: 00763 case R_AVICODEC: 00764 case R_QUICKTIME: 00765 case R_FFMPEG: 00766 case R_H264: 00767 case R_THEORA: 00768 case R_XVID: 00769 case R_FRAMESERVER: 00770 return 1; 00771 } 00772 return 0; 00773 } 00774 00775 int BKE_add_image_extension(char *string, int imtype) 00776 { 00777 const char *extension= NULL; 00778 00779 if(imtype== R_IRIS) { 00780 if(!BLI_testextensie(string, ".rgb")) 00781 extension= ".rgb"; 00782 } 00783 else if(imtype==R_IRIZ) { 00784 if(!BLI_testextensie(string, ".rgb")) 00785 extension= ".rgb"; 00786 } 00787 #ifdef WITH_HDR 00788 else if(imtype==R_RADHDR) { 00789 if(!BLI_testextensie(string, ".hdr")) 00790 extension= ".hdr"; 00791 } 00792 #endif 00793 else if (ELEM5(imtype, R_PNG, R_FFMPEG, R_H264, R_THEORA, R_XVID)) { 00794 if(!BLI_testextensie(string, ".png")) 00795 extension= ".png"; 00796 } 00797 #ifdef WITH_DDS 00798 else if(imtype==R_DDS) { 00799 if(!BLI_testextensie(string, ".dds")) 00800 extension= ".dds"; 00801 } 00802 #endif 00803 else if(imtype==R_RAWTGA) { 00804 if(!BLI_testextensie(string, ".tga")) 00805 extension= ".tga"; 00806 } 00807 else if(imtype==R_BMP) { 00808 if(!BLI_testextensie(string, ".bmp")) 00809 extension= ".bmp"; 00810 } 00811 #ifdef WITH_TIFF 00812 else if(imtype==R_TIFF) { 00813 if(!BLI_testextensie(string, ".tif") && 00814 !BLI_testextensie(string, ".tiff")) extension= ".tif"; 00815 } 00816 #endif 00817 #ifdef WITH_OPENEXR 00818 else if( ELEM(imtype, R_OPENEXR, R_MULTILAYER)) { 00819 if(!BLI_testextensie(string, ".exr")) 00820 extension= ".exr"; 00821 } 00822 #endif 00823 #ifdef WITH_CINEON 00824 else if(imtype==R_CINEON){ 00825 if (!BLI_testextensie(string, ".cin")) 00826 extension= ".cin"; 00827 } 00828 else if(imtype==R_DPX){ 00829 if (!BLI_testextensie(string, ".dpx")) 00830 extension= ".dpx"; 00831 } 00832 #endif 00833 else if(imtype==R_TARGA) { 00834 if(!BLI_testextensie(string, ".tga")) 00835 extension= ".tga"; 00836 } 00837 #ifdef WITH_OPENJPEG 00838 else if(imtype==R_JP2) { 00839 if(!BLI_testextensie(string, ".jp2")) 00840 extension= ".jp2"; 00841 } 00842 #endif 00843 else { // R_AVICODEC, R_AVIRAW, R_AVIJPEG, R_JPEG90, R_QUICKTIME etc 00844 if(!( BLI_testextensie(string, ".jpg") || BLI_testextensie(string, ".jpeg"))) 00845 extension= ".jpg"; 00846 } 00847 00848 if(extension) { 00849 /* prefer this in many cases to avoid .png.tga, but in certain cases it breaks */ 00850 /* remove any other known image extension */ 00851 if(BLI_testextensie_array(string, imb_ext_image) 00852 || (G.have_quicktime && BLI_testextensie_array(string, imb_ext_image_qt))) { 00853 return BLI_replace_extension(string, FILE_MAX, extension); 00854 } else { 00855 strcat(string, extension); 00856 return TRUE; 00857 } 00858 00859 } 00860 else { 00861 return FALSE; 00862 } 00863 } 00864 00865 /* could allow access externally - 512 is for long names, 64 is for id names */ 00866 typedef struct StampData { 00867 char file[512]; 00868 char note[512]; 00869 char date[512]; 00870 char marker[512]; 00871 char time[512]; 00872 char frame[512]; 00873 char camera[64]; 00874 char cameralens[64]; 00875 char scene[64]; 00876 char strip[64]; 00877 char rendertime[64]; 00878 } StampData; 00879 00880 static void stampdata(Scene *scene, Object *camera, StampData *stamp_data, int do_prefix) 00881 { 00882 char text[256]; 00883 struct tm *tl; 00884 time_t t; 00885 00886 if (scene->r.stamp & R_STAMP_FILENAME) { 00887 BLI_snprintf(stamp_data->file, sizeof(stamp_data->file), do_prefix ? "File %s":"%s", G.relbase_valid ? G.main->name:"<untitled>"); 00888 } else { 00889 stamp_data->file[0] = '\0'; 00890 } 00891 00892 if (scene->r.stamp & R_STAMP_NOTE) { 00893 /* Never do prefix for Note */ 00894 BLI_snprintf(stamp_data->note, sizeof(stamp_data->note), "%s", scene->r.stamp_udata); 00895 } else { 00896 stamp_data->note[0] = '\0'; 00897 } 00898 00899 if (scene->r.stamp & R_STAMP_DATE) { 00900 t = time(NULL); 00901 tl = localtime(&t); 00902 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); 00903 BLI_snprintf(stamp_data->date, sizeof(stamp_data->date), do_prefix ? "Date %s":"%s", text); 00904 } else { 00905 stamp_data->date[0] = '\0'; 00906 } 00907 00908 if (scene->r.stamp & R_STAMP_MARKER) { 00909 char *name = scene_find_last_marker_name(scene, CFRA); 00910 00911 if (name) strcpy(text, name); 00912 else strcpy(text, "<none>"); 00913 00914 BLI_snprintf(stamp_data->marker, sizeof(stamp_data->marker), do_prefix ? "Marker %s":"%s", text); 00915 } else { 00916 stamp_data->marker[0] = '\0'; 00917 } 00918 00919 if (scene->r.stamp & R_STAMP_TIME) { 00920 int f = (int)(scene->r.cfra % scene->r.frs_sec); 00921 int s = (int)(scene->r.cfra / scene->r.frs_sec); 00922 int h= 0; 00923 int m= 0; 00924 00925 if (s) { 00926 m = (int)(s / 60); 00927 s %= 60; 00928 00929 if (m) { 00930 h = (int)(m / 60); 00931 m %= 60; 00932 } 00933 } 00934 00935 if (scene->r.frs_sec < 100) 00936 BLI_snprintf(text, sizeof(text), "%02d:%02d:%02d.%02d", h, m, s, f); 00937 else 00938 BLI_snprintf(text, sizeof(text), "%02d:%02d:%02d.%03d", h, m, s, f); 00939 00940 BLI_snprintf(stamp_data->time, sizeof(stamp_data->time), do_prefix ? "Time %s":"%s", text); 00941 } else { 00942 stamp_data->time[0] = '\0'; 00943 } 00944 00945 if (scene->r.stamp & R_STAMP_FRAME) { 00946 char format[32]; 00947 int digits= 1; 00948 00949 if(scene->r.efra>9) 00950 digits= 1 + (int) log10(scene->r.efra); 00951 00952 BLI_snprintf(format, sizeof(format), do_prefix ? "Frame %%0%di":"%%0%di", digits); 00953 BLI_snprintf (stamp_data->frame, sizeof(stamp_data->frame), format, scene->r.cfra); 00954 } else { 00955 stamp_data->frame[0] = '\0'; 00956 } 00957 00958 if (scene->r.stamp & R_STAMP_CAMERA) { 00959 BLI_snprintf(stamp_data->camera, sizeof(stamp_data->camera), do_prefix ? "Camera %s":"%s", camera ? camera->id.name+2 : "<none>"); 00960 } else { 00961 stamp_data->camera[0] = '\0'; 00962 } 00963 00964 if (scene->r.stamp & R_STAMP_CAMERALENS) { 00965 if (camera && camera->type == OB_CAMERA) { 00966 BLI_snprintf(text, sizeof(text), "%.2f", ((Camera *)camera->data)->lens); 00967 } 00968 else strcpy(text, "<none>"); 00969 00970 BLI_snprintf(stamp_data->cameralens, sizeof(stamp_data->cameralens), do_prefix ? "Lens %s":"%s", text); 00971 } else { 00972 stamp_data->cameralens[0] = '\0'; 00973 } 00974 00975 if (scene->r.stamp & R_STAMP_SCENE) { 00976 BLI_snprintf(stamp_data->scene, sizeof(stamp_data->scene), do_prefix ? "Scene %s":"%s", scene->id.name+2); 00977 } else { 00978 stamp_data->scene[0] = '\0'; 00979 } 00980 00981 if (scene->r.stamp & R_STAMP_SEQSTRIP) { 00982 Sequence *seq= seq_foreground_frame_get(scene, scene->r.cfra); 00983 00984 if (seq) strcpy(text, seq->name+2); 00985 else strcpy(text, "<none>"); 00986 00987 BLI_snprintf(stamp_data->strip, sizeof(stamp_data->strip), do_prefix ? "Strip %s":"%s", text); 00988 } else { 00989 stamp_data->strip[0] = '\0'; 00990 } 00991 00992 { 00993 Render *re= RE_GetRender(scene->id.name); 00994 RenderStats *stats= re ? RE_GetStats(re):NULL; 00995 00996 if (stats && (scene->r.stamp & R_STAMP_RENDERTIME)) { 00997 BLI_timestr(stats->lastframetime, text); 00998 00999 BLI_snprintf(stamp_data->rendertime, sizeof(stamp_data->rendertime), do_prefix ? "RenderTime %s":"%s", text); 01000 } else { 01001 stamp_data->rendertime[0] = '\0'; 01002 } 01003 } 01004 } 01005 01006 void BKE_stamp_buf(Scene *scene, Object *camera, unsigned char *rect, float *rectf, int width, int height, int channels) 01007 { 01008 struct StampData stamp_data; 01009 float w, h, pad; 01010 int x, y, y_ofs; 01011 float h_fixed; 01012 const int mono= blf_mono_font_render; // XXX 01013 01014 #define BUFF_MARGIN_X 2 01015 #define BUFF_MARGIN_Y 1 01016 01017 if (!rect && !rectf) 01018 return; 01019 01020 stampdata(scene, camera, &stamp_data, 1); 01021 01022 /* TODO, do_versions */ 01023 if(scene->r.stamp_font_id < 8) 01024 scene->r.stamp_font_id= 12; 01025 01026 /* set before return */ 01027 BLF_size(mono, scene->r.stamp_font_id, 72); 01028 01029 BLF_buffer(mono, rectf, rect, width, height, channels); 01030 BLF_buffer_col(mono, scene->r.fg_stamp[0], scene->r.fg_stamp[1], scene->r.fg_stamp[2], 1.0); 01031 pad= BLF_width_max(mono); 01032 01033 /* use 'h_fixed' rather than 'h', aligns better */ 01034 h_fixed= BLF_height_max(mono); 01035 y_ofs = -BLF_descender(mono); 01036 01037 x= 0; 01038 y= height; 01039 01040 if (stamp_data.file[0]) { 01041 /* Top left corner */ 01042 BLF_width_and_height(mono, stamp_data.file, &w, &h); h= h_fixed; 01043 y -= h; 01044 01045 /* also a little of space to the background. */ 01046 buf_rectfill_area(rect, rectf, width, height, scene->r.bg_stamp, x-BUFF_MARGIN_X, y-BUFF_MARGIN_Y, w+BUFF_MARGIN_X, y+h+BUFF_MARGIN_Y); 01047 01048 /* and draw the text. */ 01049 BLF_position(mono, x, y + y_ofs, 0.0); 01050 BLF_draw_buffer(mono, stamp_data.file); 01051 01052 /* the extra pixel for background. */ 01053 y -= BUFF_MARGIN_Y * 2; 01054 } 01055 01056 /* Top left corner, below File */ 01057 if (stamp_data.note[0]) { 01058 BLF_width_and_height(mono, stamp_data.note, &w, &h); h= h_fixed; 01059 y -= h; 01060 01061 /* and space for background. */ 01062 buf_rectfill_area(rect, rectf, width, height, scene->r.bg_stamp, 0, y-BUFF_MARGIN_Y, w+BUFF_MARGIN_X, y+h+BUFF_MARGIN_Y); 01063 01064 BLF_position(mono, x, y + y_ofs, 0.0); 01065 BLF_draw_buffer(mono, stamp_data.note); 01066 01067 /* the extra pixel for background. */ 01068 y -= BUFF_MARGIN_Y * 2; 01069 } 01070 01071 /* Top left corner, below File (or Note) */ 01072 if (stamp_data.date[0]) { 01073 BLF_width_and_height(mono, stamp_data.date, &w, &h); h= h_fixed; 01074 y -= h; 01075 01076 /* and space for background. */ 01077 buf_rectfill_area(rect, rectf, width, height, scene->r.bg_stamp, 0, y-BUFF_MARGIN_Y, w+BUFF_MARGIN_X, y+h+BUFF_MARGIN_Y); 01078 01079 BLF_position(mono, x, y + y_ofs, 0.0); 01080 BLF_draw_buffer(mono, stamp_data.date); 01081 01082 /* the extra pixel for background. */ 01083 y -= BUFF_MARGIN_Y * 2; 01084 } 01085 01086 /* Top left corner, below File, Date or Note */ 01087 if (stamp_data.rendertime[0]) { 01088 BLF_width_and_height(mono, stamp_data.rendertime, &w, &h); h= h_fixed; 01089 y -= h; 01090 01091 /* and space for background. */ 01092 buf_rectfill_area(rect, rectf, width, height, scene->r.bg_stamp, 0, y-BUFF_MARGIN_Y, w+BUFF_MARGIN_X, y+h+BUFF_MARGIN_Y); 01093 01094 BLF_position(mono, x, y + y_ofs, 0.0); 01095 BLF_draw_buffer(mono, stamp_data.rendertime); 01096 } 01097 01098 x= 0; 01099 y= 0; 01100 01101 /* Bottom left corner, leaving space for timing */ 01102 if (stamp_data.marker[0]) { 01103 BLF_width_and_height(mono, stamp_data.marker, &w, &h); h= h_fixed; 01104 01105 /* extra space for background. */ 01106 buf_rectfill_area(rect, rectf, width, height, scene->r.bg_stamp, x-BUFF_MARGIN_X, y-BUFF_MARGIN_Y, w+BUFF_MARGIN_X, y+h+BUFF_MARGIN_Y); 01107 01108 /* and pad the text. */ 01109 BLF_position(mono, x, y + y_ofs, 0.0); 01110 BLF_draw_buffer(mono, stamp_data.marker); 01111 01112 /* space width. */ 01113 x += w + pad; 01114 } 01115 01116 /* Left bottom corner */ 01117 if (stamp_data.time[0]) { 01118 BLF_width_and_height(mono, stamp_data.time, &w, &h); h= h_fixed; 01119 01120 /* extra space for background */ 01121 buf_rectfill_area(rect, rectf, width, height, scene->r.bg_stamp, x-BUFF_MARGIN_X, y, x+w+BUFF_MARGIN_X, y+h+BUFF_MARGIN_Y); 01122 01123 /* and pad the text. */ 01124 BLF_position(mono, x, y + y_ofs, 0.0); 01125 BLF_draw_buffer(mono, stamp_data.time); 01126 01127 /* space width. */ 01128 x += w + pad; 01129 } 01130 01131 if (stamp_data.frame[0]) { 01132 BLF_width_and_height(mono, stamp_data.frame, &w, &h); h= h_fixed; 01133 01134 /* extra space for background. */ 01135 buf_rectfill_area(rect, rectf, width, height, scene->r.bg_stamp, x-BUFF_MARGIN_X, y-BUFF_MARGIN_Y, x+w+BUFF_MARGIN_X, y+h+BUFF_MARGIN_Y); 01136 01137 /* and pad the text. */ 01138 BLF_position(mono, x, y + y_ofs, 0.0); 01139 BLF_draw_buffer(mono, stamp_data.frame); 01140 01141 /* space width. */ 01142 x += w + pad; 01143 } 01144 01145 if (stamp_data.camera[0]) { 01146 BLF_width_and_height(mono, stamp_data.camera, &w, &h); h= h_fixed; 01147 01148 /* extra space for background. */ 01149 buf_rectfill_area(rect, rectf, width, height, scene->r.bg_stamp, x-BUFF_MARGIN_X, y-BUFF_MARGIN_Y, x+w+BUFF_MARGIN_X, y+h+BUFF_MARGIN_Y); 01150 BLF_position(mono, x, y + y_ofs, 0.0); 01151 BLF_draw_buffer(mono, stamp_data.camera); 01152 01153 /* space width. */ 01154 x += w + pad; 01155 } 01156 01157 if (stamp_data.cameralens[0]) { 01158 BLF_width_and_height(mono, stamp_data.cameralens, &w, &h); h= h_fixed; 01159 01160 /* extra space for background. */ 01161 buf_rectfill_area(rect, rectf, width, height, scene->r.bg_stamp, x-BUFF_MARGIN_X, y-BUFF_MARGIN_Y, x+w+BUFF_MARGIN_X, y+h+BUFF_MARGIN_Y); 01162 BLF_position(mono, x, y + y_ofs, 0.0); 01163 BLF_draw_buffer(mono, stamp_data.cameralens); 01164 } 01165 01166 if (stamp_data.scene[0]) { 01167 BLF_width_and_height(mono, stamp_data.scene, &w, &h); h= h_fixed; 01168 01169 /* Bottom right corner, with an extra space because blenfont is too strict! */ 01170 x= width - w - 2; 01171 01172 /* extra space for background. */ 01173 buf_rectfill_area(rect, rectf, width, height, scene->r.bg_stamp, x-BUFF_MARGIN_X, y-BUFF_MARGIN_Y, x+w+BUFF_MARGIN_X, y+h+BUFF_MARGIN_Y); 01174 01175 /* and pad the text. */ 01176 BLF_position(mono, x, y+y_ofs, 0.0); 01177 BLF_draw_buffer(mono, stamp_data.scene); 01178 } 01179 01180 if (stamp_data.strip[0]) { 01181 BLF_width_and_height(mono, stamp_data.strip, &w, &h); h= h_fixed; 01182 01183 /* Top right corner, with an extra space because blenfont is too strict! */ 01184 x= width - w - pad; 01185 y= height - h; 01186 01187 /* extra space for background. */ 01188 buf_rectfill_area(rect, rectf, width, height, scene->r.bg_stamp, x-BUFF_MARGIN_X, y-BUFF_MARGIN_Y, x+w+BUFF_MARGIN_X, y+h+BUFF_MARGIN_Y); 01189 01190 BLF_position(mono, x, y + y_ofs, 0.0); 01191 BLF_draw_buffer(mono, stamp_data.strip); 01192 } 01193 01194 /* cleanup the buffer. */ 01195 BLF_buffer(mono, NULL, NULL, 0, 0, 0); 01196 01197 #undef BUFF_MARGIN_X 01198 #undef BUFF_MARGIN_Y 01199 } 01200 01201 void BKE_stamp_info(Scene *scene, Object *camera, struct ImBuf *ibuf) 01202 { 01203 struct StampData stamp_data; 01204 01205 if (!ibuf) return; 01206 01207 /* fill all the data values, no prefix */ 01208 stampdata(scene, camera, &stamp_data, 0); 01209 01210 if (stamp_data.file[0]) IMB_metadata_change_field (ibuf, "File", stamp_data.file); 01211 if (stamp_data.note[0]) IMB_metadata_change_field (ibuf, "Note", stamp_data.note); 01212 if (stamp_data.date[0]) IMB_metadata_change_field (ibuf, "Date", stamp_data.date); 01213 if (stamp_data.marker[0]) IMB_metadata_change_field (ibuf, "Marker", stamp_data.marker); 01214 if (stamp_data.time[0]) IMB_metadata_change_field (ibuf, "Time", stamp_data.time); 01215 if (stamp_data.frame[0]) IMB_metadata_change_field (ibuf, "Frame", stamp_data.frame); 01216 if (stamp_data.camera[0]) IMB_metadata_change_field (ibuf, "Camera", stamp_data.camera); 01217 if (stamp_data.cameralens[0]) IMB_metadata_change_field (ibuf, "Lens", stamp_data.cameralens); 01218 if (stamp_data.scene[0]) IMB_metadata_change_field (ibuf, "Scene", stamp_data.scene); 01219 if (stamp_data.strip[0]) IMB_metadata_change_field (ibuf, "Strip", stamp_data.strip); 01220 if (stamp_data.rendertime[0]) IMB_metadata_change_field (ibuf, "RenderTime", stamp_data.rendertime); 01221 } 01222 01223 int BKE_alphatest_ibuf(ImBuf *ibuf) 01224 { 01225 int tot; 01226 if(ibuf->rect_float) { 01227 float *buf= ibuf->rect_float; 01228 for(tot= ibuf->x * ibuf->y; tot--; buf+=4) { 01229 if(buf[3] < 1.0f) { 01230 return TRUE; 01231 } 01232 } 01233 } 01234 else if (ibuf->rect) { 01235 unsigned char *buf= (unsigned char *)ibuf->rect; 01236 for(tot= ibuf->x * ibuf->y; tot--; buf+=4) { 01237 if(buf[3] != 255) { 01238 return TRUE; 01239 } 01240 } 01241 } 01242 01243 return FALSE; 01244 } 01245 01246 int BKE_write_ibuf(ImBuf *ibuf, const char *name, int imtype, int subimtype, int quality) 01247 { 01248 int ok; 01249 (void)subimtype; /* quies unused warnings */ 01250 01251 if(imtype == -1) { 01252 /* use whatever existing image type is set by 'ibuf' */ 01253 } 01254 else if(imtype== R_IRIS) { 01255 ibuf->ftype= IMAGIC; 01256 } 01257 #ifdef WITH_HDR 01258 else if (imtype==R_RADHDR) { 01259 ibuf->ftype= RADHDR; 01260 } 01261 #endif 01262 else if (ELEM5(imtype, R_PNG, R_FFMPEG, R_H264, R_THEORA, R_XVID)) { 01263 ibuf->ftype= PNG; 01264 01265 if(imtype==R_PNG) 01266 ibuf->ftype |= quality; /* quality is actually compression 0-100 --> 0-9 */ 01267 01268 } 01269 #ifdef WITH_DDS 01270 else if (imtype==R_DDS) { 01271 ibuf->ftype= DDS; 01272 } 01273 #endif 01274 else if (imtype==R_BMP) { 01275 ibuf->ftype= BMP; 01276 } 01277 #ifdef WITH_TIFF 01278 else if (imtype==R_TIFF) { 01279 ibuf->ftype= TIF; 01280 01281 if(subimtype & R_TIFF_16BIT) 01282 ibuf->ftype |= TIF_16BIT; 01283 } 01284 #endif 01285 #ifdef WITH_OPENEXR 01286 else if (imtype==R_OPENEXR || imtype==R_MULTILAYER) { 01287 ibuf->ftype= OPENEXR; 01288 if(subimtype & R_OPENEXR_HALF) 01289 ibuf->ftype |= OPENEXR_HALF; 01290 ibuf->ftype |= (quality & OPENEXR_COMPRESS); 01291 01292 if(!(subimtype & R_OPENEXR_ZBUF)) 01293 ibuf->zbuf_float = NULL; /* signal for exr saving */ 01294 01295 } 01296 #endif 01297 #ifdef WITH_CINEON 01298 else if (imtype==R_CINEON) { 01299 ibuf->ftype = CINEON; 01300 } 01301 else if (imtype==R_DPX) { 01302 ibuf->ftype = DPX; 01303 } 01304 #endif 01305 else if (imtype==R_TARGA) { 01306 ibuf->ftype= TGA; 01307 } 01308 else if(imtype==R_RAWTGA) { 01309 ibuf->ftype= RAWTGA; 01310 } 01311 #ifdef WITH_OPENJPEG 01312 else if(imtype==R_JP2) { 01313 if(quality < 10) quality= 90; 01314 ibuf->ftype= JP2|quality; 01315 01316 if (subimtype & R_JPEG2K_16BIT) { 01317 ibuf->ftype |= JP2_16BIT; 01318 } else if (subimtype & R_JPEG2K_12BIT) { 01319 ibuf->ftype |= JP2_12BIT; 01320 } 01321 01322 if (subimtype & R_JPEG2K_YCC) { 01323 ibuf->ftype |= JP2_YCC; 01324 } 01325 01326 if (subimtype & R_JPEG2K_CINE_PRESET) { 01327 ibuf->ftype |= JP2_CINE; 01328 if (subimtype & R_JPEG2K_CINE_48FPS) 01329 ibuf->ftype |= JP2_CINE_48FPS; 01330 } 01331 } 01332 #endif 01333 else { 01334 /* R_JPEG90, etc. default we save jpegs */ 01335 if(quality < 10) quality= 90; 01336 ibuf->ftype= JPG|quality; 01337 if(ibuf->depth==32) ibuf->depth= 24; /* unsupported feature only confuses other s/w */ 01338 } 01339 01340 BLI_make_existing_file(name); 01341 01342 ok = IMB_saveiff(ibuf, name, IB_rect | IB_zbuf | IB_zbuffloat); 01343 if (ok == 0) { 01344 perror(name); 01345 } 01346 01347 return(ok); 01348 } 01349 01350 int BKE_write_ibuf_stamp(Scene *scene, struct Object *camera, ImBuf *ibuf, const char *name, int imtype, int subimtype, int quality) 01351 { 01352 if(scene && scene->r.stamp & R_STAMP_ALL) 01353 BKE_stamp_info(scene, camera, ibuf); 01354 01355 return BKE_write_ibuf(ibuf, name, imtype, subimtype, quality); 01356 } 01357 01358 01359 void BKE_makepicstring(char *string, const char *base, int frame, int imtype, const short use_ext, const short use_frames) 01360 { 01361 if (string==NULL) return; 01362 BLI_strncpy(string, base, FILE_MAX - 10); /* weak assumption */ 01363 BLI_path_abs(string, G.main->name); 01364 01365 if(use_frames) 01366 BLI_path_frame(string, frame, 4); 01367 01368 if(use_ext) 01369 BKE_add_image_extension(string, imtype); 01370 01371 } 01372 01373 /* used by sequencer too */ 01374 struct anim *openanim(char *name, int flags) 01375 { 01376 struct anim *anim; 01377 struct ImBuf *ibuf; 01378 01379 anim = IMB_open_anim(name, flags); 01380 if (anim == NULL) return NULL; 01381 01382 ibuf = IMB_anim_absolute(anim, 0); 01383 if (ibuf == NULL) { 01384 if(BLI_exists(name)) 01385 printf("not an anim: %s\n", name); 01386 else 01387 printf("anim file doesn't exist: %s\n", name); 01388 IMB_free_anim(anim); 01389 return NULL; 01390 } 01391 IMB_freeImBuf(ibuf); 01392 01393 return(anim); 01394 } 01395 01396 /* ************************* New Image API *************** */ 01397 01398 01399 /* Notes about Image storage 01400 - packedfile 01401 -> written in .blend 01402 - filename 01403 -> written in .blend 01404 - movie 01405 -> comes from packedfile or filename 01406 - renderresult 01407 -> comes from packedfile or filename 01408 - listbase 01409 -> ibufs from exrhandle 01410 - flipbook array 01411 -> ibufs come from movie, temporary renderresult or sequence 01412 - ibuf 01413 -> comes from packedfile or filename or generated 01414 01415 */ 01416 01417 01418 /* forces existence of 1 Image for renderout or nodes, returns Image */ 01419 /* name is only for default, when making new one */ 01420 Image *BKE_image_verify_viewer(int type, const char *name) 01421 { 01422 Image *ima; 01423 01424 for(ima=G.main->image.first; ima; ima= ima->id.next) 01425 if(ima->source==IMA_SRC_VIEWER) 01426 if(ima->type==type) 01427 break; 01428 01429 if(ima==NULL) 01430 ima= image_alloc(name, IMA_SRC_VIEWER, type); 01431 01432 /* happens on reload, imagewindow cannot be image user when hidden*/ 01433 if(ima->id.us==0) 01434 id_us_plus(&ima->id); 01435 01436 return ima; 01437 } 01438 01439 void BKE_image_assign_ibuf(Image *ima, ImBuf *ibuf) 01440 { 01441 image_assign_ibuf(ima, ibuf, IMA_NO_INDEX, 0); 01442 } 01443 01444 void BKE_image_signal(Image *ima, ImageUser *iuser, int signal) 01445 { 01446 if(ima==NULL) 01447 return; 01448 01449 switch(signal) { 01450 case IMA_SIGNAL_FREE: 01451 image_free_buffers(ima); 01452 if(iuser) 01453 iuser->ok= 1; 01454 break; 01455 case IMA_SIGNAL_SRC_CHANGE: 01456 if(ima->type == IMA_TYPE_UV_TEST) 01457 if(ima->source != IMA_SRC_GENERATED) 01458 ima->type= IMA_TYPE_IMAGE; 01459 01460 if(ima->source==IMA_SRC_GENERATED) { 01461 if(ima->gen_x==0 || ima->gen_y==0) { 01462 ImBuf *ibuf= image_get_ibuf(ima, IMA_NO_INDEX, 0); 01463 if(ibuf) { 01464 ima->gen_x= ibuf->x; 01465 ima->gen_y= ibuf->y; 01466 } 01467 } 01468 } 01469 01470 /* force reload on first use, but not for multilayer, that makes nodes and buttons in ui drawing fail */ 01471 if(ima->type!=IMA_TYPE_MULTILAYER) 01472 image_free_buffers(ima); 01473 01474 ima->ok= 1; 01475 if(iuser) 01476 iuser->ok= 1; 01477 break; 01478 01479 case IMA_SIGNAL_RELOAD: 01480 /* try to repack file */ 01481 if(ima->packedfile) { 01482 PackedFile *pf; 01483 pf = newPackedFile(NULL, ima->name); 01484 if (pf) { 01485 freePackedFile(ima->packedfile); 01486 ima->packedfile = pf; 01487 image_free_buffers(ima); 01488 } else { 01489 printf("ERROR: Image not available. Keeping packed image\n"); 01490 } 01491 } 01492 else 01493 image_free_buffers(ima); 01494 01495 if(iuser) 01496 iuser->ok= 1; 01497 01498 break; 01499 case IMA_SIGNAL_USER_NEW_IMAGE: 01500 if(iuser) { 01501 iuser->ok= 1; 01502 if(ima->source==IMA_SRC_FILE || ima->source==IMA_SRC_SEQUENCE) { 01503 if(ima->type==IMA_TYPE_MULTILAYER) { 01504 iuser->multi_index= 0; 01505 iuser->layer= iuser->pass= 0; 01506 } 01507 } 01508 } 01509 break; 01510 } 01511 01512 /* dont use notifiers because they are not 100% sure to succseed 01513 * this also makes sure all scenes are accounted for. */ 01514 { 01515 Scene *scene; 01516 for(scene= G.main->scene.first; scene; scene= scene->id.next) { 01517 if(scene->nodetree) { 01518 NodeTagIDChanged(scene->nodetree, &ima->id); 01519 } 01520 } 01521 } 01522 } 01523 01524 /* if layer or pass changes, we need an index for the imbufs list */ 01525 /* note it is called for rendered results, but it doesnt use the index! */ 01526 /* and because rendered results use fake layer/passes, don't correct for wrong indices here */ 01527 RenderPass *BKE_image_multilayer_index(RenderResult *rr, ImageUser *iuser) 01528 { 01529 RenderLayer *rl; 01530 RenderPass *rpass= NULL; 01531 01532 if(rr==NULL) 01533 return NULL; 01534 01535 if(iuser) { 01536 short index= 0, rl_index= 0, rp_index; 01537 01538 for(rl= rr->layers.first; rl; rl= rl->next, rl_index++) { 01539 rp_index= 0; 01540 for(rpass= rl->passes.first; rpass; rpass= rpass->next, index++, rp_index++) 01541 if(iuser->layer==rl_index && iuser->pass==rp_index) 01542 break; 01543 if(rpass) 01544 break; 01545 } 01546 01547 if(rpass) 01548 iuser->multi_index= index; 01549 else 01550 iuser->multi_index= 0; 01551 } 01552 if(rpass==NULL) { 01553 rl= rr->layers.first; 01554 if(rl) 01555 rpass= rl->passes.first; 01556 } 01557 01558 return rpass; 01559 } 01560 01561 RenderResult *BKE_image_acquire_renderresult(Scene *scene, Image *ima) 01562 { 01563 if(ima->rr) { 01564 return ima->rr; 01565 } 01566 else if(ima->type==IMA_TYPE_R_RESULT) { 01567 if(ima->render_slot == ima->last_render_slot) 01568 return RE_AcquireResultRead(RE_GetRender(scene->id.name)); 01569 else 01570 return ima->renders[ima->render_slot]; 01571 } 01572 else 01573 return NULL; 01574 } 01575 01576 void BKE_image_release_renderresult(Scene *scene, Image *ima) 01577 { 01578 if(ima->rr); 01579 else if(ima->type==IMA_TYPE_R_RESULT) { 01580 if(ima->render_slot == ima->last_render_slot) 01581 RE_ReleaseResult(RE_GetRender(scene->id.name)); 01582 } 01583 } 01584 01585 void BKE_image_backup_render(Scene *scene, Image *ima) 01586 { 01587 /* called right before rendering, ima->renders contains render 01588 result pointers for everything but the current render */ 01589 Render *re= RE_GetRender(scene->id.name); 01590 int slot= ima->render_slot, last= ima->last_render_slot; 01591 01592 if(slot != last) { 01593 if(ima->renders[slot]) { 01594 RE_FreeRenderResult(ima->renders[slot]); 01595 ima->renders[slot]= NULL; 01596 } 01597 01598 ima->renders[last]= NULL; 01599 RE_SwapResult(re, &ima->renders[last]); 01600 } 01601 01602 ima->last_render_slot= slot; 01603 } 01604 01605 /* after imbuf load, openexr type can return with a exrhandle open */ 01606 /* in that case we have to build a render-result */ 01607 static void image_create_multilayer(Image *ima, ImBuf *ibuf, int framenr) 01608 { 01609 01610 ima->rr= RE_MultilayerConvert(ibuf->userdata, ibuf->x, ibuf->y); 01611 01612 #ifdef WITH_OPENEXR 01613 IMB_exr_close(ibuf->userdata); 01614 #endif 01615 01616 ibuf->userdata= NULL; 01617 if(ima->rr) 01618 ima->rr->framenr= framenr; 01619 } 01620 01621 /* common stuff to do with images after loading */ 01622 static void image_initialize_after_load(Image *ima, ImBuf *ibuf) 01623 { 01624 /* preview is NULL when it has never been used as an icon before */ 01625 if(G.background==0 && ima->preview==NULL) 01626 BKE_icon_changed(BKE_icon_getid(&ima->id)); 01627 01628 /* fields */ 01629 if (ima->flag & IMA_FIELDS) { 01630 if(ima->flag & IMA_STD_FIELD) de_interlace_st(ibuf); 01631 else de_interlace_ng(ibuf); 01632 } 01633 /* timer */ 01634 ima->lastused = clock() / CLOCKS_PER_SEC; 01635 01636 ima->ok= IMA_OK_LOADED; 01637 01638 } 01639 01640 static ImBuf *image_load_sequence_file(Image *ima, ImageUser *iuser, int frame) 01641 { 01642 struct ImBuf *ibuf; 01643 unsigned short numlen; 01644 char name[FILE_MAX], head[FILE_MAX], tail[FILE_MAX]; 01645 int flag; 01646 01647 /* XXX temp stuff? */ 01648 if(ima->lastframe != frame) 01649 ima->tpageflag |= IMA_TPAGE_REFRESH; 01650 01651 ima->lastframe= frame; 01652 BLI_strncpy(name, ima->name, sizeof(name)); 01653 BLI_stringdec(name, head, tail, &numlen); 01654 BLI_stringenc(name, head, tail, numlen, frame); 01655 01656 if(ima->id.lib) 01657 BLI_path_abs(name, ima->id.lib->filepath); 01658 else 01659 BLI_path_abs(name, G.main->name); 01660 01661 flag= IB_rect|IB_multilayer; 01662 if(ima->flag & IMA_DO_PREMUL) 01663 flag |= IB_premul; 01664 01665 /* read ibuf */ 01666 ibuf = IMB_loadiffname(name, flag); 01667 01668 #if 0 01669 if(ibuf) { 01670 printf(AT" loaded %s\n", name); 01671 } else { 01672 printf(AT" missed %s\n", name); 01673 } 01674 #endif 01675 01676 if (ibuf) { 01677 #ifdef WITH_OPENEXR 01678 /* handle multilayer case, don't assign ibuf. will be handled in BKE_image_get_ibuf */ 01679 if (ibuf->ftype==OPENEXR && ibuf->userdata) { 01680 image_create_multilayer(ima, ibuf, frame); 01681 ima->type= IMA_TYPE_MULTILAYER; 01682 IMB_freeImBuf(ibuf); 01683 ibuf= NULL; 01684 } 01685 else { 01686 image_initialize_after_load(ima, ibuf); 01687 image_assign_ibuf(ima, ibuf, 0, frame); 01688 } 01689 #else 01690 image_initialize_after_load(ima, ibuf); 01691 image_assign_ibuf(ima, ibuf, 0, frame); 01692 #endif 01693 } 01694 else 01695 ima->ok= 0; 01696 01697 if(iuser) 01698 iuser->ok= ima->ok; 01699 01700 return ibuf; 01701 } 01702 01703 static ImBuf *image_load_sequence_multilayer(Image *ima, ImageUser *iuser, int frame) 01704 { 01705 struct ImBuf *ibuf= NULL; 01706 01707 /* either we load from RenderResult, or we have to load a new one */ 01708 01709 /* check for new RenderResult */ 01710 if(ima->rr==NULL || frame!=ima->rr->framenr) { 01711 /* copy to survive not found multilayer image */ 01712 RenderResult *oldrr= ima->rr; 01713 01714 ima->rr= NULL; 01715 ibuf = image_load_sequence_file(ima, iuser, frame); 01716 01717 if(ibuf) { /* actually an error */ 01718 ima->type= IMA_TYPE_IMAGE; 01719 printf("error, multi is normal image\n"); 01720 } 01721 // printf("loaded new result %p\n", ima->rr); 01722 /* free result if new one found */ 01723 if(ima->rr) { 01724 // if(oldrr) printf("freed previous result %p\n", oldrr); 01725 if(oldrr) RE_FreeRenderResult(oldrr); 01726 } 01727 else { 01728 ima->rr= oldrr; 01729 } 01730 01731 } 01732 if(ima->rr) { 01733 RenderPass *rpass= BKE_image_multilayer_index(ima->rr, iuser); 01734 01735 if(rpass) { 01736 // printf("load from pass %s\n", rpass->name); 01737 /* since we free render results, we copy the rect */ 01738 ibuf= IMB_allocImBuf(ima->rr->rectx, ima->rr->recty, 32, 0); 01739 ibuf->rect_float= MEM_dupallocN(rpass->rect); 01740 ibuf->flags |= IB_rectfloat; 01741 ibuf->mall= IB_rectfloat; 01742 ibuf->channels= rpass->channels; 01743 ibuf->profile = IB_PROFILE_LINEAR_RGB; 01744 01745 image_initialize_after_load(ima, ibuf); 01746 image_assign_ibuf(ima, ibuf, iuser?iuser->multi_index:0, frame); 01747 01748 } 01749 // else printf("pass not found\n"); 01750 } 01751 else 01752 ima->ok= 0; 01753 01754 if(iuser) 01755 iuser->ok= ima->ok; 01756 01757 return ibuf; 01758 } 01759 01760 01761 static ImBuf *image_load_movie_file(Image *ima, ImageUser *iuser, int frame) 01762 { 01763 struct ImBuf *ibuf= NULL; 01764 01765 ima->lastframe= frame; 01766 01767 if(ima->anim==NULL) { 01768 char str[FILE_MAX]; 01769 01770 BLI_strncpy(str, ima->name, FILE_MAX); 01771 if(ima->id.lib) 01772 BLI_path_abs(str, ima->id.lib->filepath); 01773 else 01774 BLI_path_abs(str, G.main->name); 01775 01776 ima->anim = openanim(str, IB_rect); 01777 01778 /* let's initialize this user */ 01779 if(ima->anim && iuser && iuser->frames==0) 01780 iuser->frames= IMB_anim_get_duration(ima->anim); 01781 } 01782 01783 if(ima->anim) { 01784 int dur = IMB_anim_get_duration(ima->anim); 01785 int fra= frame-1; 01786 01787 if(fra<0) fra = 0; 01788 if(fra>(dur-1)) fra= dur-1; 01789 ibuf = IMB_anim_absolute(ima->anim, fra); 01790 01791 if(ibuf) { 01792 image_initialize_after_load(ima, ibuf); 01793 image_assign_ibuf(ima, ibuf, 0, frame); 01794 } 01795 else 01796 ima->ok= 0; 01797 } 01798 else 01799 ima->ok= 0; 01800 01801 if(iuser) 01802 iuser->ok= ima->ok; 01803 01804 return ibuf; 01805 } 01806 01807 /* warning, 'iuser' can be NULL */ 01808 static ImBuf *image_load_image_file(Image *ima, ImageUser *iuser, int cfra) 01809 { 01810 struct ImBuf *ibuf; 01811 char str[FILE_MAX]; 01812 int assign = 0, flag; 01813 01814 /* always ensure clean ima */ 01815 image_free_buffers(ima); 01816 01817 /* is there a PackedFile with this image ? */ 01818 if (ima->packedfile) { 01819 flag = IB_rect|IB_multilayer; 01820 if(ima->flag & IMA_DO_PREMUL) flag |= IB_premul; 01821 01822 ibuf = IMB_ibImageFromMemory((unsigned char*)ima->packedfile->data, ima->packedfile->size, flag); 01823 } 01824 else { 01825 flag= IB_rect|IB_multilayer|IB_metadata; 01826 if(ima->flag & IMA_DO_PREMUL) 01827 flag |= IB_premul; 01828 01829 /* get the right string */ 01830 BLI_strncpy(str, ima->name, sizeof(str)); 01831 if(ima->id.lib) 01832 BLI_path_abs(str, ima->id.lib->filepath); 01833 else 01834 BLI_path_abs(str, G.main->name); 01835 01836 /* read ibuf */ 01837 ibuf = IMB_loadiffname(str, flag); 01838 } 01839 01840 if (ibuf) { 01841 /* handle multilayer case, don't assign ibuf. will be handled in BKE_image_get_ibuf */ 01842 if (ibuf->ftype==OPENEXR && ibuf->userdata) { 01843 image_create_multilayer(ima, ibuf, cfra); 01844 ima->type= IMA_TYPE_MULTILAYER; 01845 IMB_freeImBuf(ibuf); 01846 ibuf= NULL; 01847 } 01848 else { 01849 image_initialize_after_load(ima, ibuf); 01850 assign= 1; 01851 01852 /* check if the image is a font image... */ 01853 detectBitmapFont(ibuf); 01854 01855 /* make packed file for autopack */ 01856 if ((ima->packedfile == NULL) && (G.fileflags & G_AUTOPACK)) 01857 ima->packedfile = newPackedFile(NULL, str); 01858 } 01859 } 01860 else 01861 ima->ok= 0; 01862 01863 if(assign) 01864 image_assign_ibuf(ima, ibuf, IMA_NO_INDEX, 0); 01865 01866 if(iuser) 01867 iuser->ok= ima->ok; 01868 01869 return ibuf; 01870 } 01871 01872 static ImBuf *image_get_ibuf_multilayer(Image *ima, ImageUser *iuser) 01873 { 01874 ImBuf *ibuf= NULL; 01875 01876 if(ima->rr==NULL) { 01877 ibuf = image_load_image_file(ima, iuser, 0); 01878 if(ibuf) { /* actually an error */ 01879 ima->type= IMA_TYPE_IMAGE; 01880 return ibuf; 01881 } 01882 } 01883 if(ima->rr) { 01884 RenderPass *rpass= BKE_image_multilayer_index(ima->rr, iuser); 01885 01886 if(rpass) { 01887 ibuf= IMB_allocImBuf(ima->rr->rectx, ima->rr->recty, 32, 0); 01888 01889 image_initialize_after_load(ima, ibuf); 01890 01891 ibuf->rect_float= rpass->rect; 01892 ibuf->flags |= IB_rectfloat; 01893 ibuf->channels= rpass->channels; 01894 ibuf->profile = IB_PROFILE_LINEAR_RGB; 01895 01896 image_assign_ibuf(ima, ibuf, iuser?iuser->multi_index:IMA_NO_INDEX, 0); 01897 } 01898 } 01899 01900 if(ibuf==NULL) 01901 ima->ok= 0; 01902 if(iuser) 01903 iuser->ok= ima->ok; 01904 01905 return ibuf; 01906 } 01907 01908 01909 /* showing RGBA result itself (from compo/sequence) or 01910 like exr, using layers etc */ 01911 /* always returns a single ibuf, also during render progress */ 01912 static ImBuf *image_get_render_result(Image *ima, ImageUser *iuser, void **lock_r) 01913 { 01914 Render *re; 01915 RenderResult rres; 01916 float *rectf, *rectz; 01917 unsigned int *rect; 01918 float dither; 01919 int channels, layer, pass; 01920 ImBuf *ibuf; 01921 int from_render= (ima->render_slot == ima->last_render_slot); 01922 01923 if(!(iuser && iuser->scene)) 01924 return NULL; 01925 01926 /* if we the caller is not going to release the lock, don't give the image */ 01927 if(!lock_r) 01928 return NULL; 01929 01930 re= RE_GetRender(iuser->scene->id.name); 01931 01932 channels= 4; 01933 layer= (iuser)? iuser->layer: 0; 01934 pass= (iuser)? iuser->pass: 0; 01935 01936 if(from_render) { 01937 RE_AcquireResultImage(re, &rres); 01938 } 01939 else if(ima->renders[ima->render_slot]) { 01940 rres= *(ima->renders[ima->render_slot]); 01941 rres.have_combined= rres.rectf != NULL; 01942 } 01943 else 01944 memset(&rres, 0, sizeof(RenderResult)); 01945 01946 if(!(rres.rectx > 0 && rres.recty > 0)) { 01947 if(from_render) 01948 RE_ReleaseResultImage(re); 01949 return NULL; 01950 } 01951 01952 /* release is done in BKE_image_release_ibuf using lock_r */ 01953 if(from_render) { 01954 BLI_lock_thread(LOCK_VIEWER); 01955 *lock_r= re; 01956 } 01957 01958 /* this gives active layer, composite or seqence result */ 01959 rect= (unsigned int *)rres.rect32; 01960 rectf= rres.rectf; 01961 rectz= rres.rectz; 01962 dither= iuser->scene->r.dither_intensity; 01963 01964 /* combined layer gets added as first layer */ 01965 if(rres.have_combined && layer==0); 01966 else if(rres.layers.first) { 01967 RenderLayer *rl= BLI_findlink(&rres.layers, layer-(rres.have_combined?1:0)); 01968 if(rl) { 01969 RenderPass *rpass; 01970 01971 /* there's no combined pass, is in renderlayer itself */ 01972 if(pass==0) { 01973 rectf= rl->rectf; 01974 } 01975 else { 01976 rpass= BLI_findlink(&rl->passes, pass-1); 01977 if(rpass) { 01978 channels= rpass->channels; 01979 rectf= rpass->rect; 01980 dither= 0.0f; /* don't dither passes */ 01981 } 01982 } 01983 01984 for(rpass= rl->passes.first; rpass; rpass= rpass->next) 01985 if(rpass->passtype == SCE_PASS_Z) 01986 rectz= rpass->rect; 01987 } 01988 } 01989 01990 ibuf= image_get_ibuf(ima, IMA_NO_INDEX, 0); 01991 01992 /* make ibuf if needed, and initialize it */ 01993 if(ibuf==NULL) { 01994 ibuf= IMB_allocImBuf(rres.rectx, rres.recty, 32, 0); 01995 image_assign_ibuf(ima, ibuf, IMA_NO_INDEX, 0); 01996 } 01997 01998 ibuf->x= rres.rectx; 01999 ibuf->y= rres.recty; 02000 02001 if(ibuf->rect_float!=rectf || rect) /* ensure correct redraw */ 02002 imb_freerectImBuf(ibuf); 02003 02004 if(rect) 02005 ibuf->rect= rect; 02006 02007 if(rectf) { 02008 ibuf->rect_float= rectf; 02009 ibuf->flags |= IB_rectfloat; 02010 ibuf->channels= channels; 02011 } 02012 else { 02013 ibuf->rect_float= NULL; 02014 ibuf->flags &= ~IB_rectfloat; 02015 } 02016 02017 if(rectz) { 02018 ibuf->zbuf_float= rectz; 02019 ibuf->flags |= IB_zbuffloat; 02020 } 02021 else { 02022 ibuf->zbuf_float= NULL; 02023 ibuf->flags &= ~IB_zbuffloat; 02024 } 02025 02026 /* since its possible to access the buffer from the image directly, set the profile [#25073] */ 02027 ibuf->profile= (iuser->scene->r.color_mgt_flag & R_COLOR_MANAGEMENT) ? IB_PROFILE_LINEAR_RGB : IB_PROFILE_NONE; 02028 02029 ibuf->dither= dither; 02030 02031 ima->ok= IMA_OK_LOADED; 02032 02033 return ibuf; 02034 } 02035 02036 static ImBuf *image_get_ibuf_threadsafe(Image *ima, ImageUser *iuser, int *frame_r, int *index_r) 02037 { 02038 ImBuf *ibuf = NULL; 02039 int frame = 0, index = 0; 02040 02041 /* see if we already have an appropriate ibuf, with image source and type */ 02042 if(ima->source==IMA_SRC_MOVIE) { 02043 frame= iuser?iuser->framenr:ima->lastframe; 02044 ibuf= image_get_ibuf(ima, 0, frame); 02045 /* XXX temp stuff? */ 02046 if(ima->lastframe != frame) 02047 ima->tpageflag |= IMA_TPAGE_REFRESH; 02048 ima->lastframe = frame; 02049 } 02050 else if(ima->source==IMA_SRC_SEQUENCE) { 02051 if(ima->type==IMA_TYPE_IMAGE) { 02052 frame= iuser?iuser->framenr:ima->lastframe; 02053 ibuf= image_get_ibuf(ima, 0, frame); 02054 02055 /* XXX temp stuff? */ 02056 if(ima->lastframe != frame) { 02057 ima->tpageflag |= IMA_TPAGE_REFRESH; 02058 } 02059 ima->lastframe = frame; 02060 } 02061 else if(ima->type==IMA_TYPE_MULTILAYER) { 02062 frame= iuser?iuser->framenr:ima->lastframe; 02063 index= iuser?iuser->multi_index:IMA_NO_INDEX; 02064 ibuf= image_get_ibuf(ima, index, frame); 02065 } 02066 } 02067 else if(ima->source==IMA_SRC_FILE) { 02068 if(ima->type==IMA_TYPE_IMAGE) 02069 ibuf= image_get_ibuf(ima, IMA_NO_INDEX, 0); 02070 else if(ima->type==IMA_TYPE_MULTILAYER) 02071 ibuf= image_get_ibuf(ima, iuser?iuser->multi_index:IMA_NO_INDEX, 0); 02072 } 02073 else if(ima->source == IMA_SRC_GENERATED) { 02074 ibuf= image_get_ibuf(ima, IMA_NO_INDEX, 0); 02075 } 02076 else if(ima->source == IMA_SRC_VIEWER) { 02077 /* always verify entirely, not that this shouldn't happen 02078 * as part of texture sampling in rendering anyway, so not 02079 * a big bottleneck */ 02080 } 02081 02082 *frame_r = frame; 02083 *index_r = index; 02084 02085 return ibuf; 02086 } 02087 02088 /* Checks optional ImageUser and verifies/creates ImBuf. */ 02089 /* use this one if you want to get a render result in progress, 02090 * if not, use BKE_image_get_ibuf which doesn't require a release */ 02091 ImBuf *BKE_image_acquire_ibuf(Image *ima, ImageUser *iuser, void **lock_r) 02092 { 02093 ImBuf *ibuf= NULL; 02094 float color[] = {0, 0, 0, 1}; 02095 int frame= 0, index= 0; 02096 02097 /* This function is intended to be thread-safe. It postpones the mutex lock 02098 * until it needs to load the image, if the image is already there it 02099 * should just get the pointer and return. The reason is that a lot of mutex 02100 * locks appears to be very slow on certain multicore macs, causing a render 02101 * with image textures to actually slow down as more threads are used. 02102 * 02103 * Note that all the image loading functions should also make sure they do 02104 * things in a threadsafe way for image_get_ibuf_threadsafe to work correct. 02105 * That means, the last two steps must be, 1) add the ibuf to the list and 02106 * 2) set ima/iuser->ok to 0 to IMA_OK_LOADED */ 02107 02108 if(lock_r) 02109 *lock_r= NULL; 02110 02111 /* quick reject tests */ 02112 if(ima==NULL) 02113 return NULL; 02114 if(iuser) { 02115 if(iuser->ok==0) 02116 return NULL; 02117 } 02118 else if(ima->ok==0) 02119 return NULL; 02120 02121 /* try to get the ibuf without locking */ 02122 ibuf= image_get_ibuf_threadsafe(ima, iuser, &frame, &index); 02123 02124 if(ibuf == NULL) { 02125 /* couldn't get ibuf and image is not ok, so let's lock and try to 02126 * load the image */ 02127 BLI_lock_thread(LOCK_IMAGE); 02128 02129 /* need to check ok flag and loading ibuf again, because the situation 02130 * might have changed in the meantime */ 02131 if(iuser) { 02132 if(iuser->ok==0) { 02133 BLI_unlock_thread(LOCK_IMAGE); 02134 return NULL; 02135 } 02136 } 02137 else if(ima->ok==0) { 02138 BLI_unlock_thread(LOCK_IMAGE); 02139 return NULL; 02140 } 02141 02142 ibuf= image_get_ibuf_threadsafe(ima, iuser, &frame, &index); 02143 02144 if(ibuf == NULL) { 02145 /* we are sure we have to load the ibuf, using source and type */ 02146 if(ima->source==IMA_SRC_MOVIE) { 02147 /* source is from single file, use flipbook to store ibuf */ 02148 ibuf= image_load_movie_file(ima, iuser, frame); 02149 } 02150 else if(ima->source==IMA_SRC_SEQUENCE) { 02151 if(ima->type==IMA_TYPE_IMAGE) { 02152 /* regular files, ibufs in flipbook, allows saving */ 02153 ibuf= image_load_sequence_file(ima, iuser, frame); 02154 } 02155 /* no else; on load the ima type can change */ 02156 if(ima->type==IMA_TYPE_MULTILAYER) { 02157 /* only 1 layer/pass stored in imbufs, no exrhandle anim storage, no saving */ 02158 ibuf= image_load_sequence_multilayer(ima, iuser, frame); 02159 } 02160 } 02161 else if(ima->source==IMA_SRC_FILE) { 02162 02163 if(ima->type==IMA_TYPE_IMAGE) 02164 ibuf= image_load_image_file(ima, iuser, frame); /* cfra only for '#', this global is OK */ 02165 /* no else; on load the ima type can change */ 02166 if(ima->type==IMA_TYPE_MULTILAYER) 02167 /* keeps render result, stores ibufs in listbase, allows saving */ 02168 ibuf= image_get_ibuf_multilayer(ima, iuser); 02169 02170 } 02171 else if(ima->source == IMA_SRC_GENERATED) { 02172 /* generated is: ibuf is allocated dynamically */ 02173 /* UV testgrid or black or solid etc */ 02174 if(ima->gen_x==0) ima->gen_x= 1024; 02175 if(ima->gen_y==0) ima->gen_y= 1024; 02176 ibuf= add_ibuf_size(ima->gen_x, ima->gen_y, ima->name, 24, (ima->gen_flag & IMA_GEN_FLOAT) != 0, ima->gen_type, color); 02177 image_assign_ibuf(ima, ibuf, IMA_NO_INDEX, 0); 02178 ima->ok= IMA_OK_LOADED; 02179 } 02180 else if(ima->source == IMA_SRC_VIEWER) { 02181 if(ima->type==IMA_TYPE_R_RESULT) { 02182 /* always verify entirely, and potentially 02183 returns pointer to release later */ 02184 ibuf= image_get_render_result(ima, iuser, lock_r); 02185 } 02186 else if(ima->type==IMA_TYPE_COMPOSITE) { 02187 /* requires lock/unlock, otherwise don't return image */ 02188 if(lock_r) { 02189 /* unlock in BKE_image_release_ibuf */ 02190 BLI_lock_thread(LOCK_VIEWER); 02191 *lock_r= ima; 02192 02193 /* XXX anim play for viewer nodes not yet supported */ 02194 frame= 0; // XXX iuser?iuser->framenr:0; 02195 ibuf= image_get_ibuf(ima, 0, frame); 02196 02197 if(!ibuf) { 02198 /* Composite Viewer, all handled in compositor */ 02199 /* fake ibuf, will be filled in compositor */ 02200 ibuf= IMB_allocImBuf(256, 256, 32, IB_rect); 02201 image_assign_ibuf(ima, ibuf, 0, frame); 02202 } 02203 } 02204 } 02205 } 02206 } 02207 02208 BLI_unlock_thread(LOCK_IMAGE); 02209 } 02210 02211 tag_image_time(ima); 02212 02213 return ibuf; 02214 } 02215 02216 void BKE_image_release_ibuf(Image *ima, void *lock) 02217 { 02218 /* for getting image during threaded render / compositing, need to release */ 02219 if(lock == ima) { 02220 BLI_unlock_thread(LOCK_VIEWER); /* viewer image */ 02221 } 02222 else if(lock) { 02223 RE_ReleaseResultImage(lock); /* render result */ 02224 BLI_unlock_thread(LOCK_VIEWER); /* view image imbuf */ 02225 } 02226 } 02227 02228 /* warning, this can allocate generated images */ 02229 ImBuf *BKE_image_get_ibuf(Image *ima, ImageUser *iuser) 02230 { 02231 /* here (+fie_ima/2-1) makes sure that division happens correctly */ 02232 return BKE_image_acquire_ibuf(ima, iuser, NULL); 02233 } 02234 02235 int BKE_image_user_get_frame(const ImageUser *iuser, int cfra, int fieldnr) 02236 { 02237 const int len= (iuser->fie_ima*iuser->frames)/2; 02238 02239 if(len==0) { 02240 return 0; 02241 } 02242 else { 02243 int framenr; 02244 cfra= cfra - iuser->sfra+1; 02245 02246 /* cyclic */ 02247 if(iuser->cycl) { 02248 cfra= ( (cfra) % len ); 02249 if(cfra < 0) cfra+= len; 02250 if(cfra==0) cfra= len; 02251 } 02252 02253 if(cfra<0) cfra= 0; 02254 else if(cfra>len) cfra= len; 02255 02256 /* convert current frame to current field */ 02257 cfra= 2*(cfra); 02258 if(fieldnr) cfra++; 02259 02260 /* transform to images space */ 02261 framenr= (cfra+iuser->fie_ima-2)/iuser->fie_ima; 02262 if(framenr>iuser->frames) framenr= iuser->frames; 02263 framenr+= iuser->offset; 02264 02265 if(iuser->cycl) { 02266 framenr= ( (framenr) % len ); 02267 while(framenr < 0) framenr+= len; 02268 if(framenr==0) framenr= len; 02269 } 02270 02271 return framenr; 02272 } 02273 } 02274 02275 void BKE_image_user_calc_frame(ImageUser *iuser, int cfra, int fieldnr) 02276 { 02277 const int framenr= BKE_image_user_get_frame(iuser, cfra, fieldnr); 02278 02279 /* allows image users to handle redraws */ 02280 if(iuser->flag & IMA_ANIM_ALWAYS) 02281 if(framenr!=iuser->framenr) 02282 iuser->flag |= IMA_ANIM_REFRESHED; 02283 02284 iuser->framenr= framenr; 02285 if(iuser->ok==0) iuser->ok= 1; 02286 }