style cleanup: follow style guide for formatting of if/for/while loops, and else...
[blender.git] / source / blender / imbuf / intern / jp2.c
index a6591adfff1197e23fe929ee56f231220a8f8933..275d19d518c5b5db4e7b62ffba34c3f7a0475277 100644 (file)
@@ -1,5 +1,4 @@
-/**
- *
+/*
  * ***** BEGIN GPL LICENSE BLOCK *****
  *
  * This program is free software; you can redistribute it and/or
  *
  * You should have received a copy of the GNU General Public License
  * along with this program; if not, write to the Free Software Foundation,
- * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  *
  * Contributor(s): Campbell Barton
  *
  * ***** END GPL LICENSE BLOCK *****
  */
 
-#ifdef WITH_OPENJPEG
+/** \file blender/imbuf/intern/jp2.c
+ *  \ingroup imbuf
+ */
+
+#include "MEM_guardedalloc.h"
 
 #include "BLI_blenlib.h"
+#include "BLI_math.h"
 
 #include "imbuf.h"
-#include "imbuf_patch.h"
 
 #include "IMB_imbuf_types.h"
 #include "IMB_imbuf.h"
 #include "IMB_allocimbuf.h"
-#include "IMB_jp2.h"
+#include "IMB_filetype.h"
 
 #include "openjpeg.h"
 
@@ -53,54 +56,55 @@ typedef struct img_folder{
        float *rates;
 }img_fol_t;
 
-static int checkj2p(unsigned char *mem) /* J2K_CFMT */
+static int check_jp2(unsigned char *mem) /* J2K_CFMT */
 {
        return memcmp(JP2_HEAD, mem, 12) ? 0 : 1;
 }
 
-int imb_is_a_jp2(void *buf)
+int imb_is_a_jp2(unsigned char *buf)
 {      
-       return checkj2p(buf);
+       return check_jp2(buf);
 }
 
 
 /**
-sample error callback expecting a FILE* client object
-*/
-void error_callback(const char *msg, void *client_data) {
+ * sample error callback expecting a FILE* client object
+ */
+static void error_callback(const char *msg, void *client_data)
+{
        FILE *stream = (FILE*)client_data;
        fprintf(stream, "[ERROR] %s", msg);
 }
 /**
-sample warning callback expecting a FILE* client object
-*/
-void warning_callback(const char *msg, void *client_data) {
+ * sample warning callback expecting a FILE* client object
+ */
+static void warning_callback(const char *msg, void *client_data)
+{
        FILE *stream = (FILE*)client_data;
        fprintf(stream, "[WARNING] %s", msg);
 }
 /**
-sample debug callback expecting no client object
-*/
-void info_callback(const char *msg, void *client_data) {
+ * sample debug callback expecting no client object
+ */
+static void info_callback(const char *msg, void *client_data)
+{
        (void)client_data;
        fprintf(stdout, "[INFO] %s", msg);
 }
 
 
 
-struct ImBuf *imb_jp2_decode(unsigned char *mem, int size, int flags)
+struct ImBuf *imb_jp2_decode(unsigned char *mem, size_t size, int flags)
 {
-       struct ImBuf *ibuf = 0;
+       struct ImBuf *ibuf = NULL;
        int use_float = 0; /* for precision higher then 8 use float */
-       unsigned char *rect= NULL;
-       float *rect_float= NULL;
-       
-       long signed_offsets[4] = {0,0,0,0};
-       int float_divs[4];
        
+       long signed_offsets[4]= {0, 0, 0, 0};
+       int float_divs[4]= {1, 1, 1, 1};
+
        int index;
        
-       int w, h, depth;
+       int w, h, planes;
        
        opj_dparameters_t parameters;   /* decompression parameters */
        
@@ -112,7 +116,7 @@ struct ImBuf *imb_jp2_decode(unsigned char *mem, int size, int flags)
        opj_dinfo_t* dinfo = NULL;      /* handle to a decompressor */
        opj_cio_t *cio = NULL;
 
-       if (checkj2p(mem) == 0) return(0);
+       if (check_jp2(mem) == 0) return(NULL);
 
        /* configure the event callbacks (not required) */
        memset(&event_mgr, 0, sizeof(opj_event_mgr_t));
@@ -142,7 +146,7 @@ struct ImBuf *imb_jp2_decode(unsigned char *mem, int size, int flags)
        /* decode the stream and fill the image structure */
        image = opj_decode(dinfo, cio);
        
-       if(!image) {
+       if (!image) {
                fprintf(stderr, "ERROR -> j2k_to_image: failed to decode image!\n");
                opj_destroy_decompress(dinfo);
                opj_cio_close(cio);
@@ -153,22 +157,21 @@ struct ImBuf *imb_jp2_decode(unsigned char *mem, int size, int flags)
        opj_cio_close(cio);
 
 
-       if((image->numcomps * image->x1 * image->y1) == 0)
-       {
+       if ((image->numcomps * image->x1 * image->y1) == 0) {
                fprintf(stderr,"\nError: invalid raw image parameters\n");
                return NULL;
        }
        
-       w = image->comps[0].w;      
+       w = image->comps[0].w;
        h = image->comps[0].h;
        
        switch (image->numcomps) {
        case 1: /* Greyscale */
        case 3: /* Color */
-               depth= 24; 
+               planes= 24;
                break;
        default: /* 2 or 4 - Greyscale or Color + alpha */
-               depth= 32; /* greyscale + alpha */
+               planes= 32; /* greyscale + alpha */
                break;
        }
        
@@ -189,16 +192,10 @@ struct ImBuf *imb_jp2_decode(unsigned char *mem, int size, int flags)
                float_divs[i]= (1<<image->comps[i].prec)-1;
        }
        
-       if (use_float) {
-               ibuf= IMB_allocImBuf(w, h, depth, IB_rectfloat, 0);
-               rect_float = ibuf->rect_float;
-       } else {
-               ibuf= IMB_allocImBuf(w, h, depth, IB_rect, 0);
-               rect = (unsigned char *) ibuf->rect;
-       }
+       ibuf= IMB_allocImBuf(w, h, planes, use_float ? IB_rectfloat : IB_rect);
        
        if (ibuf==NULL) {
-               if(dinfo)
+               if (dinfo)
                        opj_destroy_decompress(dinfo);
                return NULL;
        }
@@ -206,8 +203,8 @@ struct ImBuf *imb_jp2_decode(unsigned char *mem, int size, int flags)
        ibuf->ftype = JP2;
        
        if (use_float) {
-               rect_float = ibuf->rect_float;
-               
+               float *rect_float= ibuf->rect_float;
+
                if (image->numcomps < 3) {
                        /* greyscale 12bits+ */
                        for (i = 0; i < w * h; i++, rect_float+=4) {
@@ -220,7 +217,8 @@ struct ImBuf *imb_jp2_decode(unsigned char *mem, int size, int flags)
                                else
                                        rect_float[3]= 1.0f;
                        }
-               } else {
+               }
+               else {
                        /* rgb or rgba 12bits+ */
                        for (i = 0; i < w * h; i++, rect_float+=4) {
                                index = w * h - ((i) / (w) + 1) * w + (i) % (w);
@@ -236,21 +234,24 @@ struct ImBuf *imb_jp2_decode(unsigned char *mem, int size, int flags)
                        }
                }
                
-       } else {
-               
+       }
+       else {
+               unsigned char *rect= (unsigned char *)ibuf->rect;
+
                if (image->numcomps < 3) {
                        /* greyscale */
                        for (i = 0; i < w * h; i++, rect+=4) {
                                index = w * h - ((i) / (w) + 1) * w + (i) % (w);
                                
-                               rect_float[0]= rect_float[1]= rect_float[2]= (image->comps[0].data[index] + signed_offsets[0]);
+                               rect[0]= rect[1]= rect[2]= (image->comps[0].data[index] + signed_offsets[0]);
                                
                                if (image->numcomps == 2)
                                        rect[3]= image->comps[1].data[index] + signed_offsets[1];
                                else
                                        rect[3]= 255;
                        }
-               } else {
+               }
+               else {
                        /* 8bit rgb or rgba */
                        for (i = 0; i < w * h; i++, rect+=4) {
                                int index = w * h - ((i) / (w) + 1) * w + (i) % (w);
@@ -268,7 +269,7 @@ struct ImBuf *imb_jp2_decode(unsigned char *mem, int size, int flags)
        }
        
        /* free remaining structures */
-       if(dinfo) {
+       if (dinfo) {
                opj_destroy_decompress(dinfo);
        }
        
@@ -294,11 +295,11 @@ struct ImBuf *imb_jp2_decode(unsigned char *mem, int size, int flags)
 
 
 /*
-2048x1080 (2K) at 24 fps or 48 fps, or 4096x2160 (4K) at 24 fps; 3×12 bits per pixel, XYZ color space
-
   * In 2K, for Scope (2.39:1) presentation 2048x858 pixels of the imager is used
   * In 2K, for Flat (1.85:1) presentation 1998x1080 pixels of the imager is used
-*/
+ * 2048x1080 (2K) at 24 fps or 48 fps, or 4096x2160 (4K) at 24 fps; 3x12 bits per pixel, XYZ color space
+ *
* - In 2K, for Scope (2.39:1) presentation 2048x858 pixels of the imager is used
* - In 2K, for Flat (1.85:1) presentation 1998x1080 pixels of the imager is used
+ */
 
 /* ****************************** COPIED FROM image_to_j2k.c */
 
@@ -309,7 +310,8 @@ struct ImBuf *imb_jp2_decode(unsigned char *mem, int size, int flags)
 #define COMP_48_CS 520833              /*Maximum size per color component for 2K @ 48fps*/
 
 
-static int initialise_4K_poc(opj_poc_t *POC, int numres){
+static int initialise_4K_poc(opj_poc_t *POC, int numres)
+{
        POC[0].tile  = 1; 
        POC[0].resno0  = 0; 
        POC[0].compno0 = 0;
@@ -327,8 +329,9 @@ static int initialise_4K_poc(opj_poc_t *POC, int numres){
        return 2;
 }
 
-void cinema_parameters(opj_cparameters_t *parameters){
-       parameters->tile_size_on = false;
+static void cinema_parameters(opj_cparameters_t *parameters)
+{
+       parameters->tile_size_on = FALSE;
        parameters->cp_tdx=1;
        parameters->cp_tdy=1;
        
@@ -360,17 +363,18 @@ void cinema_parameters(opj_cparameters_t *parameters){
 
 }
 
-void cinema_setup_encoder(opj_cparameters_t *parameters,opj_image_t *image, img_fol_t *img_fol){
+static void cinema_setup_encoder(opj_cparameters_t *parameters,opj_image_t *image, img_fol_t *img_fol)
+{
        int i;
        float temp_rate;
 
-       switch (parameters->cp_cinema){
+       switch (parameters->cp_cinema) {
        case CINEMA2K_24:
        case CINEMA2K_48:
-               if(parameters->numresolution > 6){
+               if (parameters->numresolution > 6) {
                        parameters->numresolution = 6;
                }
-               if (!((image->comps[0].w == 2048) || (image->comps[0].h == 1080))){
+               if (!((image->comps[0].w == 2048) || (image->comps[0].h == 1080))) {
                        fprintf(stdout,"Image coordinates %d x %d is not 2K compliant.\nJPEG Digital Cinema Profile-3 "
                                "(2K profile) compliance requires that at least one of coordinates match 2048 x 1080\n",
                                image->comps[0].w,image->comps[0].h);
@@ -379,15 +383,16 @@ void cinema_setup_encoder(opj_cparameters_t *parameters,opj_image_t *image, img_
        break;
        
        case CINEMA4K_24:
-               if(parameters->numresolution < 1){
-                               parameters->numresolution = 1;
-                       }else if(parameters->numresolution > 7){
-                               parameters->numresolution = 7;
-                       }
-               if (!((image->comps[0].w == 4096) || (image->comps[0].h == 2160))){
-                       fprintf(stdout,"Image coordinates %d x %d is not 4K compliant.\nJPEG Digital Cinema Profile-4" 
-                               "(4K profile) compliance requires that at least one of coordinates match 4096 x 2160\n",
-                               image->comps[0].w,image->comps[0].h);
+               if (parameters->numresolution < 1) {
+                       parameters->numresolution = 1;
+               }
+               else if (parameters->numresolution > 7) {
+                       parameters->numresolution = 7;
+               }
+               if (!((image->comps[0].w == 4096) || (image->comps[0].h == 2160))) {
+                       fprintf(stdout,"Image coordinates %d x %d is not 4K compliant.\nJPEG Digital Cinema Profile-4"
+                                       "(4K profile) compliance requires that at least one of coordinates match 4096 x 2160\n",
+                                       image->comps[0].w,image->comps[0].h);
                        parameters->cp_rsiz = STD_RSIZ;
                }
                parameters->numpocs = initialise_4K_poc(parameters->POC,parameters->numresolution);
@@ -397,21 +402,23 @@ void cinema_setup_encoder(opj_cparameters_t *parameters,opj_image_t *image, img_
                break;
        }
 
-       switch (parameters->cp_cinema){
+       switch (parameters->cp_cinema) {
        case CINEMA2K_24:
        case CINEMA4K_24:
-               for(i=0 ; i<parameters->tcp_numlayers ; i++){
-                       temp_rate = 0 ;
-                       if (img_fol->rates[i]== 0){
+               for (i=0 ; i<parameters->tcp_numlayers ; i++) {
+                       temp_rate = 0;
+                       if (img_fol->rates[i]== 0) {
                                parameters->tcp_rates[0]= ((float) (image->numcomps * image->comps[0].w * image->comps[0].h * image->comps[0].prec))/ 
                                        (CINEMA_24_CS * 8 * image->comps[0].dx * image->comps[0].dy);
-                       }else{
+                       }
+                       else {
                                temp_rate =((float) (image->numcomps * image->comps[0].w * image->comps[0].h * image->comps[0].prec))/ 
                                        (img_fol->rates[i] * 8 * image->comps[0].dx * image->comps[0].dy);
-                               if (temp_rate > CINEMA_24_CS ){
+                               if (temp_rate > CINEMA_24_CS ) {
                                        parameters->tcp_rates[i]= ((float) (image->numcomps * image->comps[0].w * image->comps[0].h * image->comps[0].prec))/ 
                                                (CINEMA_24_CS * 8 * image->comps[0].dx * image->comps[0].dy);
-                               }else{
+                               }
+                               else {
                                        parameters->tcp_rates[i]= img_fol->rates[i];
                                }
                        }
@@ -420,18 +427,20 @@ void cinema_setup_encoder(opj_cparameters_t *parameters,opj_image_t *image, img_
                break;
                
        case CINEMA2K_48:
-               for(i=0 ; i<parameters->tcp_numlayers ; i++){
-                       temp_rate = 0 ;
-                       if (img_fol->rates[i]== 0){
+               for (i=0 ; i<parameters->tcp_numlayers ; i++) {
+                       temp_rate = 0;
+                       if (img_fol->rates[i]== 0) {
                                parameters->tcp_rates[0]= ((float) (image->numcomps * image->comps[0].w * image->comps[0].h * image->comps[0].prec))/ 
                                        (CINEMA_48_CS * 8 * image->comps[0].dx * image->comps[0].dy);
-                       }else{
+                       }
+                       else {
                                temp_rate =((float) (image->numcomps * image->comps[0].w * image->comps[0].h * image->comps[0].prec))/ 
                                        (img_fol->rates[i] * 8 * image->comps[0].dx * image->comps[0].dy);
-                               if (temp_rate > CINEMA_48_CS ){
+                               if (temp_rate > CINEMA_48_CS ) {
                                        parameters->tcp_rates[0]= ((float) (image->numcomps * image->comps[0].w * image->comps[0].h * image->comps[0].prec))/ 
                                                (CINEMA_48_CS * 8 * image->comps[0].dx * image->comps[0].dy);
-                               }else{
+                               }
+                               else {
                                        parameters->tcp_rates[i]= img_fol->rates[i];
                                }
                        }
@@ -446,8 +455,8 @@ void cinema_setup_encoder(opj_cparameters_t *parameters,opj_image_t *image, img_
 }
 
 
-static opj_image_t* ibuftoimage(ImBuf *ibuf, opj_cparameters_t *parameters) {
-       
+static opj_image_t* ibuftoimage(ImBuf *ibuf, opj_cparameters_t *parameters)
+{
        unsigned char *rect;
        float *rect_float;
        
@@ -476,9 +485,9 @@ static opj_image_t* ibuftoimage(ImBuf *ibuf, opj_cparameters_t *parameters) {
                                parameters->cp_cinema= CINEMA2K_24;
                        }
                }
-               if (parameters->cp_cinema){
+               if (parameters->cp_cinema) {
                        img_fol.rates = (float*)MEM_mallocN(parameters->tcp_numlayers * sizeof(float), "jp2_rates");
-                       for(i=0; i< parameters->tcp_numlayers; i++){
+                       for (i=0; i< parameters->tcp_numlayers; i++) {
                                img_fol.rates[i] = parameters->tcp_rates[i];
                        }
                        cinema_parameters(parameters);
@@ -498,7 +507,7 @@ static opj_image_t* ibuftoimage(ImBuf *ibuf, opj_cparameters_t *parameters) {
                
                /* 32bit images == alpha channel */
                /* grayscale not supported yet */
-               numcomps= (ibuf->depth==32) ? 4 : 3;
+               numcomps= (ibuf->planes==32) ? 4 : 3;
        }
        
        w= ibuf->x;
@@ -507,7 +516,7 @@ static opj_image_t* ibuftoimage(ImBuf *ibuf, opj_cparameters_t *parameters) {
        
        /* initialize image components */
        memset(&cmptparm[0], 0, 4 * sizeof(opj_image_cmptparm_t));
-       for(i = 0; i < numcomps; i++) {
+       for (i = 0; i < numcomps; i++) {
                cmptparm[i].prec = prec;
                cmptparm[i].bpp = prec;
                cmptparm[i].sgnd = 0;
@@ -518,7 +527,7 @@ static opj_image_t* ibuftoimage(ImBuf *ibuf, opj_cparameters_t *parameters) {
        }
        /* create the image */
        image = opj_image_create(numcomps, &cmptparm[0], color_space);
-       if(!image) {
+       if (!image) {
                printf("Error: opj_image_create() failed\n");
                return NULL;
        }
@@ -540,16 +549,23 @@ static opj_image_t* ibuftoimage(ImBuf *ibuf, opj_cparameters_t *parameters) {
        
        
        if (rect_float) {
+               float rgb[3];
+               
                switch (prec) {
                case 8: /* Convert blenders float color channels to 8,12 or 16bit ints */
-                       for(y=h-1; y>=0; y--) {
+                       for (y=h-1; y>=0; y--) {
                                y_row = y*w;
-                               for(x=0; x<w; x++, rect_float+=4) {
+                               for (x=0; x<w; x++, rect_float+=4) {
                                        i = y_row + x;
+                                       
+                                       if (ibuf->profile == IB_PROFILE_LINEAR_RGB)
+                                               linearrgb_to_srgb_v3_v3(rgb, rect_float);
+                                       else
+                                               copy_v3_v3(rgb, rect_float);
                                
-                                       image->comps[0].data[i] = DOWNSAMPLE_FLOAT_TO_8BIT(rect_float[0]);
-                                       image->comps[1].data[i] = DOWNSAMPLE_FLOAT_TO_8BIT(rect_float[1]);
-                                       image->comps[2].data[i] = DOWNSAMPLE_FLOAT_TO_8BIT(rect_float[2]);
+                                       image->comps[0].data[i] = DOWNSAMPLE_FLOAT_TO_8BIT(rgb[0]);
+                                       image->comps[1].data[i] = DOWNSAMPLE_FLOAT_TO_8BIT(rgb[1]);
+                                       image->comps[2].data[i] = DOWNSAMPLE_FLOAT_TO_8BIT(rgb[2]);
                                        if (numcomps>3)
                                                image->comps[3].data[i] = DOWNSAMPLE_FLOAT_TO_8BIT(rect_float[3]);
                                }
@@ -557,41 +573,52 @@ static opj_image_t* ibuftoimage(ImBuf *ibuf, opj_cparameters_t *parameters) {
                        break;
                        
                case 12:
-                       for(y=h-1; y>=0; y--) {
+                       for (y=h-1; y>=0; y--) {
                                y_row = y*w;
-                               for(x=0; x<w; x++, rect_float+=4) {
+                               for (x=0; x<w; x++, rect_float+=4) {
                                        i = y_row + x;
+                                       
+                                       if (ibuf->profile == IB_PROFILE_LINEAR_RGB)
+                                               linearrgb_to_srgb_v3_v3(rgb, rect_float);
+                                       else
+                                               copy_v3_v3(rgb, rect_float);
                                
-                                       image->comps[0].data[i] = DOWNSAMPLE_FLOAT_TO_12BIT(rect_float[0]);
-                                       image->comps[1].data[i] = DOWNSAMPLE_FLOAT_TO_12BIT(rect_float[1]);
-                                       image->comps[2].data[i] = DOWNSAMPLE_FLOAT_TO_12BIT(rect_float[2]);
+                                       image->comps[0].data[i] = DOWNSAMPLE_FLOAT_TO_12BIT(rgb[0]);
+                                       image->comps[1].data[i] = DOWNSAMPLE_FLOAT_TO_12BIT(rgb[1]);
+                                       image->comps[2].data[i] = DOWNSAMPLE_FLOAT_TO_12BIT(rgb[2]);
                                        if (numcomps>3)
                                                image->comps[3].data[i] = DOWNSAMPLE_FLOAT_TO_12BIT(rect_float[3]);
                                }
                        }
                        break;
                case 16:
-                       for(y=h-1; y>=0; y--) {
+                       for (y=h-1; y>=0; y--) {
                                y_row = y*w;
-                               for(x=0; x<w; x++, rect_float+=4) {
+                               for (x=0; x<w; x++, rect_float+=4) {
                                        i = y_row + x;
+                                       
+                                       if (ibuf->profile == IB_PROFILE_LINEAR_RGB)
+                                               linearrgb_to_srgb_v3_v3(rgb, rect_float);
+                                       else
+                                               copy_v3_v3(rgb, rect_float);
                                
-                                       image->comps[0].data[i] = DOWNSAMPLE_FLOAT_TO_16BIT(rect_float[0]);
-                                       image->comps[1].data[i] = DOWNSAMPLE_FLOAT_TO_16BIT(rect_float[1]);
-                                       image->comps[2].data[i] = DOWNSAMPLE_FLOAT_TO_16BIT(rect_float[2]);
+                                       image->comps[0].data[i] = DOWNSAMPLE_FLOAT_TO_16BIT(rgb[0]);
+                                       image->comps[1].data[i] = DOWNSAMPLE_FLOAT_TO_16BIT(rgb[1]);
+                                       image->comps[2].data[i] = DOWNSAMPLE_FLOAT_TO_16BIT(rgb[2]);
                                        if (numcomps>3)
                                                image->comps[3].data[i] = DOWNSAMPLE_FLOAT_TO_16BIT(rect_float[3]);
                                }
                        }
                        break;
                }
-       } else {
+       }
+       else {
                /* just use rect*/
                switch (prec) {
                case 8:
-                       for(y=h-1; y>=0; y--) {
+                       for (y=h-1; y>=0; y--) {
                                y_row = y*w;
-                               for(x=0; x<w; x++, rect+=4) {
+                               for (x=0; x<w; x++, rect+=4) {
                                        i = y_row + x;
                                
                                        image->comps[0].data[i] = rect[0];
@@ -604,9 +631,9 @@ static opj_image_t* ibuftoimage(ImBuf *ibuf, opj_cparameters_t *parameters) {
                        break;
                        
                case 12: /* Up Sampling, a bit pointless but best write the bit depth requested */
-                       for(y=h-1; y>=0; y--) {
+                       for (y=h-1; y>=0; y--) {
                                y_row = y*w;
-                               for(x=0; x<w; x++, rect+=4) {
+                               for (x=0; x<w; x++, rect+=4) {
                                        i = y_row + x;
                                
                                        image->comps[0].data[i]= UPSAMPLE_8_TO_12(rect[0]);
@@ -618,9 +645,9 @@ static opj_image_t* ibuftoimage(ImBuf *ibuf, opj_cparameters_t *parameters) {
                        }
                        break;
                case 16:
-                       for(y=h-1; y>=0; y--) {
+                       for (y=h-1; y>=0; y--) {
                                y_row = y*w;
-                               for(x=0; x<w; x++, rect+=4) {
+                               for (x=0; x<w; x++, rect+=4) {
                                        i = y_row + x;
                                
                                        image->comps[0].data[i]= UPSAMPLE_8_TO_16(rect[0]);
@@ -637,7 +664,7 @@ static opj_image_t* ibuftoimage(ImBuf *ibuf, opj_cparameters_t *parameters) {
        /* Decide if MCT should be used */
        parameters->tcp_mct = image->numcomps == 3 ? 1 : 0;
        
-       if(parameters->cp_cinema){
+       if (parameters->cp_cinema) {
                cinema_setup_encoder(parameters,image,&img_fol);
        }
        
@@ -649,8 +676,8 @@ static opj_image_t* ibuftoimage(ImBuf *ibuf, opj_cparameters_t *parameters) {
 
 
 /* Found write info at http://users.ece.gatech.edu/~slabaugh/personal/c/bitmapUnix.c */
-short imb_savejp2(struct ImBuf *ibuf, char *name, int flags) {
-       
+int imb_savejp2(struct ImBuf *ibuf, const char *name, int flags)
+{
        int quality = ibuf->ftype & 0xff;
        
        int bSuccess;
@@ -658,10 +685,12 @@ short imb_savejp2(struct ImBuf *ibuf, char *name, int flags) {
        opj_event_mgr_t event_mgr;              /* event manager */
        opj_image_t *image = NULL;
        
+       (void)flags; /* unused */
+       
        /*
-       configure the event callbacks (not required)
-       setting of each callback is optionnal
-       */
+        * configure the event callbacks (not required)
+        * setting of each callback is optionnal
+        */
        memset(&event_mgr, 0, sizeof(opj_event_mgr_t));
        event_mgr.error_handler = error_callback;
        event_mgr.warning_handler = warning_callback;
@@ -672,7 +701,7 @@ short imb_savejp2(struct ImBuf *ibuf, char *name, int flags) {
        
        /* compression ratio */
        /* invert range, from 10-100, 100-1
-       * where jpeg see's 1 and highest quality (lossless) and 100 is very low quality*/
+        * where jpeg see's 1 and highest quality (lossless) and 100 is very low quality*/
        parameters.tcp_rates[0]= ((100-quality)/90.0f*99.0f) + 1;
 
        
@@ -711,7 +740,7 @@ short imb_savejp2(struct ImBuf *ibuf, char *name, int flags) {
                codestream_length = cio_tell(cio);
 
                /* write the buffer to disk */
-               f = fopen(name, "wb");
+               f = BLI_fopen(name, "wb");
                
                if (!f) {
                        fprintf(stderr, "failed to open %s for writing\n", name);
@@ -732,5 +761,3 @@ short imb_savejp2(struct ImBuf *ibuf, char *name, int flags) {
        
        return 1;
 }
-
-#endif /* WITH_OPENJPEG */