replace ImBuf.depth with ImBuf.planes to match ImageFormatData.planes & to avoid...
[blender.git] / source / blender / blenkernel / intern / image.c
index c48497c45a12c82bda4bb9ab669a93ac1031e168..c99396e6939fd54146e6d0561955e78c67759a7d 100644 (file)
@@ -1,7 +1,4 @@
-/*  image.c
- * 
- * $Id$
- *
+/*
  * ***** BEGIN GPL LICENSE BLOCK *****
  *
  * This program is free software; you can redistribute it and/or
 #include "DNA_camera_types.h"
 #include "DNA_sequence_types.h"
 #include "DNA_userdef_types.h"
+#include "DNA_brush_types.h"
+#include "DNA_mesh_types.h"
+#include "DNA_meshdata_types.h"
 
 #include "BLI_blenlib.h"
 #include "BLI_threads.h"
 #include "BLI_utildefines.h"
+#include "BLI_bpath.h"
 
 #include "BKE_bmfont.h"
 #include "BKE_global.h"
@@ -316,6 +317,133 @@ Image *copy_image(Image *ima)
        return nima;
 }
 
+static void extern_local_image(Image *UNUSED(ima))
+{
+       /* Nothing to do: images don't link to other IDs. This function exists to
+          match id_make_local pattern. */
+}
+
+void make_local_image(struct Image *ima)
+{
+       Main *bmain= G.main;
+       Tex *tex;
+       Brush *brush;
+       Mesh *me;
+       int is_local= FALSE, is_lib= FALSE;
+
+       /* - only lib users: do nothing
+        * - only local users: set flag
+        * - mixed: make copy
+        */
+
+       if(ima->id.lib==NULL) return;
+
+       /* Can't take short cut here: must check meshes at least because of bogus
+          texface ID refs. - z0r */
+#if 0
+       if(ima->id.us==1) {
+               id_clear_lib_data(bmain, &ima->id);
+               extern_local_image(ima);
+               return;
+       }
+#endif
+
+       for(tex= bmain->tex.first; tex; tex= tex->id.next) {
+               if(tex->ima == ima) {
+                       if(tex->id.lib) is_lib= TRUE;
+                       else is_local= TRUE;
+               }
+       }
+       for(brush= bmain->brush.first; brush; brush= brush->id.next) {
+               if(brush->clone.image == ima) {
+                       if(brush->id.lib) is_lib= TRUE;
+                       else is_local= TRUE;
+               }
+       }
+       for(me= bmain->mesh.first; me; me= me->id.next) {
+               if(me->mtface) {
+                       MTFace *tface;
+                       int a, i;
+
+                       for(i=0; i<me->fdata.totlayer; i++) {
+                               if(me->fdata.layers[i].type == CD_MTFACE) {
+                                       tface= (MTFace*)me->fdata.layers[i].data;
+
+                                       for(a=0; a<me->totface; a++, tface++) {
+                                               if(tface->tpage == ima) {
+                                                       if(me->id.lib) is_lib= TRUE;
+                                                       else is_local= TRUE;
+                                               }
+                                       }
+                               }
+                       }
+               }
+       }
+
+       if(is_local && is_lib == FALSE) {
+               id_clear_lib_data(bmain, &ima->id);
+               extern_local_image(ima);
+       }
+       else if(is_local && is_lib) {
+               Image *iman= copy_image(ima);
+
+               iman->id.us= 0;
+
+               /* Remap paths of new ID using old library as base. */
+               BKE_id_lib_local_paths(bmain, &iman->id);
+
+               tex= bmain->tex.first;
+               while(tex) {
+                       if(tex->id.lib==NULL) {
+                               if(tex->ima==ima) {
+                                       tex->ima = iman;
+                                       iman->id.us++;
+                                       ima->id.us--;
+                               }
+                       }
+                       tex= tex->id.next;
+               }
+               brush= bmain->brush.first;
+               while(brush) {
+                       if(brush->id.lib==NULL) {
+                               if(brush->clone.image==ima) {
+                                       brush->clone.image = iman;
+                                       iman->id.us++;
+                                       ima->id.us--;
+                               }
+                       }
+                       brush= brush->id.next;
+               }
+               /* Transfer references in texfaces. Texfaces don't add to image ID
+                  user count *unless* there are no other users. See
+                  readfile.c:lib_link_mtface. */
+               me= bmain->mesh.first;
+               while(me) {
+                       if(me->mtface) {
+                               MTFace *tface;
+                               int a, i;
+
+                               for(i=0; i<me->fdata.totlayer; i++) {
+                                       if(me->fdata.layers[i].type == CD_MTFACE) {
+                                               tface= (MTFace*)me->fdata.layers[i].data;
+
+                                               for(a=0; a<me->totface; a++, tface++) { 
+                                                       if(tface->tpage == ima) {
+                                                               tface->tpage = iman;
+                                                               if(iman->id.us == 0) {
+                                                                       tface->tpage->id.us= 1;
+                                                               }
+                                                               id_lib_extern((ID*)iman);
+                                                       }
+                                               }
+                                       }
+                               }
+                       }
+                       me= me->id.next;
+               }
+       }
+}
+
 void BKE_image_merge(Image *dest, Image *source)
 {
        ImBuf *ibuf;
@@ -432,6 +560,7 @@ Image *BKE_add_image_size(unsigned int width, unsigned int height, const char *n
                ima->gen_x= width;
                ima->gen_y= height;
                ima->gen_type= uvtestgrid;
+               ima->gen_flag |= (floatbuf ? IMA_GEN_FLOAT : 0);
                
                ibuf= add_ibuf_size(width, height, name, depth, floatbuf, uvtestgrid, color);
                image_assign_ibuf(ima, ibuf, IMA_NO_INDEX, 0);
@@ -472,7 +601,7 @@ void BKE_image_memorypack(Image *ima)
        }
        
        ibuf->ftype= PNG;
-       ibuf->depth= 32;
+       ibuf->planes= R_IMF_PLANES_RGBA;
        
        IMB_saveiff(ibuf, ibuf->name, IB_rect | IB_mem);
        if(ibuf->encodedbuffer==NULL) {
@@ -591,7 +720,7 @@ void BKE_image_print_memlist(void)
        for(ima= G.main->image.first; ima; ima= ima->id.next)
                totsize += image_mem_size(ima);
 
-       printf("\ntotal image memory len: %.3lf MB\n", (double)totsize/(double)(1024*1024));
+       printf("\ntotal image memory len: %.3f MB\n", (double)totsize/(double)(1024*1024));
 
        for(ima= G.main->image.first; ima; ima= ima->id.next) {
                size= image_mem_size(ima);
@@ -771,6 +900,76 @@ int BKE_imtype_is_movie(int imtype)
        return 0;
 }
 
+int BKE_imtype_is_alpha_ok(int imtype)
+{
+       switch(imtype) {
+       case R_TARGA:
+       case R_IRIS:
+       case R_PNG:
+       case R_BMP:
+       case R_RADHDR:
+       case R_TIFF:
+       case R_OPENEXR:
+       case R_MULTILAYER:
+       case R_DDS:
+       case R_JP2:
+                       return 1;
+       }
+       return 0;
+}
+
+int BKE_imtype_is_zbuf_ok(int imtype)
+{
+       switch(imtype) {
+       case R_IRIZ:
+       case R_OPENEXR: /* but not R_MULTILAYER */
+                       return 1;
+       }
+       return 0;
+}
+
+int BKE_imtype_is_compression_ok(int imtype)
+{
+       switch(imtype) {
+       case R_PNG:
+                       return 1;
+       }
+       return 0;
+}
+
+int BKE_imtype_is_quality_ok(int imtype)
+{
+       switch(imtype) {
+       case R_JPEG90:
+       case R_JP2:
+                       return 1;
+       }
+       return 0;
+}
+
+int BKE_imtype_is_depth_ok(int imtype)
+{
+       switch (imtype) {
+       case R_RADHDR:
+               return R_IMF_CHAN_DEPTH_32;
+       case R_TIFF:
+               return R_IMF_CHAN_DEPTH_8 | R_IMF_CHAN_DEPTH_16;
+       case R_OPENEXR:
+               return R_IMF_CHAN_DEPTH_16 | R_IMF_CHAN_DEPTH_32;
+       case R_MULTILAYER:
+               return R_IMF_CHAN_DEPTH_32;
+       /* eeh, cineone does some strange 10bits per channel */
+       case R_DPX:
+       case R_CINEON:
+               return R_IMF_CHAN_DEPTH_12;
+       case R_JP2:
+               return R_IMF_CHAN_DEPTH_8 | R_IMF_CHAN_DEPTH_12 | R_IMF_CHAN_DEPTH_16;
+       /* most formats are 8bit only */
+       default:
+               return R_IMF_CHAN_DEPTH_8;
+       }
+}
+
 int BKE_add_image_extension(char *string, int imtype)
 {
        const char *extension= NULL;
@@ -851,7 +1050,7 @@ int BKE_add_image_extension(char *string, int imtype)
                                  || (G.have_quicktime && BLI_testextensie_array(string, imb_ext_image_qt))) {
                        return BLI_replace_extension(string, FILE_MAX, extension);
                } else {
-                       strcat(string, extension);
+                       return BLI_ensure_extension(string, FILE_MAX, extension);
                        return TRUE;
                }
                
@@ -906,8 +1105,8 @@ static void stampdata(Scene *scene, Object *camera, StampData *stamp_data, int d
        
        if (scene->r.stamp & R_STAMP_MARKER) {
                char *name = scene_find_last_marker_name(scene, CFRA);
-       
-               if (name)       strcpy(text, name);
+
+               if (name)       BLI_strncpy(text, name, sizeof(text));
                else            strcpy(text, "<none>");
 
                BLI_snprintf(stamp_data->marker, sizeof(stamp_data->marker), do_prefix ? "Marker %s":"%s", text);
@@ -980,7 +1179,7 @@ static void stampdata(Scene *scene, Object *camera, StampData *stamp_data, int d
        if (scene->r.stamp & R_STAMP_SEQSTRIP) {
                Sequence *seq= seq_foreground_frame_get(scene, scene->r.cfra);
        
-               if (seq) strcpy(text, seq->name+2);
+               if (seq)        BLI_strncpy(text, seq->name+2, sizeof(text));
                else            strcpy(text, "<none>");
 
                BLI_snprintf(stamp_data->strip, sizeof(stamp_data->strip), do_prefix ? "Strip %s":"%s", text);
@@ -1242,10 +1441,13 @@ int BKE_alphatest_ibuf(ImBuf *ibuf)
        return FALSE;
 }
 
-int BKE_write_ibuf(ImBuf *ibuf, const char *name, int imtype, int subimtype, int quality)
+int BKE_write_ibuf(ImBuf *ibuf, const char *name, ImageFormatData *imf)
 {
+       char imtype= imf->imtype;
+       char compress= imf->compress;
+       char quality= imf->quality;
+
        int ok;
-       (void)subimtype; /* quies unused warnings */
 
        if(imtype == -1) {
                /* use whatever existing image type is set by 'ibuf' */
@@ -1262,7 +1464,7 @@ int BKE_write_ibuf(ImBuf *ibuf, const char *name, int imtype, int subimtype, int
                ibuf->ftype= PNG;
 
                if(imtype==R_PNG)
-                       ibuf->ftype |= quality;  /* quality is actually compression 0-100 --> 0-9 */
+                       ibuf->ftype |= compress;
 
        }
 #ifdef WITH_DDS
@@ -1277,18 +1479,18 @@ int BKE_write_ibuf(ImBuf *ibuf, const char *name, int imtype, int subimtype, int
        else if (imtype==R_TIFF) {
                ibuf->ftype= TIF;
 
-               if(subimtype & R_TIFF_16BIT)
+               if(imf->depth == R_IMF_CHAN_DEPTH_16)
                        ibuf->ftype |= TIF_16BIT;
        }
 #endif
 #ifdef WITH_OPENEXR
        else if (imtype==R_OPENEXR || imtype==R_MULTILAYER) {
                ibuf->ftype= OPENEXR;
-               if(subimtype & R_OPENEXR_HALF)
+               if(imf->depth == R_IMF_CHAN_DEPTH_16)
                        ibuf->ftype |= OPENEXR_HALF;
                ibuf->ftype |= (quality & OPENEXR_COMPRESS);
                
-               if(!(subimtype & R_OPENEXR_ZBUF))
+               if(!(imf->flag & R_IMF_FLAG_ZBUF))
                        ibuf->zbuf_float = NULL;        /* signal for exr saving */
                
        }
@@ -1312,19 +1514,19 @@ int BKE_write_ibuf(ImBuf *ibuf, const char *name, int imtype, int subimtype, int
                if(quality < 10) quality= 90;
                ibuf->ftype= JP2|quality;
                
-               if (subimtype & R_JPEG2K_16BIT) {
+               if (imf->depth == R_IMF_CHAN_DEPTH_16) {
                        ibuf->ftype |= JP2_16BIT;
-               } else if (subimtype & R_JPEG2K_12BIT) {
+               } else if (imf->depth == R_IMF_CHAN_DEPTH_12) {
                        ibuf->ftype |= JP2_12BIT;
                }
                
-               if (subimtype & R_JPEG2K_YCC) {
+               if (imf->jp2_flag & R_IMF_JP2_FLAG_YCC) {
                        ibuf->ftype |= JP2_YCC;
                }
-               
-               if (subimtype & R_JPEG2K_CINE_PRESET) {
+
+               if (imf->jp2_flag & R_IMF_JP2_FLAG_CINE_PRESET) {
                        ibuf->ftype |= JP2_CINE;
-                       if (subimtype & R_JPEG2K_CINE_48FPS)
+                       if (imf->jp2_flag & R_IMF_JP2_FLAG_CINE_48)
                                ibuf->ftype |= JP2_CINE_48FPS;
                }
        }
@@ -1333,7 +1535,7 @@ int BKE_write_ibuf(ImBuf *ibuf, const char *name, int imtype, int subimtype, int
                /* R_JPEG90, etc. default we save jpegs */
                if(quality < 10) quality= 90;
                ibuf->ftype= JPG|quality;
-               if(ibuf->depth==32) ibuf->depth= 24;    /* unsupported feature only confuses other s/w */
+               if(ibuf->planes==32) ibuf->planes= 24;  /* unsupported feature only confuses other s/w */
        }
        
        BLI_make_existing_file(name);
@@ -1346,20 +1548,20 @@ int BKE_write_ibuf(ImBuf *ibuf, const char *name, int imtype, int subimtype, int
        return(ok);
 }
 
-int BKE_write_ibuf_stamp(Scene *scene, struct Object *camera, ImBuf *ibuf, const char *name, int imtype, int subimtype, int quality)
+int BKE_write_ibuf_stamp(Scene *scene, struct Object *camera, ImBuf *ibuf, const char *name, struct ImageFormatData *imf)
 {
        if(scene && scene->r.stamp & R_STAMP_ALL)
                BKE_stamp_info(scene, camera, ibuf);
 
-       return BKE_write_ibuf(ibuf, name, imtype, subimtype, quality);
+       return BKE_write_ibuf(ibuf, name, imf);
 }
 
 
-void BKE_makepicstring(char *string, const char *base, int frame, int imtype, const short use_ext, const short use_frames)
+void BKE_makepicstring(char *string, const char *base, const char *relbase, int frame, int imtype, const short use_ext, const short use_frames)
 {
        if (string==NULL) return;
        BLI_strncpy(string, base, FILE_MAX - 10);       /* weak assumption */
-       BLI_path_abs(string, G.main->name);
+       BLI_path_abs(string, relbase);
 
        if(use_frames)
                BLI_path_frame(string, frame, 4);
@@ -1370,15 +1572,15 @@ void BKE_makepicstring(char *string, const char *base, int frame, int imtype, co
 }
 
 /* used by sequencer too */
-struct anim *openanim(char *name, int flags)
+struct anim *openanim(const char *name, int flags, int streamindex)
 {
        struct anim *anim;
        struct ImBuf *ibuf;
        
-       anim = IMB_open_anim(name, flags);
+       anim = IMB_open_anim(name, flags, streamindex);
        if (anim == NULL) return NULL;
 
-       ibuf = IMB_anim_absolute(anim, 0);
+       ibuf = IMB_anim_absolute(anim, 0, IMB_TC_NONE, IMB_PROXY_NONE);
        if (ibuf == NULL) {
                if(BLI_exists(name))
                        printf("not an anim: %s\n", name);
@@ -1479,7 +1681,7 @@ void BKE_image_signal(Image *ima, ImageUser *iuser, int signal)
                /* try to repack file */
                if(ima->packedfile) {
                        PackedFile *pf;
-                       pf = newPackedFile(NULL, ima->name);
+                       pf = newPackedFile(NULL, ima->name, ID_BLEND_PATH(G.main, &ima->id));
                        if (pf) {
                                freePackedFile(ima->packedfile);
                                ima->packedfile = pf;
@@ -1514,7 +1716,7 @@ void BKE_image_signal(Image *ima, ImageUser *iuser, int signal)
                Scene *scene;
                for(scene= G.main->scene.first; scene; scene= scene->id.next) {
                        if(scene->nodetree) {
-                               NodeTagIDChanged(scene->nodetree, &ima->id);
+                               nodeUpdateID(scene->nodetree, &ima->id);
                        }
                }
        }
@@ -1652,10 +1854,7 @@ static ImBuf *image_load_sequence_file(Image *ima, ImageUser *iuser, int frame)
        BLI_stringdec(name, head, tail, &numlen);
        BLI_stringenc(name, head, tail, numlen, frame);
 
-       if(ima->id.lib)
-               BLI_path_abs(name, ima->id.lib->filepath);
-       else
-               BLI_path_abs(name, G.main->name);
+       BLI_path_abs(name, ID_BLEND_PATH(G.main, &ima->id));
        
        flag= IB_rect|IB_multilayer;
        if(ima->flag & IMA_DO_PREMUL)
@@ -1767,25 +1966,28 @@ static ImBuf *image_load_movie_file(Image *ima, ImageUser *iuser, int frame)
                char str[FILE_MAX];
                
                BLI_strncpy(str, ima->name, FILE_MAX);
-               if(ima->id.lib)
-                       BLI_path_abs(str, ima->id.lib->filepath);
-               else
-                       BLI_path_abs(str, G.main->name);
-               
-               ima->anim = openanim(str, IB_rect);
+               BLI_path_abs(str, ID_BLEND_PATH(G.main, &ima->id));
+
+               /* FIXME: make several stream accessible in image editor, too*/
+               ima->anim = openanim(str, IB_rect, 0);
                
                /* let's initialize this user */
                if(ima->anim && iuser && iuser->frames==0)
-                       iuser->frames= IMB_anim_get_duration(ima->anim);
+                       iuser->frames= IMB_anim_get_duration(ima->anim,
+                                                            IMB_TC_RECORD_RUN);
        }
        
        if(ima->anim) {
-               int dur = IMB_anim_get_duration(ima->anim);
+               int dur = IMB_anim_get_duration(ima->anim,
+                                               IMB_TC_RECORD_RUN);
                int fra= frame-1;
                
                if(fra<0) fra = 0;
                if(fra>(dur-1)) fra= dur-1;
-               ibuf = IMB_anim_absolute(ima->anim, fra);
+               ibuf = IMB_makeSingleUser(
+                       IMB_anim_absolute(ima->anim, fra,
+                                         IMB_TC_RECORD_RUN,
+                                         IMB_PROXY_NONE));
                
                if(ibuf) {
                        image_initialize_after_load(ima, ibuf);
@@ -1818,7 +2020,7 @@ static ImBuf *image_load_image_file(Image *ima, ImageUser *iuser, int cfra)
                flag = IB_rect|IB_multilayer;
                if(ima->flag & IMA_DO_PREMUL) flag |= IB_premul;
                
-               ibuf = IMB_ibImageFromMemory((unsigned char*)ima->packedfile->data, ima->packedfile->size, flag);
+               ibuf = IMB_ibImageFromMemory((unsigned char*)ima->packedfile->data, ima->packedfile->size, flag, "<packed data>");
        } 
        else {
                flag= IB_rect|IB_multilayer|IB_metadata;
@@ -1827,10 +2029,7 @@ static ImBuf *image_load_image_file(Image *ima, ImageUser *iuser, int cfra)
                        
                /* get the right string */
                BLI_strncpy(str, ima->name, sizeof(str));
-               if(ima->id.lib)
-                       BLI_path_abs(str, ima->id.lib->filepath);
-               else
-                       BLI_path_abs(str, G.main->name);
+               BLI_path_abs(str, ID_BLEND_PATH(G.main, &ima->id));
                
                /* read ibuf */
                ibuf = IMB_loadiffname(str, flag);
@@ -1853,7 +2052,7 @@ static ImBuf *image_load_image_file(Image *ima, ImageUser *iuser, int cfra)
                        
                        /* make packed file for autopack */
                        if ((ima->packedfile == NULL) && (G.fileflags & G_AUTOPACK))
-                               ima->packedfile = newPackedFile(NULL, str);
+                               ima->packedfile = newPackedFile(NULL, str, ID_BLEND_PATH(G.main, &ima->id));
                }
        }
        else
@@ -2172,7 +2371,7 @@ ImBuf *BKE_image_acquire_ibuf(Image *ima, ImageUser *iuser, void **lock_r)
                                /* UV testgrid or black or solid etc */
                                if(ima->gen_x==0) ima->gen_x= 1024;
                                if(ima->gen_y==0) ima->gen_y= 1024;
-                               ibuf= add_ibuf_size(ima->gen_x, ima->gen_y, ima->name, 24, 0, ima->gen_type, color);
+                               ibuf= add_ibuf_size(ima->gen_x, ima->gen_y, ima->name, 24, (ima->gen_flag & IMA_GEN_FLOAT) != 0, ima->gen_type, color);
                                image_assign_ibuf(ima, ibuf, IMA_NO_INDEX, 0);
                                ima->ok= IMA_OK_LOADED;
                        }
@@ -2283,3 +2482,20 @@ void BKE_image_user_calc_frame(ImageUser *iuser, int cfra, int fieldnr)
        iuser->framenr= framenr;
        if(iuser->ok==0) iuser->ok= 1;
 }
+
+int BKE_image_has_alpha(struct Image *image)
+{
+       ImBuf *ibuf;
+       void *lock;
+       int planes;
+       
+       ibuf= BKE_image_acquire_ibuf(image, NULL, &lock);
+       planes = (ibuf?ibuf->planes:0);
+       BKE_image_release_ibuf(image, lock);
+
+       if (planes == 32)
+               return 1;
+       else
+               return 0;
+}
+