Blender V2.27

scaling.c

00001 
00032 #include "BLI_blenlib.h"
00033 #include "MEM_guardedalloc.h"
00034 
00035 #include "imbuf.h"
00036 #include "IMB_imbuf_types.h"
00037 #include "IMB_imbuf.h"
00038 
00039 #include "IMB_allocimbuf.h"
00040 #include "IMB_filter.h"
00041 
00042 #include "BLO_sys_types.h" // for intptr_t support
00043 
00044 /************************************************************************/
00045 /*                                                              SCALING                                                                 */
00046 /************************************************************************/
00047 
00048 
00049 struct ImBuf *IMB_half_x(struct ImBuf *ibuf1)
00050 {
00051         struct ImBuf *ibuf2;
00052         uchar *p1,*_p1,*dest;
00053         short a,r,g,b,x,y;
00054         float af,rf,gf,bf, *p1f, *_p1f, *destf;
00055         int do_rect, do_float;
00056 
00057         if (ibuf1==NULL) return (0);
00058         if (ibuf1->rect==NULL && ibuf1->rect_float==NULL) return (0);
00059 
00060         do_rect= (ibuf1->rect != NULL);
00061         do_float= (ibuf1->rect_float != NULL);
00062         
00063         if (ibuf1->x <= 1) return(IMB_dupImBuf(ibuf1));
00064         
00065         ibuf2 = IMB_allocImBuf((ibuf1->x)/2, ibuf1->y, ibuf1->depth, ibuf1->flags);
00066         if (ibuf2==NULL) return (0);
00067 
00068         _p1 = (uchar *) ibuf1->rect;
00069         dest=(uchar *) ibuf2->rect;
00070          
00071         _p1f = ibuf1->rect_float;
00072         destf= ibuf2->rect_float;
00073 
00074         for(y=ibuf2->y;y>0;y--){
00075                 p1 = _p1;
00076                 p1f = _p1f;
00077                 for(x = ibuf2->x ; x>0 ; x--){
00078                         if (do_rect) {
00079                                 a = *(p1++) ;
00080                                 b = *(p1++) ;
00081                                 g = *(p1++) ;
00082                                 r = *(p1++);
00083                                 a += *(p1++) ;
00084                                 b += *(p1++) ;
00085                                 g += *(p1++) ;
00086                                 r += *(p1++);
00087                                 *(dest++) = a >> 1;
00088                                 *(dest++) = b >> 1;
00089                                 *(dest++) = g >> 1;
00090                                 *(dest++) = r >> 1;
00091                         }
00092                         if (do_float) {
00093                                 af = *(p1f++);
00094                                 bf = *(p1f++);
00095                                 gf = *(p1f++);
00096                                 rf = *(p1f++);
00097                                 af += *(p1f++);
00098                                 bf += *(p1f++);
00099                                 gf += *(p1f++);
00100                                 rf += *(p1f++);
00101                                 *(destf++) = 0.5f*af;
00102                                 *(destf++) = 0.5f*bf;
00103                                 *(destf++) = 0.5f*gf;
00104                                 *(destf++) = 0.5f*rf;
00105                         }
00106                 }
00107                 if (do_rect) _p1 += (ibuf1->x << 2);
00108                 if (do_float) _p1f += (ibuf1->x << 2);
00109         }
00110         return (ibuf2);
00111 }
00112 
00113 
00114 struct ImBuf *IMB_double_fast_x(struct ImBuf *ibuf1)
00115 {
00116         struct ImBuf *ibuf2;
00117         int *p1,*dest, i, col, do_rect, do_float;
00118         float *p1f, *destf;
00119 
00120         if (ibuf1==NULL) return (0);
00121         if (ibuf1->rect==NULL && ibuf1->rect_float==NULL) return (0);
00122 
00123         do_rect= (ibuf1->rect != NULL);
00124         do_float= (ibuf1->rect_float != NULL);
00125         
00126         ibuf2 = IMB_allocImBuf(2 * ibuf1->x , ibuf1->y , ibuf1->depth, ibuf1->flags);
00127         if (ibuf2==NULL) return (0);
00128 
00129         p1 = (int *) ibuf1->rect;
00130         dest=(int *) ibuf2->rect;
00131         p1f = (float *)ibuf1->rect_float;
00132         destf = (float *)ibuf2->rect_float;
00133 
00134         for(i = ibuf1->y * ibuf1->x ; i>0 ; i--) {
00135                 if (do_rect) {
00136                         col = *p1++;
00137                         *dest++ = col;
00138                         *dest++ = col;
00139                 }
00140                 if (do_float) {
00141                         destf[0]= destf[4] =p1f[0];
00142                         destf[1]= destf[5] =p1f[1];
00143                         destf[2]= destf[6] =p1f[2];
00144                         destf[3]= destf[7] =p1f[3];
00145                         destf+= 8;
00146                         p1f+= 4;
00147                 }
00148         }
00149 
00150         return (ibuf2);
00151 }
00152 
00153 struct ImBuf *IMB_double_x(struct ImBuf *ibuf1)
00154 {
00155         struct ImBuf *ibuf2;
00156 
00157         if (ibuf1==NULL) return (0);
00158         if (ibuf1->rect==NULL && ibuf1->rect_float==NULL) return (0);
00159 
00160         ibuf2 = IMB_double_fast_x(ibuf1);
00161 
00162         imb_filterx(ibuf2);
00163         return (ibuf2);
00164 }
00165 
00166 
00167 struct ImBuf *IMB_half_y(struct ImBuf *ibuf1)
00168 {
00169         struct ImBuf *ibuf2;
00170         uchar *p1,*p2,*_p1,*dest;
00171         short a,r,g,b,x,y;
00172         int do_rect, do_float;
00173         float af,rf,gf,bf,*p1f,*p2f,*_p1f,*destf;
00174 
00175         p1= p2= NULL;
00176         p1f= p2f= NULL;
00177         if (ibuf1==NULL) return (0);
00178         if (ibuf1->rect==NULL && ibuf1->rect_float==NULL) return (0);
00179         if (ibuf1->y <= 1) return(IMB_dupImBuf(ibuf1));
00180 
00181         do_rect= (ibuf1->rect != NULL);
00182         do_float= (ibuf1->rect_float != NULL);
00183 
00184         ibuf2 = IMB_allocImBuf(ibuf1->x , (ibuf1->y) / 2 , ibuf1->depth, ibuf1->flags);
00185         if (ibuf2==NULL) return (0);
00186 
00187         _p1 = (uchar *) ibuf1->rect;
00188         dest=(uchar *) ibuf2->rect;
00189         _p1f = (float *) ibuf1->rect_float;
00190         destf= (float *) ibuf2->rect_float;
00191 
00192         for(y=ibuf2->y ; y>0 ; y--){
00193                 if (do_rect) {
00194                         p1 = _p1;
00195                         p2 = _p1 + (ibuf1->x << 2);
00196                 }
00197                 if (do_float) {
00198                         p1f = _p1f;
00199                         p2f = _p1f + (ibuf1->x << 2);
00200                 }
00201                 for(x = ibuf2->x ; x>0 ; x--){
00202                         if (do_rect) {
00203                                 a = *(p1++) ;
00204                                 b = *(p1++) ;
00205                                 g = *(p1++) ;
00206                                 r = *(p1++);
00207                                 a += *(p2++) ;
00208                                 b += *(p2++) ;
00209                                 g += *(p2++) ;
00210                                 r += *(p2++);
00211                                 *(dest++) = a >> 1;
00212                                 *(dest++) = b >> 1;
00213                                 *(dest++) = g >> 1;
00214                                 *(dest++) = r >> 1;
00215                         }
00216                         if (do_float) {
00217                                 af = *(p1f++) ;
00218                                 bf = *(p1f++) ;
00219                                 gf = *(p1f++) ;
00220                                 rf = *(p1f++);
00221                                 af += *(p2f++) ;
00222                                 bf += *(p2f++) ;
00223                                 gf += *(p2f++) ;
00224                                 rf += *(p2f++);
00225                                 *(destf++) = 0.5f*af;
00226                                 *(destf++) = 0.5f*bf;
00227                                 *(destf++) = 0.5f*gf;
00228                                 *(destf++) = 0.5f*rf;
00229                         }
00230                 }
00231                 if (do_rect) _p1 += (ibuf1->x << 3);
00232                 if (do_float) _p1f += (ibuf1->x << 3);
00233         }
00234         return (ibuf2);
00235 }
00236 
00237 
00238 struct ImBuf *IMB_double_fast_y(struct ImBuf *ibuf1)
00239 {
00240         struct ImBuf *ibuf2;
00241         int *p1, *dest1, *dest2;
00242         float *p1f, *dest1f, *dest2f;
00243         short x,y;
00244         int do_rect, do_float;
00245 
00246         if (ibuf1==NULL) return (0);
00247         if (ibuf1->rect==NULL && ibuf1->rect_float==NULL) return (0);
00248 
00249         do_rect= (ibuf1->rect != NULL);
00250         do_float= (ibuf1->rect_float != NULL);
00251 
00252         ibuf2 = IMB_allocImBuf(ibuf1->x , 2 * ibuf1->y , ibuf1->depth, ibuf1->flags);
00253         if (ibuf2==NULL) return (0);
00254 
00255         p1 = (int *) ibuf1->rect;
00256         dest1= (int *) ibuf2->rect;
00257         p1f = (float *) ibuf1->rect_float;
00258         dest1f= (float *) ibuf2->rect_float;
00259 
00260         for(y = ibuf1->y ; y>0 ; y--){
00261                 if (do_rect) {
00262                         dest2 = dest1 + ibuf2->x;
00263                         for(x = ibuf2->x ; x>0 ; x--) *dest1++ = *dest2++ = *p1++;
00264                         dest1 = dest2;
00265                 }
00266                  if (do_float) {
00267                         dest2f = dest1f + (4*ibuf2->x);
00268                         for(x = ibuf2->x*4 ; x>0 ; x--) *dest1f++ = *dest2f++ = *p1f++;
00269                         dest1f = dest2f;
00270                 }
00271         }
00272 
00273         return (ibuf2);
00274 }
00275 
00276 struct ImBuf *IMB_double_y(struct ImBuf *ibuf1)
00277 {
00278         struct ImBuf *ibuf2;
00279 
00280         if (ibuf1==NULL) return (0);
00281         if (ibuf1->rect==NULL) return (0);
00282 
00283         ibuf2 = IMB_double_fast_y(ibuf1);
00284         
00285         IMB_filtery(ibuf2);
00286         return (ibuf2);
00287 }
00288 
00289 /* result in ibuf2, scaling should be done correctly */
00290 void imb_onehalf_no_alloc(struct ImBuf *ibuf2, struct ImBuf *ibuf1)
00291 {
00292         uchar *p1, *p2 = NULL, *dest;
00293         float *p1f, *destf, *p2f = NULL;
00294         int x,y;
00295         int do_rect, do_float;
00296 
00297         do_rect= (ibuf1->rect != NULL);
00298         
00299         p1f = ibuf1->rect_float;
00300         destf=ibuf2->rect_float;
00301         p1 = (uchar *) ibuf1->rect;
00302         dest=(uchar *) ibuf2->rect;
00303 
00304         do_float= (ibuf1->rect_float != NULL && ibuf2->rect_float != NULL);
00305 
00306         for(y=ibuf2->y;y>0;y--){
00307                 if (do_rect) p2 = p1 + (ibuf1->x << 2);
00308                 if (do_float) p2f = p1f + (ibuf1->x << 2);
00309                 for(x=ibuf2->x;x>0;x--){
00310                         if (do_rect) {
00311                                 dest[0] = (p1[0] + p2[0] + p1[4] + p2[4]) >> 2;
00312                                 dest[1] = (p1[1] + p2[1] + p1[5] + p2[5]) >> 2;
00313                                 dest[2] = (p1[2] + p2[2] + p1[6] + p2[6]) >> 2;
00314                                 dest[3] = (p1[3] + p2[3] + p1[7] + p2[7]) >> 2;
00315                                 p1 += 8; 
00316                                 p2 += 8; 
00317                                 dest += 4;
00318                         }
00319                         if (do_float){ 
00320                                 destf[0] = 0.25f*(p1f[0] + p2f[0] + p1f[4] + p2f[4]);
00321                                 destf[1] = 0.25f*(p1f[1] + p2f[1] + p1f[5] + p2f[5]);
00322                                 destf[2] = 0.25f*(p1f[2] + p2f[2] + p1f[6] + p2f[6]);
00323                                 destf[3] = 0.25f*(p1f[3] + p2f[3] + p1f[7] + p2f[7]);
00324                                 p1f += 8; 
00325                                 p2f += 8; 
00326                                 destf += 4;
00327                         }
00328                 }
00329                 if (do_rect) p1=p2;
00330                 if (do_float) p1f=p2f;
00331                 if(ibuf1->x & 1) {
00332                         if (do_rect) p1+=4;
00333                         if (do_float) p1f+=4;
00334                 }
00335         }
00336         
00337 }
00338 
00339 struct ImBuf *IMB_onehalf(struct ImBuf *ibuf1)
00340 {
00341         struct ImBuf *ibuf2;
00342 
00343         if (ibuf1==NULL) return (0);
00344         if (ibuf1->rect==NULL && ibuf1->rect_float==NULL) return (0);
00345         
00346         if (ibuf1->x <= 1) return(IMB_half_y(ibuf1));
00347         if (ibuf1->y <= 1) return(IMB_half_x(ibuf1));
00348         
00349         ibuf2=IMB_allocImBuf((ibuf1->x)/2, (ibuf1->y)/2, ibuf1->depth, ibuf1->flags);
00350         if (ibuf2==NULL) return (0);
00351         
00352         imb_onehalf_no_alloc(ibuf2, ibuf1);
00353         
00354         return (ibuf2);
00355 }
00356 
00357 /* q_scale_linear_interpolation helper functions */
00358 
00359 static void enlarge_picture_byte(
00360         unsigned char* src, unsigned char* dst, int src_width, 
00361         int src_height, int dst_width, int dst_height)
00362 {
00363         double ratiox = (double) (dst_width - 1.0) 
00364                 / (double) (src_width - 1.001);
00365         double ratioy = (double) (dst_height - 1.0) 
00366                 / (double) (src_height - 1.001);
00367         uintptr_t x_src, dx_src, x_dst;
00368         uintptr_t y_src, dy_src, y_dst;
00369 
00370         dx_src = 65536.0 / ratiox;
00371         dy_src = 65536.0 / ratioy;
00372 
00373         y_src = 0;
00374         for (y_dst = 0; y_dst < dst_height; y_dst++) {
00375                 unsigned char* line1 = src + (y_src >> 16) * 4 * src_width;
00376                 unsigned char* line2 = line1 + 4 * src_width;
00377                 uintptr_t weight1y = 65536 - (y_src & 0xffff);
00378                 uintptr_t weight2y = 65536 - weight1y;
00379 
00380                 if ((y_src >> 16) == src_height - 1) {
00381                         line2 = line1;
00382                 }
00383 
00384                 x_src = 0;
00385                 for (x_dst = 0; x_dst < dst_width; x_dst++) {
00386                         uintptr_t weight1x = 65536 - (x_src & 0xffff);
00387                         uintptr_t weight2x = 65536 - weight1x;
00388 
00389                         unsigned long x = (x_src >> 16) * 4;
00390 
00391                         *dst++ = ((((line1[x] * weight1y) >> 16) 
00392                                    * weight1x) >> 16)
00393                                 + ((((line2[x] * weight2y) >> 16) 
00394                                         * weight1x) >> 16)
00395                                 + ((((line1[4 + x] * weight1y) >> 16) 
00396                                    * weight2x) >> 16)
00397                                 + ((((line2[4 + x] * weight2y) >> 16) 
00398                                         * weight2x) >> 16);
00399 
00400                         *dst++ = ((((line1[x + 1] * weight1y) >> 16) 
00401                                    * weight1x) >> 16)
00402                                 + ((((line2[x + 1] * weight2y) >> 16) 
00403                                         * weight1x) >> 16)
00404                                 + ((((line1[4 + x + 1] * weight1y) >> 16) 
00405                                    * weight2x) >> 16)
00406                                 + ((((line2[4 + x + 1] * weight2y) >> 16) 
00407                                         * weight2x) >> 16);
00408 
00409                         *dst++ = ((((line1[x + 2] * weight1y) >> 16) 
00410                                    * weight1x) >> 16)
00411                                 + ((((line2[x + 2] * weight2y) >> 16) 
00412                                         * weight1x) >> 16)
00413                                 + ((((line1[4 + x + 2] * weight1y) >> 16) 
00414                                    * weight2x) >> 16)
00415                                 + ((((line2[4 + x + 2] * weight2y) >> 16) 
00416                                         * weight2x) >> 16);
00417 
00418                         *dst++ = ((((line1[x + 3] * weight1y) >> 16) 
00419                                    * weight1x) >> 16)
00420                                 + ((((line2[x + 3] * weight2y) >> 16) 
00421                                         * weight1x) >> 16)
00422                                 + ((((line1[4 + x + 3] * weight1y) >> 16) 
00423                                    * weight2x) >> 16)
00424                                 + ((((line2[4 + x + 3] * weight2y) >> 16) 
00425                                         * weight2x) >> 16);
00426 
00427                         x_src += dx_src;
00428                 }
00429                 y_src += dy_src;
00430         }
00431 }
00432 
00433 struct scale_outpix_byte {
00434         uintptr_t r;
00435         uintptr_t g;
00436         uintptr_t b;
00437         uintptr_t a;
00438 
00439         uintptr_t weight;
00440 };
00441 
00442 static void shrink_picture_byte(
00443         unsigned char* src, unsigned char* dst, int src_width, 
00444         int src_height, int dst_width, int dst_height)
00445 {
00446         double ratiox = (double) (dst_width) / (double) (src_width);
00447         double ratioy = (double) (dst_height) / (double) (src_height);
00448         uintptr_t x_src, dx_dst, x_dst;
00449         uintptr_t y_src, dy_dst, y_dst;
00450         intptr_t y_counter;
00451         unsigned char * dst_begin = dst;
00452 
00453         struct scale_outpix_byte * dst_line1 = NULL;
00454         struct scale_outpix_byte * dst_line2 = NULL;
00455 
00456         dst_line1 = (struct scale_outpix_byte*) MEM_callocN(
00457                 (dst_width + 1) * sizeof(struct scale_outpix_byte), 
00458                 "shrink_picture_byte 1");
00459         dst_line2 = (struct scale_outpix_byte*) MEM_callocN(
00460                 (dst_width + 1) * sizeof(struct scale_outpix_byte),
00461                 "shrink_picture_byte 2");
00462 
00463         dx_dst = 65536.0 * ratiox;
00464         dy_dst = 65536.0 * ratioy;
00465 
00466         y_dst = 0;
00467         y_counter = 65536;
00468         for (y_src = 0; y_src < src_height; y_src++) {
00469                 unsigned char* line = src + y_src * 4 * src_width;
00470                 uintptr_t weight1y = 65535 - (y_dst & 0xffff);
00471                 uintptr_t weight2y = 65535 - weight1y;
00472                 x_dst = 0;
00473                 for (x_src = 0; x_src < src_width; x_src++) {
00474                         uintptr_t weight1x = 65535 - (x_dst & 0xffff);
00475                         uintptr_t weight2x = 65535 - weight1x;
00476 
00477                         uintptr_t x = x_dst >> 16;
00478 
00479                         uintptr_t w;
00480 
00481                         w = (weight1y * weight1x) >> 16;
00482 
00483                         /* ensure correct rounding, without this you get ugly banding, or too low color values (ton) */
00484                         dst_line1[x].r += (line[0] * w + 32767) >> 16;
00485                         dst_line1[x].g += (line[1] * w + 32767) >> 16;
00486                         dst_line1[x].b += (line[2] * w + 32767) >> 16;
00487                         dst_line1[x].a += (line[3] * w + 32767) >> 16;
00488                         dst_line1[x].weight += w;
00489 
00490                         w = (weight2y * weight1x) >> 16;
00491 
00492                         dst_line2[x].r += (line[0] * w + 32767) >> 16;
00493                         dst_line2[x].g += (line[1] * w + 32767) >> 16;
00494                         dst_line2[x].b += (line[2] * w + 32767) >> 16;
00495                         dst_line2[x].a += (line[3] * w + 32767) >> 16;
00496                         dst_line2[x].weight += w;
00497 
00498                         w = (weight1y * weight2x) >> 16;
00499 
00500                         dst_line1[x+1].r += (line[0] * w + 32767) >> 16;
00501                         dst_line1[x+1].g += (line[1] * w + 32767) >> 16;
00502                         dst_line1[x+1].b += (line[2] * w + 32767) >> 16;
00503                         dst_line1[x+1].a += (line[3] * w + 32767) >> 16;
00504                         dst_line1[x+1].weight += w;
00505 
00506                         w = (weight2y * weight2x) >> 16;
00507 
00508                         dst_line2[x+1].r += (line[0] * w + 32767) >> 16;
00509                         dst_line2[x+1].g += (line[1] * w + 32767) >> 16;
00510                         dst_line2[x+1].b += (line[2] * w + 32767) >> 16;
00511                         dst_line2[x+1].a += (line[3] * w + 32767) >> 16;
00512                         dst_line2[x+1].weight += w;
00513 
00514                         x_dst += dx_dst;
00515                         line += 4;
00516                 }
00517 
00518                 y_dst += dy_dst;
00519                 y_counter -= dy_dst;
00520                 if (y_counter < 0) {
00521                         int val;
00522                         uintptr_t x;
00523                         struct scale_outpix_byte * temp;
00524 
00525                         y_counter += 65536;
00526                         
00527                         for (x=0; x < dst_width; x++) {
00528                                 uintptr_t f =  0x80000000UL / dst_line1[x].weight;
00529                                 *dst++ = (val= (dst_line1[x].r * f) >> 15) > 255 ? 255: val;
00530                                 *dst++ = (val= (dst_line1[x].g * f) >> 15) > 255 ? 255: val;
00531                                 *dst++ = (val= (dst_line1[x].b * f) >> 15) > 255 ? 255: val;
00532                                 *dst++ = (val= (dst_line1[x].a * f) >> 15) > 255 ? 255: val;
00533                         }
00534                         memset(dst_line1, 0, dst_width *
00535                                    sizeof(struct scale_outpix_byte));
00536                         temp = dst_line1;
00537                         dst_line1 = dst_line2;
00538                         dst_line2 = temp;
00539                 }
00540         }
00541         if (dst - dst_begin < dst_width * dst_height * 4) {
00542                 int val;
00543                 uintptr_t x;
00544                 for (x = 0; x < dst_width; x++) {
00545                         uintptr_t f = 0x80000000UL / dst_line1[x].weight;
00546                         *dst++ = (val= (dst_line1[x].r * f) >> 15) > 255 ? 255: val;
00547                         *dst++ = (val= (dst_line1[x].g * f) >> 15) > 255 ? 255: val;
00548                         *dst++ = (val= (dst_line1[x].b * f) >> 15) > 255 ? 255: val;
00549                         *dst++ = (val= (dst_line1[x].a * f) >> 15) > 255 ? 255: val;
00550                 }
00551         }
00552         MEM_freeN(dst_line1);
00553         MEM_freeN(dst_line2);
00554 }
00555 
00556 
00557 static void q_scale_byte(unsigned char* in, unsigned char* out, int in_width, 
00558                          int in_height, int dst_width, int dst_height)
00559 {
00560         if (dst_width > in_width && dst_height > in_height) {
00561                 enlarge_picture_byte(in, out, in_width, in_height,
00562                                          dst_width, dst_height);
00563         } else if (dst_width < in_width && dst_height < in_height) {
00564                 shrink_picture_byte(in, out, in_width, in_height,
00565                                         dst_width, dst_height);
00566         }
00567 }
00568 
00569 static void enlarge_picture_float(
00570         float* src, float* dst, int src_width, 
00571         int src_height, int dst_width, int dst_height)
00572 {
00573         double ratiox = (double) (dst_width - 1.0) 
00574                 / (double) (src_width - 1.001);
00575         double ratioy = (double) (dst_height - 1.0) 
00576                 / (double) (src_height - 1.001);
00577         uintptr_t x_dst;
00578         uintptr_t y_dst;
00579         double x_src, dx_src;
00580         double y_src, dy_src;
00581 
00582         dx_src = 1.0 / ratiox;
00583         dy_src = 1.0 / ratioy;
00584 
00585         y_src = 0;
00586         for (y_dst = 0; y_dst < dst_height; y_dst++) {
00587                 float* line1 = src + ((int) y_src) * 4 * src_width;
00588                 float* line2 = line1 + 4 * src_width;
00589                 float weight1y = 1.0 - (y_src - (int) y_src);
00590                 float weight2y = 1.0 - weight1y;
00591 
00592                 if ((int) y_src == src_height - 1) {
00593                         line2 = line1;
00594                 }
00595                        
00596                 x_src = 0;
00597                 for (x_dst = 0; x_dst < dst_width; x_dst++) {
00598                         float weight1x = 1.0 - (x_src - (int) x_src);
00599                         float weight2x = 1.0 - weight1x;
00600 
00601                         float w11 = weight1y * weight1x;
00602                         float w21 = weight2y * weight1x;
00603                         float w12 = weight1y * weight2x;
00604                         float w22 = weight2y * weight2x;
00605 
00606                         uintptr_t x = ((int) x_src) * 4;
00607 
00608                         *dst++ =  line1[x]     * w11    
00609                                 + line2[x]     * w21
00610                                 + line1[4 + x] * w12 
00611                                 + line2[4 + x] * w22;
00612 
00613                         *dst++ =  line1[x + 1] * w11 
00614                                 + line2[x + 1] * w21
00615                                 + line1[4 + x + 1] * w12
00616                                 + line2[4 + x + 1] * w22;
00617 
00618                         *dst++ =  line1[x + 2] * w11 
00619                                 + line2[x + 2] * w21
00620                                 + line1[4 + x + 2] * w12  
00621                                 + line2[4 + x + 2] * w22;
00622 
00623                         *dst++ =  line1[x + 3] * w11 
00624                                 + line2[x + 3] * w21
00625                                 + line1[4 + x + 3] * w12  
00626                                 + line2[4 + x + 3] * w22;
00627 
00628                         x_src += dx_src;
00629                 }
00630                 y_src += dy_src;
00631         }
00632 }
00633 
00634 struct scale_outpix_float {
00635         float r;
00636         float g;
00637         float b;
00638         float a;
00639 
00640         float weight;
00641 };
00642 
00643 static void shrink_picture_float(
00644         float* src, float* dst, int src_width, 
00645         int src_height, int dst_width, int dst_height)
00646 {
00647         double ratiox = (double) (dst_width) / (double) (src_width);
00648         double ratioy = (double) (dst_height) / (double) (src_height);
00649         uintptr_t x_src;
00650         uintptr_t y_src;
00651                 float dx_dst, x_dst;
00652         float dy_dst, y_dst;
00653         float y_counter;
00654         float * dst_begin = dst;
00655 
00656         struct scale_outpix_float * dst_line1;
00657         struct scale_outpix_float * dst_line2;
00658 
00659         dst_line1 = (struct scale_outpix_float*) MEM_callocN(
00660                 (dst_width + 1) * sizeof(struct scale_outpix_float), 
00661                 "shrink_picture_float 1");
00662         dst_line2 = (struct scale_outpix_float*) MEM_callocN(
00663                 (dst_width + 1) * sizeof(struct scale_outpix_float),
00664                 "shrink_picture_float 2");
00665 
00666         dx_dst = ratiox;
00667         dy_dst = ratioy;
00668 
00669         y_dst = 0;
00670         y_counter = 1.0;
00671         for (y_src = 0; y_src < src_height; y_src++) {
00672                 float* line = src + y_src * 4 * src_width;
00673                 uintptr_t weight1y = 1.0 - (y_dst - (int) y_dst);
00674                 uintptr_t weight2y = 1.0 - weight1y;
00675                 x_dst = 0;
00676                 for (x_src = 0; x_src < src_width; x_src++) {
00677                         uintptr_t weight1x = 1.0 - (x_dst - (int) x_dst);
00678                         uintptr_t weight2x = 1.0 - weight1x;
00679 
00680                         uintptr_t x = (int) x_dst;
00681 
00682                         float w;
00683 
00684                         w = weight1y * weight1x;
00685 
00686                         dst_line1[x].r += line[0] * w;
00687                         dst_line1[x].g += line[1] * w;
00688                         dst_line1[x].b += line[2] * w;
00689                         dst_line1[x].a += line[3] * w;
00690                         dst_line1[x].weight += w;
00691 
00692                         w = weight2y * weight1x;
00693 
00694                         dst_line2[x].r += line[0] * w;
00695                         dst_line2[x].g += line[1] * w;
00696                         dst_line2[x].b += line[2] * w;
00697                         dst_line2[x].a += line[3] * w;
00698                         dst_line2[x].weight += w;
00699 
00700                         w = weight1y * weight2x;
00701 
00702                         dst_line1[x+1].r += line[0] * w;
00703                         dst_line1[x+1].g += line[1] * w;
00704                         dst_line1[x+1].b += line[2] * w;
00705                         dst_line1[x+1].a += line[3] * w;
00706                         dst_line1[x+1].weight += w;
00707 
00708                         w = weight2y * weight2x;
00709 
00710                         dst_line2[x+1].r += line[0] * w;
00711                         dst_line2[x+1].g += line[1] * w;
00712                         dst_line2[x+1].b += line[2] * w;
00713                         dst_line2[x+1].a += line[3] * w;
00714                         dst_line2[x+1].weight += w;
00715 
00716                         x_dst += dx_dst;
00717                         line += 4;
00718                 }
00719 
00720                 y_dst += dy_dst;
00721                 y_counter -= dy_dst;
00722                 if (y_counter < 0) {
00723                         uintptr_t x;
00724                         struct scale_outpix_float * temp;
00725 
00726                         y_counter += 1.0;
00727                         
00728                         for (x=0; x < dst_width; x++) {
00729                                 float f = 1.0 / dst_line1[x].weight;
00730                                 *dst++ = dst_line1[x].r * f;
00731                                 *dst++ = dst_line1[x].g * f;
00732                                 *dst++ = dst_line1[x].b * f;
00733                                 *dst++ = dst_line1[x].a * f;
00734                         }
00735                         memset(dst_line1, 0, dst_width *
00736                                    sizeof(struct scale_outpix_float));
00737                         temp = dst_line1;
00738                         dst_line1 = dst_line2;
00739                         dst_line2 = temp;
00740                 }
00741         }
00742         if (dst - dst_begin < dst_width * dst_height * 4) {
00743                 uintptr_t x;
00744                 for (x = 0; x < dst_width; x++) {
00745                         float f = 1.0 / dst_line1[x].weight;
00746                         *dst++ = dst_line1[x].r * f;
00747                         *dst++ = dst_line1[x].g * f;
00748                         *dst++ = dst_line1[x].b * f;
00749                         *dst++ = dst_line1[x].a * f;
00750                 }
00751         }
00752         MEM_freeN(dst_line1);
00753         MEM_freeN(dst_line2);
00754 }
00755 
00756 
00757 static void q_scale_float(float* in, float* out, int in_width, 
00758                          int in_height, int dst_width, int dst_height)
00759 {
00760         if (dst_width > in_width && dst_height > in_height) {
00761                 enlarge_picture_float(in, out, in_width, in_height,
00762                                           dst_width, dst_height);
00763         } else if (dst_width < in_width && dst_height < in_height) {
00764                 shrink_picture_float(in, out, in_width, in_height,
00765                                          dst_width, dst_height);
00766         }
00767 }
00768 
00769 /* q_scale_linear_interpolation (derived from ppmqscale, http://libdv.sf.net)
00770 
00771    q stands for quick _and_ quality :)
00772 
00773    only handles common cases when we either
00774 
00775    scale  both, x and y or
00776    shrink both, x and y
00777 
00778    but that is pretty fast:
00779    * does only blit once instead of two passes like the old code
00780          (fewer cache misses)
00781    * uses fixed point integer arithmetic for byte buffers
00782    * doesn't branch in tight loops
00783 
00784    Should be comparable in speed to the ImBuf ..._fast functions at least 
00785    for byte-buffers.
00786 
00787    NOTE: disabled, due to inacceptable inaccuracy and quality loss, see bug #18609 (ton)
00788 
00789 */
00790 static int q_scale_linear_interpolation(
00791         struct ImBuf *ibuf, int newx, int newy)
00792 {
00793         if ((newx >= ibuf->x && newy <= ibuf->y) ||
00794                 (newx <= ibuf->x && newy >= ibuf->y)) {
00795                 return FALSE;
00796         }
00797 
00798         if (ibuf->rect) {
00799                 unsigned char * newrect = 
00800                         MEM_mallocN(newx * newy * sizeof(int), "q_scale rect");
00801                 q_scale_byte((unsigned char *)ibuf->rect, newrect, ibuf->x, ibuf->y,
00802                                  newx, newy);
00803 
00804                 imb_freerectImBuf(ibuf);
00805                 ibuf->mall |= IB_rect;
00806                 ibuf->rect = (unsigned int *) newrect;
00807         }
00808         if (ibuf->rect_float) {
00809                 float * newrect = 
00810                         MEM_mallocN(newx * newy * 4 *sizeof(float), 
00811                                         "q_scale rectfloat");
00812                 q_scale_float(ibuf->rect_float, newrect, ibuf->x, ibuf->y,
00813                                   newx, newy);
00814                 imb_freerectfloatImBuf(ibuf);
00815                 ibuf->mall |= IB_rectfloat;
00816                 ibuf->rect_float = newrect;
00817         }
00818         ibuf->x = newx;
00819         ibuf->y = newy;
00820 
00821         return TRUE;
00822 }
00823 
00824 static struct ImBuf *scaledownx(struct ImBuf *ibuf, int newx)
00825 {
00826         uchar *rect, *_newrect, *newrect;
00827         float *rectf, *_newrectf, *newrectf;
00828         float sample, add, val[4], nval[4], valf[4], nvalf[4];
00829         int x, y, do_rect = 0, do_float = 0;
00830 
00831         rectf= _newrectf= newrectf= NULL; 
00832         rect=_newrect= newrect= NULL; 
00833         nval[0]=  nval[1]= nval[2]= nval[3]= 0.0f;
00834         nvalf[0]=nvalf[1]=nvalf[2]=nvalf[3]= 0.0f;
00835         
00836         if (ibuf==NULL) return(0);
00837         if (ibuf->rect==NULL && ibuf->rect_float==NULL) return (ibuf);
00838 
00839         if (ibuf->rect) {
00840                 do_rect = 1;
00841                 _newrect = MEM_mallocN(newx * ibuf->y * sizeof(int), "scaledownx");
00842                 if (_newrect==NULL) return(ibuf);
00843         }
00844         if (ibuf->rect_float) {
00845                 do_float = 1;
00846                 _newrectf = MEM_mallocN(newx * ibuf->y * sizeof(float) * 4, "scaledownxf");
00847                 if (_newrectf==NULL) {
00848                         if (_newrect) MEM_freeN(_newrect);
00849                         return(ibuf);
00850                 }
00851         }
00852 
00853         add = (ibuf->x - 0.001) / newx;
00854 
00855         if (do_rect) {
00856                 rect = (uchar *) ibuf->rect;
00857                 newrect = _newrect;
00858         }
00859         if (do_float) {
00860                 rectf = ibuf->rect_float;
00861                 newrectf = _newrectf;
00862         }
00863                 
00864         for (y = ibuf->y; y>0 ; y--) {
00865                 sample = 0.0f;
00866                 val[0]=  val[1]= val[2]= val[3]= 0.0f;
00867                 valf[0]=valf[1]=valf[2]=valf[3]= 0.0f;
00868 
00869                 for (x = newx ; x>0 ; x--) {
00870                         if (do_rect) {
00871                                 nval[0] = - val[0] * sample;
00872                                 nval[1] = - val[1] * sample;
00873                                 nval[2] = - val[2] * sample;
00874                                 nval[3] = - val[3] * sample;
00875                         }
00876                         if (do_float) {
00877                                 nvalf[0] = - valf[0] * sample;
00878                                 nvalf[1] = - valf[1] * sample;
00879                                 nvalf[2] = - valf[2] * sample;
00880                                 nvalf[3] = - valf[3] * sample;
00881                         }
00882                         
00883                         sample += add;
00884 
00885                         while (sample >= 1.0f){
00886                                 sample -= 1.0f;
00887                                 
00888                                 if (do_rect) {
00889                                         nval[0] += rect[0];
00890                                         nval[1] += rect[1];
00891                                         nval[2] += rect[2];
00892                                         nval[3] += rect[3];
00893                                         rect += 4;
00894                                 }
00895                                 if (do_float) {
00896                                         nvalf[0] += rectf[0];
00897                                         nvalf[1] += rectf[1];
00898                                         nvalf[2] += rectf[2];
00899                                         nvalf[3] += rectf[3];
00900                                         rectf += 4;
00901                                 }
00902                         }
00903                         
00904                         if (do_rect) {
00905                                 val[0]= rect[0];val[1]= rect[1];val[2]= rect[2];val[3]= rect[3];
00906                                 rect += 4;
00907                                 
00908                                 newrect[0] = ((nval[0] + sample * val[0])/add + 0.5f);
00909                                 newrect[1] = ((nval[1] + sample * val[1])/add + 0.5f);
00910                                 newrect[2] = ((nval[2] + sample * val[2])/add + 0.5f);
00911                                 newrect[3] = ((nval[3] + sample * val[3])/add + 0.5f);
00912                                 
00913                                 newrect += 4;
00914                         }
00915                         if (do_float) {
00916                                 
00917                                 valf[0]= rectf[0];valf[1]= rectf[1];valf[2]= rectf[2];valf[3]= rectf[3];
00918                                 rectf += 4;
00919                                 
00920                                 newrectf[0] = ((nvalf[0] + sample * valf[0])/add);
00921                                 newrectf[1] = ((nvalf[1] + sample * valf[1])/add);
00922                                 newrectf[2] = ((nvalf[2] + sample * valf[2])/add);
00923                                 newrectf[3] = ((nvalf[3] + sample * valf[3])/add);
00924                                 
00925                                 newrectf += 4;
00926                         }
00927                         
00928                         sample -= 1.0f;
00929                 }
00930         }
00931 
00932         if (do_rect) {
00933                 imb_freerectImBuf(ibuf);
00934                 ibuf->mall |= IB_rect;
00935                 ibuf->rect = (unsigned int *) _newrect;
00936         }
00937         if (do_float) {
00938                 imb_freerectfloatImBuf(ibuf);
00939                 ibuf->mall |= IB_rectfloat;
00940                 ibuf->rect_float = _newrectf;
00941         }
00942         
00943         ibuf->x = newx;
00944         return(ibuf);
00945 }
00946 
00947 
00948 static struct ImBuf *scaledowny(struct ImBuf *ibuf, int newy)
00949 {
00950         uchar *rect, *_newrect, *newrect;
00951         float *rectf, *_newrectf, *newrectf;
00952         float sample, add, val[4], nval[4], valf[4], nvalf[4];
00953         int x, y, skipx, do_rect = 0, do_float = 0;
00954 
00955         rectf= _newrectf= newrectf= NULL; 
00956         rect= _newrect= newrect= NULL; 
00957         nval[0]=  nval[1]= nval[2]= nval[3]= 0.0f;
00958         nvalf[0]=nvalf[1]=nvalf[2]=nvalf[3]= 0.0f;
00959 
00960         if (ibuf==NULL) return(0);
00961         if (ibuf->rect==NULL && ibuf->rect_float==NULL) return (ibuf);
00962 
00963         if (ibuf->rect) {
00964                 do_rect = 1;
00965                 _newrect = MEM_mallocN(newy * ibuf->x * sizeof(int), "scaledowny");
00966                 if (_newrect==NULL) return(ibuf);
00967         }
00968         if (ibuf->rect_float) {
00969                 do_float = 1;
00970                 _newrectf = MEM_mallocN(newy * ibuf->x * sizeof(float) * 4, "scaledownyf");
00971                 if (_newrectf==NULL) {
00972                         if (_newrect) MEM_freeN(_newrect);
00973                         return(ibuf);
00974                 }
00975         }
00976 
00977         add = (ibuf->y - 0.001) / newy;
00978         skipx = 4 * ibuf->x;
00979 
00980         for (x = skipx - 4; x>=0 ; x-= 4) {
00981                 if (do_rect) {
00982                         rect = ((uchar *) ibuf->rect) + x;
00983                         newrect = _newrect + x;
00984                 }
00985                 if (do_float) {
00986                         rectf = ibuf->rect_float + x;
00987                         newrectf = _newrectf + x;
00988                 }
00989                 
00990                 sample = 0.0f;
00991                 val[0]=  val[1]= val[2]= val[3]= 0.0f;
00992                 valf[0]=valf[1]=valf[2]=valf[3]= 0.0f;
00993 
00994                 for (y = newy ; y>0 ; y--) {
00995                         if (do_rect) {
00996                                 nval[0] = - val[0] * sample;
00997                                 nval[1] = - val[1] * sample;
00998                                 nval[2] = - val[2] * sample;
00999                                 nval[3] = - val[3] * sample;
01000                         }
01001                         if (do_float) {
01002                                 nvalf[0] = - valf[0] * sample;
01003                                 nvalf[1] = - valf[1] * sample;
01004                                 nvalf[2] = - valf[2] * sample;
01005                                 nvalf[3] = - valf[3] * sample;
01006                         }
01007                         
01008                         sample += add;
01009 
01010                         while (sample >= 1.0) {
01011                                 sample -= 1.0;
01012                                 
01013                                 if (do_rect) {
01014                                         nval[0] += rect[0];
01015                                         nval[1] += rect[1];
01016                                         nval[2] += rect[2];
01017                                         nval[3] += rect[3];
01018                                         rect += skipx;
01019                                 }
01020                                 if (do_float) {
01021                                         nvalf[0] += rectf[0];
01022                                         nvalf[1] += rectf[1];
01023                                         nvalf[2] += rectf[2];
01024                                         nvalf[3] += rectf[3];
01025                                         rectf += skipx;
01026                                 }
01027                         }
01028 
01029                         if (do_rect) {
01030                                 val[0]= rect[0];val[1]= rect[1];val[2]= rect[2];val[3]= rect[3];
01031                                 rect += skipx;
01032                                 
01033                                 newrect[0] = ((nval[0] + sample * val[0])/add + 0.5f);
01034                                 newrect[1] = ((nval[1] + sample * val[1])/add + 0.5f);
01035                                 newrect[2] = ((nval[2] + sample * val[2])/add + 0.5f);
01036                                 newrect[3] = ((nval[3] + sample * val[3])/add + 0.5f);
01037                                 
01038                                 newrect += skipx;
01039                         }
01040                         if (do_float) {
01041                                 
01042                                 valf[0]= rectf[0];valf[1]= rectf[1];valf[2]= rectf[2];valf[3]= rectf[3];
01043                                 rectf += skipx;
01044                                 
01045                                 newrectf[0] = ((nvalf[0] + sample * valf[0])/add);
01046                                 newrectf[1] = ((nvalf[1] + sample * valf[1])/add);
01047                                 newrectf[2] = ((nvalf[2] + sample * valf[2])/add);
01048                                 newrectf[3] = ((nvalf[3] + sample * valf[3])/add);
01049                                 
01050                                 newrectf += skipx;
01051                         }
01052                         
01053                         sample -= 1.0;
01054                 }
01055         }       
01056 
01057         if (do_rect) {
01058                 imb_freerectImBuf(ibuf);
01059                 ibuf->mall |= IB_rect;
01060                 ibuf->rect = (unsigned int *) _newrect;
01061         }
01062         if (do_float) {
01063                 imb_freerectfloatImBuf(ibuf);
01064                 ibuf->mall |= IB_rectfloat;
01065                 ibuf->rect_float = (float *) _newrectf;
01066         }
01067         
01068         ibuf->y = newy;
01069         return(ibuf);
01070 }
01071 
01072 
01073 static struct ImBuf *scaleupx(struct ImBuf *ibuf, int newx)
01074 {
01075         uchar *rect,*_newrect=NULL,*newrect;
01076         float *rectf,*_newrectf=NULL,*newrectf;
01077         float sample,add;
01078         float val_a,nval_a,diff_a;
01079         float val_b,nval_b,diff_b;
01080         float val_g,nval_g,diff_g;
01081         float val_r,nval_r,diff_r;
01082         float val_af,nval_af,diff_af;
01083         float val_bf,nval_bf,diff_bf;
01084         float val_gf,nval_gf,diff_gf;
01085         float val_rf,nval_rf,diff_rf;
01086         int x,y, do_rect = 0, do_float = 0;
01087 
01088         val_a = nval_a = diff_a = val_b = nval_b = diff_b = 0;
01089         val_g = nval_g = diff_g = val_r = nval_r = diff_r = 0;
01090         val_af = nval_af = diff_af = val_bf = nval_bf = diff_bf = 0;
01091         val_gf = nval_gf = diff_gf = val_rf = nval_rf = diff_rf = 0;
01092         if (ibuf==NULL) return(0);
01093         if (ibuf->rect==NULL && ibuf->rect_float==NULL) return (ibuf);
01094 
01095         if (ibuf->rect) {
01096                 do_rect = 1;
01097                 _newrect = MEM_mallocN(newx * ibuf->y * sizeof(int), "scaleupx");
01098                 if (_newrect==NULL) return(ibuf);
01099         }
01100         if (ibuf->rect_float) {
01101                 do_float = 1;
01102                 _newrectf = MEM_mallocN(newx * ibuf->y * sizeof(float) * 4, "scaleupxf");
01103                 if (_newrectf==NULL) {
01104                         if (_newrect) MEM_freeN(_newrect);
01105                         return(ibuf);
01106                 }
01107         }
01108 
01109         add = (ibuf->x - 1.001) / (newx - 1.0);
01110 
01111         rect = (uchar *) ibuf->rect;
01112         rectf = (float *) ibuf->rect_float;
01113         newrect = _newrect;
01114         newrectf = _newrectf;
01115 
01116         for (y = ibuf->y; y>0 ; y--){
01117 
01118                 sample = 0;
01119                 
01120                 if (do_rect) {
01121                         val_a = rect[0] ;
01122                         nval_a = rect[4];
01123                         diff_a = nval_a - val_a ;
01124                         val_a += 0.5;
01125 
01126                         val_b = rect[1] ;
01127                         nval_b = rect[5];
01128                         diff_b = nval_b - val_b ;
01129                         val_b += 0.5;
01130 
01131                         val_g = rect[2] ;
01132                         nval_g = rect[6];
01133                         diff_g = nval_g - val_g ;
01134                         val_g += 0.5;
01135 
01136                         val_r = rect[3] ;
01137                         nval_r = rect[7];
01138                         diff_r = nval_r - val_r ;
01139                         val_r += 0.5;
01140 
01141                         rect += 8;
01142                 }
01143                 if (do_float) {
01144                         val_af = rectf[0] ;
01145                         nval_af = rectf[4];
01146                         diff_af = nval_af - val_af;
01147         
01148                         val_bf = rectf[1] ;
01149                         nval_bf = rectf[5];
01150                         diff_bf = nval_bf - val_bf;
01151 
01152                         val_gf = rectf[2] ;
01153                         nval_gf = rectf[6];
01154                         diff_gf = nval_gf - val_gf;
01155 
01156                         val_rf = rectf[3] ;
01157                         nval_rf = rectf[7];
01158                         diff_rf = nval_rf - val_rf;
01159 
01160                         rectf += 8;
01161                 }
01162                 for (x = newx ; x>0 ; x--){
01163                         if (sample >= 1.0){
01164                                 sample -= 1.0;
01165 
01166                                 if (do_rect) {
01167                                         val_a = nval_a ;
01168                                         nval_a = rect[0] ;
01169                                         diff_a = nval_a - val_a ;
01170                                         val_a += 0.5;
01171 
01172                                         val_b = nval_b ;
01173                                         nval_b = rect[1] ;
01174                                         diff_b = nval_b - val_b ;
01175                                         val_b += 0.5;
01176 
01177                                         val_g = nval_g ;
01178                                         nval_g = rect[2] ;
01179                                         diff_g = nval_g - val_g ;
01180                                         val_g += 0.5;
01181 
01182                                         val_r = nval_r ;
01183                                         nval_r = rect[3] ;
01184                                         diff_r = nval_r - val_r ;
01185                                         val_r += 0.5;
01186                                         rect += 4;
01187                                 }
01188                                 if (do_float) {
01189                                         val_af = nval_af ;
01190                                         nval_af = rectf[0] ;
01191                                         diff_af = nval_af - val_af ;
01192         
01193                                         val_bf = nval_bf ;
01194                                         nval_bf = rectf[1] ;
01195                                         diff_bf = nval_bf - val_bf ;
01196 
01197                                         val_gf = nval_gf ;
01198                                         nval_gf = rectf[2] ;
01199                                         diff_gf = nval_gf - val_gf ;
01200 
01201                                         val_rf = nval_rf ;
01202                                         nval_rf = rectf[3] ;
01203                                         diff_rf = nval_rf - val_rf;
01204                                         rectf += 4;
01205                                 }
01206                         }
01207                         if (do_rect) {
01208                                 newrect[0] = val_a + sample * diff_a;
01209                                 newrect[1] = val_b + sample * diff_b;
01210                                 newrect[2] = val_g + sample * diff_g;
01211                                 newrect[3] = val_r + sample * diff_r;
01212                                 newrect += 4;
01213                         }
01214                         if (do_float) {
01215                                 newrectf[0] = val_af + sample * diff_af;
01216                                 newrectf[1] = val_bf + sample * diff_bf;
01217                                 newrectf[2] = val_gf + sample * diff_gf;
01218                                 newrectf[3] = val_rf + sample * diff_rf;
01219                                 newrectf += 4;
01220                         }
01221                         sample += add;
01222                 }
01223         }
01224 
01225         if (do_rect) {
01226                 imb_freerectImBuf(ibuf);
01227                 ibuf->mall |= IB_rect;
01228                 ibuf->rect = (unsigned int *) _newrect;
01229         }
01230         if (do_float) {
01231                 imb_freerectfloatImBuf(ibuf);
01232                 ibuf->mall |= IB_rectfloat;
01233                 ibuf->rect_float = (float *) _newrectf;
01234         }
01235         
01236         ibuf->x = newx;
01237         return(ibuf);
01238 }
01239 
01240 static struct ImBuf *scaleupy(struct ImBuf *ibuf, int newy)
01241 {
01242         uchar *rect,*_newrect=NULL,*newrect;
01243         float *rectf,*_newrectf=NULL,*newrectf;
01244         float sample,add;
01245         float val_a,nval_a,diff_a;
01246         float val_b,nval_b,diff_b;
01247         float val_g,nval_g,diff_g;
01248         float val_r,nval_r,diff_r;
01249         float val_af,nval_af,diff_af;
01250         float val_bf,nval_bf,diff_bf;
01251         float val_gf,nval_gf,diff_gf;
01252         float val_rf,nval_rf,diff_rf;
01253         int x,y, do_rect = 0, do_float = 0, skipx;
01254 
01255         val_a = nval_a = diff_a = val_b = nval_b = diff_b = 0;
01256         val_g = nval_g = diff_g = val_r = nval_r = diff_r = 0;
01257         val_af = nval_af = diff_af = val_bf = nval_bf = diff_bf = 0;
01258         val_gf = nval_gf = diff_gf = val_rf = nval_rf = diff_rf = 0;
01259         if (ibuf==NULL) return(0);
01260         if (ibuf->rect==NULL && ibuf->rect_float==NULL) return (ibuf);
01261 
01262         if (ibuf->rect) {
01263                 do_rect = 1;
01264                 _newrect = MEM_mallocN(ibuf->x * newy * sizeof(int), "scaleupy");
01265                 if (_newrect==NULL) return(ibuf);
01266         }
01267         if (ibuf->rect_float) {
01268                 do_float = 1;
01269                 _newrectf = MEM_mallocN(ibuf->x * newy * sizeof(float) * 4, "scaleupyf");
01270                 if (_newrectf==NULL) {
01271                         if (_newrect) MEM_freeN(_newrect);
01272                         return(ibuf);
01273                 }
01274         }
01275 
01276         add = (ibuf->y - 1.001) / (newy - 1.0);
01277         skipx = 4 * ibuf->x;
01278 
01279         rect = (uchar *) ibuf->rect;
01280         rectf = (float *) ibuf->rect_float;
01281         newrect = _newrect;
01282         newrectf = _newrectf;
01283 
01284         for (x = ibuf->x; x>0 ; x--){
01285 
01286                 sample = 0;
01287                 if (do_rect) {
01288                         rect = ((uchar *)ibuf->rect) + 4*(x-1);
01289                         newrect = _newrect + 4*(x-1);
01290 
01291                         val_a = rect[0] ;
01292                         nval_a = rect[skipx];
01293                         diff_a = nval_a - val_a ;
01294                         val_a += 0.5;
01295 
01296                         val_b = rect[1] ;
01297                         nval_b = rect[skipx+1];
01298                         diff_b = nval_b - val_b ;
01299                         val_b += 0.5;
01300 
01301                         val_g = rect[2] ;
01302                         nval_g = rect[skipx+2];
01303                         diff_g = nval_g - val_g ;
01304                         val_g += 0.5;
01305 
01306                         val_r = rect[3] ;
01307                         nval_r = rect[skipx+4];
01308                         diff_r = nval_r - val_r ;
01309                         val_r += 0.5;
01310 
01311                         rect += 2*skipx;
01312                 }
01313                 if (do_float) {
01314                         rectf = ((float *)ibuf->rect_float) + 4*(x-1);
01315                         newrectf = _newrectf + 4*(x-1);
01316 
01317                         val_af = rectf[0] ;
01318                         nval_af = rectf[skipx];
01319                         diff_af = nval_af - val_af;
01320         
01321                         val_bf = rectf[1] ;
01322                         nval_bf = rectf[skipx+1];
01323                         diff_bf = nval_bf - val_bf;
01324 
01325                         val_gf = rectf[2] ;
01326                         nval_gf = rectf[skipx+2];
01327                         diff_gf = nval_gf - val_gf;
01328 
01329                         val_rf = rectf[3] ;
01330                         nval_rf = rectf[skipx+3];
01331                         diff_rf = nval_rf - val_rf;
01332 
01333                         rectf += 2*skipx;
01334                 }
01335                 
01336                 for (y = newy ; y>0 ; y--){
01337                         if (sample >= 1.0){
01338                                 sample -= 1.0;
01339 
01340                                 if (do_rect) {
01341                                         val_a = nval_a ;
01342                                         nval_a = rect[0] ;
01343                                         diff_a = nval_a - val_a ;
01344                                         val_a += 0.5;
01345 
01346                                         val_b = nval_b ;
01347                                         nval_b = rect[1] ;
01348                                         diff_b = nval_b - val_b ;
01349                                         val_b += 0.5;
01350 
01351                                         val_g = nval_g ;
01352                                         nval_g = rect[2] ;
01353                                         diff_g = nval_g - val_g ;
01354                                         val_g += 0.5;
01355 
01356                                         val_r = nval_r ;
01357                                         nval_r = rect[3] ;
01358                                         diff_r = nval_r - val_r ;
01359                                         val_r += 0.5;
01360                                         rect += skipx;
01361                                 }
01362                                 if (do_float) {
01363                                         val_af = nval_af ;
01364                                         nval_af = rectf[0] ;
01365                                         diff_af = nval_af - val_af ;
01366         
01367                                         val_bf = nval_bf ;
01368                                         nval_bf = rectf[1] ;
01369                                         diff_bf = nval_bf - val_bf ;
01370 
01371                                         val_gf = nval_gf ;
01372                                         nval_gf = rectf[2] ;
01373                                         diff_gf = nval_gf - val_gf ;
01374 
01375                                         val_rf = nval_rf ;
01376                                         nval_rf = rectf[3] ;
01377                                         diff_rf = nval_rf - val_rf;
01378                                         rectf += skipx;
01379                                 }
01380                         }
01381                         if (do_rect) {
01382                                 newrect[0] = val_a + sample * diff_a;
01383                                 newrect[1] = val_b + sample * diff_b;
01384                                 newrect[2] = val_g + sample * diff_g;
01385                                 newrect[3] = val_r + sample * diff_r;
01386                                 newrect += skipx;
01387                         }
01388                         if (do_float) {
01389                                 newrectf[0] = val_af + sample * diff_af;
01390                                 newrectf[1] = val_bf + sample * diff_bf;
01391                                 newrectf[2] = val_gf + sample * diff_gf;
01392                                 newrectf[3] = val_rf + sample * diff_rf;
01393                                 newrectf += skipx;
01394                         }
01395                         sample += add;
01396                 }
01397         }
01398 
01399         if (do_rect) {
01400                 imb_freerectImBuf(ibuf);
01401                 ibuf->mall |= IB_rect;
01402                 ibuf->rect = (unsigned int *) _newrect;
01403         }
01404         if (do_float) {
01405                 imb_freerectfloatImBuf(ibuf);
01406                 ibuf->mall |= IB_rectfloat;
01407                 ibuf->rect_float = (float *) _newrectf;
01408         }
01409         
01410         ibuf->y = newy;
01411         return(ibuf);
01412 }
01413 
01414 
01415 /* no float buf needed here! */
01416 static void scalefast_Z_ImBuf(ImBuf *ibuf, short newx, short newy)
01417 {
01418         unsigned int *rect, *_newrect, *newrect;
01419         int x, y;
01420         int ofsx, ofsy, stepx, stepy;
01421 
01422         if (ibuf->zbuf) {
01423                 _newrect = MEM_mallocN(newx * newy * sizeof(int), "z rect");
01424                 if (_newrect==NULL) return;
01425                 
01426                 stepx = (65536.0 * (ibuf->x - 1.0) / (newx - 1.0)) + 0.5;
01427                 stepy = (65536.0 * (ibuf->y - 1.0) / (newy - 1.0)) + 0.5;
01428                 ofsy = 32768;
01429 
01430                 newrect = _newrect;
01431         
01432                 for (y = newy; y > 0 ; y--){
01433                         rect = (unsigned int*) ibuf->zbuf;
01434                         rect += (ofsy >> 16) * ibuf->x;
01435                         ofsy += stepy;
01436                         ofsx = 32768;
01437                         for (x = newx ; x > 0 ; x--){
01438                                 *newrect++ = rect[ofsx >> 16];
01439                                 ofsx += stepx;
01440                         }
01441                 }
01442         
01443                 IMB_freezbufImBuf(ibuf);
01444                 ibuf->mall |= IB_zbuf;
01445                 ibuf->zbuf = (int*) _newrect;
01446         }
01447 }
01448 
01449 struct ImBuf *IMB_scaleImBuf(struct ImBuf * ibuf, unsigned int newx, unsigned int newy)
01450 {
01451         if (ibuf==NULL) return (0);
01452         if (ibuf->rect==NULL && ibuf->rect_float==NULL) return (ibuf);
01453         
01454         if (newx == ibuf->x && newy == ibuf->y) { return ibuf; }
01455 
01456         /* scaleup / scaledown functions below change ibuf->x and ibuf->y
01457            so we first scale the Z-buffer (if any) */
01458         scalefast_Z_ImBuf(ibuf, newx, newy);
01459 
01460         /* try to scale common cases in a fast way */
01461         /* disabled, quality loss is inacceptable, see report #18609  (ton) */
01462         if (0 && q_scale_linear_interpolation(ibuf, newx, newy)) {
01463                 return ibuf;
01464         }
01465 
01466         if (newx < ibuf->x) if (newx) scaledownx(ibuf,newx);
01467         if (newy < ibuf->y) if (newy) scaledowny(ibuf,newy);
01468         if (newx > ibuf->x) if (newx) scaleupx(ibuf,newx);
01469         if (newy > ibuf->y) if (newy) scaleupy(ibuf,newy);
01470         
01471         return(ibuf);
01472 }
01473 
01474 struct imbufRGBA {
01475         float r, g, b, a;
01476 };
01477 
01478 struct ImBuf *IMB_scalefastImBuf(struct ImBuf *ibuf, unsigned int newx, unsigned int newy)
01479 {
01480         unsigned int *rect,*_newrect,*newrect;
01481         struct imbufRGBA *rectf, *_newrectf, *newrectf;
01482         int x,y, do_float=0, do_rect=0;
01483         int ofsx,ofsy,stepx,stepy;
01484 
01485         rect = NULL; _newrect = NULL; newrect = NULL;
01486         rectf = NULL; _newrectf = NULL; newrectf = NULL;
01487 
01488         if (ibuf==NULL) return(0);
01489         if (ibuf->rect) do_rect = 1;
01490         if (ibuf->rect_float) do_float = 1;
01491         if (do_rect==0 && do_float==0) return(ibuf);
01492         
01493         if (newx == ibuf->x && newy == ibuf->y) return(ibuf);
01494         
01495         if(do_rect) {
01496                 _newrect = MEM_mallocN(newx * newy * sizeof(int), "scalefastimbuf");
01497                 if (_newrect==NULL) return(ibuf);
01498                 newrect = _newrect;
01499         }
01500         
01501         if (do_float) {
01502                 _newrectf = MEM_mallocN(newx * newy * sizeof(float) * 4, "scalefastimbuf f");
01503                 if (_newrectf==NULL) {
01504                         if (_newrect) MEM_freeN(_newrect);
01505                         return(ibuf);
01506                 }
01507                 newrectf = _newrectf;
01508         }
01509 
01510         stepx = (65536.0 * (ibuf->x - 1.0) / (newx - 1.0)) + 0.5;
01511         stepy = (65536.0 * (ibuf->y - 1.0) / (newy - 1.0)) + 0.5;
01512         ofsy = 32768;
01513 
01514         for (y = newy; y > 0 ; y--){
01515                 if(do_rect) {
01516                         rect = ibuf->rect;
01517                         rect += (ofsy >> 16) * ibuf->x;
01518                 }
01519                 if (do_float) {
01520                         rectf = (struct imbufRGBA *)ibuf->rect_float;
01521                         rectf += (ofsy >> 16) * ibuf->x;
01522                 }
01523                 ofsy += stepy;
01524                 ofsx = 32768;
01525                 
01526                 if (do_rect) {
01527                         for (x = newx ; x>0 ; x--){
01528                                 *newrect++ = rect[ofsx >> 16];
01529                                 ofsx += stepx;
01530                         }
01531                 }
01532 
01533                 if (do_float) {
01534                         ofsx = 32768;
01535                         for (x = newx ; x>0 ; x--){
01536                                 *newrectf++ = rectf[ofsx >> 16];
01537                                 ofsx += stepx;
01538                         }
01539                 }
01540         }
01541 
01542         if (do_rect) {
01543                 imb_freerectImBuf(ibuf);
01544                 ibuf->mall |= IB_rect;
01545                 ibuf->rect = _newrect;
01546         }
01547         
01548         if (do_float) {
01549                 imb_freerectfloatImBuf(ibuf);
01550                 ibuf->mall |= IB_rectfloat;
01551                 ibuf->rect_float = (float *)_newrectf;
01552         }
01553         
01554         scalefast_Z_ImBuf(ibuf, newx, newy);
01555         
01556         ibuf->x = newx;
01557         ibuf->y = newy;
01558         return(ibuf);
01559 }
01560