Blender V2.27

jp2.c

00001 
00024 #ifdef WITH_OPENJPEG
00025 
00026 #include "MEM_guardedalloc.h"
00027 
00028 #include "BLI_blenlib.h"
00029 #include "BLI_math.h"
00030 
00031 #include "imbuf.h"
00032 
00033 #include "IMB_imbuf_types.h"
00034 #include "IMB_imbuf.h"
00035 #include "IMB_allocimbuf.h"
00036 #include "IMB_filetype.h"
00037 
00038 #include "openjpeg.h"
00039 
00040 #define JP2_FILEHEADER_SIZE 14
00041 
00042 static char JP2_HEAD[]= {0x0, 0x0, 0x0, 0x0C, 0x6A, 0x50, 0x20, 0x20, 0x0D, 0x0A, 0x87, 0x0A};
00043 
00044 /* We only need this because of how the presets are set */
00045 typedef struct img_folder{
00047         char *imgdirpath;
00049         char *out_format;
00051         char set_imgdir;
00053         char set_out_format;
00055         float *rates;
00056 }img_fol_t;
00057 
00058 static int check_jp2(unsigned char *mem) /* J2K_CFMT */
00059 {
00060         return memcmp(JP2_HEAD, mem, 12) ? 0 : 1;
00061 }
00062 
00063 int imb_is_a_jp2(unsigned char *buf)
00064 {       
00065         return check_jp2(buf);
00066 }
00067 
00068 
00072 void error_callback(const char *msg, void *client_data) {
00073         FILE *stream = (FILE*)client_data;
00074         fprintf(stream, "[ERROR] %s", msg);
00075 }
00079 void warning_callback(const char *msg, void *client_data) {
00080         FILE *stream = (FILE*)client_data;
00081         fprintf(stream, "[WARNING] %s", msg);
00082 }
00086 void info_callback(const char *msg, void *client_data) {
00087         (void)client_data;
00088         fprintf(stdout, "[INFO] %s", msg);
00089 }
00090 
00091 
00092 
00093 struct ImBuf *imb_jp2_decode(unsigned char *mem, size_t size, int flags)
00094 {
00095         struct ImBuf *ibuf = 0;
00096         int use_float = 0; /* for precision higher then 8 use float */
00097         
00098         long signed_offsets[4]= {0, 0, 0, 0};
00099         int float_divs[4]= {1, 1, 1, 1};
00100 
00101         int index;
00102         
00103         int w, h, depth;
00104         
00105         opj_dparameters_t parameters;   /* decompression parameters */
00106         
00107         opj_event_mgr_t event_mgr;              /* event manager */
00108         opj_image_t *image = NULL;
00109         
00110         int i;
00111         
00112         opj_dinfo_t* dinfo = NULL;      /* handle to a decompressor */
00113         opj_cio_t *cio = NULL;
00114 
00115         if (check_jp2(mem) == 0) return(0);
00116 
00117         /* configure the event callbacks (not required) */
00118         memset(&event_mgr, 0, sizeof(opj_event_mgr_t));
00119         event_mgr.error_handler = error_callback;
00120         event_mgr.warning_handler = warning_callback;
00121         event_mgr.info_handler = info_callback;
00122 
00123 
00124         /* set decoding parameters to default values */
00125         opj_set_default_decoder_parameters(&parameters);
00126 
00127 
00128         /* JPEG 2000 compressed image data */
00129 
00130         /* get a decoder handle */
00131         dinfo = opj_create_decompress(CODEC_JP2);
00132 
00133         /* catch events using our callbacks and give a local context */
00134         opj_set_event_mgr((opj_common_ptr)dinfo, &event_mgr, stderr);
00135 
00136         /* setup the decoder decoding parameters using the current image and user parameters */
00137         opj_setup_decoder(dinfo, &parameters);
00138 
00139         /* open a byte stream */
00140         cio = opj_cio_open((opj_common_ptr)dinfo, mem, size);
00141 
00142         /* decode the stream and fill the image structure */
00143         image = opj_decode(dinfo, cio);
00144         
00145         if(!image) {
00146                 fprintf(stderr, "ERROR -> j2k_to_image: failed to decode image!\n");
00147                 opj_destroy_decompress(dinfo);
00148                 opj_cio_close(cio);
00149                 return NULL;
00150         }
00151 
00152         /* close the byte stream */
00153         opj_cio_close(cio);
00154 
00155 
00156         if((image->numcomps * image->x1 * image->y1) == 0)
00157         {
00158                 fprintf(stderr,"\nError: invalid raw image parameters\n");
00159                 return NULL;
00160         }
00161         
00162         w = image->comps[0].w;
00163         h = image->comps[0].h;
00164         
00165         switch (image->numcomps) {
00166         case 1: /* Greyscale */
00167         case 3: /* Color */
00168                 depth= 24; 
00169                 break;
00170         default: /* 2 or 4 - Greyscale or Color + alpha */
00171                 depth= 32; /* greyscale + alpha */
00172                 break;
00173         }
00174         
00175         
00176         i = image->numcomps;
00177         if (i>4) i= 4;
00178         
00179         while (i) {
00180                 i--;
00181                 
00182                 if (image->comps[i].prec > 8)
00183                         use_float = 1;
00184                 
00185                 if (image->comps[i].sgnd)
00186                         signed_offsets[i]=  1 << (image->comps[i].prec - 1); 
00187                 
00188                 /* only needed for float images but dosnt hurt to calc this */
00189                 float_divs[i]= (1<<image->comps[i].prec)-1;
00190         }
00191         
00192         ibuf= IMB_allocImBuf(w, h, depth, use_float ? IB_rectfloat : IB_rect);
00193         
00194         if (ibuf==NULL) {
00195                 if(dinfo)
00196                         opj_destroy_decompress(dinfo);
00197                 return NULL;
00198         }
00199         
00200         ibuf->ftype = JP2;
00201         
00202         if (use_float) {
00203                 float *rect_float= ibuf->rect_float;
00204 
00205                 if (image->numcomps < 3) {
00206                         /* greyscale 12bits+ */
00207                         for (i = 0; i < w * h; i++, rect_float+=4) {
00208                                 index = w * h - ((i) / (w) + 1) * w + (i) % (w);
00209                                 
00210                                 rect_float[0]= rect_float[1]= rect_float[2]= (float)(image->comps[0].data[index] + signed_offsets[0]) / float_divs[0];
00211                                 
00212                                 if (image->numcomps == 2)
00213                                         rect_float[3]= (image->comps[1].data[index] + signed_offsets[1]) / float_divs[1];
00214                                 else
00215                                         rect_float[3]= 1.0f;
00216                         }
00217                 } else {
00218                         /* rgb or rgba 12bits+ */
00219                         for (i = 0; i < w * h; i++, rect_float+=4) {
00220                                 index = w * h - ((i) / (w) + 1) * w + (i) % (w);
00221                                 
00222                                 rect_float[0]= (float)(image->comps[0].data[index] + signed_offsets[0]) / float_divs[0];
00223                                 rect_float[1]= (float)(image->comps[1].data[index] + signed_offsets[1]) / float_divs[1];
00224                                 rect_float[2]= (float)(image->comps[2].data[index] + signed_offsets[2]) / float_divs[2];
00225                                 
00226                                 if (image->numcomps >= 4)
00227                                         rect_float[3]= (float)(image->comps[3].data[index] + signed_offsets[3]) / float_divs[3];
00228                                 else
00229                                         rect_float[3]= 1.0f;
00230                         }
00231                 }
00232                 
00233         } else {
00234                 unsigned char *rect= (unsigned char *)ibuf->rect;
00235 
00236                 if (image->numcomps < 3) {
00237                         /* greyscale */
00238                         for (i = 0; i < w * h; i++, rect+=4) {
00239                                 index = w * h - ((i) / (w) + 1) * w + (i) % (w);
00240                                 
00241                                 rect[0]= rect[1]= rect[2]= (image->comps[0].data[index] + signed_offsets[0]);
00242                                 
00243                                 if (image->numcomps == 2)
00244                                         rect[3]= image->comps[1].data[index] + signed_offsets[1];
00245                                 else
00246                                         rect[3]= 255;
00247                         }
00248                 } else {
00249                         /* 8bit rgb or rgba */
00250                         for (i = 0; i < w * h; i++, rect+=4) {
00251                                 int index = w * h - ((i) / (w) + 1) * w + (i) % (w);
00252                                 
00253                                 rect[0]= image->comps[0].data[index] + signed_offsets[0];
00254                                 rect[1]= image->comps[1].data[index] + signed_offsets[1];
00255                                 rect[2]= image->comps[2].data[index] + signed_offsets[2];
00256                                 
00257                                 if (image->numcomps >= 4)
00258                                         rect[3]= image->comps[3].data[index] + signed_offsets[3];
00259                                 else
00260                                         rect[3]= 255;
00261                         }
00262                 }
00263         }
00264         
00265         /* free remaining structures */
00266         if(dinfo) {
00267                 opj_destroy_decompress(dinfo);
00268         }
00269         
00270         /* free image data structure */
00271         opj_image_destroy(image);
00272         
00273         if (flags & IB_rect) {
00274                 IMB_rect_from_float(ibuf);
00275         }
00276         
00277         return(ibuf);
00278 }
00279 
00280 //static opj_image_t* rawtoimage(const char *filename, opj_cparameters_t *parameters, raw_cparameters_t *raw_cp) {
00281 /* prec can be 8, 12, 16 */
00282 
00283 #define UPSAMPLE_8_TO_12(_val) ((_val<<4) | (_val & ((1<<4)-1)))
00284 #define UPSAMPLE_8_TO_16(_val) ((_val<<8)+_val)
00285 
00286 #define DOWNSAMPLE_FLOAT_TO_8BIT(_val)  (_val)<=0.0f?0: ((_val)>=1.0f?255: (int)(255.0f*(_val)))
00287 #define DOWNSAMPLE_FLOAT_TO_12BIT(_val) (_val)<=0.0f?0: ((_val)>=1.0f?4095: (int)(4095.0f*(_val)))
00288 #define DOWNSAMPLE_FLOAT_TO_16BIT(_val) (_val)<=0.0f?0: ((_val)>=1.0f?65535: (int)(65535.0f*(_val)))
00289 
00290 
00291 /*
00292 2048x1080 (2K) at 24 fps or 48 fps, or 4096x2160 (4K) at 24 fps; 3x12 bits per pixel, XYZ color space
00293 
00294         * In 2K, for Scope (2.39:1) presentation 2048x858 pixels of the imager is used
00295         * In 2K, for Flat (1.85:1) presentation 1998x1080 pixels of the imager is used
00296 */
00297 
00298 /* ****************************** COPIED FROM image_to_j2k.c */
00299 
00300 /* ----------------------------------------------------------------------- */
00301 #define CINEMA_24_CS 1302083    /*Codestream length for 24fps*/
00302 #define CINEMA_48_CS 651041             /*Codestream length for 48fps*/
00303 #define COMP_24_CS 1041666              /*Maximum size per color component for 2K & 4K @ 24fps*/
00304 #define COMP_48_CS 520833               /*Maximum size per color component for 2K @ 48fps*/
00305 
00306 
00307 static int initialise_4K_poc(opj_poc_t *POC, int numres){
00308         POC[0].tile  = 1; 
00309         POC[0].resno0  = 0; 
00310         POC[0].compno0 = 0;
00311         POC[0].layno1  = 1;
00312         POC[0].resno1  = numres-1;
00313         POC[0].compno1 = 3;
00314         POC[0].prg1 = CPRL;
00315         POC[1].tile  = 1;
00316         POC[1].resno0  = numres-1; 
00317         POC[1].compno0 = 0;
00318         POC[1].layno1  = 1;
00319         POC[1].resno1  = numres;
00320         POC[1].compno1 = 3;
00321         POC[1].prg1 = CPRL;
00322         return 2;
00323 }
00324 
00325 void cinema_parameters(opj_cparameters_t *parameters){
00326         parameters->tile_size_on = false;
00327         parameters->cp_tdx=1;
00328         parameters->cp_tdy=1;
00329         
00330         /*Tile part*/
00331         parameters->tp_flag = 'C';
00332         parameters->tp_on = 1;
00333 
00334         /*Tile and Image shall be at (0,0)*/
00335         parameters->cp_tx0 = 0;
00336         parameters->cp_ty0 = 0;
00337         parameters->image_offset_x0 = 0;
00338         parameters->image_offset_y0 = 0;
00339 
00340         /*Codeblock size= 32*32*/
00341         parameters->cblockw_init = 32;  
00342         parameters->cblockh_init = 32;
00343         parameters->csty |= 0x01;
00344 
00345         /*The progression order shall be CPRL*/
00346         parameters->prog_order = CPRL;
00347 
00348         /* No ROI */
00349         parameters->roi_compno = -1;
00350 
00351         parameters->subsampling_dx = 1;         parameters->subsampling_dy = 1;
00352 
00353         /* 9-7 transform */
00354         parameters->irreversible = 1;
00355 
00356 }
00357 
00358 void cinema_setup_encoder(opj_cparameters_t *parameters,opj_image_t *image, img_fol_t *img_fol){
00359         int i;
00360         float temp_rate;
00361 
00362         switch (parameters->cp_cinema){
00363         case CINEMA2K_24:
00364         case CINEMA2K_48:
00365                 if(parameters->numresolution > 6){
00366                         parameters->numresolution = 6;
00367                 }
00368                 if (!((image->comps[0].w == 2048) || (image->comps[0].h == 1080))){
00369                         fprintf(stdout,"Image coordinates %d x %d is not 2K compliant.\nJPEG Digital Cinema Profile-3 "
00370                                 "(2K profile) compliance requires that at least one of coordinates match 2048 x 1080\n",
00371                                 image->comps[0].w,image->comps[0].h);
00372                         parameters->cp_rsiz = STD_RSIZ;
00373                 }
00374         break;
00375         
00376         case CINEMA4K_24:
00377                 if(parameters->numresolution < 1){
00378                                 parameters->numresolution = 1;
00379                         }else if(parameters->numresolution > 7){
00380                                 parameters->numresolution = 7;
00381                         }
00382                 if (!((image->comps[0].w == 4096) || (image->comps[0].h == 2160))){
00383                         fprintf(stdout,"Image coordinates %d x %d is not 4K compliant.\nJPEG Digital Cinema Profile-4" 
00384                                 "(4K profile) compliance requires that at least one of coordinates match 4096 x 2160\n",
00385                                 image->comps[0].w,image->comps[0].h);
00386                         parameters->cp_rsiz = STD_RSIZ;
00387                 }
00388                 parameters->numpocs = initialise_4K_poc(parameters->POC,parameters->numresolution);
00389                 break;
00390         case OFF:
00391                 /* do nothing */
00392                 break;
00393         }
00394 
00395         switch (parameters->cp_cinema){
00396         case CINEMA2K_24:
00397         case CINEMA4K_24:
00398                 for(i=0 ; i<parameters->tcp_numlayers ; i++){
00399                         temp_rate = 0 ;
00400                         if (img_fol->rates[i]== 0){
00401                                 parameters->tcp_rates[0]= ((float) (image->numcomps * image->comps[0].w * image->comps[0].h * image->comps[0].prec))/ 
00402                                         (CINEMA_24_CS * 8 * image->comps[0].dx * image->comps[0].dy);
00403                         }else{
00404                                 temp_rate =((float) (image->numcomps * image->comps[0].w * image->comps[0].h * image->comps[0].prec))/ 
00405                                         (img_fol->rates[i] * 8 * image->comps[0].dx * image->comps[0].dy);
00406                                 if (temp_rate > CINEMA_24_CS ){
00407                                         parameters->tcp_rates[i]= ((float) (image->numcomps * image->comps[0].w * image->comps[0].h * image->comps[0].prec))/ 
00408                                                 (CINEMA_24_CS * 8 * image->comps[0].dx * image->comps[0].dy);
00409                                 }else{
00410                                         parameters->tcp_rates[i]= img_fol->rates[i];
00411                                 }
00412                         }
00413                 }
00414                 parameters->max_comp_size = COMP_24_CS;
00415                 break;
00416                 
00417         case CINEMA2K_48:
00418                 for(i=0 ; i<parameters->tcp_numlayers ; i++){
00419                         temp_rate = 0 ;
00420                         if (img_fol->rates[i]== 0){
00421                                 parameters->tcp_rates[0]= ((float) (image->numcomps * image->comps[0].w * image->comps[0].h * image->comps[0].prec))/ 
00422                                         (CINEMA_48_CS * 8 * image->comps[0].dx * image->comps[0].dy);
00423                         }else{
00424                                 temp_rate =((float) (image->numcomps * image->comps[0].w * image->comps[0].h * image->comps[0].prec))/ 
00425                                         (img_fol->rates[i] * 8 * image->comps[0].dx * image->comps[0].dy);
00426                                 if (temp_rate > CINEMA_48_CS ){
00427                                         parameters->tcp_rates[0]= ((float) (image->numcomps * image->comps[0].w * image->comps[0].h * image->comps[0].prec))/ 
00428                                                 (CINEMA_48_CS * 8 * image->comps[0].dx * image->comps[0].dy);
00429                                 }else{
00430                                         parameters->tcp_rates[i]= img_fol->rates[i];
00431                                 }
00432                         }
00433                 }
00434                 parameters->max_comp_size = COMP_48_CS;
00435                 break;
00436         case OFF:
00437                 /* do nothing */
00438                 break;
00439         }
00440         parameters->cp_disto_alloc = 1;
00441 }
00442 
00443 
00444 static opj_image_t* ibuftoimage(ImBuf *ibuf, opj_cparameters_t *parameters) {
00445         
00446         unsigned char *rect;
00447         float *rect_float;
00448         
00449         int subsampling_dx = parameters->subsampling_dx;
00450         int subsampling_dy = parameters->subsampling_dy;
00451         
00452 
00453         int i, numcomps, w, h, prec;
00454         int x,y, y_row;
00455         OPJ_COLOR_SPACE color_space;
00456         opj_image_cmptparm_t cmptparm[4];       /* maximum of 4 components */
00457         opj_image_t * image = NULL;
00458         
00459         img_fol_t img_fol; /* only needed for cinema presets */
00460         memset(&img_fol,0,sizeof(img_fol_t));
00461         
00462         if (ibuf->ftype & JP2_CINE) {
00463                 
00464                 if (ibuf->x==4096 || ibuf->y==2160)
00465                         parameters->cp_cinema= CINEMA4K_24;
00466                 else {
00467                         if (ibuf->ftype & JP2_CINE_48FPS) {
00468                                 parameters->cp_cinema= CINEMA2K_48;
00469                         }
00470                         else {
00471                                 parameters->cp_cinema= CINEMA2K_24;
00472                         }
00473                 }
00474                 if (parameters->cp_cinema){
00475                         img_fol.rates = (float*)MEM_mallocN(parameters->tcp_numlayers * sizeof(float), "jp2_rates");
00476                         for(i=0; i< parameters->tcp_numlayers; i++){
00477                                 img_fol.rates[i] = parameters->tcp_rates[i];
00478                         }
00479                         cinema_parameters(parameters);
00480                 }
00481                 
00482                 color_space= CLRSPC_SYCC;
00483                 prec= 12;
00484                 numcomps= 3;
00485         }
00486         else { 
00487                 /* Get settings from the imbuf */
00488                 color_space = (ibuf->ftype & JP2_YCC) ? CLRSPC_SYCC : CLRSPC_SRGB;
00489                 
00490                 if (ibuf->ftype & JP2_16BIT)            prec= 16;
00491                 else if (ibuf->ftype & JP2_12BIT)       prec= 12;
00492                 else                                            prec= 8;
00493                 
00494                 /* 32bit images == alpha channel */
00495                 /* grayscale not supported yet */
00496                 numcomps= (ibuf->depth==32) ? 4 : 3;
00497         }
00498         
00499         w= ibuf->x;
00500         h= ibuf->y;
00501         
00502         
00503         /* initialize image components */
00504         memset(&cmptparm[0], 0, 4 * sizeof(opj_image_cmptparm_t));
00505         for(i = 0; i < numcomps; i++) {
00506                 cmptparm[i].prec = prec;
00507                 cmptparm[i].bpp = prec;
00508                 cmptparm[i].sgnd = 0;
00509                 cmptparm[i].dx = subsampling_dx;
00510                 cmptparm[i].dy = subsampling_dy;
00511                 cmptparm[i].w = w;
00512                 cmptparm[i].h = h;
00513         }
00514         /* create the image */
00515         image = opj_image_create(numcomps, &cmptparm[0], color_space);
00516         if(!image) {
00517                 printf("Error: opj_image_create() failed\n");
00518                 return NULL;
00519         }
00520 
00521         /* set image offset and reference grid */
00522         image->x0 = parameters->image_offset_x0;
00523         image->y0 = parameters->image_offset_y0;
00524         image->x1 = parameters->image_offset_x0 + (w - 1) *     subsampling_dx + 1;
00525         image->y1 = parameters->image_offset_y0 + (h - 1) *     subsampling_dy + 1;
00526         
00527         /* set image data */
00528         rect = (unsigned char*) ibuf->rect;
00529         rect_float= ibuf->rect_float;
00530         
00531         if (rect_float && rect && prec==8) {
00532                 /* No need to use the floating point buffer, just write the 8 bits from the char buffer */
00533                 rect_float= NULL;
00534         }
00535         
00536         
00537         if (rect_float) {
00538                 float rgb[3];
00539                 
00540                 switch (prec) {
00541                 case 8: /* Convert blenders float color channels to 8,12 or 16bit ints */
00542                         for(y=h-1; y>=0; y--) {
00543                                 y_row = y*w;
00544                                 for(x=0; x<w; x++, rect_float+=4) {
00545                                         i = y_row + x;
00546                                         
00547                                         if (ibuf->profile == IB_PROFILE_LINEAR_RGB)
00548                                                 linearrgb_to_srgb_v3_v3(rgb, rect_float);
00549                                         else
00550                                                 copy_v3_v3(rgb, rect_float);
00551                                 
00552                                         image->comps[0].data[i] = DOWNSAMPLE_FLOAT_TO_8BIT(rgb[0]);
00553                                         image->comps[1].data[i] = DOWNSAMPLE_FLOAT_TO_8BIT(rgb[1]);
00554                                         image->comps[2].data[i] = DOWNSAMPLE_FLOAT_TO_8BIT(rgb[2]);
00555                                         if (numcomps>3)
00556                                                 image->comps[3].data[i] = DOWNSAMPLE_FLOAT_TO_8BIT(rect_float[3]);
00557                                 }
00558                         }
00559                         break;
00560                         
00561                 case 12:
00562                         for(y=h-1; y>=0; y--) {
00563                                 y_row = y*w;
00564                                 for(x=0; x<w; x++, rect_float+=4) {
00565                                         i = y_row + x;
00566                                         
00567                                         if (ibuf->profile == IB_PROFILE_LINEAR_RGB)
00568                                                 linearrgb_to_srgb_v3_v3(rgb, rect_float);
00569                                         else
00570                                                 copy_v3_v3(rgb, rect_float);
00571                                 
00572                                         image->comps[0].data[i] = DOWNSAMPLE_FLOAT_TO_12BIT(rgb[0]);
00573                                         image->comps[1].data[i] = DOWNSAMPLE_FLOAT_TO_12BIT(rgb[1]);
00574                                         image->comps[2].data[i] = DOWNSAMPLE_FLOAT_TO_12BIT(rgb[2]);
00575                                         if (numcomps>3)
00576                                                 image->comps[3].data[i] = DOWNSAMPLE_FLOAT_TO_12BIT(rect_float[3]);
00577                                 }
00578                         }
00579                         break;
00580                 case 16:
00581                         for(y=h-1; y>=0; y--) {
00582                                 y_row = y*w;
00583                                 for(x=0; x<w; x++, rect_float+=4) {
00584                                         i = y_row + x;
00585                                         
00586                                         if (ibuf->profile == IB_PROFILE_LINEAR_RGB)
00587                                                 linearrgb_to_srgb_v3_v3(rgb, rect_float);
00588                                         else
00589                                                 copy_v3_v3(rgb, rect_float);
00590                                 
00591                                         image->comps[0].data[i] = DOWNSAMPLE_FLOAT_TO_16BIT(rgb[0]);
00592                                         image->comps[1].data[i] = DOWNSAMPLE_FLOAT_TO_16BIT(rgb[1]);
00593                                         image->comps[2].data[i] = DOWNSAMPLE_FLOAT_TO_16BIT(rgb[2]);
00594                                         if (numcomps>3)
00595                                                 image->comps[3].data[i] = DOWNSAMPLE_FLOAT_TO_16BIT(rect_float[3]);
00596                                 }
00597                         }
00598                         break;
00599                 }
00600         } else {
00601                 /* just use rect*/
00602                 switch (prec) {
00603                 case 8:
00604                         for(y=h-1; y>=0; y--) {
00605                                 y_row = y*w;
00606                                 for(x=0; x<w; x++, rect+=4) {
00607                                         i = y_row + x;
00608                                 
00609                                         image->comps[0].data[i] = rect[0];
00610                                         image->comps[1].data[i] = rect[1];
00611                                         image->comps[2].data[i] = rect[2];
00612                                         if (numcomps>3)
00613                                                 image->comps[3].data[i] = rect[3];
00614                                 }
00615                         }
00616                         break;
00617                         
00618                 case 12: /* Up Sampling, a bit pointless but best write the bit depth requested */
00619                         for(y=h-1; y>=0; y--) {
00620                                 y_row = y*w;
00621                                 for(x=0; x<w; x++, rect+=4) {
00622                                         i = y_row + x;
00623                                 
00624                                         image->comps[0].data[i]= UPSAMPLE_8_TO_12(rect[0]);
00625                                         image->comps[1].data[i]= UPSAMPLE_8_TO_12(rect[1]);
00626                                         image->comps[2].data[i]= UPSAMPLE_8_TO_12(rect[2]);
00627                                         if (numcomps>3)
00628                                                 image->comps[3].data[i]= UPSAMPLE_8_TO_12(rect[3]);
00629                                 }
00630                         }
00631                         break;
00632                 case 16:
00633                         for(y=h-1; y>=0; y--) {
00634                                 y_row = y*w;
00635                                 for(x=0; x<w; x++, rect+=4) {
00636                                         i = y_row + x;
00637                                 
00638                                         image->comps[0].data[i]= UPSAMPLE_8_TO_16(rect[0]);
00639                                         image->comps[1].data[i]= UPSAMPLE_8_TO_16(rect[1]);
00640                                         image->comps[2].data[i]= UPSAMPLE_8_TO_16(rect[2]);
00641                                         if (numcomps>3)
00642                                                 image->comps[3].data[i]= UPSAMPLE_8_TO_16(rect[3]);
00643                                 }
00644                         }
00645                         break;
00646                 }
00647         }
00648         
00649         /* Decide if MCT should be used */
00650         parameters->tcp_mct = image->numcomps == 3 ? 1 : 0;
00651         
00652         if(parameters->cp_cinema){
00653                 cinema_setup_encoder(parameters,image,&img_fol);
00654         }
00655         
00656         if (img_fol.rates)
00657                 MEM_freeN(img_fol.rates);
00658         
00659         return image;
00660 }
00661 
00662 
00663 /* Found write info at http://users.ece.gatech.edu/~slabaugh/personal/c/bitmapUnix.c */
00664 int imb_savejp2(struct ImBuf *ibuf, const char *name, int flags) {
00665         
00666         int quality = ibuf->ftype & 0xff;
00667         
00668         int bSuccess;
00669         opj_cparameters_t parameters;   /* compression parameters */
00670         opj_event_mgr_t event_mgr;              /* event manager */
00671         opj_image_t *image = NULL;
00672         
00673         (void)flags; /* unused */
00674         
00675         /*
00676         configure the event callbacks (not required)
00677         setting of each callback is optionnal
00678         */
00679         memset(&event_mgr, 0, sizeof(opj_event_mgr_t));
00680         event_mgr.error_handler = error_callback;
00681         event_mgr.warning_handler = warning_callback;
00682         event_mgr.info_handler = info_callback;
00683         
00684         /* set encoding parameters to default values */
00685         opj_set_default_encoder_parameters(&parameters);
00686         
00687         /* compression ratio */
00688         /* invert range, from 10-100, 100-1
00689         * where jpeg see's 1 and highest quality (lossless) and 100 is very low quality*/
00690         parameters.tcp_rates[0]= ((100-quality)/90.0f*99.0f) + 1;
00691 
00692         
00693         parameters.tcp_numlayers = 1; // only one resolution
00694         parameters.cp_disto_alloc = 1;
00695 
00696         image= ibuftoimage(ibuf, &parameters);
00697         
00698         
00699         {                       /* JP2 format output */
00700                 int codestream_length;
00701                 opj_cio_t *cio = NULL;
00702                 FILE *f = NULL;
00703 
00704                 /* get a JP2 compressor handle */
00705                 opj_cinfo_t* cinfo = opj_create_compress(CODEC_JP2);
00706 
00707                 /* catch events using our callbacks and give a local context */
00708                 opj_set_event_mgr((opj_common_ptr)cinfo, &event_mgr, stderr);                   
00709 
00710                 /* setup the encoder parameters using the current image and using user parameters */
00711                 opj_setup_encoder(cinfo, &parameters, image);
00712 
00713                 /* open a byte stream for writing */
00714                 /* allocate memory for all tiles */
00715                 cio = opj_cio_open((opj_common_ptr)cinfo, NULL, 0);
00716 
00717                 /* encode the image */
00718                 bSuccess = opj_encode(cinfo, cio, image, NULL); /* last arg used to be parameters.index but this deprecated */
00719                 
00720                 if (!bSuccess) {
00721                         opj_cio_close(cio);
00722                         fprintf(stderr, "failed to encode image\n");
00723                         return 0;
00724                 }
00725                 codestream_length = cio_tell(cio);
00726 
00727                 /* write the buffer to disk */
00728                 f = fopen(name, "wb");
00729                 
00730                 if (!f) {
00731                         fprintf(stderr, "failed to open %s for writing\n", name);
00732                         return 1;
00733                 }
00734                 fwrite(cio->buffer, 1, codestream_length, f);
00735                 fclose(f);
00736                 fprintf(stderr,"Generated outfile %s\n",name);
00737                 /* close and free the byte stream */
00738                 opj_cio_close(cio);
00739                 
00740                 /* free remaining compression structures */
00741                 opj_destroy_compress(cinfo);
00742         }
00743 
00744         /* free image data */
00745         opj_image_destroy(image);
00746         
00747         return 1;
00748 }
00749 
00750 #endif /* WITH_OPENJPEG */