Blender V2.27

divers.c

00001 
00032 #include "BLI_blenlib.h"
00033 #include "BLI_rand.h"
00034 #include "BLI_math.h"
00035 #include "BLI_utildefines.h"
00036 
00037 #include "imbuf.h"
00038 #include "IMB_imbuf_types.h"
00039 #include "IMB_imbuf.h"
00040 #include "IMB_allocimbuf.h"
00041 
00042 #include "BKE_colortools.h"
00043 
00044 #include "MEM_guardedalloc.h"
00045 
00046 void IMB_de_interlace(struct ImBuf *ibuf)
00047 {
00048         struct ImBuf * tbuf1, * tbuf2;
00049         
00050         if (ibuf == 0) return;
00051         if (ibuf->flags & IB_fields) return;
00052         ibuf->flags |= IB_fields;
00053         
00054         if (ibuf->rect) {
00055                 /* make copies */
00056                 tbuf1 = IMB_allocImBuf(ibuf->x, ibuf->y / 2, 32, IB_rect);
00057                 tbuf2 = IMB_allocImBuf(ibuf->x, ibuf->y / 2, 32, IB_rect);
00058                 
00059                 ibuf->x *= 2;   
00060                 IMB_rectcpy(tbuf1, ibuf, 0, 0, 0, 0, ibuf->x, ibuf->y);
00061                 IMB_rectcpy(tbuf2, ibuf, 0, 0, tbuf2->x, 0, ibuf->x, ibuf->y);
00062         
00063                 ibuf->x /= 2;
00064                 IMB_rectcpy(ibuf, tbuf1, 0, 0, 0, 0, tbuf1->x, tbuf1->y);
00065                 IMB_rectcpy(ibuf, tbuf2, 0, tbuf2->y, 0, 0, tbuf2->x, tbuf2->y);
00066                 
00067                 IMB_freeImBuf(tbuf1);
00068                 IMB_freeImBuf(tbuf2);
00069         }
00070         ibuf->y /= 2;
00071 }
00072 
00073 void IMB_interlace(struct ImBuf *ibuf)
00074 {
00075         struct ImBuf * tbuf1, * tbuf2;
00076 
00077         if (ibuf == 0) return;
00078         ibuf->flags &= ~IB_fields;
00079 
00080         ibuf->y *= 2;
00081 
00082         if (ibuf->rect) {
00083                 /* make copies */
00084                 tbuf1 = IMB_allocImBuf(ibuf->x, ibuf->y / 2, 32, IB_rect);
00085                 tbuf2 = IMB_allocImBuf(ibuf->x, ibuf->y / 2, 32, IB_rect);
00086 
00087                 IMB_rectcpy(tbuf1, ibuf, 0, 0, 0, 0, ibuf->x, ibuf->y);
00088                 IMB_rectcpy(tbuf2, ibuf, 0, 0, 0, tbuf2->y, ibuf->x, ibuf->y);
00089 
00090                 ibuf->x *= 2;
00091                 IMB_rectcpy(ibuf, tbuf1, 0, 0, 0, 0, tbuf1->x, tbuf1->y);
00092                 IMB_rectcpy(ibuf, tbuf2, tbuf2->x, 0, 0, 0, tbuf2->x, tbuf2->y);
00093                 ibuf->x /= 2;
00094 
00095                 IMB_freeImBuf(tbuf1);
00096                 IMB_freeImBuf(tbuf2);
00097         }
00098 }
00099 
00100 
00101 /* assume converting from linear float to sRGB byte */
00102 void IMB_rect_from_float(struct ImBuf *ibuf)
00103 {
00104         /* quick method to convert floatbuf to byte */
00105         float *tof = (float *)ibuf->rect_float;
00106 //      int do_dither = ibuf->dither != 0.f;
00107         float dither= ibuf->dither / 255.0;
00108         float srgb[4];
00109         int i, channels= ibuf->channels;
00110         short profile= ibuf->profile;
00111         unsigned char *to = (unsigned char *) ibuf->rect;
00112         
00113         if(tof==NULL) return;
00114         if(to==NULL) {
00115                 imb_addrectImBuf(ibuf);
00116                 to = (unsigned char *) ibuf->rect;
00117         }
00118         
00119         if(channels==1) {
00120                 for (i = ibuf->x * ibuf->y; i > 0; i--, to+=4, tof++)
00121                         to[1]= to[2]= to[3]= to[0] = FTOCHAR(tof[0]);
00122         }
00123         else if (profile == IB_PROFILE_LINEAR_RGB) {
00124                 if(channels == 3) {
00125                         for (i = ibuf->x * ibuf->y; i > 0; i--, to+=4, tof+=3) {
00126                                 srgb[0]= linearrgb_to_srgb(tof[0]);
00127                                 srgb[1]= linearrgb_to_srgb(tof[1]);
00128                                 srgb[2]= linearrgb_to_srgb(tof[2]);
00129 
00130                                 to[0] = FTOCHAR(srgb[0]);
00131                                 to[1] = FTOCHAR(srgb[1]);
00132                                 to[2] = FTOCHAR(srgb[2]);
00133                                 to[3] = 255;
00134                         }
00135                 }
00136                 else if (channels == 4) {
00137                         if (dither != 0.f) {
00138                                 for (i = ibuf->x * ibuf->y; i > 0; i--, to+=4, tof+=4) {
00139                                         const float d = (BLI_frand()-0.5)*dither;
00140                                         
00141                                         srgb[0]= d + linearrgb_to_srgb(tof[0]);
00142                                         srgb[1]= d + linearrgb_to_srgb(tof[1]);
00143                                         srgb[2]= d + linearrgb_to_srgb(tof[2]);
00144                                         srgb[3]= d + tof[3]; 
00145                                         
00146                                         to[0] = FTOCHAR(srgb[0]);
00147                                         to[1] = FTOCHAR(srgb[1]);
00148                                         to[2] = FTOCHAR(srgb[2]);
00149                                         to[3] = FTOCHAR(srgb[3]);
00150                                 }
00151                         } else {
00152                                 floatbuf_to_srgb_byte(tof, to, 0, ibuf->x, 0, ibuf->y, ibuf->x);
00153                         }
00154                 }
00155         }
00156         else if(ELEM(profile, IB_PROFILE_NONE, IB_PROFILE_SRGB)) {
00157                 if(channels==3) {
00158                         for (i = ibuf->x * ibuf->y; i > 0; i--, to+=4, tof+=3) {
00159                                 to[0] = FTOCHAR(tof[0]);
00160                                 to[1] = FTOCHAR(tof[1]);
00161                                 to[2] = FTOCHAR(tof[2]);
00162                                 to[3] = 255;
00163                         }
00164                 }
00165                 else {
00166                         if (dither != 0.f) {
00167                                 for (i = ibuf->x * ibuf->y; i > 0; i--, to+=4, tof+=4) {
00168                                         const float d = (BLI_frand()-0.5)*dither;
00169                                         float col[4];
00170 
00171                                         col[0]= d + tof[0];
00172                                         col[1]= d + tof[1];
00173                                         col[2]= d + tof[2];
00174                                         col[3]= d + tof[3];
00175 
00176                                         to[0] = FTOCHAR(col[0]);
00177                                         to[1] = FTOCHAR(col[1]);
00178                                         to[2] = FTOCHAR(col[2]);
00179                                         to[3] = FTOCHAR(col[3]);
00180                                 }
00181                         } else {
00182                                 for (i = ibuf->x * ibuf->y; i > 0; i--, to+=4, tof+=4) {
00183                                         to[0] = FTOCHAR(tof[0]);
00184                                         to[1] = FTOCHAR(tof[1]);
00185                                         to[2] = FTOCHAR(tof[2]);
00186                                         to[3] = FTOCHAR(tof[3]);
00187                                 }
00188                         }
00189                 }
00190         }
00191         /* ensure user flag is reset */
00192         ibuf->userflags &= ~IB_RECT_INVALID;
00193 }
00194 
00195 static void imb_float_from_rect_nonlinear(struct ImBuf *ibuf, float *fbuf)
00196 {
00197         float *tof = fbuf;
00198         int i;
00199         unsigned char *to = (unsigned char *) ibuf->rect;
00200 
00201         for (i = ibuf->x * ibuf->y; i > 0; i--) 
00202         {
00203                 tof[0] = ((float)to[0])*(1.0f/255.0f);
00204                 tof[1] = ((float)to[1])*(1.0f/255.0f);
00205                 tof[2] = ((float)to[2])*(1.0f/255.0f);
00206                 tof[3] = ((float)to[3])*(1.0f/255.0f);
00207                 to += 4; 
00208                 tof += 4;
00209         }
00210 }
00211 
00212 
00213 static void imb_float_from_rect_linear(struct ImBuf *ibuf, float *fbuf)
00214 {
00215         float *tof = fbuf;
00216         int i;
00217         unsigned char *to = (unsigned char *) ibuf->rect;
00218 
00219         for (i = ibuf->x * ibuf->y; i > 0; i--) 
00220         {
00221                 tof[0] = srgb_to_linearrgb(((float)to[0])*(1.0f/255.0f));
00222                 tof[1] = srgb_to_linearrgb(((float)to[1])*(1.0f/255.0f));
00223                 tof[2] = srgb_to_linearrgb(((float)to[2])*(1.0f/255.0f));
00224                 tof[3] = ((float)to[3])*(1.0f/255.0f);
00225                 to += 4; 
00226                 tof += 4;
00227         }
00228 }
00229 
00230 void IMB_float_from_rect(struct ImBuf *ibuf)
00231 {
00232         /* quick method to convert byte to floatbuf */
00233         if(ibuf->rect==NULL) return;
00234         if(ibuf->rect_float==NULL) {
00235                 if (imb_addrectfloatImBuf(ibuf) == 0) return;
00236         }
00237         
00238         /* Float bufs should be stored linear */
00239 
00240         if (ibuf->profile != IB_PROFILE_NONE) {
00241                 /* if the image has been given a profile then we're working 
00242                  * with color management in mind, so convert it to linear space */
00243                 imb_float_from_rect_linear(ibuf, ibuf->rect_float);
00244         } else {
00245                 imb_float_from_rect_nonlinear(ibuf, ibuf->rect_float);
00246         }
00247 }
00248 
00249 /* no profile conversion */
00250 void IMB_float_from_rect_simple(struct ImBuf *ibuf)
00251 {
00252         if(ibuf->rect_float==NULL)
00253                 imb_addrectfloatImBuf(ibuf);
00254         imb_float_from_rect_nonlinear(ibuf, ibuf->rect_float);
00255 }
00256 
00257 void IMB_convert_profile(struct ImBuf *ibuf, int profile)
00258 {
00259         int ok= FALSE;
00260         int i;
00261 
00262         unsigned char *rct= (unsigned char *)ibuf->rect;
00263         float *rctf= ibuf->rect_float;
00264 
00265         if(ibuf->profile == profile)
00266                 return;
00267 
00268         if(ELEM(ibuf->profile, IB_PROFILE_NONE, IB_PROFILE_SRGB)) { /* from */
00269                 if(profile == IB_PROFILE_LINEAR_RGB) { /* to */
00270                         if(ibuf->rect_float) {
00271                                 for (i = ibuf->x * ibuf->y; i > 0; i--, rctf+=4) {
00272                                         rctf[0]= srgb_to_linearrgb(rctf[0]);
00273                                         rctf[1]= srgb_to_linearrgb(rctf[1]);
00274                                         rctf[2]= srgb_to_linearrgb(rctf[2]);
00275                                 }
00276                         }
00277                         if(ibuf->rect) {
00278                                 for (i = ibuf->x * ibuf->y; i > 0; i--, rct+=4) {
00279                                         rct[0]= (unsigned char)((srgb_to_linearrgb((float)rct[0]/255.0f) * 255.0f) + 0.5f);
00280                                         rct[1]= (unsigned char)((srgb_to_linearrgb((float)rct[1]/255.0f) * 255.0f) + 0.5f);
00281                                         rct[2]= (unsigned char)((srgb_to_linearrgb((float)rct[2]/255.0f) * 255.0f) + 0.5f);
00282                                 }
00283                         }
00284                         ok= TRUE;
00285                 }
00286         }
00287         else if (ibuf->profile == IB_PROFILE_LINEAR_RGB) { /* from */
00288                 if(ELEM(profile, IB_PROFILE_NONE, IB_PROFILE_SRGB)) { /* to */
00289                         if(ibuf->rect_float) {
00290                                 for (i = ibuf->x * ibuf->y; i > 0; i--, rctf+=4) {
00291                                         rctf[0]= linearrgb_to_srgb(rctf[0]);
00292                                         rctf[1]= linearrgb_to_srgb(rctf[1]);
00293                                         rctf[2]= linearrgb_to_srgb(rctf[2]);
00294                                 }
00295                         }
00296                         if(ibuf->rect) {
00297                                 for (i = ibuf->x * ibuf->y; i > 0; i--, rct+=4) {
00298                                         rct[0]= (unsigned char)((linearrgb_to_srgb((float)rct[0]/255.0f) * 255.0f) + 0.5f);
00299                                         rct[1]= (unsigned char)((linearrgb_to_srgb((float)rct[1]/255.0f) * 255.0f) + 0.5f);
00300                                         rct[2]= (unsigned char)((linearrgb_to_srgb((float)rct[2]/255.0f) * 255.0f) + 0.5f);
00301                                 }
00302                         }
00303                         ok= TRUE;
00304                 }
00305         }
00306 
00307         if(ok==FALSE){
00308                 printf("IMB_convert_profile: failed profile conversion %d -> %d\n", ibuf->profile, profile);
00309                 return;
00310         }
00311 
00312         ibuf->profile= profile;
00313 }
00314 
00315 /* use when you need to get a buffer with a certain profile
00316  * if the return  */
00317 float *IMB_float_profile_ensure(struct ImBuf *ibuf, int profile, int *alloc)
00318 {
00319         /* stupid but it works like this everywhere now */
00320         const short is_lin_from= (ibuf->profile != IB_PROFILE_NONE);
00321         const short is_lin_to= (profile != IB_PROFILE_NONE);
00322 
00323         
00324         if(is_lin_from == is_lin_to) {
00325                 *alloc= 0;
00326 
00327                 /* simple case, just allocate the buffer and return */
00328                 if(ibuf->rect_float == NULL) {
00329                         IMB_float_from_rect(ibuf);
00330                 }
00331 
00332                 return ibuf->rect_float;
00333         }
00334         else {
00335                 /* conversion is needed, first check */
00336                 float *fbuf= MEM_mallocN(ibuf->x * ibuf->y * sizeof(float) * 4, "IMB_float_profile_ensure");
00337                 *alloc= 1;
00338 
00339                 if(ibuf->rect_float == NULL) {
00340                         if(is_lin_to) {
00341                                 imb_float_from_rect_linear(ibuf, fbuf);
00342                         }
00343                         else {
00344                                 imb_float_from_rect_nonlinear(ibuf, fbuf);
00345                         }
00346                 }
00347                 else {
00348                         if(is_lin_to) { /* lin -> nonlin */
00349                                 linearrgb_to_srgb_rgba_rgba_buf(fbuf, ibuf->rect_float, ibuf->x * ibuf->y);
00350                         }
00351                         else { /* nonlin -> lin */
00352                                 srgb_to_linearrgb_rgba_rgba_buf(fbuf, ibuf->rect_float, ibuf->x * ibuf->y);
00353                         }
00354                 }
00355 
00356                 return fbuf;
00357         }
00358 }