~siretart/ubuntu/utopic/blender/libav10

« back to all changes in this revision

Viewing changes to source/blender/render/intern/source/imagetexture.c

  • Committer: Package Import Robot
  • Author(s): Matteo F. Vescovi
  • Date: 2012-07-23 08:54:18 UTC
  • mfrom: (14.2.16 sid)
  • mto: (14.2.19 sid)
  • mto: This revision was merged to the branch mainline in revision 42.
  • Revision ID: package-import@ubuntu.com-20120723085418-9foz30v6afaf5ffs
Tags: 2.63a-2
* debian/: Cycles support added (Closes: #658075)
  For now, this top feature has been enabled only
  on [any-amd64 any-i386] architectures because
  of OpenImageIO failing on all others
* debian/: scripts installation path changed
  from /usr/lib to /usr/share:
  + debian/patches/: patchset re-worked for path changing
  + debian/control: "Breaks" field added on yafaray-exporter

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/**
 
1
/*
2
2
 *
3
 
 * $Id: imagetexture.c 29516 2010-06-17 07:20:12Z broken $
4
3
 *
5
4
 * ***** BEGIN GPL LICENSE BLOCK *****
6
5
 *
26
25
 * ***** END GPL/BL DUAL LICENSE BLOCK *****
27
26
 */
28
27
 
 
28
/** \file blender/render/intern/source/imagetexture.c
 
29
 *  \ingroup render
 
30
 */
 
31
 
 
32
 
29
33
 
30
34
 
31
35
#include <stdio.h>
51
55
#include "BLI_math.h"
52
56
#include "BLI_blenlib.h"
53
57
#include "BLI_threads.h"
 
58
#include "BLI_utildefines.h"
54
59
 
55
 
#include "BKE_utildefines.h"
56
60
#include "BKE_global.h"
57
61
#include "BKE_main.h"
58
62
#include "BKE_image.h"
59
63
#include "BKE_texture.h"
60
64
#include "BKE_library.h"
61
65
 
 
66
#include "RE_render_ext.h"
 
67
 
62
68
#include "renderpipeline.h"
63
69
#include "render_types.h"
64
70
#include "texture.h"
69
75
extern struct Render R;
70
76
/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
71
77
 
 
78
static void boxsample(ImBuf *ibuf, float minx, float miny, float maxx, float maxy, TexResult *texres, const short imaprepeat, const short imapextend);
 
79
 
72
80
/* *********** IMAGEWRAPPING ****************** */
73
81
 
74
82
 
77
85
{
78
86
        int ofs = y * ibuf->x + x;
79
87
        
80
 
        if(ibuf->rect_float) {
81
 
                if(ibuf->channels==4) {
 
88
        if (ibuf->rect_float) {
 
89
                if (ibuf->channels==4) {
82
90
                        float *fp= ibuf->rect_float + 4*ofs;
83
 
                        QUATCOPY(col, fp);
 
91
                        copy_v4_v4(col, fp);
84
92
                }
85
 
                else if(ibuf->channels==3) {
 
93
                else if (ibuf->channels==3) {
86
94
                        float *fp= ibuf->rect_float + 3*ofs;
87
 
                        VECCOPY(col, fp);
 
95
                        copy_v3_v3(col, fp);
88
96
                        col[3]= 1.0f;
89
97
                }
90
98
                else {
102
110
        }       
103
111
}
104
112
 
105
 
int imagewrap(Tex *tex, Image *ima, ImBuf *ibuf, float *texvec, TexResult *texres)
 
113
int imagewrap(Tex *tex, Image *ima, ImBuf *ibuf, const float texvec[3], TexResult *texres)
106
114
{
107
115
        float fx, fy, val1, val2, val3;
108
116
        int x, y, retval;
 
117
        int xi, yi; /* original values */
109
118
 
110
119
        texres->tin= texres->ta= texres->tr= texres->tg= texres->tb= 0.0f;
111
120
        
113
122
        retval= texres->nor?3:1;
114
123
        
115
124
        /* quick tests */
116
 
        if(ibuf==NULL && ima==NULL)
 
125
        if (ibuf==NULL && ima==NULL)
117
126
                return retval;
118
 
        if(ima) {
 
127
        if (ima) {
119
128
                
120
129
                /* hack for icon render */
121
 
                if(ima->ibufs.first==NULL && (R.r.scemode & R_NO_IMAGE_LOAD))
 
130
                if (ima->ibufs.first==NULL && (R.r.scemode & R_NO_IMAGE_LOAD))
122
131
                        return retval;
123
132
                
124
133
                ibuf= BKE_image_get_ibuf(ima, &tex->iuser);
 
134
 
 
135
                ima->flag|= IMA_USED_FOR_RENDER;
125
136
        }
126
 
        if(ibuf==NULL || (ibuf->rect==NULL && ibuf->rect_float==NULL))
 
137
        if (ibuf==NULL || (ibuf->rect==NULL && ibuf->rect_float==NULL))
127
138
                return retval;
128
139
        
129
140
        /* setup mapping */
130
 
        if(tex->imaflag & TEX_IMAROT) {
 
141
        if (tex->imaflag & TEX_IMAROT) {
131
142
                fy= texvec[0];
132
143
                fx= texvec[1];
133
144
        }
136
147
                fy= texvec[1];
137
148
        }
138
149
        
139
 
        if(tex->extend == TEX_CHECKER) {
 
150
        if (tex->extend == TEX_CHECKER) {
140
151
                int xs, ys;
141
152
                
142
153
                xs= (int)floor(fx);
144
155
                fx-= xs;
145
156
                fy-= ys;
146
157
 
147
 
                if( (tex->flag & TEX_CHECKER_ODD)==0) {
148
 
                        if((xs+ys) & 1);else return retval;
 
158
                if ( (tex->flag & TEX_CHECKER_ODD)==0) {
 
159
                        if ((xs+ys) & 1);else return retval;
149
160
                }
150
 
                if( (tex->flag & TEX_CHECKER_EVEN)==0) {
151
 
                        if((xs+ys) & 1) return retval; 
 
161
                if ( (tex->flag & TEX_CHECKER_EVEN)==0) {
 
162
                        if ((xs+ys) & 1) return retval;
152
163
                }
153
164
                /* scale around center, (0.5, 0.5) */
154
 
                if(tex->checkerdist<1.0) {
155
 
                        fx= (fx-0.5)/(1.0-tex->checkerdist) +0.5;
156
 
                        fy= (fy-0.5)/(1.0-tex->checkerdist) +0.5;
 
165
                if (tex->checkerdist<1.0f) {
 
166
                        fx= (fx-0.5f)/(1.0f-tex->checkerdist) +0.5f;
 
167
                        fy= (fy-0.5f)/(1.0f-tex->checkerdist) +0.5f;
157
168
                }
158
169
        }
159
170
 
160
 
        x = (int)(fx*ibuf->x);
161
 
        y = (int)(fy*ibuf->y);
 
171
        x= xi= (int)floorf(fx*ibuf->x);
 
172
        y= yi= (int)floorf(fy*ibuf->y);
162
173
 
163
 
        if(tex->extend == TEX_CLIPCUBE) {
164
 
                if(x<0 || y<0 || x>=ibuf->x || y>=ibuf->y || texvec[2]<-1.0 || texvec[2]>1.0) {
 
174
        if (tex->extend == TEX_CLIPCUBE) {
 
175
                if (x<0 || y<0 || x>=ibuf->x || y>=ibuf->y || texvec[2]<-1.0f || texvec[2]>1.0f) {
165
176
                        return retval;
166
177
                }
167
178
        }
168
 
        else if( tex->extend==TEX_CLIP || tex->extend==TEX_CHECKER) {
169
 
                if(x<0 || y<0 || x>=ibuf->x || y>=ibuf->y) {
 
179
        else if ( tex->extend==TEX_CLIP || tex->extend==TEX_CHECKER) {
 
180
                if (x<0 || y<0 || x>=ibuf->x || y>=ibuf->y) {
170
181
                        return retval;
171
182
                }
172
183
        }
173
184
        else {
174
 
                if(tex->extend==TEX_EXTEND) {
175
 
                        if(x>=ibuf->x) x = ibuf->x-1;
176
 
                        else if(x<0) x= 0;
 
185
                if (tex->extend==TEX_EXTEND) {
 
186
                        if (x>=ibuf->x) x = ibuf->x-1;
 
187
                        else if (x<0) x= 0;
177
188
                }
178
189
                else {
179
190
                        x= x % ibuf->x;
180
 
                        if(x<0) x+= ibuf->x;
 
191
                        if (x<0) x+= ibuf->x;
181
192
                }
182
 
                if(tex->extend==TEX_EXTEND) {
183
 
                        if(y>=ibuf->y) y = ibuf->y-1;
184
 
                        else if(y<0) y= 0;
 
193
                if (tex->extend==TEX_EXTEND) {
 
194
                        if (y>=ibuf->y) y = ibuf->y-1;
 
195
                        else if (y<0) y= 0;
185
196
                }
186
197
                else {
187
198
                        y= y % ibuf->y;
188
 
                        if(y<0) y+= ibuf->y;
 
199
                        if (y<0) y+= ibuf->y;
189
200
                }
190
201
        }
191
202
        
192
203
        /* warning, no return before setting back! */
193
 
        if( (R.flag & R_SEC_FIELD) && (ibuf->flags & IB_fields) ) {
 
204
        if ( (R.flag & R_SEC_FIELD) && (ibuf->flags & IB_fields) ) {
194
205
                ibuf->rect+= (ibuf->x*ibuf->y);
195
206
        }
196
207
 
197
 
        ibuf_get_color(&texres->tr, ibuf, x, y);
 
208
        /* keep this before interpolation [#29761] */
 
209
        if (tex->imaflag & TEX_USEALPHA) {
 
210
                if ((tex->imaflag & TEX_CALCALPHA) == 0) {
 
211
                        texres->talpha = TRUE;
 
212
                } 
 
213
        }
 
214
 
 
215
        /* interpolate */
 
216
        if (tex->imaflag & TEX_INTERPOL) {
 
217
                float filterx, filtery;
 
218
                filterx = (0.5f * tex->filtersize) / ibuf->x;
 
219
                filtery = (0.5f * tex->filtersize) / ibuf->y;
 
220
 
 
221
                /* important that this value is wrapped [#27782]
 
222
                 * this applies the modifications made by the checks above,
 
223
                 * back to the floating point values */
 
224
                fx -= (float)(xi - x) / (float)ibuf->x;
 
225
                fy -= (float)(yi - y) / (float)ibuf->y;
 
226
 
 
227
                boxsample(ibuf, fx-filterx, fy-filtery, fx+filterx, fy+filtery, texres, (tex->extend==TEX_REPEAT), (tex->extend==TEX_EXTEND));
 
228
        }
 
229
        else { /* no filtering */
 
230
                ibuf_get_color(&texres->tr, ibuf, x, y);
 
231
        }
198
232
        
199
 
        if( (R.flag & R_SEC_FIELD) && (ibuf->flags & IB_fields) ) {
 
233
        if ( (R.flag & R_SEC_FIELD) && (ibuf->flags & IB_fields) ) {
200
234
                ibuf->rect-= (ibuf->x*ibuf->y);
201
235
        }
202
236
 
203
 
        if(tex->imaflag & TEX_USEALPHA) {
204
 
                if(tex->imaflag & TEX_CALCALPHA);
205
 
                else texres->talpha= 1;
206
 
        }
207
 
        
208
 
        if(texres->nor) {
209
 
                if(tex->imaflag & TEX_NORMALMAP) {
 
237
        if (texres->nor) {
 
238
                if (tex->imaflag & TEX_NORMALMAP) {
210
239
                        // qdn: normal from color
211
 
                        texres->nor[0] = 2.f*(texres->tr - 0.5f);
212
 
                        texres->nor[1] = 2.f*(0.5f - texres->tg);
 
240
                        // The invert of the red channel is to make
 
241
                        // the normal map compliant with the outside world.
 
242
                        // It needs to be done because in Blender
 
243
                        // the normal used in the renderer points inward. It is generated
 
244
                        // this way in calc_vertexnormals(). Should this ever change
 
245
                        // this negate must be removed.
 
246
                        texres->nor[0] = -2.f*(texres->tr - 0.5f);
 
247
                        texres->nor[1] = 2.f*(texres->tg - 0.5f);
213
248
                        texres->nor[2] = 2.f*(texres->tb - 0.5f);
214
249
                }
215
250
                else {
216
251
                        /* bump: take three samples */
217
252
                        val1= texres->tr+texres->tg+texres->tb;
218
253
 
219
 
                        if(x<ibuf->x-1) {
 
254
                        if (x<ibuf->x-1) {
220
255
                                float col[4];
221
256
                                ibuf_get_color(col, ibuf, x+1, y);
222
257
                                val2= (col[0]+col[1]+col[2]);
223
258
                        }
224
259
                        else val2= val1;
225
260
 
226
 
                        if(y<ibuf->y-1) {
 
261
                        if (y<ibuf->y-1) {
227
262
                                float col[4];
228
263
                                ibuf_get_color(col, ibuf, x, y+1);
229
264
                                val3= (col[0]+col[1]+col[2]);
236
271
                }
237
272
        }
238
273
 
239
 
        if(texres->talpha) texres->tin= texres->ta;
240
 
        else if(tex->imaflag & TEX_CALCALPHA) {
 
274
        if (texres->talpha) texres->tin= texres->ta;
 
275
        else if (tex->imaflag & TEX_CALCALPHA) {
241
276
                texres->ta= texres->tin= MAX3(texres->tr, texres->tg, texres->tb);
242
277
        }
243
278
        else texres->ta= texres->tin= 1.0;
244
279
        
245
 
        if(tex->flag & TEX_NEGALPHA) texres->ta= 1.0f-texres->ta;
 
280
        if (tex->flag & TEX_NEGALPHA) texres->ta= 1.0f-texres->ta;
246
281
 
247
282
        /* de-premul, this is being premulled in shade_input_do_shade() */
248
 
        if(texres->ta!=1.0f && texres->ta>FLT_EPSILON) {
 
283
        if (texres->ta!=1.0f && texres->ta>1e-4f) {
249
284
                fx= 1.0f/texres->ta;
250
285
                texres->tr*= fx;
251
286
                texres->tg*= fx;
264
299
 
265
300
        a= *count;
266
301
        rf= stack;
267
 
        for(;a>0;a--) {
268
 
                if(rf->xmin<x1) {
269
 
                        if(rf->xmax<x1) {
 
302
        for (;a>0;a--) {
 
303
                if (rf->xmin<x1) {
 
304
                        if (rf->xmax<x1) {
270
305
                                rf->xmin+= (x2-x1);
271
306
                                rf->xmax+= (x2-x1);
272
307
                        }
273
308
                        else {
274
 
                                if(rf->xmax>x2) rf->xmax= x2;
 
309
                                if (rf->xmax>x2) rf->xmax = x2;
275
310
                                newrct= stack+ *count;
276
311
                                (*count)++;
277
312
 
278
 
                                newrct->xmax= x2;
279
 
                                newrct->xmin= rf->xmin+(x2-x1);
280
 
                                newrct->ymin= rf->ymin;
281
 
                                newrct->ymax= rf->ymax;
282
 
                                
283
 
                                if(newrct->xmin==newrct->xmax) (*count)--;
284
 
                                
285
 
                                rf->xmin= x1;
 
313
                                newrct->xmax = x2;
 
314
                                newrct->xmin = rf->xmin+(x2-x1);
 
315
                                newrct->ymin = rf->ymin;
 
316
                                newrct->ymax = rf->ymax;
 
317
                                
 
318
                                if (newrct->xmin ==newrct->xmax) (*count)--;
 
319
                                
 
320
                                rf->xmin = x1;
286
321
                        }
287
322
                }
288
 
                else if(rf->xmax>x2) {
289
 
                        if(rf->xmin>x2) {
 
323
                else if (rf->xmax>x2) {
 
324
                        if (rf->xmin>x2) {
290
325
                                rf->xmin-= (x2-x1);
291
326
                                rf->xmax-= (x2-x1);
292
327
                        }
293
328
                        else {
294
 
                                if(rf->xmin<x1) rf->xmin= x1;
 
329
                                if (rf->xmin<x1) rf->xmin = x1;
295
330
                                newrct= stack+ *count;
296
331
                                (*count)++;
297
332
 
298
 
                                newrct->xmin= x1;
299
 
                                newrct->xmax= rf->xmax-(x2-x1);
300
 
                                newrct->ymin= rf->ymin;
301
 
                                newrct->ymax= rf->ymax;
302
 
 
303
 
                                if(newrct->xmin==newrct->xmax) (*count)--;
304
 
 
305
 
                                rf->xmax= x2;
 
333
                                newrct->xmin = x1;
 
334
                                newrct->xmax = rf->xmax-(x2-x1);
 
335
                                newrct->ymin = rf->ymin;
 
336
                                newrct->ymax = rf->ymax;
 
337
 
 
338
                                if (newrct->xmin ==newrct->xmax) (*count)--;
 
339
 
 
340
                                rf->xmax = x2;
306
341
                        }
307
342
                }
308
343
                rf++;
317
352
 
318
353
        a= *count;
319
354
        rf= stack;
320
 
        for(;a>0;a--) {
321
 
                if(rf->ymin<y1) {
322
 
                        if(rf->ymax<y1) {
 
355
        for (;a>0;a--) {
 
356
                if (rf->ymin<y1) {
 
357
                        if (rf->ymax<y1) {
323
358
                                rf->ymin+= (y2-y1);
324
359
                                rf->ymax+= (y2-y1);
325
360
                        }
326
361
                        else {
327
 
                                if(rf->ymax>y2) rf->ymax= y2;
 
362
                                if (rf->ymax>y2) rf->ymax = y2;
328
363
                                newrct= stack+ *count;
329
364
                                (*count)++;
330
365
 
331
 
                                newrct->ymax= y2;
332
 
                                newrct->ymin= rf->ymin+(y2-y1);
333
 
                                newrct->xmin= rf->xmin;
334
 
                                newrct->xmax= rf->xmax;
335
 
 
336
 
                                if(newrct->ymin==newrct->ymax) (*count)--;
337
 
 
338
 
                                rf->ymin= y1;
 
366
                                newrct->ymax = y2;
 
367
                                newrct->ymin = rf->ymin+(y2-y1);
 
368
                                newrct->xmin = rf->xmin;
 
369
                                newrct->xmax = rf->xmax;
 
370
 
 
371
                                if (newrct->ymin==newrct->ymax) (*count)--;
 
372
 
 
373
                                rf->ymin = y1;
339
374
                        }
340
375
                }
341
 
                else if(rf->ymax>y2) {
342
 
                        if(rf->ymin>y2) {
 
376
                else if (rf->ymax>y2) {
 
377
                        if (rf->ymin>y2) {
343
378
                                rf->ymin-= (y2-y1);
344
379
                                rf->ymax-= (y2-y1);
345
380
                        }
346
381
                        else {
347
 
                                if(rf->ymin<y1) rf->ymin= y1;
 
382
                                if (rf->ymin<y1) rf->ymin = y1;
348
383
                                newrct= stack+ *count;
349
384
                                (*count)++;
350
385
 
351
 
                                newrct->ymin= y1;
352
 
                                newrct->ymax= rf->ymax-(y2-y1);
353
 
                                newrct->xmin= rf->xmin;
354
 
                                newrct->xmax= rf->xmax;
355
 
 
356
 
                                if(newrct->ymin==newrct->ymax) (*count)--;
357
 
 
358
 
                                rf->ymax= y2;
 
386
                                newrct->ymin = y1;
 
387
                                newrct->ymax = rf->ymax-(y2-y1);
 
388
                                newrct->xmin = rf->xmin;
 
389
                                newrct->xmax = rf->xmax;
 
390
 
 
391
                                if (newrct->ymin==newrct->ymax) (*count)--;
 
392
 
 
393
                                rf->ymax = y2;
359
394
                        }
360
395
                }
361
396
                rf++;
377
412
 
378
413
        size= rf->xmax - rf->xmin;
379
414
 
380
 
        if(rf->xmin<x1) {
381
 
                rf->xmin= x1;
382
 
        }
383
 
        if(rf->xmax>x2) {
384
 
                rf->xmax= x2;
385
 
        }
386
 
        if(rf->xmin > rf->xmax) {
 
415
        if (rf->xmin<x1) {
 
416
                rf->xmin = x1;
 
417
        }
 
418
        if (rf->xmax>x2) {
 
419
                rf->xmax = x2;
 
420
        }
 
421
        if (rf->xmin > rf->xmax) {
387
422
                rf->xmin = rf->xmax;
388
423
                return 0.0;
389
424
        }
390
 
        else if(size!=0.0) {
 
425
        else if (size!=0.0f) {
391
426
                return (rf->xmax - rf->xmin)/size;
392
427
        }
393
428
        return 1.0;
399
434
 
400
435
        size= rf->ymax - rf->ymin;
401
436
 
402
 
        if(rf->ymin<y1) {
403
 
                rf->ymin= y1;
 
437
        if (rf->ymin<y1) {
 
438
                rf->ymin = y1;
404
439
        }
405
 
        if(rf->ymax>y2) {
406
 
                rf->ymax= y2;
 
440
        if (rf->ymax>y2) {
 
441
                rf->ymax = y2;
407
442
        }
408
443
 
409
 
        if(rf->ymin > rf->ymax) {
 
444
        if (rf->ymin > rf->ymax) {
410
445
                rf->ymin = rf->ymax;
411
446
                return 0.0;
412
447
        }
413
 
        else if(size!=0.0) {
 
448
        else if (size!=0.0f) {
414
449
                return (rf->ymax - rf->ymin)/size;
415
450
        }
416
451
        return 1.0;
420
455
static void boxsampleclip(struct ImBuf *ibuf, rctf *rf, TexResult *texres)
421
456
{
422
457
        /* sample box, is clipped already, and minx etc. have been set at ibuf size.
423
 
           Enlarge with antialiased edges of the pixels */
 
458
         * Enlarge with antialiased edges of the pixels */
424
459
 
425
460
        float muly, mulx, div, col[4];
426
461
        int x, y, startx, endx, starty, endy;
430
465
        starty= (int)floor(rf->ymin);
431
466
        endy= (int)floor(rf->ymax);
432
467
 
433
 
        if(startx < 0) startx= 0;
434
 
        if(starty < 0) starty= 0;
435
 
        if(endx>=ibuf->x) endx= ibuf->x-1;
436
 
        if(endy>=ibuf->y) endy= ibuf->y-1;
 
468
        if (startx < 0) startx= 0;
 
469
        if (starty < 0) starty= 0;
 
470
        if (endx>=ibuf->x) endx= ibuf->x-1;
 
471
        if (endy>=ibuf->y) endy= ibuf->y-1;
437
472
 
438
 
        if(starty==endy && startx==endx) {
 
473
        if (starty==endy && startx==endx) {
439
474
                ibuf_get_color(&texres->tr, ibuf, startx, starty);
440
475
        }
441
476
        else {
442
477
                div= texres->tr= texres->tg= texres->tb= texres->ta= 0.0;
443
 
                for(y=starty; y<=endy; y++) {
 
478
                for (y=starty; y<=endy; y++) {
444
479
                        
445
480
                        muly= 1.0;
446
481
 
447
 
                        if(starty==endy);
 
482
                        if (starty==endy);
448
483
                        else {
449
 
                                if(y==starty) muly= 1.0f-(rf->ymin - y);
450
 
                                if(y==endy) muly= (rf->ymax - y);
 
484
                                if (y==starty) muly= 1.0f-(rf->ymin - y);
 
485
                                if (y==endy) muly= (rf->ymax - y);
451
486
                        }
452
487
                        
453
 
                        if(startx==endx) {
 
488
                        if (startx==endx) {
454
489
                                mulx= muly;
455
490
                                
456
491
                                ibuf_get_color(col, ibuf, startx, y);
462
497
                                div+= mulx;
463
498
                        }
464
499
                        else {
465
 
                                for(x=startx; x<=endx; x++) {
 
500
                                for (x=startx; x<=endx; x++) {
466
501
                                        mulx= muly;
467
 
                                        if(x==startx) mulx*= 1.0f-(rf->xmin - x);
468
 
                                        if(x==endx) mulx*= (rf->xmax - x);
 
502
                                        if (x==startx) mulx*= 1.0f-(rf->xmin - x);
 
503
                                        if (x==endx) mulx*= (rf->xmax - x);
469
504
 
470
505
                                        ibuf_get_color(col, ibuf, x, y);
471
506
                                        
472
 
                                        if(mulx==1.0) {
 
507
                                        if (mulx==1.0f) {
473
508
                                                texres->ta+= col[3];
474
509
                                                texres->tr+= col[0];
475
510
                                                texres->tg+= col[1];
476
511
                                                texres->tb+= col[2];
477
 
                                                div+= 1.0;
 
512
                                                div+= 1.0f;
478
513
                                        }
479
514
                                        else {
480
515
                                                texres->ta+= mulx*col[3];
487
522
                        }
488
523
                }
489
524
 
490
 
                if(div!=0.0) {
 
525
                if (div!=0.0f) {
491
526
                        div= 1.0f/div;
492
527
                        texres->tb*= div;
493
528
                        texres->tg*= div;
500
535
        }
501
536
}
502
537
 
503
 
static void boxsample(ImBuf *ibuf, float minx, float miny, float maxx, float maxy, TexResult *texres, int imaprepeat, int imapextend, int intpol)
 
538
static void boxsample(ImBuf *ibuf, float minx, float miny, float maxx, float maxy, TexResult *texres, const short imaprepeat, const short imapextend)
504
539
{
505
540
        /* Sample box, performs clip. minx etc are in range 0.0 - 1.0 .
506
 
   * Enlarge with antialiased edges of pixels.
507
 
   * If variable 'imaprepeat' has been set, the
508
 
   * clipped-away parts are sampled as well.
509
 
   */
510
 
        /* note: actually minx etc isnt in the proper range... this due to filter size and offset vectors for bump */
 
541
         * Enlarge with antialiased edges of pixels.
 
542
         * If variable 'imaprepeat' has been set, the
 
543
         * clipped-away parts are sampled as well.
 
544
         */
 
545
        /* note: actually minx etc isn't in the proper range... this due to filter size and offset vectors for bump */
 
546
        /* note: talpha must be initialized */
 
547
        /* note: even when 'imaprepeat' is set, this can only repeate once in any direction.
 
548
         * the point which min/max is derived from is assumed to be wrapped */
511
549
        TexResult texr;
512
550
        rctf *rf, stack[8];
513
551
        float opp, tot, alphaclip= 1.0;
514
552
        short count=1;
515
553
 
516
554
        rf= stack;
517
 
        rf->xmin= minx*(ibuf->x);
518
 
        rf->xmax= maxx*(ibuf->x);
519
 
        rf->ymin= miny*(ibuf->y);
520
 
        rf->ymax= maxy*(ibuf->y);
 
555
        rf->xmin = minx*(ibuf->x);
 
556
        rf->xmax = maxx*(ibuf->x);
 
557
        rf->ymin = miny*(ibuf->y);
 
558
        rf->ymax = maxy*(ibuf->y);
521
559
 
522
560
        texr.talpha= texres->talpha;    /* is read by boxsample_clip */
523
561
        
524
 
        if(imapextend) {
 
562
        if (imapextend) {
525
563
                CLAMP(rf->xmin, 0.0f, ibuf->x-1);
526
564
                CLAMP(rf->xmax, 0.0f, ibuf->x-1);
527
565
        }
528
 
        else if(imaprepeat) 
 
566
        else if (imaprepeat)
529
567
                clipx_rctf_swap(stack, &count, 0.0, (float)(ibuf->x));
530
568
        else {
531
569
                alphaclip= clipx_rctf(rf, 0.0, (float)(ibuf->x));
532
570
 
533
 
                if(alphaclip<=0.0) {
 
571
                if (alphaclip<=0.0f) {
534
572
                        texres->tr= texres->tb= texres->tg= texres->ta= 0.0;
535
573
                        return;
536
574
                }
537
575
        }
538
576
 
539
 
        if(imapextend) {
 
577
        if (imapextend) {
540
578
                CLAMP(rf->ymin, 0.0f, ibuf->y-1);
541
579
                CLAMP(rf->ymax, 0.0f, ibuf->y-1);
542
580
        }
543
 
        else if(imaprepeat) 
 
581
        else if (imaprepeat)
544
582
                clipy_rctf_swap(stack, &count, 0.0, (float)(ibuf->y));
545
583
        else {
546
584
                alphaclip*= clipy_rctf(rf, 0.0, (float)(ibuf->y));
547
585
 
548
 
                if(alphaclip<=0.0) {
 
586
                if (alphaclip<=0.0f) {
549
587
                        texres->tr= texres->tb= texres->tg= texres->ta= 0.0;
550
588
                        return;
551
589
                }
552
590
        }
553
591
 
554
 
        if(count>1) {
 
592
        if (count>1) {
555
593
                tot= texres->tr= texres->tb= texres->tg= texres->ta= 0.0;
556
 
                while(count--) {
 
594
                while (count--) {
557
595
                        boxsampleclip(ibuf, rf, &texr);
558
596
                        
559
597
                        opp= square_rctf(rf);
562
600
                        texres->tr+= opp*texr.tr;
563
601
                        texres->tg+= opp*texr.tg;
564
602
                        texres->tb+= opp*texr.tb;
565
 
                        if(texres->talpha) texres->ta+= opp*texr.ta;
 
603
                        if (texres->talpha) texres->ta+= opp*texr.ta;
566
604
                        rf++;
567
605
                }
568
 
                if(tot!= 0.0) {
 
606
                if (tot!= 0.0f) {
569
607
                        texres->tr/= tot;
570
608
                        texres->tg/= tot;
571
609
                        texres->tb/= tot;
572
 
                        if(texres->talpha) texres->ta/= tot;
 
610
                        if (texres->talpha) texres->ta/= tot;
573
611
                }
574
612
        }
575
613
        else
576
614
                boxsampleclip(ibuf, rf, texres);
577
615
 
578
 
        if(texres->talpha==0) texres->ta= 1.0;
 
616
        if (texres->talpha==0) texres->ta= 1.0;
579
617
        
580
 
        if(alphaclip!=1.0) {
 
618
        if (alphaclip!=1.0f) {
581
619
                /* premul it all */
582
620
                texres->tr*= alphaclip;
583
621
                texres->tg*= alphaclip;
808
846
static void ewa_eval(TexResult* texr, ImBuf* ibuf, float fx, float fy, afdata_t* AFD)
809
847
{
810
848
        // scaling dxt/dyt by full resolution can cause overflow because of huge A/B/C and esp. F values,
811
 
        // scaling by aspect ratio alone does the opposite, so try something inbetween instead...
 
849
        // scaling by aspect ratio alone does the opposite, so try something in between instead...
812
850
        const float ff2 = ibuf->x, ff = sqrtf(ff2), q = ibuf->y / ff;
813
851
        const float Ux = AFD->dxt[0]*ff, Vx = AFD->dxt[1]*q, Uy = AFD->dyt[0]*ff, Vy = AFD->dyt[1]*q;
814
852
        float A = Vx*Vx + Vy*Vy;
940
978
        // TXF apha: we're doing the same alphaclip here as boxsample, but i'm doubting
941
979
        // if this is actually correct for the all the filtering algorithms ..
942
980
 
943
 
        if(!(extflag == TXC_REPT || extflag == TXC_EXTD)) {
944
 
                rf.xmin= minx*(ibuf->x);
945
 
                rf.xmax= maxx*(ibuf->x);
946
 
                rf.ymin= miny*(ibuf->y);
947
 
                rf.ymax= maxy*(ibuf->y);
948
 
   
 
981
        if (!(extflag == TXC_REPT || extflag == TXC_EXTD)) {
 
982
                rf.xmin = minx*(ibuf->x);
 
983
                rf.xmax = maxx*(ibuf->x);
 
984
                rf.ymin = miny*(ibuf->y);
 
985
                rf.ymax = maxy*(ibuf->y);
 
986
 
949
987
                alphaclip = clipx_rctf(&rf, 0.0, (float)(ibuf->x));
950
988
                alphaclip*= clipy_rctf(&rf, 0.0, (float)(ibuf->y));
951
989
                alphaclip= MAX2(alphaclip, 0.0f);
952
990
 
953
 
                if(alphaclip!=1.0) {
 
991
                if (alphaclip!=1.0f) {
954
992
                        /* premul it all */
955
993
                        texres->tr*= alphaclip;
956
994
                        texres->tg*= alphaclip;
960
998
        }
961
999
}
962
1000
 
963
 
static int imagewraposa_aniso(Tex *tex, Image *ima, ImBuf *ibuf, float *texvec, float *dxt, float *dyt, TexResult *texres)
 
1001
static void image_mipmap_test(Tex *tex, ImBuf *ibuf)
 
1002
{
 
1003
        if (tex->imaflag & TEX_MIPMAP) {
 
1004
                if ((ibuf->flags & IB_fields) == 0) {
 
1005
                        
 
1006
                        if (ibuf->mipmap[0] && (ibuf->userflags & IB_MIPMAP_INVALID)) {
 
1007
                                BLI_lock_thread(LOCK_IMAGE);
 
1008
                                if (ibuf->userflags & IB_MIPMAP_INVALID) {
 
1009
                                        IMB_remakemipmap(ibuf, tex->imaflag & TEX_GAUSS_MIP);
 
1010
                                        ibuf->userflags &= ~IB_MIPMAP_INVALID;
 
1011
                                }                               
 
1012
                                BLI_unlock_thread(LOCK_IMAGE);
 
1013
                        }
 
1014
                        if (ibuf->mipmap[0] == NULL) {
 
1015
                                BLI_lock_thread(LOCK_IMAGE);
 
1016
                                if (ibuf->mipmap[0] == NULL) 
 
1017
                                        IMB_makemipmap(ibuf, tex->imaflag & TEX_GAUSS_MIP);
 
1018
                                BLI_unlock_thread(LOCK_IMAGE);
 
1019
                        }
 
1020
                }
 
1021
        }
 
1022
        
 
1023
}
 
1024
 
 
1025
static int imagewraposa_aniso(Tex *tex, Image *ima, ImBuf *ibuf, const float texvec[3], float dxt[3], float dyt[3], TexResult *texres)
964
1026
{
965
1027
        TexResult texr;
966
1028
        float fx, fy, minx, maxx, miny, maxy;
996
1058
 
997
1059
        if ((ibuf == NULL) || ((ibuf->rect == NULL) && (ibuf->rect_float == NULL))) return retval;
998
1060
 
999
 
        // mipmap test
1000
 
        if (tex->imaflag & TEX_MIPMAP) {
1001
 
                if (((ibuf->flags & IB_fields) == 0) && (ibuf->mipmap[0] == NULL)) {
1002
 
                        BLI_lock_thread(LOCK_IMAGE);
1003
 
                        if (ibuf->mipmap[0] == NULL) IMB_makemipmap(ibuf, tex->imaflag & TEX_GAUSS_MIP);
1004
 
                        BLI_unlock_thread(LOCK_IMAGE);
1005
 
                }
1006
 
        }
1007
 
 
 
1061
        /* mipmap test */
 
1062
        image_mipmap_test(tex, ibuf);
 
1063
        
1008
1064
        if ((tex->imaflag & TEX_USEALPHA) && ((tex->imaflag & TEX_CALCALPHA) == 0)) texres->talpha = 1;
1009
1065
        texr.talpha = texres->talpha;
1010
1066
 
1040
1096
 
1041
1097
        if (tex->imaflag & TEX_FILTER_MIN) {
1042
1098
                // make sure the filtersize is minimal in pixels (normal, ref map can have miniature pixel dx/dy)
1043
 
                 const float addval = (0.5f * tex->filtersize) / (float)MIN2(ibuf->x, ibuf->y);
 
1099
                const float addval = (0.5f * tex->filtersize) / (float)MIN2(ibuf->x, ibuf->y);
1044
1100
                if (addval > minx) minx = addval;
1045
1101
                if (addval > miny) miny = addval;
1046
1102
        }
1068
1124
        }
1069
1125
 
1070
1126
        // side faces of unit-cube
1071
 
        minx = (minx > 0.25f) ? 0.25f : ((minx < 1e-5f) ? 1e-5 : minx);
1072
 
        miny = (miny > 0.25f) ? 0.25f : ((miny < 1e-5f) ? 1e-5 : miny);
 
1127
        minx = (minx > 0.25f) ? 0.25f : ((minx < 1e-5f) ? 1e-5f : minx);
 
1128
        miny = (miny > 0.25f) ? 0.25f : ((miny < 1e-5f) ? 1e-5f : miny);
1073
1129
 
1074
1130
        // repeat and clip
1075
1131
 
1157
1213
        // brecht: added stupid clamping here, large dx/dy can give very large
1158
1214
        // filter sizes which take ages to render, it may be better to do this
1159
1215
        // more intelligently later in the code .. probably it's not noticeable
1160
 
        if(AFD.dxt[0]*AFD.dxt[0] + AFD.dxt[1]*AFD.dxt[1] > 2.0f*2.0f) {
 
1216
        if (AFD.dxt[0]*AFD.dxt[0] + AFD.dxt[1]*AFD.dxt[1] > 2.0f*2.0f)
1161
1217
                mul_v2_fl(AFD.dxt, 2.0f/len_v2(AFD.dxt));
1162
 
                mul_v2_fl(AFD.dyt, 2.0f/len_v2(AFD.dyt));
1163
 
        }
1164
 
        if(AFD.dyt[0]*AFD.dyt[0] + AFD.dyt[1]*AFD.dyt[1] > 2.0f*2.0f)
 
1218
        if (AFD.dyt[0]*AFD.dyt[0] + AFD.dyt[1]*AFD.dyt[1] > 2.0f*2.0f)
1165
1219
                mul_v2_fl(AFD.dyt, 2.0f/len_v2(AFD.dyt));
1166
1220
 
1167
1221
        // choice:
1328
1382
                ibuf->rect -= ibuf->x*ibuf->y;
1329
1383
 
1330
1384
        if (texres->nor && (tex->imaflag & TEX_NORMALMAP)) {    // normal from color
1331
 
                texres->nor[0] = 2.f*(texres->tr - 0.5f);
1332
 
                texres->nor[1] = 2.f*(0.5f - texres->tg);
 
1385
                // The invert of the red channel is to make
 
1386
                // the normal map compliant with the outside world.
 
1387
                // It needs to be done because in Blender
 
1388
                // the normal used in the renderer points inward. It is generated
 
1389
                // this way in calc_vertexnormals(). Should this ever change
 
1390
                // this negate must be removed.
 
1391
                texres->nor[0] = -2.f*(texres->tr - 0.5f);
 
1392
                texres->nor[1] = 2.f*(texres->tg - 0.5f);
1333
1393
                texres->nor[2] = 2.f*(texres->tb - 0.5f);
1334
1394
        }
1335
1395
        
1339
1399
 
1340
1400
        // brecht: tried to fix this, see "TXF alpha" comments
1341
1401
 
1342
 
        if (texres->ta != 1.f && (texres->ta > FLT_EPSILON)) {
 
1402
        if (texres->ta != 1.f && (texres->ta > 1e-4f)) {
1343
1403
                fx = 1.f/texres->ta;
1344
1404
                texres->tr *= fx;
1345
1405
                texres->tg *= fx;
1352
1412
}
1353
1413
 
1354
1414
 
1355
 
int imagewraposa(Tex *tex, Image *ima, ImBuf *ibuf, float *texvec, float *DXT, float *DYT, TexResult *texres)
 
1415
int imagewraposa(Tex *tex, Image *ima, ImBuf *ibuf, const float texvec[3], const float DXT[3], const float DYT[3], TexResult *texres)
1356
1416
{
1357
1417
        TexResult texr;
1358
1418
        float fx, fy, minx, maxx, miny, maxy, dx, dy, dxt[3], dyt[3];
1361
1421
 
1362
1422
        // TXF: since dxt/dyt might be modified here and since they might be needed after imagewraposa() call,
1363
1423
        // make a local copy here so that original vecs remain untouched
1364
 
        VECCOPY(dxt, DXT);
1365
 
        VECCOPY(dyt, DYT);
 
1424
        copy_v3_v3(dxt, DXT);
 
1425
        copy_v3_v3(dyt, DYT);
1366
1426
 
1367
1427
        // anisotropic filtering
1368
1428
        if (tex->texfilter != TXF_BOX)
1374
1434
        retval= texres->nor?3:1;
1375
1435
        
1376
1436
        /* quick tests */
1377
 
        if(ibuf==NULL && ima==NULL)
 
1437
        if (ibuf==NULL && ima==NULL)
1378
1438
                return retval;
1379
 
        if(ima) {
 
1439
        if (ima) {
1380
1440
 
1381
1441
                /* hack for icon render */
1382
 
                if(ima->ibufs.first==NULL && (R.r.scemode & R_NO_IMAGE_LOAD))
 
1442
                if (ima->ibufs.first==NULL && (R.r.scemode & R_NO_IMAGE_LOAD))
1383
1443
                        return retval;
1384
1444
                
1385
1445
                ibuf= BKE_image_get_ibuf(ima, &tex->iuser); 
 
1446
 
 
1447
                ima->flag|= IMA_USED_FOR_RENDER;
1386
1448
        }
1387
 
        if(ibuf==NULL || (ibuf->rect==NULL && ibuf->rect_float==NULL))
1388
 
           return retval;
 
1449
        if (ibuf==NULL || (ibuf->rect==NULL && ibuf->rect_float==NULL))
 
1450
                return retval;
1389
1451
        
1390
1452
        /* mipmap test */
1391
 
        if (tex->imaflag & TEX_MIPMAP) {
1392
 
                if(ibuf->flags & IB_fields);
1393
 
                else if(ibuf->mipmap[0]==NULL) {
1394
 
                        BLI_lock_thread(LOCK_IMAGE);
1395
 
                        
1396
 
                        if(ibuf->mipmap[0]==NULL)
1397
 
                                IMB_makemipmap(ibuf, tex->imaflag & TEX_GAUSS_MIP);
1398
 
 
1399
 
                        BLI_unlock_thread(LOCK_IMAGE);
1400
 
                }
1401
 
        }
1402
 
 
1403
 
        if(tex->imaflag & TEX_USEALPHA) {
1404
 
                if(tex->imaflag & TEX_CALCALPHA);
 
1453
        image_mipmap_test(tex, ibuf);
 
1454
 
 
1455
        if (tex->imaflag & TEX_USEALPHA) {
 
1456
                if (tex->imaflag & TEX_CALCALPHA);
1405
1457
                else texres->talpha= 1;
1406
1458
        }
1407
1459
        
1408
1460
        texr.talpha= texres->talpha;
1409
1461
        
1410
 
        if(tex->imaflag & TEX_IMAROT) {
 
1462
        if (tex->imaflag & TEX_IMAROT) {
1411
1463
                fy= texvec[0];
1412
1464
                fx= texvec[1];
1413
1465
        }
1416
1468
                fy= texvec[1];
1417
1469
        }
1418
1470
        
1419
 
        if(ibuf->flags & IB_fields) {
1420
 
                if(R.r.mode & R_FIELDS) {                       /* field render */
1421
 
                        if(R.flag & R_SEC_FIELD) {              /* correction for 2nd field */
 
1471
        if (ibuf->flags & IB_fields) {
 
1472
                if (R.r.mode & R_FIELDS) {                      /* field render */
 
1473
                        if (R.flag & R_SEC_FIELD) {             /* correction for 2nd field */
1422
1474
                                /* fac1= 0.5/( (float)ibuf->y ); */
1423
1475
                                /* fy-= fac1; */
1424
1476
                        }
1439
1491
        minx= (maxx-minx)/2.0f;
1440
1492
        miny= (maxy-miny)/2.0f;
1441
1493
        
1442
 
        if(tex->imaflag & TEX_FILTER_MIN) {
 
1494
        if (tex->imaflag & TEX_FILTER_MIN) {
1443
1495
                /* make sure the filtersize is minimal in pixels (normal, ref map can have miniature pixel dx/dy) */
1444
 
                 float addval= (0.5f * tex->filtersize) / (float) MIN2(ibuf->x, ibuf->y);
1445
 
                
1446
 
                if(addval > minx)
 
1496
                float addval= (0.5f * tex->filtersize) / (float) MIN2(ibuf->x, ibuf->y);
 
1497
 
 
1498
                if (addval > minx)
1447
1499
                        minx= addval;
1448
 
                if(addval > miny)
 
1500
                if (addval > miny)
1449
1501
                        miny= addval;
1450
1502
        }
1451
 
        else if(tex->filtersize!=1.0f) {
 
1503
        else if (tex->filtersize!=1.0f) {
1452
1504
                minx*= tex->filtersize;
1453
1505
                miny*= tex->filtersize;
1454
1506
                
1458
1510
                dyt[1]*= tex->filtersize;
1459
1511
        }
1460
1512
 
1461
 
        if(tex->imaflag & TEX_IMAROT) SWAP(float, minx, miny);
 
1513
        if (tex->imaflag & TEX_IMAROT) SWAP(float, minx, miny);
1462
1514
        
1463
 
        if(minx>0.25) minx= 0.25;
1464
 
        else if(minx<0.00001f) minx= 0.00001f;  /* side faces of unit-cube */
1465
 
        if(miny>0.25) miny= 0.25;
1466
 
        else if(miny<0.00001f) miny= 0.00001f;
 
1515
        if (minx>0.25f) minx= 0.25f;
 
1516
        else if (minx<0.00001f) minx= 0.00001f; /* side faces of unit-cube */
 
1517
        if (miny>0.25f) miny= 0.25f;
 
1518
        else if (miny<0.00001f) miny= 0.00001f;
1467
1519
 
1468
1520
        
1469
1521
        /* repeat and clip */
1470
1522
        imaprepeat= (tex->extend==TEX_REPEAT);
1471
1523
        imapextend= (tex->extend==TEX_EXTEND);
1472
1524
 
1473
 
        if(tex->extend == TEX_REPEAT) {
1474
 
                if(tex->flag & (TEX_REPEAT_XMIR|TEX_REPEAT_YMIR)) {
 
1525
        if (tex->extend == TEX_REPEAT) {
 
1526
                if (tex->flag & (TEX_REPEAT_XMIR|TEX_REPEAT_YMIR)) {
1475
1527
                        imaprepeat= 0;
1476
1528
                        imapextend= 1;
1477
1529
                }
1478
1530
        }
1479
1531
 
1480
 
        if(tex->extend == TEX_CHECKER) {
 
1532
        if (tex->extend == TEX_CHECKER) {
1481
1533
                int xs, ys, xs1, ys1, xs2, ys2, boundary;
1482
1534
                
1483
1535
                xs= (int)floor(fx);
1484
1536
                ys= (int)floor(fy);
1485
1537
                
1486
1538
                // both checkers available, no boundary exceptions, checkerdist will eat aliasing
1487
 
                if( (tex->flag & TEX_CHECKER_ODD) && (tex->flag & TEX_CHECKER_EVEN) ) {
 
1539
                if ( (tex->flag & TEX_CHECKER_ODD) && (tex->flag & TEX_CHECKER_EVEN) ) {
1488
1540
                        fx-= xs;
1489
1541
                        fy-= ys;
1490
1542
                }
1496
1548
                        ys2= (int)floor(fy+miny);
1497
1549
                        boundary= (xs1!=xs2) || (ys1!=ys2);
1498
1550
 
1499
 
                        if(boundary==0) {
1500
 
                                if( (tex->flag & TEX_CHECKER_ODD)==0) {
1501
 
                                        if((xs+ys) & 1); 
 
1551
                        if (boundary==0) {
 
1552
                                if ( (tex->flag & TEX_CHECKER_ODD)==0) {
 
1553
                                        if ((xs+ys) & 1);
1502
1554
                                        else return retval;
1503
1555
                                }
1504
 
                                if( (tex->flag & TEX_CHECKER_EVEN)==0) {
1505
 
                                        if((xs+ys) & 1) return retval;
 
1556
                                if ( (tex->flag & TEX_CHECKER_EVEN)==0) {
 
1557
                                        if ((xs+ys) & 1) return retval;
1506
1558
                                }
1507
1559
                                fx-= xs;
1508
1560
                                fy-= ys;
1509
1561
                        }
1510
1562
                        else {
1511
 
                                if(tex->flag & TEX_CHECKER_ODD) {
1512
 
                                        if((xs1+ys) & 1) fx-= xs2;
 
1563
                                if (tex->flag & TEX_CHECKER_ODD) {
 
1564
                                        if ((xs1+ys) & 1) fx-= xs2;
1513
1565
                                        else fx-= xs1;
1514
1566
                                        
1515
 
                                        if((ys1+xs) & 1) fy-= ys2;
 
1567
                                        if ((ys1+xs) & 1) fy-= ys2;
1516
1568
                                        else fy-= ys1;
1517
1569
                                }
1518
 
                                if(tex->flag & TEX_CHECKER_EVEN) {
1519
 
                                        if((xs1+ys) & 1) fx-= xs1;
 
1570
                                if (tex->flag & TEX_CHECKER_EVEN) {
 
1571
                                        if ((xs1+ys) & 1) fx-= xs1;
1520
1572
                                        else fx-= xs2;
1521
1573
                                        
1522
 
                                        if((ys1+xs) & 1) fy-= ys1;
 
1574
                                        if ((ys1+xs) & 1) fy-= ys1;
1523
1575
                                        else fy-= ys2;
1524
1576
                                }
1525
1577
                        }
1526
1578
                }
1527
1579
 
1528
1580
                /* scale around center, (0.5, 0.5) */
1529
 
                if(tex->checkerdist<1.0) {
1530
 
                        fx= (fx-0.5)/(1.0-tex->checkerdist) +0.5;
1531
 
                        fy= (fy-0.5)/(1.0-tex->checkerdist) +0.5;
1532
 
                        minx/= (1.0-tex->checkerdist);
1533
 
                        miny/= (1.0-tex->checkerdist);
 
1581
                if (tex->checkerdist<1.0f) {
 
1582
                        fx= (fx-0.5f)/(1.0f-tex->checkerdist) +0.5f;
 
1583
                        fy= (fy-0.5f)/(1.0f-tex->checkerdist) +0.5f;
 
1584
                        minx/= (1.0f-tex->checkerdist);
 
1585
                        miny/= (1.0f-tex->checkerdist);
1534
1586
                }
1535
1587
        }
1536
1588
 
1537
 
        if(tex->extend == TEX_CLIPCUBE) {
1538
 
                if(fx+minx<0.0 || fy+miny<0.0 || fx-minx>1.0 || fy-miny>1.0 || texvec[2]<-1.0 || texvec[2]>1.0) {
 
1589
        if (tex->extend == TEX_CLIPCUBE) {
 
1590
                if (fx+minx<0.0f || fy+miny<0.0f || fx-minx>1.0f || fy-miny>1.0f || texvec[2]<-1.0f || texvec[2]>1.0f) {
1539
1591
                        return retval;
1540
1592
                }
1541
1593
        }
1542
 
        else if(tex->extend==TEX_CLIP || tex->extend==TEX_CHECKER) {
1543
 
                if(fx+minx<0.0 || fy+miny<0.0 || fx-minx>1.0 || fy-miny>1.0) {
 
1594
        else if (tex->extend==TEX_CLIP || tex->extend==TEX_CHECKER) {
 
1595
                if (fx+minx<0.0f || fy+miny<0.0f || fx-minx>1.0f || fy-miny>1.0f) {
1544
1596
                        return retval;
1545
1597
                }
1546
1598
        }
1547
1599
        else {
1548
 
                if(imapextend) {
1549
 
                        if(fx>1.0) fx = 1.0;
1550
 
                        else if(fx<0.0) fx= 0.0;
 
1600
                if (imapextend) {
 
1601
                        if (fx>1.0f) fx = 1.0f;
 
1602
                        else if (fx<0.0f) fx= 0.0f;
1551
1603
                }
1552
1604
                else {
1553
 
                        if(fx>1.0) fx -= (int)(fx);
1554
 
                        else if(fx<0.0) fx+= 1-(int)(fx);
 
1605
                        if (fx>1.0f) fx -= (int)(fx);
 
1606
                        else if (fx<0.0f) fx+= 1-(int)(fx);
1555
1607
                }
1556
1608
                
1557
 
                if(imapextend) {
1558
 
                        if(fy>1.0) fy = 1.0;
1559
 
                        else if(fy<0.0) fy= 0.0;
 
1609
                if (imapextend) {
 
1610
                        if (fy>1.0f) fy = 1.0f;
 
1611
                        else if (fy<0.0f) fy= 0.0f;
1560
1612
                }
1561
1613
                else {
1562
 
                        if(fy>1.0) fy -= (int)(fy);
1563
 
                        else if(fy<0.0) fy+= 1-(int)(fy);
 
1614
                        if (fy>1.0f) fy -= (int)(fy);
 
1615
                        else if (fy<0.0f) fy+= 1-(int)(fy);
1564
1616
                }
1565
1617
        }
1566
1618
 
1567
1619
        /* warning no return! */
1568
 
        if( (R.flag & R_SEC_FIELD) && (ibuf->flags & IB_fields) ) {
 
1620
        if ( (R.flag & R_SEC_FIELD) && (ibuf->flags & IB_fields) ) {
1569
1621
                ibuf->rect+= (ibuf->x*ibuf->y);
1570
1622
        }
1571
1623
 
1572
1624
        /* choice:  */
1573
 
        if(tex->imaflag & TEX_MIPMAP) {
 
1625
        if (tex->imaflag & TEX_MIPMAP) {
1574
1626
                ImBuf *previbuf, *curibuf;
1575
1627
                float bumpscale;
1576
1628
                
1577
1629
                dx= minx;
1578
1630
                dy= miny;
1579
1631
                maxd= MAX2(dx, dy);
1580
 
                if(maxd>0.5) maxd= 0.5;
 
1632
                if (maxd>0.5f) maxd= 0.5f;
1581
1633
 
1582
1634
                pixsize = 1.0f/ (float) MIN2(ibuf->x, ibuf->y);
1583
1635
                
1584
1636
                bumpscale= pixsize/maxd;
1585
 
                if(bumpscale>1.0f) bumpscale= 1.0f;
 
1637
                if (bumpscale>1.0f) bumpscale= 1.0f;
1586
1638
                else bumpscale*=bumpscale;
1587
1639
                
1588
1640
                curmap= 0;
1589
1641
                previbuf= curibuf= ibuf;
1590
 
                while(curmap<IB_MIPMAP_LEVELS && ibuf->mipmap[curmap]) {
1591
 
                        if(maxd < pixsize) break;
 
1642
                while (curmap<IB_MIPMAP_LEVELS && ibuf->mipmap[curmap]) {
 
1643
                        if (maxd < pixsize) break;
1592
1644
                        previbuf= curibuf;
1593
1645
                        curibuf= ibuf->mipmap[curmap];
1594
1646
                        pixsize= 1.0f / (float)MIN2(curibuf->x, curibuf->y);
1595
1647
                        curmap++;
1596
1648
                }
1597
1649
 
1598
 
                if(previbuf!=curibuf || (tex->imaflag & TEX_INTERPOL)) {
 
1650
                if (previbuf!=curibuf || (tex->imaflag & TEX_INTERPOL)) {
1599
1651
                        /* sample at least 1 pixel */
1600
1652
                        if (minx < 0.5f / ibuf->x) minx = 0.5f / ibuf->x;
1601
1653
                        if (miny < 0.5f / ibuf->y) miny = 0.5f / ibuf->y;
1602
1654
                }
1603
1655
                
1604
 
                if(texres->nor && (tex->imaflag & TEX_NORMALMAP)==0) {
 
1656
                if (texres->nor && (tex->imaflag & TEX_NORMALMAP)==0) {
1605
1657
                        /* a bit extra filter */
1606
1658
                        //minx*= 1.35f;
1607
1659
                        //miny*= 1.35f;
1608
1660
                        
1609
 
                        boxsample(curibuf, fx-minx, fy-miny, fx+minx, fy+miny, texres, imaprepeat, imapextend, 0);
 
1661
                        boxsample(curibuf, fx-minx, fy-miny, fx+minx, fy+miny, texres, imaprepeat, imapextend);
1610
1662
                        val1= texres->tr+texres->tg+texres->tb;
1611
 
                        boxsample(curibuf, fx-minx+dxt[0], fy-miny+dxt[1], fx+minx+dxt[0], fy+miny+dxt[1], &texr, imaprepeat, imapextend, 0);
 
1663
                        boxsample(curibuf, fx-minx+dxt[0], fy-miny+dxt[1], fx+minx+dxt[0], fy+miny+dxt[1], &texr, imaprepeat, imapextend);
1612
1664
                        val2= texr.tr + texr.tg + texr.tb;
1613
 
                        boxsample(curibuf, fx-minx+dyt[0], fy-miny+dyt[1], fx+minx+dyt[0], fy+miny+dyt[1], &texr, imaprepeat, imapextend, 0);
 
1665
                        boxsample(curibuf, fx-minx+dyt[0], fy-miny+dyt[1], fx+minx+dyt[0], fy+miny+dyt[1], &texr, imaprepeat, imapextend);
1614
1666
                        val3= texr.tr + texr.tg + texr.tb;
1615
1667
 
1616
1668
                        /* don't switch x or y! */
1617
1669
                        texres->nor[0]= (val1-val2);
1618
1670
                        texres->nor[1]= (val1-val3);
1619
1671
                        
1620
 
                        if(previbuf!=curibuf) {  /* interpolate */
 
1672
                        if (previbuf!=curibuf) {  /* interpolate */
1621
1673
                                
1622
 
                                boxsample(previbuf, fx-minx, fy-miny, fx+minx, fy+miny, &texr, imaprepeat, imapextend, 0);
 
1674
                                boxsample(previbuf, fx-minx, fy-miny, fx+minx, fy+miny, &texr, imaprepeat, imapextend);
1623
1675
                                
1624
1676
                                /* calc rgb */
1625
1677
                                dx= 2.0f*(pixsize-maxd)/pixsize;
1626
 
                                if(dx>=1.0f) {
 
1678
                                if (dx>=1.0f) {
1627
1679
                                        texres->ta= texr.ta; texres->tb= texr.tb;
1628
1680
                                        texres->tg= texr.tg; texres->tr= texr.tr;
1629
1681
                                }
1636
1688
                                }
1637
1689
                                
1638
1690
                                val1= dy*val1+ dx*(texr.tr + texr.tg + texr.tb);
1639
 
                                boxsample(previbuf, fx-minx+dxt[0], fy-miny+dxt[1], fx+minx+dxt[0], fy+miny+dxt[1], &texr, imaprepeat, imapextend, 0);
 
1691
                                boxsample(previbuf, fx-minx+dxt[0], fy-miny+dxt[1], fx+minx+dxt[0], fy+miny+dxt[1], &texr, imaprepeat, imapextend);
1640
1692
                                val2= dy*val2+ dx*(texr.tr + texr.tg + texr.tb);
1641
 
                                boxsample(previbuf, fx-minx+dyt[0], fy-miny+dyt[1], fx+minx+dyt[0], fy+miny+dyt[1], &texr, imaprepeat, imapextend, 0);
 
1693
                                boxsample(previbuf, fx-minx+dyt[0], fy-miny+dyt[1], fx+minx+dyt[0], fy+miny+dyt[1], &texr, imaprepeat, imapextend);
1642
1694
                                val3= dy*val3+ dx*(texr.tr + texr.tg + texr.tb);
1643
1695
                                
1644
1696
                                texres->nor[0]= (val1-val2);    /* vals have been interpolated above! */
1645
1697
                                texres->nor[1]= (val1-val3);
1646
1698
                                
1647
 
                                if(dx<1.0f) {
 
1699
                                if (dx<1.0f) {
1648
1700
                                        dy= 1.0f-dx;
1649
1701
                                        texres->tb= dy*texres->tb+ dx*texr.tb;
1650
1702
                                        texres->tg= dy*texres->tg+ dx*texr.tg;
1661
1713
                        maxy= fy+miny;
1662
1714
                        miny= fy-miny;
1663
1715
 
1664
 
                        boxsample(curibuf, minx, miny, maxx, maxy, texres, imaprepeat, imapextend, 0);
 
1716
                        boxsample(curibuf, minx, miny, maxx, maxy, texres, imaprepeat, imapextend);
1665
1717
 
1666
 
                        if(previbuf!=curibuf) {  /* interpolate */
1667
 
                                boxsample(previbuf, minx, miny, maxx, maxy, &texr, imaprepeat, imapextend, 0);
 
1718
                        if (previbuf!=curibuf) {  /* interpolate */
 
1719
                                boxsample(previbuf, minx, miny, maxx, maxy, &texr, imaprepeat, imapextend);
1668
1720
                                
1669
1721
                                fx= 2.0f*(pixsize-maxd)/pixsize;
1670
1722
                                
1671
 
                                if(fx>=1.0) {
 
1723
                                if (fx>=1.0f) {
1672
1724
                                        texres->ta= texr.ta; texres->tb= texr.tb;
1673
1725
                                        texres->tg= texr.tg; texres->tr= texr.tr;
1674
 
                                } else {
 
1726
                                }
 
1727
                                else {
1675
1728
                                        fy= 1.0f-fx;
1676
1729
                                        texres->tb= fy*texres->tb+ fx*texr.tb;
1677
1730
                                        texres->tg= fy*texres->tg+ fx*texr.tg;
1689
1742
                        if (miny < 0.5f / ibuf->y) miny = 0.5f / ibuf->y;
1690
1743
                }
1691
1744
 
1692
 
                if(texres->nor && (tex->imaflag & TEX_NORMALMAP)==0) {
1693
 
                        boxsample(ibuf, fx-minx, fy-miny, fx+minx, fy+miny, texres, imaprepeat, imapextend, 0);
 
1745
                if (texres->nor && (tex->imaflag & TEX_NORMALMAP)==0) {
 
1746
                        boxsample(ibuf, fx-minx, fy-miny, fx+minx, fy+miny, texres, imaprepeat, imapextend);
1694
1747
                        val1= texres->tr+texres->tg+texres->tb;
1695
 
                        boxsample(ibuf, fx-minx+dxt[0], fy-miny+dxt[1], fx+minx+dxt[0], fy+miny+dxt[1], &texr, imaprepeat, imapextend, 0);
 
1748
                        boxsample(ibuf, fx-minx+dxt[0], fy-miny+dxt[1], fx+minx+dxt[0], fy+miny+dxt[1], &texr, imaprepeat, imapextend);
1696
1749
                        val2= texr.tr + texr.tg + texr.tb;
1697
 
                        boxsample(ibuf, fx-minx+dyt[0], fy-miny+dyt[1], fx+minx+dyt[0], fy+miny+dyt[1], &texr, imaprepeat, imapextend, 0);
 
1750
                        boxsample(ibuf, fx-minx+dyt[0], fy-miny+dyt[1], fx+minx+dyt[0], fy+miny+dyt[1], &texr, imaprepeat, imapextend);
1698
1751
                        val3= texr.tr + texr.tg + texr.tb;
1699
1752
 
1700
1753
                        /* don't switch x or y! */
1702
1755
                        texres->nor[1]= (val1-val3);
1703
1756
                }
1704
1757
                else
1705
 
                        boxsample(ibuf, fx-minx, fy-miny, fx+minx, fy+miny, texres, imaprepeat, imapextend, 0);
 
1758
                        boxsample(ibuf, fx-minx, fy-miny, fx+minx, fy+miny, texres, imaprepeat, imapextend);
1706
1759
        }
1707
1760
        
1708
 
        if(tex->imaflag & TEX_CALCALPHA) {
 
1761
        if (tex->imaflag & TEX_CALCALPHA) {
1709
1762
                texres->ta= texres->tin= texres->ta*MAX3(texres->tr, texres->tg, texres->tb);
1710
1763
        }
1711
1764
        else texres->tin= texres->ta;
1712
1765
 
1713
 
        if(tex->flag & TEX_NEGALPHA) texres->ta= 1.0f-texres->ta;
 
1766
        if (tex->flag & TEX_NEGALPHA) texres->ta= 1.0f-texres->ta;
1714
1767
        
1715
 
        if( (R.flag & R_SEC_FIELD) && (ibuf->flags & IB_fields) ) {
 
1768
        if ( (R.flag & R_SEC_FIELD) && (ibuf->flags & IB_fields) ) {
1716
1769
                ibuf->rect-= (ibuf->x*ibuf->y);
1717
1770
        }
1718
1771
 
1719
 
        if(texres->nor && (tex->imaflag & TEX_NORMALMAP)) {
 
1772
        if (texres->nor && (tex->imaflag & TEX_NORMALMAP)) {
1720
1773
                // qdn: normal from color
1721
 
                texres->nor[0] = 2.f*(texres->tr - 0.5f);
1722
 
                texres->nor[1] = 2.f*(0.5f - texres->tg);
 
1774
                // The invert of the red channel is to make
 
1775
                // the normal map compliant with the outside world.
 
1776
                // It needs to be done because in Blender
 
1777
                // the normal used in the renderer points inward. It is generated
 
1778
                // this way in calc_vertexnormals(). Should this ever change
 
1779
                // this negate must be removed.
 
1780
                texres->nor[0] = -2.f*(texres->tr - 0.5f);
 
1781
                texres->nor[1] = 2.f*(texres->tg - 0.5f);
1723
1782
                texres->nor[2] = 2.f*(texres->tb - 0.5f);
1724
1783
        }
1725
1784
        
1726
1785
        /* de-premul, this is being premulled in shade_input_do_shade() */
1727
 
        if(texres->ta!=1.0f && texres->ta>FLT_EPSILON) {
 
1786
        if (texres->ta!=1.0f && texres->ta>1e-4f) {
1728
1787
                fx= 1.0f/texres->ta;
1729
1788
                texres->tr*= fx;
1730
1789
                texres->tg*= fx;
1741
1800
        TexResult texres;
1742
1801
        ImBuf *ibuf= BKE_image_get_ibuf(ima, NULL);
1743
1802
        
1744
 
        if(ibuf==NULL) {
 
1803
        if (ibuf==NULL) {
1745
1804
                result[0]= result[1]= result[2]= result[3]= 0.0f;
1746
1805
                return;
1747
1806
        }
1748
1807
        
1749
 
        if( (R.flag & R_SEC_FIELD) && (ibuf->flags & IB_fields) )
 
1808
        if ( (R.flag & R_SEC_FIELD) && (ibuf->flags & IB_fields) )
1750
1809
                ibuf->rect+= (ibuf->x*ibuf->y);
1751
 
        
1752
 
        boxsample(ibuf, fx, fy, fx+dx, fy+dy, &texres, 0, 1, 0);
 
1810
 
 
1811
        texres.talpha= 1; /* boxsample expects to be initialized */
 
1812
        boxsample(ibuf, fx, fy, fx+dx, fy+dy, &texres, 0, 1);
1753
1813
        result[0]= texres.tr;
1754
1814
        result[1]= texres.tg;
1755
1815
        result[2]= texres.tb;
1756
1816
        result[3]= texres.ta;
1757
1817
        
1758
 
        if( (R.flag & R_SEC_FIELD) && (ibuf->flags & IB_fields) )
 
1818
        if ( (R.flag & R_SEC_FIELD) && (ibuf->flags & IB_fields) )
1759
1819
                ibuf->rect-= (ibuf->x*ibuf->y);
 
1820
 
 
1821
        ima->flag|= IMA_USED_FOR_RENDER;
1760
1822
}
1761
1823
 
1762
1824
void ibuf_sample(ImBuf *ibuf, float fx, float fy, float dx, float dy, float *result)
1764
1826
        TexResult texres;
1765
1827
        afdata_t AFD;
1766
1828
        
1767
 
        if(ibuf==NULL) {
 
1829
        if (ibuf==NULL) {
1768
1830
                return;
1769
1831
        }
1770
1832
        
1784
1846
        result[1]= texres.tg;
1785
1847
        result[2]= texres.tb;
1786
1848
        result[3]= texres.ta;
1787
 
}
 
 
b'\\ No newline at end of file'
 
1849
}