style cleanup: follow style guide for formatting of if/for/while loops, and else...
[blender.git] / source / blender / imbuf / intern / jp2.c
index a76c6e780ca2dba6ce0d96518dbe669692a4b23c..275d19d518c5b5db4e7b62ffba34c3f7a0475277 100644 (file)
@@ -1,5 +1,4 @@
-/**
- *
+/*
  * ***** BEGIN GPL LICENSE BLOCK *****
  *
  * This program is free software; you can redistribute it and/or
  * ***** BEGIN GPL LICENSE BLOCK *****
  *
  * This program is free software; you can redistribute it and/or
  * ***** END GPL LICENSE BLOCK *****
  */
 
  * ***** 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 "BLI_blenlib.h"
 #include "BLI_math.h"
@@ -53,52 +56,55 @@ typedef struct img_folder{
        float *rates;
 }img_fol_t;
 
        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(unsigned char *buf)
 {      
 {
        return memcmp(JP2_HEAD, mem, 12) ? 0 : 1;
 }
 
 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);
 }
 /**
        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);
 }
 /**
        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);
 }
 
 
 
        (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 */
        
        int use_float = 0; /* for precision higher then 8 use float */
        
-       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 index;
        
-       int w, h, depth;
+       int w, h, planes;
        
        opj_dparameters_t parameters;   /* decompression parameters */
        
        
        opj_dparameters_t parameters;   /* decompression parameters */
        
@@ -110,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;
 
        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));
 
        /* configure the event callbacks (not required) */
        memset(&event_mgr, 0, sizeof(opj_event_mgr_t));
@@ -140,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);
        
        /* 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);
                fprintf(stderr, "ERROR -> j2k_to_image: failed to decode image!\n");
                opj_destroy_decompress(dinfo);
                opj_cio_close(cio);
@@ -151,8 +157,7 @@ struct ImBuf *imb_jp2_decode(unsigned char *mem, int size, int flags)
        opj_cio_close(cio);
 
 
        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;
        }
                fprintf(stderr,"\nError: invalid raw image parameters\n");
                return NULL;
        }
@@ -163,10 +168,10 @@ struct ImBuf *imb_jp2_decode(unsigned char *mem, int size, int flags)
        switch (image->numcomps) {
        case 1: /* Greyscale */
        case 3: /* Color */
        switch (image->numcomps) {
        case 1: /* Greyscale */
        case 3: /* Color */
-               depth= 24; 
+               planes= 24;
                break;
        default: /* 2 or 4 - Greyscale or Color + alpha */
                break;
        default: /* 2 or 4 - Greyscale or Color + alpha */
-               depth= 32; /* greyscale + alpha */
+               planes= 32; /* greyscale + alpha */
                break;
        }
        
                break;
        }
        
@@ -187,10 +192,10 @@ struct ImBuf *imb_jp2_decode(unsigned char *mem, int size, int flags)
                float_divs[i]= (1<<image->comps[i].prec)-1;
        }
        
                float_divs[i]= (1<<image->comps[i].prec)-1;
        }
        
-       ibuf= IMB_allocImBuf(w, h, depth, use_float ? IB_rectfloat : IB_rect, 0);
+       ibuf= IMB_allocImBuf(w, h, planes, use_float ? IB_rectfloat : IB_rect);
        
        if (ibuf==NULL) {
        
        if (ibuf==NULL) {
-               if(dinfo)
+               if (dinfo)
                        opj_destroy_decompress(dinfo);
                return NULL;
        }
                        opj_destroy_decompress(dinfo);
                return NULL;
        }
@@ -212,7 +217,8 @@ struct ImBuf *imb_jp2_decode(unsigned char *mem, int size, int flags)
                                else
                                        rect_float[3]= 1.0f;
                        }
                                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);
                        /* rgb or rgba 12bits+ */
                        for (i = 0; i < w * h; i++, rect_float+=4) {
                                index = w * h - ((i) / (w) + 1) * w + (i) % (w);
@@ -228,7 +234,8 @@ 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) {
                unsigned char *rect= (unsigned char *)ibuf->rect;
 
                if (image->numcomps < 3) {
@@ -243,7 +250,8 @@ struct ImBuf *imb_jp2_decode(unsigned char *mem, int size, int flags)
                                else
                                        rect[3]= 255;
                        }
                                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);
                        /* 8bit rgb or rgba */
                        for (i = 0; i < w * h; i++, rect+=4) {
                                int index = w * h - ((i) / (w) + 1) * w + (i) % (w);
@@ -261,7 +269,7 @@ struct ImBuf *imb_jp2_decode(unsigned char *mem, int size, int flags)
        }
        
        /* free remaining structures */
        }
        
        /* free remaining structures */
-       if(dinfo) {
+       if (dinfo) {
                opj_destroy_decompress(dinfo);
        }
        
                opj_destroy_decompress(dinfo);
        }
        
@@ -287,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 */
 
 
 /* ****************************** COPIED FROM image_to_j2k.c */
 
@@ -302,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*/
 
 
 #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;
        POC[0].tile  = 1; 
        POC[0].resno0  = 0; 
        POC[0].compno0 = 0;
@@ -320,8 +329,9 @@ static int initialise_4K_poc(opj_poc_t *POC, int numres){
        return 2;
 }
 
        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;
        
        parameters->cp_tdx=1;
        parameters->cp_tdy=1;
        
@@ -353,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;
 
        int i;
        float temp_rate;
 
-       switch (parameters->cp_cinema){
+       switch (parameters->cp_cinema) {
        case CINEMA2K_24:
        case CINEMA2K_48:
        case CINEMA2K_24:
        case CINEMA2K_48:
-               if(parameters->numresolution > 6){
+               if (parameters->numresolution > 6) {
                        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);
                        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);
@@ -372,15 +383,16 @@ void cinema_setup_encoder(opj_cparameters_t *parameters,opj_image_t *image, img_
        break;
        
        case CINEMA4K_24:
        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);
                        parameters->cp_rsiz = STD_RSIZ;
                }
                parameters->numpocs = initialise_4K_poc(parameters->POC,parameters->numresolution);
@@ -390,21 +402,23 @@ void cinema_setup_encoder(opj_cparameters_t *parameters,opj_image_t *image, img_
                break;
        }
 
                break;
        }
 
-       switch (parameters->cp_cinema){
+       switch (parameters->cp_cinema) {
        case CINEMA2K_24:
        case CINEMA4K_24:
        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);
                                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);
                                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);
                                        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];
                                }
                        }
                                        parameters->tcp_rates[i]= img_fol->rates[i];
                                }
                        }
@@ -413,18 +427,20 @@ void cinema_setup_encoder(opj_cparameters_t *parameters,opj_image_t *image, img_
                break;
                
        case CINEMA2K_48:
                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);
                                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);
                                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);
                                        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];
                                }
                        }
                                        parameters->tcp_rates[i]= img_fol->rates[i];
                                }
                        }
@@ -439,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;
        
        unsigned char *rect;
        float *rect_float;
        
@@ -469,9 +485,9 @@ static opj_image_t* ibuftoimage(ImBuf *ibuf, opj_cparameters_t *parameters) {
                                parameters->cp_cinema= CINEMA2K_24;
                        }
                }
                                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");
                        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);
                                img_fol.rates[i] = parameters->tcp_rates[i];
                        }
                        cinema_parameters(parameters);
@@ -491,7 +507,7 @@ static opj_image_t* ibuftoimage(ImBuf *ibuf, opj_cparameters_t *parameters) {
                
                /* 32bit images == alpha channel */
                /* grayscale not supported yet */
                
                /* 32bit images == alpha channel */
                /* grayscale not supported yet */
-               numcomps= (ibuf->depth==32) ? 4 : 3;
+               numcomps= (ibuf->planes==32) ? 4 : 3;
        }
        
        w= ibuf->x;
        }
        
        w= ibuf->x;
@@ -500,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));
        
        /* 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;
                cmptparm[i].prec = prec;
                cmptparm[i].bpp = prec;
                cmptparm[i].sgnd = 0;
@@ -511,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);
        }
        /* 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;
        }
                printf("Error: opj_image_create() failed\n");
                return NULL;
        }
@@ -537,9 +553,9 @@ static opj_image_t* ibuftoimage(ImBuf *ibuf, opj_cparameters_t *parameters) {
                
                switch (prec) {
                case 8: /* Convert blenders float color channels to 8,12 or 16bit ints */
                
                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;
                                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)
                                        i = y_row + x;
                                        
                                        if (ibuf->profile == IB_PROFILE_LINEAR_RGB)
@@ -557,9 +573,9 @@ static opj_image_t* ibuftoimage(ImBuf *ibuf, opj_cparameters_t *parameters) {
                        break;
                        
                case 12:
                        break;
                        
                case 12:
-                       for(y=h-1; y>=0; y--) {
+                       for (y=h-1; y>=0; y--) {
                                y_row = y*w;
                                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)
                                        i = y_row + x;
                                        
                                        if (ibuf->profile == IB_PROFILE_LINEAR_RGB)
@@ -576,9 +592,9 @@ static opj_image_t* ibuftoimage(ImBuf *ibuf, opj_cparameters_t *parameters) {
                        }
                        break;
                case 16:
                        }
                        break;
                case 16:
-                       for(y=h-1; y>=0; y--) {
+                       for (y=h-1; y>=0; y--) {
                                y_row = y*w;
                                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)
                                        i = y_row + x;
                                        
                                        if (ibuf->profile == IB_PROFILE_LINEAR_RGB)
@@ -595,13 +611,14 @@ static opj_image_t* ibuftoimage(ImBuf *ibuf, opj_cparameters_t *parameters) {
                        }
                        break;
                }
                        }
                        break;
                }
-       } else {
+       }
+       else {
                /* just use rect*/
                switch (prec) {
                case 8:
                /* 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;
                                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];
                                        i = y_row + x;
                                
                                        image->comps[0].data[i] = rect[0];
@@ -614,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 */
                        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;
                                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]);
                                        i = y_row + x;
                                
                                        image->comps[0].data[i]= UPSAMPLE_8_TO_12(rect[0]);
@@ -628,9 +645,9 @@ static opj_image_t* ibuftoimage(ImBuf *ibuf, opj_cparameters_t *parameters) {
                        }
                        break;
                case 16:
                        }
                        break;
                case 16:
-                       for(y=h-1; y>=0; y--) {
+                       for (y=h-1; y>=0; y--) {
                                y_row = y*w;
                                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]);
                                        i = y_row + x;
                                
                                        image->comps[0].data[i]= UPSAMPLE_8_TO_16(rect[0]);
@@ -647,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;
        
        /* 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);
        }
        
                cinema_setup_encoder(parameters,image,&img_fol);
        }
        
@@ -659,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 */
 
 
 /* Found write info at http://users.ece.gatech.edu/~slabaugh/personal/c/bitmapUnix.c */
-int 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;
        int quality = ibuf->ftype & 0xff;
        
        int bSuccess;
@@ -668,10 +685,12 @@ int imb_savejp2(struct ImBuf *ibuf, char *name, int flags) {
        opj_event_mgr_t event_mgr;              /* event manager */
        opj_image_t *image = NULL;
        
        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;
        memset(&event_mgr, 0, sizeof(opj_event_mgr_t));
        event_mgr.error_handler = error_callback;
        event_mgr.warning_handler = warning_callback;
@@ -682,7 +701,7 @@ int imb_savejp2(struct ImBuf *ibuf, char *name, int flags) {
        
        /* compression ratio */
        /* invert range, from 10-100, 100-1
        
        /* 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;
 
        
        parameters.tcp_rates[0]= ((100-quality)/90.0f*99.0f) + 1;
 
        
@@ -721,7 +740,7 @@ int imb_savejp2(struct ImBuf *ibuf, char *name, int flags) {
                codestream_length = cio_tell(cio);
 
                /* write the buffer to disk */
                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);
                
                if (!f) {
                        fprintf(stderr, "failed to open %s for writing\n", name);
@@ -742,5 +761,3 @@ int imb_savejp2(struct ImBuf *ibuf, char *name, int flags) {
        
        return 1;
 }
        
        return 1;
 }
-
-#endif /* WITH_OPENJPEG */