diff options
Diffstat (limited to 'source/blender/imbuf/intern/scaling.c')
-rw-r--r-- | source/blender/imbuf/intern/scaling.c | 186 |
1 files changed, 102 insertions, 84 deletions
diff --git a/source/blender/imbuf/intern/scaling.c b/source/blender/imbuf/intern/scaling.c index ea6cd0ffd78..6ebb86f646d 100644 --- a/source/blender/imbuf/intern/scaling.c +++ b/source/blender/imbuf/intern/scaling.c @@ -29,7 +29,13 @@ * $Id$ */ +/** \file blender/imbuf/intern/scaling.c + * \ingroup imbuf + */ + + #include "BLI_blenlib.h" +#include "BLI_utildefines.h" #include "MEM_guardedalloc.h" #include "imbuf.h" @@ -50,12 +56,13 @@ struct ImBuf *IMB_half_x(struct ImBuf *ibuf1) { struct ImBuf *ibuf2; uchar *p1,*_p1,*dest; - short a,r,g,b,x,y; + short a,r,g,b; + int x,y; float af,rf,gf,bf, *p1f, *_p1f, *destf; int do_rect, do_float; - if (ibuf1==NULL) return (0); - if (ibuf1->rect==NULL && ibuf1->rect_float==NULL) return (0); + if (ibuf1==NULL) return (NULL); + if (ibuf1->rect==NULL && ibuf1->rect_float==NULL) return (NULL); do_rect= (ibuf1->rect != NULL); do_float= (ibuf1->rect_float != NULL); @@ -63,7 +70,7 @@ struct ImBuf *IMB_half_x(struct ImBuf *ibuf1) if (ibuf1->x <= 1) return(IMB_dupImBuf(ibuf1)); ibuf2 = IMB_allocImBuf((ibuf1->x)/2, ibuf1->y, ibuf1->depth, ibuf1->flags); - if (ibuf2==NULL) return (0); + if (ibuf2==NULL) return (NULL); _p1 = (uchar *) ibuf1->rect; dest=(uchar *) ibuf2->rect; @@ -117,14 +124,14 @@ struct ImBuf *IMB_double_fast_x(struct ImBuf *ibuf1) int *p1,*dest, i, col, do_rect, do_float; float *p1f, *destf; - if (ibuf1==NULL) return (0); - if (ibuf1->rect==NULL && ibuf1->rect_float==NULL) return (0); + if (ibuf1==NULL) return (NULL); + if (ibuf1->rect==NULL && ibuf1->rect_float==NULL) return (NULL); do_rect= (ibuf1->rect != NULL); do_float= (ibuf1->rect_float != NULL); ibuf2 = IMB_allocImBuf(2 * ibuf1->x , ibuf1->y , ibuf1->depth, ibuf1->flags); - if (ibuf2==NULL) return (0); + if (ibuf2==NULL) return (NULL); p1 = (int *) ibuf1->rect; dest=(int *) ibuf2->rect; @@ -154,8 +161,8 @@ struct ImBuf *IMB_double_x(struct ImBuf *ibuf1) { struct ImBuf *ibuf2; - if (ibuf1==NULL) return (0); - if (ibuf1->rect==NULL && ibuf1->rect_float==NULL) return (0); + if (ibuf1==NULL) return (NULL); + if (ibuf1->rect==NULL && ibuf1->rect_float==NULL) return (NULL); ibuf2 = IMB_double_fast_x(ibuf1); @@ -168,21 +175,22 @@ struct ImBuf *IMB_half_y(struct ImBuf *ibuf1) { struct ImBuf *ibuf2; uchar *p1,*p2,*_p1,*dest; - short a,r,g,b,x,y; + short a,r,g,b; + int x,y; int do_rect, do_float; float af,rf,gf,bf,*p1f,*p2f,*_p1f,*destf; p1= p2= NULL; p1f= p2f= NULL; - if (ibuf1==NULL) return (0); - if (ibuf1->rect==NULL && ibuf1->rect_float==NULL) return (0); + if (ibuf1==NULL) return (NULL); + if (ibuf1->rect==NULL && ibuf1->rect_float==NULL) return (NULL); if (ibuf1->y <= 1) return(IMB_dupImBuf(ibuf1)); do_rect= (ibuf1->rect != NULL); do_float= (ibuf1->rect_float != NULL); ibuf2 = IMB_allocImBuf(ibuf1->x , (ibuf1->y) / 2 , ibuf1->depth, ibuf1->flags); - if (ibuf2==NULL) return (0); + if (ibuf2==NULL) return (NULL); _p1 = (uchar *) ibuf1->rect; dest=(uchar *) ibuf2->rect; @@ -240,17 +248,17 @@ struct ImBuf *IMB_double_fast_y(struct ImBuf *ibuf1) struct ImBuf *ibuf2; int *p1, *dest1, *dest2; float *p1f, *dest1f, *dest2f; - short x,y; + int x,y; int do_rect, do_float; - if (ibuf1==NULL) return (0); - if (ibuf1->rect==NULL && ibuf1->rect_float==NULL) return (0); + if (ibuf1==NULL) return (NULL); + if (ibuf1->rect==NULL && ibuf1->rect_float==NULL) return (NULL); do_rect= (ibuf1->rect != NULL); do_float= (ibuf1->rect_float != NULL); ibuf2 = IMB_allocImBuf(ibuf1->x , 2 * ibuf1->y , ibuf1->depth, ibuf1->flags); - if (ibuf2==NULL) return (0); + if (ibuf2==NULL) return (NULL); p1 = (int *) ibuf1->rect; dest1= (int *) ibuf2->rect; @@ -277,8 +285,8 @@ struct ImBuf *IMB_double_y(struct ImBuf *ibuf1) { struct ImBuf *ibuf2; - if (ibuf1==NULL) return (0); - if (ibuf1->rect==NULL) return (0); + if (ibuf1==NULL) return (NULL); + if (ibuf1->rect==NULL) return (NULL); ibuf2 = IMB_double_fast_y(ibuf1); @@ -340,14 +348,14 @@ struct ImBuf *IMB_onehalf(struct ImBuf *ibuf1) { struct ImBuf *ibuf2; - if (ibuf1==NULL) return (0); - if (ibuf1->rect==NULL && ibuf1->rect_float==NULL) return (0); + if (ibuf1==NULL) return (NULL); + if (ibuf1->rect==NULL && ibuf1->rect_float==NULL) return (NULL); if (ibuf1->x <= 1) return(IMB_half_y(ibuf1)); if (ibuf1->y <= 1) return(IMB_half_x(ibuf1)); ibuf2=IMB_allocImBuf((ibuf1->x)/2, (ibuf1->y)/2, ibuf1->depth, ibuf1->flags); - if (ibuf2==NULL) return (0); + if (ibuf2==NULL) return (NULL); imb_onehalf_no_alloc(ibuf2, ibuf1); @@ -586,8 +594,8 @@ static void enlarge_picture_float( for (y_dst = 0; y_dst < dst_height; y_dst++) { float* line1 = src + ((int) y_src) * 4 * src_width; float* line2 = line1 + 4 * src_width; - float weight1y = 1.0 - (y_src - (int) y_src); - float weight2y = 1.0 - weight1y; + const float weight1y = (float)(1.0 - (y_src - (int) y_src)); + const float weight2y = 1.0f - weight1y; if ((int) y_src == src_height - 1) { line2 = line1; @@ -595,13 +603,13 @@ static void enlarge_picture_float( x_src = 0; for (x_dst = 0; x_dst < dst_width; x_dst++) { - float weight1x = 1.0 - (x_src - (int) x_src); - float weight2x = 1.0 - weight1x; + const float weight1x = (float)(1.0 - (x_src - (int) x_src)); + const float weight2x = (float)(1.0f - weight1x); - float w11 = weight1y * weight1x; - float w21 = weight2y * weight1x; - float w12 = weight1y * weight2x; - float w22 = weight2y * weight2x; + const float w11 = weight1y * weight1x; + const float w21 = weight2y * weight1x; + const float w12 = weight1y * weight2x; + const float w22 = weight2y * weight2x; uintptr_t x = ((int) x_src) * 4; @@ -670,12 +678,12 @@ static void shrink_picture_float( y_counter = 1.0; for (y_src = 0; y_src < src_height; y_src++) { float* line = src + y_src * 4 * src_width; - uintptr_t weight1y = 1.0 - (y_dst - (int) y_dst); - uintptr_t weight2y = 1.0 - weight1y; + uintptr_t weight1y = 1.0f - (y_dst - (int) y_dst); + uintptr_t weight2y = 1.0f - weight1y; x_dst = 0; for (x_src = 0; x_src < src_width; x_src++) { - uintptr_t weight1x = 1.0 - (x_dst - (int) x_dst); - uintptr_t weight2x = 1.0 - weight1x; + uintptr_t weight1x = 1.0f - (x_dst - (int) x_dst); + uintptr_t weight2x = 1.0f - weight1x; uintptr_t x = (int) x_dst; @@ -723,10 +731,10 @@ static void shrink_picture_float( uintptr_t x; struct scale_outpix_float * temp; - y_counter += 1.0; + y_counter += 1.0f; for (x=0; x < dst_width; x++) { - float f = 1.0 / dst_line1[x].weight; + float f = 1.0f / dst_line1[x].weight; *dst++ = dst_line1[x].r * f; *dst++ = dst_line1[x].g * f; *dst++ = dst_line1[x].b * f; @@ -742,7 +750,7 @@ static void shrink_picture_float( if (dst - dst_begin < dst_width * dst_height * 4) { uintptr_t x; for (x = 0; x < dst_width; x++) { - float f = 1.0 / dst_line1[x].weight; + float f = 1.0f / dst_line1[x].weight; *dst++ = dst_line1[x].r * f; *dst++ = dst_line1[x].g * f; *dst++ = dst_line1[x].b * f; @@ -823,26 +831,27 @@ static int q_scale_linear_interpolation( static struct ImBuf *scaledownx(struct ImBuf *ibuf, int newx) { + const int do_rect= (ibuf->rect != NULL); + const int do_float= (ibuf->rect_float != NULL); + const size_t rect_size= ibuf->x * ibuf->y * 4; + uchar *rect, *_newrect, *newrect; float *rectf, *_newrectf, *newrectf; float sample, add, val[4], nval[4], valf[4], nvalf[4]; - int x, y, do_rect = 0, do_float = 0; + int x, y; rectf= _newrectf= newrectf= NULL; rect=_newrect= newrect= NULL; nval[0]= nval[1]= nval[2]= nval[3]= 0.0f; nvalf[0]=nvalf[1]=nvalf[2]=nvalf[3]= 0.0f; - - if (ibuf==NULL) return(0); - if (ibuf->rect==NULL && ibuf->rect_float==NULL) return (ibuf); - if (ibuf->rect) { - do_rect = 1; - _newrect = MEM_mallocN(newx * ibuf->y * sizeof(int), "scaledownx"); + if (!do_rect && !do_float) return (ibuf); + + if (do_rect) { + _newrect = MEM_mallocN(newx * ibuf->y * sizeof(uchar) * 4, "scaledownx"); if (_newrect==NULL) return(ibuf); } - if (ibuf->rect_float) { - do_float = 1; + if (do_float) { _newrectf = MEM_mallocN(newx * ibuf->y * sizeof(float) * 4, "scaledownxf"); if (_newrectf==NULL) { if (_newrect) MEM_freeN(_newrect); @@ -850,7 +859,7 @@ static struct ImBuf *scaledownx(struct ImBuf *ibuf, int newx) } } - add = (ibuf->x - 0.001) / newx; + add = (ibuf->x - 0.01) / newx; if (do_rect) { rect = (uchar *) ibuf->rect; @@ -930,11 +939,15 @@ static struct ImBuf *scaledownx(struct ImBuf *ibuf, int newx) } if (do_rect) { + // printf("%ld %ld\n", (uchar *)rect - ((uchar *)ibuf->rect), rect_size); + BLI_assert((uchar *)rect - ((uchar *)ibuf->rect) == rect_size); /* see bug [#26502] */ imb_freerectImBuf(ibuf); ibuf->mall |= IB_rect; ibuf->rect = (unsigned int *) _newrect; } if (do_float) { + // printf("%ld %ld\n", rectf - ibuf->rect_float, rect_size); + BLI_assert((rectf - ibuf->rect_float) == rect_size); /* see bug [#26502] */ imb_freerectfloatImBuf(ibuf); ibuf->mall |= IB_rectfloat; ibuf->rect_float = _newrectf; @@ -947,26 +960,27 @@ static struct ImBuf *scaledownx(struct ImBuf *ibuf, int newx) static struct ImBuf *scaledowny(struct ImBuf *ibuf, int newy) { + const int do_rect= (ibuf->rect != NULL); + const int do_float= (ibuf->rect_float != NULL); + const size_t rect_size= ibuf->x * ibuf->y * 4; + uchar *rect, *_newrect, *newrect; float *rectf, *_newrectf, *newrectf; float sample, add, val[4], nval[4], valf[4], nvalf[4]; - int x, y, skipx, do_rect = 0, do_float = 0; + int x, y, skipx; rectf= _newrectf= newrectf= NULL; rect= _newrect= newrect= NULL; nval[0]= nval[1]= nval[2]= nval[3]= 0.0f; nvalf[0]=nvalf[1]=nvalf[2]=nvalf[3]= 0.0f; - if (ibuf==NULL) return(0); - if (ibuf->rect==NULL && ibuf->rect_float==NULL) return (ibuf); + if (!do_rect && !do_float) return (ibuf); - if (ibuf->rect) { - do_rect = 1; - _newrect = MEM_mallocN(newy * ibuf->x * sizeof(int), "scaledowny"); + if (do_rect) { + _newrect = MEM_mallocN(newy * ibuf->x * sizeof(uchar) * 4, "scaledowny"); if (_newrect==NULL) return(ibuf); } - if (ibuf->rect_float) { - do_float = 1; + if (do_float) { _newrectf = MEM_mallocN(newy * ibuf->x * sizeof(float) * 4, "scaledownyf"); if (_newrectf==NULL) { if (_newrect) MEM_freeN(_newrect); @@ -974,7 +988,7 @@ static struct ImBuf *scaledowny(struct ImBuf *ibuf, int newy) } } - add = (ibuf->y - 0.001) / newy; + add = (ibuf->y - 0.01) / newy; skipx = 4 * ibuf->x; for (x = skipx - 4; x>=0 ; x-= 4) { @@ -1007,8 +1021,8 @@ static struct ImBuf *scaledowny(struct ImBuf *ibuf, int newy) sample += add; - while (sample >= 1.0) { - sample -= 1.0; + while (sample >= 1.0f) { + sample -= 1.0f; if (do_rect) { nval[0] += rect[0]; @@ -1050,16 +1064,20 @@ static struct ImBuf *scaledowny(struct ImBuf *ibuf, int newy) newrectf += skipx; } - sample -= 1.0; + sample -= 1.0f; } } if (do_rect) { + // printf("%ld %ld\n", (uchar *)rect - ((uchar *)ibuf->rect), rect_size); + BLI_assert((uchar *)rect - ((uchar *)ibuf->rect) == rect_size); /* see bug [#26502] */ imb_freerectImBuf(ibuf); ibuf->mall |= IB_rect; ibuf->rect = (unsigned int *) _newrect; } if (do_float) { + // printf("%ld %ld\n", rectf - ibuf->rect_float, rect_size); + BLI_assert((rectf - ibuf->rect_float) == rect_size); /* see bug [#26502] */ imb_freerectfloatImBuf(ibuf); ibuf->mall |= IB_rectfloat; ibuf->rect_float = (float *) _newrectf; @@ -1089,7 +1107,7 @@ static struct ImBuf *scaleupx(struct ImBuf *ibuf, int newx) val_g = nval_g = diff_g = val_r = nval_r = diff_r = 0; val_af = nval_af = diff_af = val_bf = nval_bf = diff_bf = 0; val_gf = nval_gf = diff_gf = val_rf = nval_rf = diff_rf = 0; - if (ibuf==NULL) return(0); + if (ibuf==NULL) return(NULL); if (ibuf->rect==NULL && ibuf->rect_float==NULL) return (ibuf); if (ibuf->rect) { @@ -1121,22 +1139,22 @@ static struct ImBuf *scaleupx(struct ImBuf *ibuf, int newx) val_a = rect[0] ; nval_a = rect[4]; diff_a = nval_a - val_a ; - val_a += 0.5; + val_a += 0.5f; val_b = rect[1] ; nval_b = rect[5]; diff_b = nval_b - val_b ; - val_b += 0.5; + val_b += 0.5f; val_g = rect[2] ; nval_g = rect[6]; diff_g = nval_g - val_g ; - val_g += 0.5; + val_g += 0.5f; val_r = rect[3] ; nval_r = rect[7]; diff_r = nval_r - val_r ; - val_r += 0.5; + val_r += 0.5f; rect += 8; } @@ -1160,29 +1178,29 @@ static struct ImBuf *scaleupx(struct ImBuf *ibuf, int newx) rectf += 8; } for (x = newx ; x>0 ; x--){ - if (sample >= 1.0){ - sample -= 1.0; + if (sample >= 1.0f){ + sample -= 1.0f; if (do_rect) { val_a = nval_a ; nval_a = rect[0] ; diff_a = nval_a - val_a ; - val_a += 0.5; + val_a += 0.5f; val_b = nval_b ; nval_b = rect[1] ; diff_b = nval_b - val_b ; - val_b += 0.5; + val_b += 0.5f; val_g = nval_g ; nval_g = rect[2] ; diff_g = nval_g - val_g ; - val_g += 0.5; + val_g += 0.5f; val_r = nval_r ; nval_r = rect[3] ; diff_r = nval_r - val_r ; - val_r += 0.5; + val_r += 0.5f; rect += 4; } if (do_float) { @@ -1256,7 +1274,7 @@ static struct ImBuf *scaleupy(struct ImBuf *ibuf, int newy) val_g = nval_g = diff_g = val_r = nval_r = diff_r = 0; val_af = nval_af = diff_af = val_bf = nval_bf = diff_bf = 0; val_gf = nval_gf = diff_gf = val_rf = nval_rf = diff_rf = 0; - if (ibuf==NULL) return(0); + if (ibuf==NULL) return(NULL); if (ibuf->rect==NULL && ibuf->rect_float==NULL) return (ibuf); if (ibuf->rect) { @@ -1291,22 +1309,22 @@ static struct ImBuf *scaleupy(struct ImBuf *ibuf, int newy) val_a = rect[0] ; nval_a = rect[skipx]; diff_a = nval_a - val_a ; - val_a += 0.5; + val_a += 0.5f; val_b = rect[1] ; nval_b = rect[skipx+1]; diff_b = nval_b - val_b ; - val_b += 0.5; + val_b += 0.5f; val_g = rect[2] ; nval_g = rect[skipx+2]; diff_g = nval_g - val_g ; - val_g += 0.5; + val_g += 0.5f; val_r = rect[3] ; nval_r = rect[skipx+4]; diff_r = nval_r - val_r ; - val_r += 0.5; + val_r += 0.5f; rect += 2*skipx; } @@ -1334,29 +1352,29 @@ static struct ImBuf *scaleupy(struct ImBuf *ibuf, int newy) } for (y = newy ; y>0 ; y--){ - if (sample >= 1.0){ - sample -= 1.0; + if (sample >= 1.0f){ + sample -= 1.0f; if (do_rect) { val_a = nval_a ; nval_a = rect[0] ; diff_a = nval_a - val_a ; - val_a += 0.5; + val_a += 0.5f; val_b = nval_b ; nval_b = rect[1] ; diff_b = nval_b - val_b ; - val_b += 0.5; + val_b += 0.5f; val_g = nval_g ; nval_g = rect[2] ; diff_g = nval_g - val_g ; - val_g += 0.5; + val_g += 0.5f; val_r = nval_r ; nval_r = rect[3] ; diff_r = nval_r - val_r ; - val_r += 0.5; + val_r += 0.5f; rect += skipx; } if (do_float) { @@ -1413,7 +1431,7 @@ static struct ImBuf *scaleupy(struct ImBuf *ibuf, int newy) /* no float buf needed here! */ -static void scalefast_Z_ImBuf(ImBuf *ibuf, short newx, short newy) +static void scalefast_Z_ImBuf(ImBuf *ibuf, int newx, int newy) { unsigned int *rect, *_newrect, *newrect; int x, y; @@ -1448,7 +1466,7 @@ static void scalefast_Z_ImBuf(ImBuf *ibuf, short newx, short newy) struct ImBuf *IMB_scaleImBuf(struct ImBuf * ibuf, unsigned int newx, unsigned int newy) { - if (ibuf==NULL) return (0); + if (ibuf==NULL) return (NULL); if (ibuf->rect==NULL && ibuf->rect_float==NULL) return (ibuf); if (newx == ibuf->x && newy == ibuf->y) { return ibuf; } @@ -1485,7 +1503,7 @@ struct ImBuf *IMB_scalefastImBuf(struct ImBuf *ibuf, unsigned int newx, unsigned rect = NULL; _newrect = NULL; newrect = NULL; rectf = NULL; _newrectf = NULL; newrectf = NULL; - if (ibuf==NULL) return(0); + if (ibuf==NULL) return(NULL); if (ibuf->rect) do_rect = 1; if (ibuf->rect_float) do_float = 1; if (do_rect==0 && do_float==0) return(ibuf); |