Blender V2.27

cineon_dpx.c

00001 
00033 #include <stdio.h>
00034 #include <string.h> /*for memcpy*/
00035 
00036 #include "logImageLib.h"
00037 #include "cineonlib.h"
00038 #include "dpxlib.h"
00039 
00040 #include "IMB_imbuf_types.h"
00041 #include "IMB_imbuf.h"
00042 
00043 #include "BKE_global.h"
00044 
00045 #include "MEM_guardedalloc.h"
00046 
00047 #if 0
00048 static void cineon_conversion_parameters(LogImageByteConversionParameters *params)
00049 {
00050 //      params->blackPoint = scene?scene->r.cineonblack:95;
00051 //      params->whitePoint = scene?scene->r.cineonwhite:685;
00052 //      params->gamma = scene?scene->r.cineongamma:1.7f;
00053 //      params->doLogarithm = scene?scene->r.subimtype & R_CINEON_LOG:0;
00054         
00055         params->blackPoint = 95;
00056         params->whitePoint = 685;
00057         params->gamma = 1.0f;
00058         params->doLogarithm = 0;
00059 }
00060 #endif
00061 
00062 static struct ImBuf *imb_load_dpx_cineon(unsigned char *mem, int use_cineon, int size, int flags)
00063 {
00064         ImBuf *ibuf;
00065         LogImageFile *image;
00066         int x, y;
00067         unsigned short *row, *upix;
00068         int width, height, depth;
00069         float *frow;
00070 
00071         logImageSetVerbose((G.f & G_DEBUG) ? 1:0);
00072         
00073         image = logImageOpenFromMem(mem, size, use_cineon);
00074         
00075         if (!image) {
00076                 printf("no image!\n");
00077                 return NULL;
00078         }
00079         
00080         logImageGetSize(image, &width, &height, &depth);
00081         
00082         if (depth != 3) { /*need to do greyscale loading eventually.*/
00083                 logImageClose(image);
00084                 return NULL;
00085         }
00086         
00087         if (width == 0 && height == 0) {
00088                 logImageClose(image);
00089                 return NULL;
00090         }
00091         
00092         ibuf = IMB_allocImBuf(width, height, 32, IB_rectfloat | flags);
00093 
00094         row = MEM_mallocN(sizeof(unsigned short)*width*depth, "row in cineon_dpx.c");
00095         frow = ibuf->rect_float+width*height*4;
00096         
00097         for (y = 0; y < height; y++) {
00098                 logImageGetRowBytes(image, row, y); /* checks image->params.doLogarithm and convert */
00099                 upix = row;
00100                 frow -= width*4;
00101                 
00102                 for (x=0; x<width; x++) {
00103                         *(frow++) = ((float)*(upix++)) / 65535.0f;
00104                         *(frow++) = ((float)*(upix++)) / 65535.0f;
00105                         *(frow++) = ((float)*(upix++)) / 65535.0f;
00106                         *(frow++) = 1.0f;
00107                 }
00108                 frow -= width*4;
00109         }
00110 
00111         MEM_freeN(row);
00112         logImageClose(image);
00113         
00114         if (flags & IB_rect) {
00115                 IMB_rect_from_float(ibuf);
00116         }
00117         return ibuf;
00118 }
00119 
00120 static int imb_save_dpx_cineon(ImBuf *ibuf, const char *filename, int use_cineon, int flags)
00121 {
00122         LogImageByteConversionParameters conversion;
00123         const int width= ibuf->x;
00124         const int height= ibuf->y;
00125         const int depth= 3;
00126         LogImageFile* logImage;
00127         unsigned short* line, *pixel;
00128         int i, j;
00129         float *fline;
00130         float *fbuf;
00131         int is_alloc= 0;
00132         
00133         (void)flags; /* unused */
00134 
00135         // cineon_conversion_parameters(&conversion);
00136         logImageGetByteConversionDefaults(&conversion);
00137 
00138         /*
00139          * Get the drawable for the current image...
00140          */
00141 
00142         fbuf= IMB_float_profile_ensure(ibuf, conversion.doLogarithm ? IB_PROFILE_LINEAR_RGB : IB_PROFILE_NONE, &is_alloc);
00143 
00144         if (fbuf == NULL) { /* in the unlikely event that converting to a float buffer fails */
00145                 return 0;
00146         }
00147         
00148         logImageSetVerbose((G.f & G_DEBUG) ? 1:0);
00149         logImage = logImageCreate(filename, use_cineon, width, height, depth);
00150 
00151         if (!logImage) return 0;
00152         
00153         if(logImageSetByteConversion(logImage, &conversion)==0) {
00154                 printf("error setting args\n");
00155         }
00156 
00157         line = MEM_mallocN(sizeof(unsigned short)*depth*width, "line");
00158         
00159         /*note that image is flipped when sent to logImageSetRowBytes (see last passed parameter).*/
00160         for (j = 0; j < height; ++j) {
00161                 fline = &fbuf[width*j*4];
00162                 for (i=0; i<width; i++) {
00163                         float *fpix, fpix2[3];
00164                         /*we have to convert to cinepaint's 16-bit-per-channel here*/
00165                         pixel = &line[i*depth];
00166                         fpix = &fline[i*4];
00167                         memcpy(fpix2, fpix, sizeof(float)*3);
00168                         
00169                         if (fpix2[0]>=1.0f) fpix2[0] = 1.0f; else if (fpix2[0]<0.0f) fpix2[0]= 0.0f;
00170                         if (fpix2[1]>=1.0f) fpix2[1] = 1.0f; else if (fpix2[1]<0.0f) fpix2[1]= 0.0f;
00171                         if (fpix2[2]>=1.0f) fpix2[2] = 1.0f; else if (fpix2[2]<0.0f) fpix2[2]= 0.0f;
00172                         
00173                         pixel[0] = (unsigned short)(fpix2[0] * 65535.0f); /*float-float math is faster*/
00174                         pixel[1] = (unsigned short)(fpix2[1] * 65535.0f);
00175                         pixel[2] = (unsigned short)(fpix2[2] * 65535.0f);
00176                 }
00177                 logImageSetRowBytes(logImage, (const unsigned short*)line, height-1-j);
00178         }
00179         logImageClose(logImage);
00180 
00181         MEM_freeN(line);
00182         
00183         if(is_alloc) {
00184                 MEM_freeN(fbuf);
00185         }
00186         
00187         return 1;
00188 }
00189 
00190 short imb_savecineon(struct ImBuf *buf, const char *myfile, int flags)
00191 {
00192         return imb_save_dpx_cineon(buf, myfile, 1, flags);
00193 }
00194 
00195  
00196 int imb_is_cineon(unsigned char *buf)
00197 {
00198         return cineonIsMemFileCineon(buf);
00199 }
00200 
00201 ImBuf *imb_loadcineon(unsigned char *mem, int size, int flags)
00202 {
00203         if(imb_is_cineon(mem))
00204                 return imb_load_dpx_cineon(mem, 1, size, flags);
00205         return NULL;
00206 }
00207 
00208 short imb_save_dpx(struct ImBuf *buf, const char *myfile, int flags)
00209 {
00210         return imb_save_dpx_cineon(buf, myfile, 0, flags);
00211 }
00212 
00213 int imb_is_dpx(unsigned char *buf)
00214 {
00215         return dpxIsMemFileCineon(buf);
00216 }
00217 
00218 ImBuf *imb_loaddpx(unsigned char *mem, int size, int flags)
00219 {
00220         if(imb_is_dpx(mem))
00221                 return imb_load_dpx_cineon(mem, 0, size, flags);
00222         return NULL;
00223 }