Fix msvc 2013 compiler errors after the ingenious cleanup in 4ca67869cc7a.
[blender.git] / source / blender / imbuf / intern / allocimbuf.c
index bee4758dd0797c555445fdadcad5094588013808..556c4beeae7a523c5252c35b529a81c3a66e921b 100644 (file)
 #include "IMB_allocimbuf.h"
 #include "IMB_filetype.h"
 #include "IMB_metadata.h"
-#include "IMB_colormanagement.h"
+#include "IMB_colormanagement_intern.h"
 
 #include "imbuf.h"
 
 #include "MEM_guardedalloc.h"
-#include "MEM_CacheLimiterC-Api.h"
+
+#include "BLI_utildefines.h"
+#include "BLI_threads.h"
+
+static SpinLock refcounter_spin;
+
+void imb_refcounter_lock_init(void)
+{
+       BLI_spin_init(&refcounter_spin);
+}
+
+void imb_refcounter_lock_exit(void)
+{
+       BLI_spin_end(&refcounter_spin);
+}
 
 void imb_freemipmapImBuf(ImBuf *ibuf)
 {
@@ -152,10 +166,18 @@ void IMB_freezbuffloatImBuf(ImBuf *ibuf)
 void IMB_freeImBuf(ImBuf *ibuf)
 {
        if (ibuf) {
+               bool needs_free = false;
+
+               BLI_spin_lock(&refcounter_spin);
                if (ibuf->refcounter > 0) {
                        ibuf->refcounter--;
                }
                else {
+                       needs_free = true;
+               }
+               BLI_spin_unlock(&refcounter_spin);
+
+               if (needs_free) {
                        imb_freerectImBuf(ibuf);
                        imb_freerectfloatImBuf(ibuf);
                        imb_freetilesImBuf(ibuf);
@@ -163,7 +185,7 @@ void IMB_freeImBuf(ImBuf *ibuf)
                        IMB_freezbuffloatImBuf(ibuf);
                        freeencodedbufferImBuf(ibuf);
                        IMB_metadata_free(ibuf);
-                       IMB_colormanage_cache_free(ibuf);
+                       colormanage_cache_free(ibuf);
 
                        if (ibuf->dds_data.data != NULL) {
                                free(ibuf->dds_data.data); /* dds_data.data is allocated by DirectDrawSurface::readData(), so don't use MEM_freeN! */
@@ -175,7 +197,9 @@ void IMB_freeImBuf(ImBuf *ibuf)
 
 void IMB_refImBuf(ImBuf *ibuf)
 {
+       BLI_spin_lock(&refcounter_spin);
        ibuf->refcounter++;
+       BLI_spin_unlock(&refcounter_spin);
 }
 
 ImBuf *IMB_makeSingleUser(ImBuf *ibuf)
@@ -191,46 +215,48 @@ ImBuf *IMB_makeSingleUser(ImBuf *ibuf)
        return rval;
 }
 
-short addzbufImBuf(ImBuf *ibuf)
+bool addzbufImBuf(ImBuf *ibuf)
 {
-       int size;
+       size_t size;
        
-       if (ibuf == NULL) return FALSE;
+       if (ibuf == NULL) return false;
        
        IMB_freezbufImBuf(ibuf);
        
-       size = ibuf->x * ibuf->y * sizeof(unsigned int);
-       if ((ibuf->zbuf = MEM_mapallocN(size, "addzbufImBuf"))) {
+       size = (size_t)ibuf->x * (size_t)ibuf->y * sizeof(unsigned int);
+
+       if ((ibuf->zbuf = MEM_mapallocN(size, __func__))) {
                ibuf->mall |= IB_zbuf;
                ibuf->flags |= IB_zbuf;
-               return TRUE;
+               return true;
        }
        
-       return FALSE;
+       return false;
 }
 
-short addzbuffloatImBuf(ImBuf *ibuf)
+bool addzbuffloatImBuf(ImBuf *ibuf)
 {
-       int size;
+       size_t size;
        
-       if (ibuf == NULL) return FALSE;
+       if (ibuf == NULL) return false;
        
        IMB_freezbuffloatImBuf(ibuf);
        
-       size = ibuf->x * ibuf->y * sizeof(float);
-       if ((ibuf->zbuf_float = MEM_mapallocN(size, "addzbuffloatImBuf"))) {
+       size = (size_t)ibuf->x * (size_t)ibuf->y * sizeof(float);
+
+       if ((ibuf->zbuf_float = MEM_mapallocN(size, __func__))) {
                ibuf->mall |= IB_zbuffloat;
                ibuf->flags |= IB_zbuffloat;
-               return TRUE;
+               return true;
        }
        
-       return FALSE;
+       return false;
 }
 
 
-short imb_addencodedbufferImBuf(ImBuf *ibuf)
+bool imb_addencodedbufferImBuf(ImBuf *ibuf)
 {
-       if (ibuf == NULL) return FALSE;
+       if (ibuf == NULL) return false;
 
        freeencodedbufferImBuf(ibuf);
 
@@ -239,33 +265,33 @@ short imb_addencodedbufferImBuf(ImBuf *ibuf)
 
        ibuf->encodedsize = 0;
 
-       if ((ibuf->encodedbuffer = MEM_mallocN(ibuf->encodedbuffersize, "addencodedbufferImBuf"))) {
+       if ((ibuf->encodedbuffer = MEM_mallocN(ibuf->encodedbuffersize, __func__))) {
                ibuf->mall |= IB_mem;
                ibuf->flags |= IB_mem;
-               return TRUE;
+               return true;
        }
 
-       return FALSE;
+       return false;
 }
 
 
-short imb_enlargeencodedbufferImBuf(ImBuf *ibuf)
+bool imb_enlargeencodedbufferImBuf(ImBuf *ibuf)
 {
        unsigned int newsize, encodedsize;
        void *newbuffer;
 
-       if (ibuf == NULL) return FALSE;
+       if (ibuf == NULL) return false;
 
        if (ibuf->encodedbuffersize < ibuf->encodedsize) {
-               printf("imb_enlargeencodedbufferImBuf: error in parameters\n");
-               return FALSE;
+               printf("%s: error in parameters\n", __func__);
+               return false;
        }
 
        newsize = 2 * ibuf->encodedbuffersize;
        if (newsize < 10000) newsize = 10000;
 
-       newbuffer = MEM_mallocN(newsize, "enlargeencodedbufferImBuf");
-       if (newbuffer == NULL) return FALSE;
+       newbuffer = MEM_mallocN(newsize, __func__);
+       if (newbuffer == NULL) return false;
 
        if (ibuf->encodedbuffer) {
                memcpy(newbuffer, ibuf->encodedbuffer, ibuf->encodedsize);
@@ -284,59 +310,61 @@ short imb_enlargeencodedbufferImBuf(ImBuf *ibuf)
        ibuf->mall |= IB_mem;
        ibuf->flags |= IB_mem;
 
-       return TRUE;
+       return true;
 }
 
-short imb_addrectfloatImBuf(ImBuf *ibuf)
+bool imb_addrectfloatImBuf(ImBuf *ibuf)
 {
-       int size;
+       size_t size;
        
-       if (ibuf == NULL) return FALSE;
+       if (ibuf == NULL) return false;
        
        if (ibuf->rect_float)
                imb_freerectfloatImBuf(ibuf);  /* frees mipmap too, hrm */
        
-       size = ibuf->x * ibuf->y;
-       size = size * 4 * sizeof(float);
+       size = (size_t)ibuf->x * (size_t)ibuf->y * sizeof(float[4]);
+
        ibuf->channels = 4;
-       
-       if ((ibuf->rect_float = MEM_mapallocN(size, "imb_addrectfloatImBuf"))) {
+       if ((ibuf->rect_float = MEM_mapallocN(size, __func__))) {
                ibuf->mall |= IB_rectfloat;
                ibuf->flags |= IB_rectfloat;
-               return TRUE;
+               return true;
        }
        
-       return FALSE;
+       return false;
 }
 
 /* question; why also add zbuf? */
-short imb_addrectImBuf(ImBuf *ibuf)
+bool imb_addrectImBuf(ImBuf *ibuf)
 {
-       int size;
+       size_t size;
 
-       if (ibuf == NULL) return FALSE;
+       if (ibuf == NULL) return false;
        
        /* don't call imb_freerectImBuf, it frees mipmaps, this call is used only too give float buffers display */
        if (ibuf->rect && (ibuf->mall & IB_rect))
                MEM_freeN(ibuf->rect);
        ibuf->rect = NULL;
        
-       size = ibuf->x * ibuf->y;
-       size = size * sizeof(unsigned int);
+       size = (size_t)ibuf->x * (size_t)ibuf->y * sizeof(unsigned int);
 
-       if ((ibuf->rect = MEM_mapallocN(size, "imb_addrectImBuf"))) {
+       if ((ibuf->rect = MEM_mapallocN(size, __func__))) {
                ibuf->mall |= IB_rect;
                ibuf->flags |= IB_rect;
-               if (ibuf->planes > 32) return (addzbufImBuf(ibuf));
-               else return TRUE;
+               if (ibuf->planes > 32) {
+                       return (addzbufImBuf(ibuf));
+               }
+               else {
+                       return true;
+               }
        }
 
-       return FALSE;
+       return false;
 }
 
-short imb_addtilesImBuf(ImBuf *ibuf)
+bool imb_addtilesImBuf(ImBuf *ibuf)
 {
-       if (ibuf == NULL) return FALSE;
+       if (ibuf == NULL) return false;
 
        if (!ibuf->tiles)
                if ((ibuf->tiles = MEM_callocN(sizeof(unsigned int *) * ibuf->xtiles * ibuf->ytiles, "imb_tiles")))
@@ -355,37 +383,40 @@ ImBuf *IMB_allocImBuf(unsigned int x, unsigned int y, uchar planes, unsigned int
                ibuf->x = x;
                ibuf->y = y;
                ibuf->planes = planes;
-               ibuf->ftype = TGA;
+               ibuf->ftype = PNG | 15; /* the 15 means, set compression to low ratio but not time consuming */
                ibuf->channels = 4;  /* float option, is set to other values when buffers get assigned */
-               ibuf->ppm[0] = ibuf->ppm[1] = IMB_DPI_DEFAULT / 0.0254; /* IMB_DPI_DEFAULT -> pixels-per-meter */
-               
+               ibuf->ppm[0] = ibuf->ppm[1] = IMB_DPI_DEFAULT / 0.0254f; /* IMB_DPI_DEFAULT -> pixels-per-meter */
+
                if (flags & IB_rect) {
-                       if (imb_addrectImBuf(ibuf) == FALSE) {
+                       if (imb_addrectImBuf(ibuf) == false) {
                                IMB_freeImBuf(ibuf);
                                return NULL;
                        }
                }
                
                if (flags & IB_rectfloat) {
-                       if (imb_addrectfloatImBuf(ibuf) == FALSE) {
+                       if (imb_addrectfloatImBuf(ibuf) == false) {
                                IMB_freeImBuf(ibuf);
                                return NULL;
                        }
                }
                
                if (flags & IB_zbuf) {
-                       if (addzbufImBuf(ibuf) == FALSE) {
+                       if (addzbufImBuf(ibuf) == false) {
                                IMB_freeImBuf(ibuf);
                                return NULL;
                        }
                }
                
                if (flags & IB_zbuffloat) {
-                       if (addzbuffloatImBuf(ibuf) == FALSE) {
+                       if (addzbuffloatImBuf(ibuf) == false) {
                                IMB_freeImBuf(ibuf);
                                return NULL;
                        }
                }
+
+               /* assign default spaces */
+               colormanage_imbuf_set_default_spaces(ibuf);
        }
        return (ibuf);
 }
@@ -417,7 +448,7 @@ ImBuf *IMB_dupImBuf(ImBuf *ibuf1)
 
        if (ibuf1->encodedbuffer) {
                ibuf2->encodedbuffersize = ibuf1->encodedbuffersize;
-               if (imb_addencodedbufferImBuf(ibuf2) == FALSE) {
+               if (imb_addencodedbufferImBuf(ibuf2) == false) {
                        IMB_freeImBuf(ibuf2);
                        return NULL;
                }