svn merge https://svn.blender.org/svnroot/bf-blender/trunk/blender -r17405:HEAD
[blender.git] / source / blender / src / imagepaint.c
1 /**
2  * $Id$
3  * imagepaint.c
4  *
5  * Functions to paint images in 2D and 3D.
6  * 
7  * ***** BEGIN GPL LICENSE BLOCK *****
8  *
9  * This program is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU General Public License
11  * as published by the Free Software Foundation; either version 2
12  * of the License, or (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software Foundation,
21  * Inc., 59 Temple Place - Suite 330, Boston, MA        02111-1307, USA.
22  *
23  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
24  * All rights reserved.
25  *
26  * The Original Code is: some of this file.
27  *
28  * Contributor(s): Jens Ole Wund (bjornmose), Campbell Barton (ideasman42)
29  *
30  * ***** END GPL LICENSE BLOCK *****
31  */
32
33 #include <string.h>
34 #include <stdio.h>
35 #include <math.h>
36
37 #ifdef HAVE_CONFIG_H
38 #include <config.h>
39 #endif
40
41 #include "MEM_guardedalloc.h"
42
43 #ifdef WIN32
44 #include "BLI_winstuff.h"
45 #endif
46 #include "BLI_arithb.h"
47 #include "BLI_blenlib.h"
48 #include "BLI_dynstr.h"
49 #include "BLI_linklist.h"
50 #include "BLI_memarena.h"
51 #include "PIL_time.h"
52 #include "BLI_threads.h"
53
54 #include "IMB_imbuf.h"
55 #include "IMB_imbuf_types.h"
56
57 #include "DNA_brush_types.h"
58 #include "DNA_image_types.h"
59 #include "DNA_mesh_types.h"
60 #include "DNA_meshdata_types.h"
61 #include "DNA_node_types.h"
62 #include "DNA_object_types.h"
63 #include "DNA_scene_types.h"
64 #include "DNA_screen_types.h"
65 #include "DNA_space_types.h"
66 #include "DNA_userdef_types.h"
67 #include "DNA_view3d_types.h"
68 #include "DNA_gpencil_types.h"
69
70 #include "BKE_brush.h"
71 #include "BKE_global.h"
72 #include "BKE_image.h"
73 #include "BKE_main.h"
74 #include "BKE_mesh.h"
75 #include "BKE_node.h"
76 #include "BKE_utildefines.h"
77 #include "BKE_DerivedMesh.h"
78
79 #include "BIF_interface.h"
80 #include "BIF_mywindow.h"
81 #include "BIF_screen.h"
82 #include "BIF_space.h"
83 #include "BIF_toolbox.h"
84
85 #include "BIF_editview.h" /* only for mouse_cursor - could remove this later */
86
87 #include "BSE_drawipo.h"
88 #include "BSE_node.h"
89 #include "BSE_trans_types.h"
90 #include "BSE_view.h"
91
92 #include "BDR_imagepaint.h"
93 #include "BDR_vpaint.h"
94 #include "BDR_gpencil.h"
95 #include "GPU_draw.h"
96
97 #include "GHOST_Types.h"
98
99 #include "blendef.h"
100 #include "butspace.h"
101 #include "mydevice.h"
102
103 /* Defines and Structs */
104
105 #define IMAPAINT_CHAR_TO_FLOAT(c) ((c)/255.0f)
106
107 #define IMAPAINT_FLOAT_RGB_TO_CHAR(c, f) { (c)[0]=FTOCHAR((f)[0]); (c)[1]=FTOCHAR((f)[1]); (c)[2]=FTOCHAR((f)[2]); }
108 #define IMAPAINT_FLOAT_RGBA_TO_CHAR(c, f) { (c)[0]=FTOCHAR((f)[0]); (c)[1]=FTOCHAR((f)[1]); (c)[2]=FTOCHAR((f)[2]); (c)[3]=FTOCHAR((f)[3]); }
109
110 #define IMAPAINT_CHAR_RGB_TO_FLOAT(f, c) { (f)[0]=IMAPAINT_CHAR_TO_FLOAT((c)[0]); (f)[1]=IMAPAINT_CHAR_TO_FLOAT((c)[1]); (f)[2]=IMAPAINT_CHAR_TO_FLOAT((c)[2]); }
111 #define IMAPAINT_CHAR_RGBA_TO_FLOAT(f, c) { (f)[0]=IMAPAINT_CHAR_TO_FLOAT((c)[0]); (f)[1]=IMAPAINT_CHAR_TO_FLOAT((c)[1]); (f)[2]=IMAPAINT_CHAR_TO_FLOAT((c)[2]); (f)[3]=IMAPAINT_CHAR_TO_FLOAT((c)[3]); }
112 #define IMAPAINT_FLOAT_RGB_COPY(a, b) VECCOPY(a, b)
113
114 #define IMAPAINT_TILE_BITS                      6
115 #define IMAPAINT_TILE_SIZE                      (1 << IMAPAINT_TILE_BITS)
116 #define IMAPAINT_TILE_NUMBER(size)      (((size)+IMAPAINT_TILE_SIZE-1) >> IMAPAINT_TILE_BITS)
117
118 #define MAXUNDONAME     64
119
120 typedef struct ImagePaintState {
121         Brush *brush;
122         short tool, blend;
123         Image *image;
124         ImBuf *canvas;
125         ImBuf *clonecanvas;
126         short clonefreefloat;
127         char *warnpackedfile;
128         char *warnmultifile;
129
130         /* texture paint only */
131         Object *ob;
132         Mesh *me;
133         int faceindex;
134         float uv[2];
135 } ImagePaintState;
136
137 typedef struct ImagePaintPartialRedraw {
138         int x1, y1, x2, y2;
139         int enabled;
140 } ImagePaintPartialRedraw;
141
142 /* testing options */
143 #define PROJ_BUCKET_DIV 128 /* TODO - test other values, this is a guess, seems ok */
144 #define PROJ_BOUNDBOX_DIV 6 /* TODO - test other values, this is a guess, seems ok */
145 #define PROJ_BOUNDBOX_SQUARED  (PROJ_BOUNDBOX_DIV * PROJ_BOUNDBOX_DIV)
146
147 //#define PROJ_DEBUG_PAINT 1
148 #define PROJ_DEBUG_NOSCANLINE 1
149 //#define PROJ_DEBUG_NOSEAMBLEED 1
150 //#define PROJ_DEBUG_PRINT_THREADS 1
151
152 /* projectFaceSeamFlags options */
153 //#define PROJ_FACE_IGNORE      1<<0    /* When the face is hidden, backfacing or occluded */
154 //#define PROJ_FACE_INIT        1<<1    /* When we have initialized the faces data */
155 #define PROJ_FACE_SEAM1 1<<0    /* If this face has a seam on any of its edges */
156 #define PROJ_FACE_SEAM2 1<<1
157 #define PROJ_FACE_SEAM3 1<<2
158 #define PROJ_FACE_SEAM4 1<<3
159
160 #define PROJ_FACE_NOSEAM1       1<<4
161 #define PROJ_FACE_NOSEAM2       1<<5
162 #define PROJ_FACE_NOSEAM3       1<<6
163 #define PROJ_FACE_NOSEAM4       1<<7
164
165 #define PROJ_BUCKET_NULL                0
166 #define PROJ_BUCKET_INIT                1<<0
167 // #define PROJ_BUCKET_CLONE_INIT       1<<1
168
169 /* only for readability */
170 #define PROJ_BUCKET_LEFT                0
171 #define PROJ_BUCKET_RIGHT       1
172 #define PROJ_BUCKET_BOTTOM      2
173 #define PROJ_BUCKET_TOP         3
174
175 typedef struct ProjectPaintState {
176         Brush *brush;
177         short tool, blend;
178         Object *ob;
179         /* end similarities with ImagePaintState */
180         
181         DerivedMesh    *dm;
182         int                     dm_totface;
183         int                     dm_totvert;
184         
185         MVert              *dm_mvert;
186         MFace              *dm_mface;
187         MTFace             *dm_mtface;
188         
189         /* projection painting only */
190         MemArena *projectArena;                 /* use for alocating many pixel structs and link-lists */
191         MemArena *projectArena_mt[BLENDER_MAX_THREADS];         /* Same as above but use for multithreading */
192         LinkNode **projectBuckets;              /* screen sized 2D array, each pixel has a linked list of ProjectPixel's */
193         LinkNode **projectFaces;                /* projectBuckets alligned array linkList of faces overlapping each bucket */
194         char *projectBucketFlags;               /* store if the bucks have been initialized  */
195 #ifndef PROJ_DEBUG_NOSEAMBLEED
196         char *projectFaceSeamFlags;             /* store info about faces, if they are initialized etc*/
197         float (*projectFaceSeamUVs)[4][2];      /* expanded UVs for faces to use as seams */
198         LinkNode **projectVertFaces;    /* Only needed for when projectSeamBleed is enabled, use to find UV seams */
199 #endif
200         int bucketsX;                                   /* The size of the bucket grid, the grid span's viewMin2D/viewMax2D so you can paint outsize the screen or with 2 brushes at once */
201         int bucketsY;
202         
203         Image **projectImages;                  /* array of images we are painting onto while, use so we can tag for updates */
204         ImBuf **projectImBufs;                  /* array of imbufs we are painting onto while, use so we can get the rect and rect_float quickly */
205         ImagePaintPartialRedraw *projectPartialRedraws[PROJ_BOUNDBOX_SQUARED];  /* array of partial redraws */
206         
207         int projectImageTotal;                  /* size of projectImages array */
208         
209         float (*projectVertScreenCos)[4];       /* verts projected into floating point screen space */
210         
211         /* options for projection painting */
212         short projectIsOcclude;                 /* Use raytraced occlusion? - ortherwise will paint right through to the back*/
213         short projectIsBackfaceCull;    /* ignore faces with normals pointing away, skips a lot of raycasts if your normals are correctly flipped */
214         short projectIsOrtho;
215 #ifndef PROJ_DEBUG_NOSEAMBLEED
216         float projectSeamBleed;
217 #endif
218         /* clone vars */
219         float cloneOfs[2];
220         
221         
222         float projectMat[4][4];         /* Projection matrix, use for getting screen coords */
223         float viewMat[4][4];
224         float viewDir[3];                       /* View vector, use for projectIsBackfaceCull and for ray casting with an ortho viewport  */
225         
226         float viewMin2D[2];                     /* 2D bounds for mesh verts on the screen's plane (screenspace) */
227         float viewMax2D[2]; 
228         float viewWidth;                        /* Calculated from viewMin2D & viewMax2D */
229         float viewHeight;
230         
231         /* threads */
232         int thread_tot;
233         int min_bucket[2];
234         int max_bucket[2];
235         int context_bucket_x, context_bucket_y; /* must lock threads while accessing these */
236 } ProjectPaintState;
237
238 #ifndef PROJ_DEBUG_NOSCANLINE
239 typedef struct ProjectScanline {
240         int v[3]; /* verts for this scanline, 0,1,2 or 0,2,3 */
241         float x_limits[2]; /* UV min|max for this scanline */
242 } ProjectScanline;
243 #endif
244
245 typedef struct ProjectPixel {
246         float projCo2D[2]; /* the floating point screen projection of this pixel */
247         short x_px, y_px;
248         void *pixel;
249         short image_index; /* if anyone wants to paint onto more then 32000 images they can bite me */
250         short bb_cell_index;
251 } ProjectPixel;
252
253 typedef struct ProjectPixelClone {
254         struct ProjectPixel __pp;
255         char clonepx[4];
256 } ProjectPixelClone;
257
258 typedef struct ProjectPixelCloneFloat {
259         struct ProjectPixel __pp;
260         float clonepx[4];
261 } ProjectPixelCloneFloat;
262
263 /* Finish projection painting structs */
264
265
266 typedef struct UndoTile {
267         struct UndoTile *next, *prev;
268         ID id;
269         void *rect;
270         int x, y;
271 } UndoTile;
272
273 typedef struct UndoElem {
274         struct UndoElem *next, *prev;
275         char name[MAXUNDONAME];
276         unsigned long undosize;
277
278         ImBuf *ibuf;
279         ListBase tiles;
280 } UndoElem;
281
282 static ListBase undobase = {NULL, NULL};
283 static UndoElem *curundo = NULL;
284 static ImagePaintPartialRedraw imapaintpartial = {0, 0, 0, 0, 0};
285
286 /* UNDO */
287
288 /* internal functions */
289
290 static void undo_copy_tile(UndoTile *tile, ImBuf *tmpibuf, ImBuf *ibuf, int restore)
291 {
292         /* copy or swap contents of tile->rect and region in ibuf->rect */
293         IMB_rectcpy(tmpibuf, ibuf, 0, 0, tile->x*IMAPAINT_TILE_SIZE,
294                 tile->y*IMAPAINT_TILE_SIZE, IMAPAINT_TILE_SIZE, IMAPAINT_TILE_SIZE);
295
296         if(ibuf->rect_float) SWAP(void*, tmpibuf->rect_float, tile->rect)
297         else SWAP(void*, tmpibuf->rect, tile->rect)
298         
299         if(restore)
300                 IMB_rectcpy(ibuf, tmpibuf, tile->x*IMAPAINT_TILE_SIZE,
301                         tile->y*IMAPAINT_TILE_SIZE, 0, 0, IMAPAINT_TILE_SIZE, IMAPAINT_TILE_SIZE);
302 }
303
304 static void undo_restore(UndoElem *undo)
305 {
306         Image *ima = NULL;
307         ImBuf *ibuf, *tmpibuf;
308         UndoTile *tile;
309
310         if(!undo)
311                 return;
312
313         tmpibuf= IMB_allocImBuf(IMAPAINT_TILE_SIZE, IMAPAINT_TILE_SIZE, 32,
314                                 IB_rectfloat|IB_rect, 0);
315         
316         for(tile=undo->tiles.first; tile; tile=tile->next) {
317                 /* find image based on name, pointer becomes invalid with global undo */
318                 if(ima && strcmp(tile->id.name, ima->id.name)==0);
319                 else {
320                         for(ima=G.main->image.first; ima; ima=ima->id.next)
321                                 if(strcmp(tile->id.name, ima->id.name)==0)
322                                         break;
323                 }
324
325                 ibuf= BKE_image_get_ibuf(ima, NULL);
326
327                 if (!ima || !ibuf || !(ibuf->rect || ibuf->rect_float))
328                         continue;
329
330                 undo_copy_tile(tile, tmpibuf, ibuf, 1);
331
332                 GPU_free_image(ima); /* force OpenGL reload */
333                 if(ibuf->rect_float)
334                         imb_freerectImBuf(ibuf); /* force recreate of char rect */
335         }
336
337         IMB_freeImBuf(tmpibuf);
338 }
339
340 static void undo_free(UndoElem *undo)
341 {
342         UndoTile *tile;
343
344         for(tile=undo->tiles.first; tile; tile=tile->next)
345                 MEM_freeN(tile->rect);
346         BLI_freelistN(&undo->tiles);
347 }
348
349 static void undo_imagepaint_push_begin(char *name)
350 {
351         UndoElem *uel;
352         int nr;
353         
354         /* Undo push is split up in begin and end, the reason is that as painting
355          * happens more tiles are added to the list, and at the very end we know
356          * how much memory the undo used to remove old undo elements */
357
358         /* remove all undos after (also when curundo==NULL) */
359         while(undobase.last != curundo) {
360                 uel= undobase.last;
361                 undo_free(uel);
362                 BLI_freelinkN(&undobase, uel);
363         }
364         
365         /* make new */
366         curundo= uel= MEM_callocN(sizeof(UndoElem), "undo file");
367         BLI_addtail(&undobase, uel);
368
369         /* name can be a dynamic string */
370         strncpy(uel->name, name, MAXUNDONAME-1);
371         
372         /* limit amount to the maximum amount*/
373         nr= 0;
374         uel= undobase.last;
375         while(uel) {
376                 nr++;
377                 if(nr==U.undosteps) break;
378                 uel= uel->prev;
379         }
380         if(uel) {
381                 while(undobase.first!=uel) {
382                         UndoElem *first= undobase.first;
383                         undo_free(first);
384                         BLI_freelinkN(&undobase, first);
385                 }
386         }
387 }
388
389 static void undo_imagepaint_push_end()
390 {
391         UndoElem *uel;
392         unsigned long totmem, maxmem;
393
394         if(U.undomemory != 0) {
395                 /* limit to maximum memory (afterwards, we can't know in advance) */
396                 totmem= 0;
397                 maxmem= ((unsigned long)U.undomemory)*1024*1024;
398
399                 uel= undobase.last;
400                 while(uel) {
401                         totmem+= uel->undosize;
402                         if(totmem>maxmem) break;
403                         uel= uel->prev;
404                 }
405
406                 if(uel) {
407                         while(undobase.first!=uel) {
408                                 UndoElem *first= undobase.first;
409                                 undo_free(first);
410                                 BLI_freelinkN(&undobase, first);
411                         }
412                 }
413         }
414 }
415
416
417 static int project_paint_BucketOffset(ProjectPaintState *ps, float projCo2D[2])
418 {
419         /* If we were not dealing with screenspace 2D coords we could simple do...
420          * ps->projectBuckets[x + (y*ps->bucketsY)] */
421         
422         /* please explain?
423          * projCo2D[0] - ps->viewMin2D[0]       : zero origin
424          * ... / ps->viewWidth                          : range from 0.0 to 1.0
425          * ... * ps->bucketsX           : use as a bucket index
426          *
427          * Second multiplication does similar but for vertical offset
428          */
429         return  (       (int)(( (projCo2D[0] - ps->viewMin2D[0]) / ps->viewWidth)  * ps->bucketsX)) + 
430                 (       (       (int)(( (projCo2D[1] - ps->viewMin2D[1])  / ps->viewHeight) * ps->bucketsY)) * ps->bucketsX );
431 }
432
433 static int project_paint_BucketOffsetSafe(ProjectPaintState *ps, float projCo2D[2])
434 {
435         int bucket_index = project_paint_BucketOffset(ps, projCo2D);
436         
437         if (bucket_index < 0 || bucket_index >= ps->bucketsX*ps->bucketsY) {    
438                 return -1;
439         } else {
440                 return bucket_index;
441         }
442 }
443
444 /* The point must be inside the triangle */
445 static void BarycentricWeightsSimple2f(float v1[2], float v2[2], float v3[2], float pt[2], float w[3]) {
446         float wtot;
447         w[0] = AreaF2Dfl(v2, v3, pt);
448         w[1] = AreaF2Dfl(v3, v1, pt);
449         w[2] = AreaF2Dfl(v1, v2, pt);
450         wtot = w[0]+w[1]+w[2];
451         if (wtot > 0.0) { /* just incase */
452                 w[0]/=wtot;
453                 w[1]/=wtot;
454                 w[2]/=wtot;
455         } else {
456                 printf("WATCH oUT ZAREA FACE\n");
457                 w[0] = w[1] = w[2] = 1.0/3.0; /* dummy values for zero area face */
458         }
459 }
460
461 /* also works for points outside the triangle */
462 #define SIDE_OF_LINE(pa,pb,pp)  ((pa[0]-pp[0])*(pb[1]-pp[1]))-((pb[0]-pp[0])*(pa[1]-pp[1]))
463 static void BarycentricWeights2f(float v1[2], float v2[2], float v3[2], float pt[2], float w[3]) {
464         float wtot = AreaF2Dfl(v1, v2, v3);
465         if (wtot > 0.0) {
466                 w[0] = AreaF2Dfl(v2, v3, pt);
467                 w[1] = AreaF2Dfl(v3, v1, pt);
468                 w[2] = AreaF2Dfl(v1, v2, pt);
469                 
470                 /* negate weights when 'pt' is on the outer side of the the triangles edge */
471                 if ((SIDE_OF_LINE(v2,v3, pt)>0.0) != (SIDE_OF_LINE(v2,v3, v1)>0.0))     w[0]/= -wtot;
472                 else                                                                                                                            w[0]/=  wtot;
473
474                 if ((SIDE_OF_LINE(v3,v1, pt)>0.0) != (SIDE_OF_LINE(v3,v1, v2)>0.0))     w[1]/= -wtot;
475                 else                                                                                                                            w[1]/=  wtot;
476
477                 if ((SIDE_OF_LINE(v1,v2, pt)>0.0) != (SIDE_OF_LINE(v1,v2, v3)>0.0))     w[2]/= -wtot;
478                 else                                                                                                                            w[2]/=  wtot;
479         } else {
480                 w[0] = w[1] = w[2] = 1.0/3.0; /* dummy values for zero area face */
481         }
482 }
483
484 /* still use 2D X,Y space but this works for verts transformed by a perspective matrix, using their 4th component as a weight */
485
486 static void BarycentricWeightsPersp2f(float v1[4], float v2[4], float v3[4], float pt[2], float w[3]) {
487         float persp_tot;
488         BarycentricWeights2f(v1,v2,v3,pt,w);
489         
490         w[0] /= v1[3];
491         w[1] /= v2[3];
492         w[2] /= v3[3];
493         
494         persp_tot = w[0]+w[1]+w[2];
495         
496         w[0] /= persp_tot;
497         w[1] /= persp_tot;
498         w[2] /= persp_tot;
499 }
500
501 static void BarycentricWeightsSimplePersp2f(float v1[4], float v2[4], float v3[4], float pt[2], float w[3]) {
502         float persp_tot;
503         BarycentricWeightsSimple2f(v1,v2,v3,pt,w);
504         
505         w[0] /= v1[3];
506         w[1] /= v2[3];
507         w[2] /= v3[3];
508         
509         persp_tot = w[0]+w[1]+w[2];
510         
511         w[0] /= persp_tot;
512         w[1] /= persp_tot;
513         w[2] /= persp_tot;
514 }
515
516 static float tri_depth_2d(float v1[3], float v2[3], float v3[3], float pt[2], float w[3])
517 {
518         BarycentricWeightsSimple2f(v1,v2,v3,pt,w);
519         return (v1[2]*w[0]) + (v2[2]*w[1]) + (v3[2]*w[2]);
520 }
521
522
523 /* return the topmost face  in screen coords index or -1
524  * bucket_index can be -1 if we dont know it to begin with */
525 static int screenco_pickface(ProjectPaintState *ps, float pt[2], float w[3], int *side) {
526         LinkNode *node;
527         float w_tmp[3];
528         float *v1, *v2, *v3, *v4;
529         int bucket_index;
530         int face_index;
531         int best_side = -1;
532         int best_face_index = -1;
533         float z_depth_best = MAXFLOAT, z_depth;
534         MFace *mf;
535         
536         bucket_index = project_paint_BucketOffsetSafe(ps, pt);
537         if (bucket_index==-1)
538                 return -1;
539         
540         node = ps->projectFaces[bucket_index];
541         
542         /* we could return 0 for 1 face buckets, as long as this function assumes
543          * that the point its testing is only every originated from an existing face */
544         
545         while (node) {
546                 face_index = (int)node->link;
547                 mf = ps->dm_mface + face_index;
548                 
549                 v1 = ps->projectVertScreenCos[mf->v1];
550                 v2 = ps->projectVertScreenCos[mf->v2];
551                 v3 = ps->projectVertScreenCos[mf->v3];
552                 
553                 if ( IsectPT2Df(pt, v1, v2, v3) ) {
554                         z_depth = tri_depth_2d(v1,v2,v3,pt,w_tmp);
555                         if (z_depth < z_depth_best) {
556                                 best_face_index = face_index;
557                                 best_side = 0;
558                                 z_depth_best = z_depth;
559                                 VECCOPY(w, w_tmp);
560                         }
561                 } else if (mf->v4) {
562                         v4 = ps->projectVertScreenCos[mf->v4];
563                         
564                         if ( IsectPT2Df(pt, v1, v3, v4) ) {
565                                 z_depth = tri_depth_2d(v1,v3,v4,pt,w_tmp);
566                                 if (z_depth < z_depth_best) {
567                                         best_face_index = face_index;
568                                         best_side = 1;
569                                         z_depth_best = z_depth;
570                                         VECCOPY(w, w_tmp);
571                                 }
572                         }
573                 }
574                 
575                 node = node->next;
576         }
577         
578         *side = best_side;
579         return best_face_index; /* will be -1 or a valid face */
580 }
581
582 /**************************************************************************
583 *                            INTERPOLATIONS 
584 *
585 * Reference and docs:
586 * http://wiki.blender.org/index.php/User:Damiles#Interpolations_Algorithms
587 ***************************************************************************/
588
589 /* BICUBIC Interpolation functions */
590 /*  More info: http://wiki.blender.org/index.php/User:Damiles#Bicubic_pixel_interpolation
591 */
592 /* function assumes out to be zero'ed, only does RGBA */
593 static float P(float k){
594         return (float)(1.0f/6.0f)*( pow( MAX2(k+2.0f,0) , 3.0f ) - 4.0f * pow( MAX2(k+1.0f,0) , 3.0f ) + 6.0f * pow( MAX2(k,0) , 3.0f ) - 4.0f * pow( MAX2(k-1.0f,0) , 3.0f));
595 }
596
597 static void bicubic_interpolation_px(ImBuf *in, float x, float y, float rgba_fp[4], char rgba[4])
598 {
599         int i,j,n,m,x1,y1;
600         unsigned char *dataI;
601         float a,b,w,wx,wy[4], outR,outG,outB,outA,*dataF;
602         int do_rect=0, do_float=0;
603
604         if (in == NULL) return;
605         if (in->rect == NULL && in->rect_float == NULL) return;
606
607         if (in->rect_float)     do_float = 1;
608         else                            do_rect = 1;
609         
610         i= (int)floor(x);
611         j= (int)floor(y);
612         a= x - i;
613         b= y - j;
614
615         outR= 0.0f;
616         outG= 0.0f;
617         outB= 0.0f;
618         outA= 0.0f;
619         
620         /* avoid calling multiple times */
621         wy[0] = P(b-(-1));
622         wy[1] = P(b-  0);
623         wy[2] = P(b-  1);
624         wy[3] = P(b-  2);
625         
626         for(n= -1; n<= 2; n++){
627                 x1= i+n;
628                 if (x1>0 && x1 < in->x) {
629                         wx = P(n-a);
630                         for(m= -1; m<= 2; m++){
631                                 y1= j+m;
632                                 if (y1>0 && y1<in->y) {
633                                         /* normally we could do this */
634                                         /* w = P(n-a) * P(b-m); */
635                                         /* except that would call P() 16 times per pixel therefor pow() 64 times, better precalc these */
636                                         w = wx * wy[m+1];
637                                         
638                                         if (do_float) {
639                                                 dataF= in->rect_float + in->x * y1 * 4 + 4*x1;
640                                                 outR+= dataF[0] * w;
641                                                 outG+= dataF[1] * w;
642                                                 outB+= dataF[2] * w;
643                                                 outA+= dataF[3] * w;
644                                         }
645                                         if (do_rect) {
646                                                 dataI= (unsigned char*)in->rect + in->x * y1 * 4 + 4*x1;
647                                                 outR+= dataI[0] * w;
648                                                 outG+= dataI[1] * w;
649                                                 outB+= dataI[2] * w;
650                                                 outA+= dataI[3] * w;
651                                         }
652                                 }
653                         }
654                 }
655         }
656         if (do_rect) {
657                 rgba[0]= (int)outR;
658                 rgba[1]= (int)outG;
659                 rgba[2]= (int)outB;
660                 rgba[3]= (int)outA;
661         }
662         if (do_float) {
663                 rgba_fp[0]= outR;
664                 rgba_fp[1]= outG;
665                 rgba_fp[2]= outB;
666                 rgba_fp[3]= outA;
667         }
668 }
669
670 /* bucket_index is optional, since in some cases we know it */
671 /* TODO FLOAT */ /* add a function that does this for float buffers */
672 static int screenco_pickcol(ProjectPaintState *ps, float pt[2], float *rgba_fp, char *rgba, int interp)
673 {
674         float w[3], uv[2];
675         int side;
676         int face_index;
677         MTFace *tf;
678         ImBuf *ibuf;
679         int xi,yi;
680         
681         
682         face_index = screenco_pickface(ps,pt,w, &side);
683         
684         if (face_index == -1)
685                 return 0;
686         
687         tf = ps->dm_mtface + face_index;
688         
689         if (side==0) {
690                 uv[0] = tf->uv[0][0]*w[0] + tf->uv[1][0]*w[1] + tf->uv[2][0]*w[2];
691                 uv[1] = tf->uv[0][1]*w[0] + tf->uv[1][1]*w[1] + tf->uv[2][1]*w[2];
692         } else { /* QUAD */
693                 uv[0] = tf->uv[0][0]*w[0] + tf->uv[2][0]*w[1] + tf->uv[3][0]*w[2];
694                 uv[1] = tf->uv[0][1]*w[0] + tf->uv[2][1]*w[1] + tf->uv[3][1]*w[2];
695         }
696         
697         ibuf = BKE_image_get_ibuf((Image *)tf->tpage, NULL); /* TODO - this may be slow */
698         
699
700         
701         if (interp) {
702                 float x,y;
703                 x = uv[0]*ibuf->x;
704                 y = uv[1]*ibuf->y;
705                 if (ibuf->rect_float) {
706                         if (rgba_fp) {
707                                 bicubic_interpolation_px(ibuf, x, y, rgba_fp, NULL);
708                         } else {
709                                 float rgba_tmp_fp[4];
710                                 bicubic_interpolation_px(ibuf, x, y, rgba_tmp_fp, NULL);
711                                 IMAPAINT_FLOAT_RGBA_TO_CHAR(rgba, rgba_tmp_fp);
712                         }
713                 } else {
714                         if (rgba) {
715                                 bicubic_interpolation_px(ibuf, x, y, NULL, rgba);
716                         } else {
717                                 char rgba_tmp[4];
718                                 bicubic_interpolation_px(ibuf, x, y, NULL, rgba_tmp);
719                                 IMAPAINT_CHAR_RGBA_TO_FLOAT( rgba_fp,  rgba_tmp);
720                         }
721                 }
722         } else {
723                 xi = uv[0]*ibuf->x;
724                 yi = uv[1]*ibuf->y;
725                 
726                 if (xi<0 || xi>=ibuf->x  ||  yi<0 || yi>=ibuf->y) return 0;
727                 
728                 if (rgba) {
729                         if (ibuf->rect_float) {
730                                 IMAPAINT_FLOAT_RGBA_TO_CHAR(rgba, ((( float * ) ibuf->rect_float) + (( xi + yi * ibuf->x ) * 4)));
731                         } else {
732                                 *((unsigned int *)rgba) = *(unsigned int *) ((( char * ) ibuf->rect) + (( xi + yi * ibuf->x ) * 4));
733                         }
734                 }
735                 
736                 if (rgba_fp) {
737                         if (ibuf->rect_float) {
738                                 QUATCOPY(rgba_fp, ((( float * ) ibuf->rect_float) + (( xi + yi * ibuf->x ) * 4)));
739                         } else {
740                                 IMAPAINT_CHAR_RGBA_TO_FLOAT( rgba_fp,  ((( char * ) ibuf->rect) + (( xi + yi * ibuf->x ) * 4)));
741                         }
742                 }
743         }
744         return 1;
745 }
746
747 /* return...
748  * 0    : no occlusion
749  * -1   : no occlusion but 2D intersection is true (avoid testing the other half of a quad)
750  * 1    : occluded */
751
752 static int screenco_tri_pt_occlude(float pt[3], float v1[3], float v2[3], float v3[3])
753 {
754         /* if all are behind us, return false */
755         if(v1[2] > pt[2] && v2[2] > pt[2] && v3[2] > pt[2])
756                 return 0;
757                 
758         /* do a 2D point in try intersection */
759         if ( !IsectPT2Df(pt, v1, v2, v3) )
760                 return 0; /* we know there is  */
761         
762         /* From here on we know there IS an intersection */
763         
764         /* if ALL of the verts are infront of us then we know it intersects ? */
765         if(     v1[2] < pt[2] && v2[2] < pt[2] && v3[2] < pt[2]) {
766                 return 1;
767         } else {
768                 float w[3];
769                 /* we intersect? - find the exact depth at the point of intersection */
770                 if (tri_depth_2d(v1,v2,v3,pt,w) < pt[2]) {
771                         return 1; /* This point is occluded by another face */
772                 }
773         }
774         return -1;
775 }
776
777
778 /* check, pixelScreenCo must be in screenspace, its Z-Depth only needs to be used for comparison */
779 static int project_bucket_point_occluded(ProjectPaintState *ps, int bucket_index, int orig_face, float pixelScreenCo[4])
780 {
781         LinkNode *node = ps->projectFaces[bucket_index];
782         MFace *mf;
783         int face_index;
784         int isect_ret;
785         
786         /* we could return 0 for 1 face buckets, as long as this function assumes
787          * that the point its testing is only every originated from an existing face */
788         
789         while (node) {
790                 face_index = (int)node->link;
791                 
792                 if (orig_face != face_index) {
793                         
794                         mf = ps->dm_mface + face_index;
795                         
796                         isect_ret = screenco_tri_pt_occlude(
797                                         pixelScreenCo,
798                                         ps->projectVertScreenCos[mf->v1],
799                                         ps->projectVertScreenCos[mf->v2],
800                                         ps->projectVertScreenCos[mf->v3]);
801                         
802                         /* Note, if isect_ret==-1 then we dont want to test the other side of the quad */
803                         if (isect_ret==0 && mf->v4) {
804                                 isect_ret = screenco_tri_pt_occlude(
805                                                 pixelScreenCo,
806                                                 ps->projectVertScreenCos[mf->v1],
807                                                 ps->projectVertScreenCos[mf->v3],
808                                                 ps->projectVertScreenCos[mf->v4]);
809                         }
810                         
811                         if (isect_ret==1) {
812 #if 0
813                                 /* Cheap Optimization!
814                                  * This function runs for every UV Screen pixel,
815                                  * therefor swapping the swapping the faces for this buckets list helps because
816                                  * the next ~5 to ~200 runs will can hit the first face each time. */
817                                 if (ps->projectFaces[bucket_index] != node) {
818                                         /* SWAP(void *, ps->projectFaces[bucket_index]->link, node->link); */
819                                         
820                                         /* dont need to use swap since we alredy have face_index */
821                                         node->link = ps->projectFaces[bucket_index]->link; /* move the value item to the current value */
822                                         ps->projectFaces[bucket_index]->link = (void *) face_index;
823                                         
824                                         /*printf("swapping %d %d\n", (int)node->link, face_index);*/
825                                 } /*else {
826                                         printf("first hit %d\n", face_index);
827                                 }*/
828 #endif
829                                 return 1; 
830                         }
831                 }
832                 node = node->next;
833         }
834         
835         return 0;
836 }
837
838 /* basic line intersection, could move to arithb.c, 2 points with a horiz line
839  * 1 for an intersection, 2 if the first point is aligned, 3 if the second point is aligned */
840 #define ISECT_TRUE 1
841 #define ISECT_TRUE_P1 2
842 #define ISECT_TRUE_P2 3
843 static int line_isect_y(float p1[2], float p2[2], float y_level, float *x_isect)
844 {
845         if (y_level==p1[1]) {
846                 *x_isect = p1[0];
847                 return ISECT_TRUE_P1;
848         }
849         if (y_level==p2[1]) {
850                 *x_isect = p2[0];
851                 return ISECT_TRUE_P2;
852         }
853         
854         if (p1[1] > y_level && p2[1] < y_level) {
855                 *x_isect = (p2[0]*(p1[1]-y_level) + p1[0]*(y_level-p2[1])) / (p1[1]-p2[1]);
856                 return ISECT_TRUE;
857         } else if (p1[1] < y_level && p2[1] > y_level) {
858                 *x_isect = (p2[0]*(y_level-p1[1]) + p1[0]*(p2[1]-y_level)) / (p2[1]-p1[1]);
859                 return ISECT_TRUE;
860         } else {
861                 return 0;
862         }
863 }
864
865 static int line_isect_x(float p1[2], float p2[2], float x_level, float *y_isect)
866 {
867         if (x_level==p1[0]) {
868                 *y_isect = p1[1];
869                 return ISECT_TRUE_P1;
870         }
871         if (x_level==p2[0]) {
872                 *y_isect = p2[1];
873                 return ISECT_TRUE_P2;
874         }
875         
876         if (p1[0] > x_level && p2[0] < x_level) {
877                 *y_isect = (p2[1]*(p1[0]-x_level) + p1[1]*(x_level-p2[0])) / (p1[0]-p2[0]);
878                 return ISECT_TRUE;
879         } else if (p1[0] < x_level && p2[0] > x_level) {
880                 *y_isect = (p2[1]*(x_level-p1[0]) + p1[1]*(p2[0]-x_level)) / (p2[0]-p1[0]);
881                 return ISECT_TRUE;
882         } else {
883                 return 0;
884         }
885 }
886
887 #ifndef PROJ_DEBUG_NOSCANLINE
888 static int project_face_scanline(ProjectScanline *sc, float y_level, float v1[2], float v2[2], float v3[2], float v4[2])
889 {       
890         /* Create a scanlines for the face at this Y level 
891          * triangles will only ever have 1 scanline, quads may have 2 */
892         int totscanlines = 0;
893         short i1=0,i2=0,i3=0;
894         
895         if (v4) { /* This is a quad?*/
896                 int i4=0, i_mid=0;
897                 float xi1, xi2, xi3, xi4, xi_mid;
898                                 
899                 i1 = line_isect_y(v1, v2, y_level, &xi1);
900                 if (i1 != ISECT_TRUE_P2) /* rare cases we could be on the line, in these cases we dont want to intersect with the same point twice */
901                         i2 = line_isect_y(v2, v3, y_level, &xi2);
902                 
903                 if (i1 && i2) { /* both the first 2 edges intersect, this means the second half of the quad wont intersect */
904                         sc->v[0] = 0;
905                         sc->v[1] = 1;
906                         sc->v[2] = 2;
907                         sc->x_limits[0] = MIN2(xi1, xi2);
908                         sc->x_limits[1] = MAX2(xi1, xi2);
909                         totscanlines = 1;
910                 } else {
911                         if (i2 != ISECT_TRUE_P2) 
912                                 i3 = line_isect_y(v3, v4, y_level, &xi3);
913                         if (i1 != ISECT_TRUE_P1 && i3 != ISECT_TRUE_P2) 
914                                 i4 = line_isect_y(v4, v1, y_level, &xi4);
915                         
916                         if (i3 && i4) { /* second 2 edges only intersect, same as above */
917                                 sc->v[0] = 0;
918                                 sc->v[1] = 2;
919                                 sc->v[2] = 3;
920                                 sc->x_limits[0] = MIN2(xi3, xi4);
921                                 sc->x_limits[1] = MAX2(xi3, xi4);
922                                 totscanlines = 1;
923                         } else {
924                                 /* OK - we have a not-so-simple case, both sides of the quad intersect.
925                                  * Will need to have 2 scanlines */
926                                 if ((i1||i2) && (i3||i4)) {
927                                         i_mid = line_isect_y(v1, v3, y_level, &xi_mid);
928                                         /* it would be very rare this would be false, but possible */
929                                         sc->v[0] = 0;
930                                         sc->v[1] = 1;
931                                         sc->v[2] = 2;
932                                         sc->x_limits[0] = MIN2((i1?xi1:xi2), xi_mid);
933                                         sc->x_limits[1] = MAX2((i1?xi1:xi2), xi_mid);
934                                         
935                                         sc++;
936                                         sc->v[0] = 0;
937                                         sc->v[1] = 2;
938                                         sc->v[2] = 3;
939                                         sc->x_limits[0] = MIN2((i3?xi3:xi4), xi_mid);
940                                         sc->x_limits[1] = MAX2((i3?xi3:xi4), xi_mid);
941                                         
942                                         totscanlines = 2;
943                                 }
944                         }
945                 }
946         } else { /* triangle */
947                 int i = 0;
948                 
949                 i1 = line_isect_y(v1, v2, y_level, &sc->x_limits[0]);
950                 if (i1) i++;
951                 
952                 if (i1 != ISECT_TRUE_P2) {
953                         i2 = line_isect_y(v2, v3, y_level, &sc->x_limits[i]);
954                         if (i2) i++;
955                 }
956                 
957                 /* if the triangle intersects then the first 2 lines must */
958                 if (i!=0) {
959                         if (i!=2) {
960                                 /* if we are here then this really should not fail since 2 edges MUST intersect  */
961                                 if (i1 != ISECT_TRUE_P1 && i2 != ISECT_TRUE_P2) {
962                                         i3 = line_isect_y(v3, v1, y_level, &sc->x_limits[i]);
963                                         if (i3) i++;
964                                         
965                                 }
966                         }
967                         
968                         if (i==2) {
969                                 if (sc->x_limits[0] > sc->x_limits[1]) {
970                                         SWAP(float, sc->x_limits[0], sc->x_limits[1]);
971                                 }
972                                 sc->v[0] = 0;
973                                 sc->v[1] = 1;
974                                 sc->v[2] = 2;
975                                 totscanlines = 1;
976                         }
977                 }
978         }
979         /* done setting up scanlines */
980         return totscanlines;
981 }
982 #endif // PROJ_DEBUG_NOSCANLINE
983 static int cmp_uv(float vec2a[2], float vec2b[2])
984 {
985         return ((fabs(vec2a[0]-vec2b[0]) < 0.0001) && (fabs(vec2a[1]-vec2b[1]) < 0.0001)) ? 1:0;
986 }
987
988 /* return zero if there is no area in the returned rectangle */
989 static int uv_image_rect(float uv1[2], float uv2[2], float uv3[2], float uv4[2], int min_px[2], int max_px[2], int x_px, int y_px, int is_quad)
990 {
991         float min_uv[2], max_uv[2]; /* UV bounds */
992         
993         INIT_MINMAX2(min_uv, max_uv);
994         
995         DO_MINMAX2(uv1, min_uv, max_uv);
996         DO_MINMAX2(uv2, min_uv, max_uv);
997         DO_MINMAX2(uv3, min_uv, max_uv);
998         if (is_quad)
999                 DO_MINMAX2(uv4, min_uv, max_uv);
1000         
1001         min_px[0] = (int)(x_px * min_uv[0]);
1002         min_px[1] = (int)(y_px * min_uv[1]);
1003         
1004         max_px[0] = (int)(x_px * max_uv[0]) +1;
1005         max_px[1] = (int)(y_px * max_uv[1]) +1;
1006         
1007         /*printf("%d %d %d %d \n", min_px[0], min_px[1], max_px[0], max_px[1]);*/
1008         CLAMP(min_px[0], 0, x_px);
1009         CLAMP(max_px[0], 0, x_px);
1010         
1011         CLAMP(min_px[1], 0, y_px);
1012         CLAMP(max_px[1], 0, y_px);
1013         
1014         /* face uses no UV area when quantized to pixels? */
1015         return (min_px[0] == max_px[0] || min_px[1] == max_px[1]) ? 0 : 1;
1016 }
1017
1018 #ifndef PROJ_DEBUG_NOSEAMBLEED
1019 /* TODO - set the seam flag on the other face to avoid double lookups */
1020 static int check_seam(ProjectPaintState *ps, int orig_face, int orig_i1_fidx, int orig_i2_fidx, int *other_face, int *orig_fidx)
1021 {
1022         LinkNode *node;
1023         int face_index;
1024         int i, i1, i2;
1025         int i1_fidx = -1, i2_fidx = -1; /* indexi in face */
1026         MFace *orig_mf, *mf;
1027         MTFace *orig_tf, *tf;
1028         
1029         orig_mf = ps->dm_mface + orig_face;
1030         orig_tf = ps->dm_mtface + orig_face;
1031         
1032         /* vert indicies from face vert order indicies */
1033         i1 = (*(&orig_mf->v1 + orig_i1_fidx));
1034         i2 = (*(&orig_mf->v1 + orig_i2_fidx));
1035         
1036         for (node = ps->projectVertFaces[i1]; node; node = node->next) {
1037                 face_index = (int)node->link;
1038                 if (face_index != orig_face) {
1039                         mf = ps->dm_mface + face_index;
1040                         
1041                         /* We need to know the order of the verts in the adjacent face 
1042                          * set the i1_fidx and i2_fidx to (0,1,2,3) */
1043                         i = mf->v4 ? 3:2;
1044                         do {
1045                                 if (i1 == (*(&mf->v1 + i))) {
1046                                         i1_fidx = i;
1047                                 } else if (i2 == (*(&mf->v1 + i))) {
1048                                         i2_fidx = i;
1049                                 }
1050                                 
1051                         } while (i--);
1052                         
1053                         if (i2_fidx != -1) {
1054                                 /* This IS an adjacent face!, now lets check if the UVs are ok */
1055                                 
1056
1057                                 
1058                                 tf = ps->dm_mtface + face_index;
1059                                 
1060                                 /* set up the other face */
1061                                 *other_face = face_index;
1062                                 *orig_fidx = (i1_fidx < i2_fidx) ? i1_fidx : i2_fidx;
1063                                 
1064                                 /* first test if they have the same image */
1065                                 if (    (orig_tf->tpage == tf->tpage) &&
1066                                                 cmp_uv(orig_tf->uv[orig_i1_fidx], tf->uv[i1_fidx]) &&
1067                                                 cmp_uv(orig_tf->uv[orig_i2_fidx], tf->uv[i2_fidx]) )
1068                                 {
1069                                         // printf("SEAM (NONE)\n");
1070                                         return 0;
1071                                         
1072                                 } else {
1073                                         // printf("SEAM (UV GAP)\n");
1074                                         return 1;
1075                                 }
1076                         }
1077                 }
1078         }
1079         // printf("SEAM (NO FACE)\n");
1080         *other_face = -1;
1081         return 1;
1082 }
1083
1084
1085 static float angleToLength(float angle)
1086 {
1087         float x,y, fac;
1088         
1089         // Alredy accounted for
1090         if (angle < 0.000001)
1091                 return 1.0;
1092         
1093         angle = (2.0*M_PI/360.0) * angle;
1094         x = cos(angle);
1095         y = sin(angle);
1096         
1097         // print "YX", x,y
1098         // 0 d is hoz to the right.
1099         // 90d is vert upward.
1100         fac = 1.0/x;
1101         x = x*fac;
1102         y = y*fac;
1103         return sqrt((x*x)+(y*y));
1104 }
1105
1106 /* takes a faces UV's and assigns outset coords to outset_uv */
1107 static void uv_image_outset(float (*orig_uv)[2], float (*outset_uv)[2], float scaler, int x_px, int y_px, int is_quad)
1108 {
1109         float a1,a2,a3,a4=0.0;
1110         float puv[4][2]; /* pixelspace uv's */
1111         float no1[2], no2[2], no3[2], no4[2]; /* normals */
1112         float dir1[2], dir2[2], dir3[2], dir4[2];
1113         
1114         /* make UV's in pixel space so we can */
1115         puv[0][0] = orig_uv[0][0] * x_px;
1116         puv[0][1] = orig_uv[0][1] * y_px;
1117         
1118         puv[1][0] = orig_uv[1][0] * x_px;
1119         puv[1][1] = orig_uv[1][1] * y_px;
1120         
1121         puv[2][0] = orig_uv[2][0] * x_px;
1122         puv[2][1] = orig_uv[2][1] * y_px;
1123         
1124         if (is_quad) {
1125                 puv[3][0] = orig_uv[3][0] * x_px;
1126                 puv[3][1] = orig_uv[3][1] * y_px;
1127         }
1128         
1129         /* face edge directions */
1130         Vec2Subf(dir1, puv[1], puv[0]);
1131         Vec2Subf(dir2, puv[2], puv[1]);
1132         Normalize2(dir1);
1133         Normalize2(dir2);
1134         
1135         if (is_quad) {
1136                 Vec2Subf(dir3, puv[3], puv[2]);
1137                 Vec2Subf(dir4, puv[0], puv[3]);
1138                 Normalize2(dir3);
1139                 Normalize2(dir4);
1140         } else {
1141                 Vec2Subf(dir3, puv[0], puv[2]);
1142                 Normalize2(dir3);
1143         }
1144         
1145         if (is_quad) {
1146                 a1 = NormalizedVecAngle2_2D(dir4, dir1);
1147                 a2 = NormalizedVecAngle2_2D(dir1, dir2);
1148                 a3 = NormalizedVecAngle2_2D(dir2, dir3);
1149                 a4 = NormalizedVecAngle2_2D(dir3, dir4);
1150         } else {
1151                 a1 = NormalizedVecAngle2_2D(dir3, dir1);
1152                 a2 = NormalizedVecAngle2_2D(dir1, dir2);
1153                 a3 = NormalizedVecAngle2_2D(dir2, dir3);
1154         }
1155         
1156         a1 = angleToLength(a1);
1157         a2 = angleToLength(a2);
1158         a3 = angleToLength(a3);
1159         if (is_quad)
1160                 a4 = angleToLength(a4);
1161         
1162         if (is_quad) {
1163                 Vec2Subf(no1, dir4, dir1);
1164                 Vec2Subf(no2, dir1, dir2);
1165                 Vec2Subf(no3, dir2, dir3);
1166                 Vec2Subf(no4, dir3, dir4);
1167                 Normalize2(no1);
1168                 Normalize2(no2);
1169                 Normalize2(no3);
1170                 Normalize2(no4);
1171                 Vec2Mulf(no1, a1*scaler);
1172                 Vec2Mulf(no2, a2*scaler);
1173                 Vec2Mulf(no3, a3*scaler);
1174                 Vec2Mulf(no4, a4*scaler);
1175                 Vec2Addf(outset_uv[0], puv[0], no1);
1176                 Vec2Addf(outset_uv[1], puv[1], no2);
1177                 Vec2Addf(outset_uv[2], puv[2], no3);
1178                 Vec2Addf(outset_uv[3], puv[3], no4);
1179                 outset_uv[0][0] /= x_px;
1180                 outset_uv[0][1] /= y_px;
1181                 
1182                 outset_uv[1][0] /= x_px;
1183                 outset_uv[1][1] /= y_px;
1184                 
1185                 outset_uv[2][0] /= x_px;
1186                 outset_uv[2][1] /= y_px;
1187                 
1188                 outset_uv[3][0] /= x_px;
1189                 outset_uv[3][1] /= y_px;
1190         } else {
1191                 Vec2Subf(no1, dir3, dir1);
1192                 Vec2Subf(no2, dir1, dir2);
1193                 Vec2Subf(no3, dir2, dir3);
1194                 Normalize2(no1);
1195                 Normalize2(no2);
1196                 Normalize2(no3);
1197                 Vec2Mulf(no1, a1*scaler);
1198                 Vec2Mulf(no2, a2*scaler);
1199                 Vec2Mulf(no3, a3*scaler);
1200                 Vec2Addf(outset_uv[0], puv[0], no1);
1201                 Vec2Addf(outset_uv[1], puv[1], no2);
1202                 Vec2Addf(outset_uv[2], puv[2], no3);
1203                 outset_uv[0][0] /= x_px;
1204                 outset_uv[0][1] /= y_px;
1205                 
1206                 outset_uv[1][0] /= x_px;
1207                 outset_uv[1][1] /= y_px;
1208                 
1209                 outset_uv[2][0] /= x_px;
1210                 outset_uv[2][1] /= y_px;
1211         }
1212 }
1213
1214 /* 
1215  * Be tricky with flags, first 4 bits are PROJ_FACE_SEAM1 to 4, last 4 bits are PROJ_FACE_NOSEAM1 to 4
1216  * 1<<i - where i is (0-3) 
1217  * 
1218  * If we're multithreadng, make sure threads are locked when this is called
1219  */
1220 static void project_face_seams_init(ProjectPaintState *ps, int face_index, int is_quad)
1221 {
1222         int other_face, other_fidx; /* vars for the other face, we also set its flag */
1223         int fidx1, fidx2;
1224         
1225         fidx1 = is_quad ? 3 : 2;
1226         do {
1227                 if (is_quad)
1228                         fidx2 = (fidx1==3) ? 0 : fidx1+1; /* next fidx in the face (0,1,2,3) -> (1,2,3,0) */
1229                 else
1230                         fidx2 = (fidx1==2) ? 0 : fidx1+1; /* next fidx in the face (0,1,2) -> (1,2,0) */
1231                 
1232                 if ((ps->projectFaceSeamFlags[face_index] & (1<<fidx1|16<<fidx1)) == 0) {
1233                         if (check_seam(ps, face_index, fidx1,fidx2, &other_face, &other_fidx)) {
1234                                 ps->projectFaceSeamFlags[face_index] |= 1<<fidx1;
1235                                 if (other_face != -1)
1236                                         ps->projectFaceSeamFlags[other_face] |= 1<<other_fidx;
1237                         } else {
1238                                 ps->projectFaceSeamFlags[face_index] |= 16<<fidx1;
1239                                 if (other_face != -1)
1240                                         ps->projectFaceSeamFlags[other_face] |= 16<<other_fidx; /* second 4 bits for disabled */
1241                         }
1242                 }
1243         } while (fidx1--);
1244 }
1245 #endif // PROJ_DEBUG_NOSEAMBLEED
1246
1247
1248 /* TODO - move to arithb.c */
1249
1250 /* little sister we only need to know lambda */
1251 static float lambda_cp_line2(float p[2], float l1[2], float l2[2])
1252 {
1253         float h[2],u[2];
1254         Vec2Subf(u, l2, l1);
1255         Vec2Subf(h, p, l1);
1256         return(Inp2f(u,h)/Inp2f(u,u));
1257 }
1258
1259 static void screen_px_from_ortho(
1260                 ProjectPaintState *ps, float uv[2],
1261                 float v1co[3], float v2co[3], float v3co[3], /* Screenspace coords */
1262                 float uv1co[2], float uv2co[2], float uv3co[2],
1263                 float pixelScreenCo[4] )
1264 {
1265         float w[3];
1266         BarycentricWeightsSimple2f(uv1co,uv2co,uv3co,uv,w);
1267         pixelScreenCo[0] = v1co[0]*w[0] + v2co[0]*w[1] + v3co[0]*w[2];
1268         pixelScreenCo[1] = v1co[1]*w[0] + v2co[1]*w[1] + v3co[1]*w[2];
1269         pixelScreenCo[2] = v1co[2]*w[0] + v2co[2]*w[1] + v3co[2]*w[2];  
1270 }
1271
1272 static void screen_px_from_persp(
1273                 ProjectPaintState *ps, float uv[2],
1274                 float v1co[3], float v2co[3], float v3co[3], /* Worldspace coords */
1275                 float uv1co[2], float uv2co[2], float uv3co[2],
1276                 float pixelScreenCo[4])
1277 {
1278         float w[3];
1279         BarycentricWeightsSimple2f(uv1co,uv2co,uv3co,uv,w);
1280         pixelScreenCo[0] = v1co[0]*w[0] + v2co[0]*w[1] + v3co[0]*w[2];
1281         pixelScreenCo[1] = v1co[1]*w[0] + v2co[1]*w[1] + v3co[1]*w[2];
1282         pixelScreenCo[2] = v1co[2]*w[0] + v2co[2]*w[1] + v3co[2]*w[2];
1283         pixelScreenCo[3] = 1.0;
1284         
1285         Mat4MulVec4fl(ps->projectMat, pixelScreenCo);
1286         
1287         // if( pixelScreenCo[3] > 0.001 ) { ??? TODO
1288         /* screen space, not clamped */
1289         pixelScreenCo[0] = (float)(curarea->winx/2.0)+(curarea->winx/2.0)*pixelScreenCo[0]/pixelScreenCo[3];    
1290         pixelScreenCo[1] = (float)(curarea->winy/2.0)+(curarea->winy/2.0)*pixelScreenCo[1]/pixelScreenCo[3];
1291         pixelScreenCo[2] = pixelScreenCo[2]/pixelScreenCo[3]; /* Use the depth for bucket point occlusion */
1292 }
1293
1294
1295 /* Only run this function once for new ProjectPixelClone's */
1296 #define pixel_size 4
1297
1298 static void project_paint_uvpixel_init(ProjectPaintState *ps, int thread_index, ImBuf *ibuf, short x, short y, int bucket_index, int face_index, int image_index, float pixelScreenCo[4])
1299 {
1300         ProjectPixel *projPixel;
1301         short size;
1302         
1303         // printf("adding px (%d %d), (%f %f)\n", x,y,uv[0],uv[1]);
1304         
1305         
1306         /* Use viewMin2D to make (0,0) the bottom left of the bounds 
1307          * Then this can be used to index the bucket array */
1308         
1309         /* Is this UV visible from the view? - raytrace */
1310         /* screenco_pickface is less complex, use for testing */
1311         //if (screenco_pickface(ps, pixelScreenCo, w, &side) == face_index) {
1312         if (ps->projectIsOcclude==0 || !project_bucket_point_occluded(ps, bucket_index, face_index, pixelScreenCo)) {
1313                 
1314                 if (ps->tool==PAINT_TOOL_CLONE) {
1315                         if (ibuf->rect_float) {
1316                                 size = sizeof(ProjectPixelCloneFloat);
1317                         } else {
1318                                 size = sizeof(ProjectPixelClone);
1319                         }
1320                 } else if (ps->tool==PAINT_TOOL_SMEAR) {
1321                         size = sizeof(ProjectPixelClone);
1322                 } else {
1323                         size = sizeof(ProjectPixel);
1324                 }
1325                 
1326                 projPixel = (ProjectPixel *)BLI_memarena_alloc(ps->projectArena_mt[thread_index], size);
1327                 
1328                 if (ibuf->rect_float) {
1329                         projPixel->pixel = (void *) ((( float * ) ibuf->rect_float) + (( x + y * ibuf->x ) * pixel_size));
1330                 } else {
1331                         projPixel->pixel = (void *) ((( char * ) ibuf->rect) + (( x + y * ibuf->x ) * pixel_size));
1332                 }
1333                 
1334                 VECCOPY2D(projPixel->projCo2D, pixelScreenCo);
1335                 projPixel->x_px = x;
1336                 projPixel->y_px = y;
1337                 
1338                 /* which bounding box cell are we in? */
1339                 //projPixel->x_bb = (char) ;
1340                 //projPixel->y_bb = (char) (((float)y/(float)ibuf->y) * PROJ_BOUNDBOX_DIV);
1341                 
1342                 projPixel->bb_cell_index = ((int)((((float)x)/((float)ibuf->x)) * PROJ_BOUNDBOX_DIV)) + ((int)((((float)y)/((float)ibuf->y)) * PROJ_BOUNDBOX_DIV)) * PROJ_BOUNDBOX_DIV ;
1343                 
1344                 /* done with view3d_project_float inline */
1345                 if (ps->tool==PAINT_TOOL_CLONE) {
1346                         float co[2];
1347                         
1348                         /* Initialize clone pixels - note that this is a bit of a waste since some of these are being indirectly initialized :/ */
1349                         /* TODO - possibly only run this for directly ativated buckets when cloning */
1350                         Vec2Subf(co, projPixel->projCo2D, ps->cloneOfs);
1351                         
1352                         /* no need to initialize the bucket, we're only checking buckets faces and for this
1353                          * the faces are alredy initialized in project_paint_delayed_face_init(...) */
1354                         if (ibuf->rect_float) {
1355                                 if (!screenco_pickcol(ps, co, ((ProjectPixelCloneFloat *)projPixel)->clonepx, NULL, 1)) {
1356                                         ((ProjectPixelCloneFloat *)projPixel)->clonepx[3] = 0; /* zero alpha - ignore */
1357                                 }
1358                         } else {
1359                                 if (!screenco_pickcol(ps, co, NULL, ((ProjectPixelClone *)projPixel)->clonepx, 1)) {
1360                                         ((ProjectPixelClone *)projPixel)->clonepx[3] = 0; /* zero alpha - ignore */
1361                                 }
1362                         }
1363                 }
1364                 
1365                 /* screenspace unclamped */
1366                 
1367 #ifdef PROJ_DEBUG_PAINT
1368                 if (ibuf->rect_float)   ((float *)projPixel->pixel)[1] = 0;
1369                 else                                    ((char *)projPixel->pixel)[1] = 0;
1370 #endif
1371                 projPixel->image_index = image_index;
1372                 
1373                 BLI_linklist_prepend_arena(
1374                         &ps->projectBuckets[ bucket_index ],
1375                         projPixel,
1376                         ps->projectArena_mt[thread_index]
1377                 );
1378         }
1379 }
1380
1381 static void uvpixel_rect_intersect(int min_target[2], int max_target[2],  int min_a[2], int max_a[2],  int min_b[2], int max_b[2])
1382 {
1383         min_target[0] = MAX2(min_a[0], min_b[0]);
1384         min_target[1] = MAX2(min_a[1], min_b[1]);
1385         
1386         max_target[0] = MIN2(max_a[0], max_b[0]);
1387         max_target[1] = MIN2(max_a[1], max_b[1]);
1388 }
1389
1390 static int line_clip_rect2f(float rect[4], float l1[2], float l2[2], float l1_clip[2], float l2_clip[2])
1391 {
1392         float isect;
1393         short ok1 = 0;
1394         short ok2 = 0;
1395         
1396         /* are either of the points inside the rectangle ? */
1397         if (    l1[1] >= rect[PROJ_BUCKET_BOTTOM] &&    l1[1] <= rect[PROJ_BUCKET_TOP] &&
1398                         l1[0] >= rect[PROJ_BUCKET_LEFT] &&              l1[0] <= rect[PROJ_BUCKET_RIGHT]
1399         ) {
1400                 VECCOPY2D(l1_clip, l1);
1401                 ok1 = 1;
1402         }
1403         
1404         if (    l2[1] >= rect[PROJ_BUCKET_BOTTOM] &&    l2[1] <= rect[PROJ_BUCKET_TOP] &&
1405                         l2[0] >= rect[PROJ_BUCKET_LEFT] &&              l2[0] <= rect[PROJ_BUCKET_RIGHT]
1406         ) {
1407                 VECCOPY2D(l2_clip, l2);
1408                 ok2 = 1;
1409         }
1410         
1411         /* line inside rect */
1412         if (ok1 && ok2) {
1413                 return 1;
1414         }
1415         
1416         /* top/bottom */
1417         if (line_isect_y(l1,l2, rect[PROJ_BUCKET_BOTTOM], &isect) && (isect > rect[PROJ_BUCKET_LEFT]) && (isect < rect[PROJ_BUCKET_RIGHT])) {
1418                 if (l1[1] < l2[1]) { /* line 1 is outside */
1419                         l1_clip[0] = isect;
1420                         l1_clip[1] = rect[PROJ_BUCKET_BOTTOM];
1421                         ok1 = 1;
1422                 } else {
1423                         l2_clip[0] = isect;
1424                         l2_clip[1] = rect[PROJ_BUCKET_BOTTOM];
1425                         ok2 = 2;
1426                 }
1427         }
1428         if (line_isect_y(l1,l2, rect[PROJ_BUCKET_TOP], &isect) && (isect > rect[PROJ_BUCKET_LEFT]) && (isect < rect[PROJ_BUCKET_RIGHT])) {
1429                 if (l1[1] > l2[1]) { /* line 1 is outside */
1430                         l1_clip[0] = isect;
1431                         l1_clip[1] = rect[PROJ_BUCKET_TOP];
1432                         ok1 = 1;
1433                 } else {
1434                         l2_clip[0] = isect;
1435                         l2_clip[1] = rect[PROJ_BUCKET_TOP];
1436                         ok2 = 2;
1437                 }
1438         }
1439         
1440         /* left/right */
1441         if (line_isect_x(l1,l2, rect[PROJ_BUCKET_LEFT], &isect) && (isect > rect[PROJ_BUCKET_BOTTOM]) && (isect < rect[PROJ_BUCKET_TOP])) {
1442                 if (l1[0] < l2[0]) { /* line 1 is outside */
1443                         l1_clip[0] = rect[PROJ_BUCKET_LEFT];
1444                         l1_clip[1] = isect;
1445                         ok1 = 1;
1446                 } else {
1447                         l2_clip[0] = rect[PROJ_BUCKET_LEFT];
1448                         l2_clip[1] = isect;
1449                         ok2 = 2;
1450                 }
1451         }
1452         if (line_isect_x(l1,l2, rect[PROJ_BUCKET_RIGHT], &isect) && (isect > rect[PROJ_BUCKET_BOTTOM]) && (isect < rect[PROJ_BUCKET_TOP])) {
1453                 if (l1[0] > l2[0]) { /* line 1 is outside */
1454                         l1_clip[0] = rect[PROJ_BUCKET_RIGHT];
1455                         l1_clip[1] = isect;
1456                         ok1 = 1;
1457                 } else {
1458                         l2_clip[0] = rect[PROJ_BUCKET_RIGHT];
1459                         l2_clip[1] = isect;
1460                         ok2 = 2;
1461                 }
1462         }
1463         
1464         if (ok1 && ok2) {
1465                 return 1;
1466         } else {
1467                 return 0;
1468         }
1469 }
1470
1471
1472
1473 /* scale the quad & tri about its center
1474  * scaling by 0.99999 is used for getting fake UV pixel coords that are on the
1475  * edge of the face but slightly inside it occlusion tests dont return hits on adjacent faces */
1476 static void scale_quad(float *origCos[4], float insetCos[4][3], float inset)
1477 {
1478         float cent[3];
1479         cent[0] = (origCos[0][0] + origCos[1][0] + origCos[2][0] + origCos[3][0]) / 4.0;
1480         cent[1] = (origCos[0][1] + origCos[1][1] + origCos[2][1] + origCos[3][1]) / 4.0;
1481         cent[2] = (origCos[0][2] + origCos[1][2] + origCos[2][2] + origCos[3][2]) / 4.0;
1482         
1483         VecSubf(insetCos[0], origCos[0], cent);
1484         VecSubf(insetCos[1], origCos[1], cent);
1485         VecSubf(insetCos[2], origCos[2], cent);
1486         VecSubf(insetCos[3], origCos[3], cent);
1487         
1488         VecMulf(insetCos[0], inset);
1489         VecMulf(insetCos[1], inset);
1490         VecMulf(insetCos[2], inset);
1491         VecMulf(insetCos[3], inset);
1492         
1493         VecAddf(insetCos[0], insetCos[0], cent);
1494         VecAddf(insetCos[1], insetCos[1], cent);
1495         VecAddf(insetCos[2], insetCos[2], cent);
1496         VecAddf(insetCos[3], insetCos[3], cent);
1497 }
1498
1499 static void scale_tri(float *origCos[4], float insetCos[4][3], float inset)
1500 {
1501         float cent[3];
1502         cent[0] = (origCos[0][0] + origCos[1][0] + origCos[2][0]) / 3.0;
1503         cent[1] = (origCos[0][1] + origCos[1][1] + origCos[2][1]) / 3.0;
1504         cent[2] = (origCos[0][2] + origCos[1][2] + origCos[2][2]) / 3.0;
1505         
1506         VecSubf(insetCos[0], origCos[0], cent);
1507         VecSubf(insetCos[1], origCos[1], cent);
1508         VecSubf(insetCos[2], origCos[2], cent);
1509         
1510         VecMulf(insetCos[0], inset);
1511         VecMulf(insetCos[1], inset);
1512         VecMulf(insetCos[2], inset);
1513         
1514         VecAddf(insetCos[0], insetCos[0], cent);
1515         VecAddf(insetCos[1], insetCos[1], cent);
1516         VecAddf(insetCos[2], insetCos[2], cent);
1517 }
1518
1519 static void rect_to_uvspace(
1520                 ProjectPaintState *ps, float bucket_bounds[4],
1521                 float *v1coSS, float *v2coSS, float *v3coSS,
1522                 float *uv1co, float *uv2co, float *uv3co,
1523                 float bucket_bounds_uv[4][2]
1524         )
1525 {
1526         float uv[2];
1527         float w[3];
1528         
1529         /* get the UV space bounding box */
1530         uv[0] = bucket_bounds[PROJ_BUCKET_RIGHT];
1531         uv[1] = bucket_bounds[PROJ_BUCKET_BOTTOM];
1532         if (ps->projectIsOrtho) BarycentricWeights2f(v1coSS, v2coSS, v3coSS, uv, w);    
1533         else                                    BarycentricWeightsPersp2f(v1coSS, v2coSS, v3coSS, uv, w);       
1534         bucket_bounds_uv[0][0] = uv1co[0]*w[0] + uv2co[0]*w[1] + uv3co[0]*w[2];
1535         bucket_bounds_uv[0][1] = uv1co[1]*w[0] + uv2co[1]*w[1] + uv3co[1]*w[2];
1536         
1537         //uv[0] = bucket_bounds[PROJ_BUCKET_RIGHT]; // set above
1538         uv[1] = bucket_bounds[PROJ_BUCKET_TOP];
1539         if (ps->projectIsOrtho) BarycentricWeights2f(v1coSS, v2coSS, v3coSS, uv, w);
1540         else                                    BarycentricWeightsPersp2f(v1coSS, v2coSS, v3coSS, uv, w);
1541         bucket_bounds_uv[1][0] = uv1co[0]*w[0] + uv2co[0]*w[1] + uv3co[0]*w[2];
1542         bucket_bounds_uv[1][1] = uv1co[1]*w[0] + uv2co[1]*w[1] + uv3co[1]*w[2];
1543         
1544         
1545         uv[0] = bucket_bounds[PROJ_BUCKET_LEFT];
1546         //uv[1] = bucket_bounds[PROJ_BUCKET_TOP]; // set above
1547         if (ps->projectIsOrtho) BarycentricWeights2f(v1coSS, v2coSS, v3coSS, uv, w);
1548         else                    BarycentricWeightsPersp2f(v1coSS, v2coSS, v3coSS, uv, w);
1549         bucket_bounds_uv[2][0] = uv1co[0]*w[0] + uv2co[0]*w[1] + uv3co[0]*w[2];
1550         bucket_bounds_uv[2][1] = uv1co[1]*w[0] + uv2co[1]*w[1] + uv3co[1]*w[2];
1551         
1552         //uv[0] = bucket_bounds[PROJ_BUCKET_LEFT]; // set above
1553         uv[1] = bucket_bounds[PROJ_BUCKET_BOTTOM];
1554         if (ps->projectIsOrtho) BarycentricWeights2f(v1coSS, v2coSS, v3coSS, uv, w);
1555         else                                    BarycentricWeightsPersp2f(v1coSS, v2coSS, v3coSS, uv, w);
1556         bucket_bounds_uv[3][0] = uv1co[0]*w[0] + uv2co[0]*w[1] + uv3co[0]*w[2];
1557         bucket_bounds_uv[3][1] = uv1co[1]*w[0] + uv2co[1]*w[1] + uv3co[1]*w[2]; 
1558 }
1559
1560
1561 /* initialize pixels from this face where it intersects with the bucket_index, initialize pixels for removing seams */
1562 static void project_paint_face_init(ProjectPaintState *ps, int thread_index, int bucket_index, int face_index, int image_index, float bucket_bounds[4], ImBuf *ibuf)
1563 {
1564         /* Projection vars, to get the 3D locations into screen space  */
1565         
1566         MFace *mf = ps->dm_mface + face_index;
1567         MTFace *tf = ps->dm_mtface + face_index;
1568         
1569         /* UV/pixel seeking data */
1570         int x; /* Image X-Pixel */
1571         int y;/* Image Y-Pixel */
1572         float uv[2]; /* Image floating point UV - same as x,y but from 0.0-1.0 */
1573         
1574         int min_px[2], max_px[2]; /* UV Bounds converted to int's for pixel */
1575         int min_px_tf[2], max_px_tf[2]; /* UV Bounds converted to int's for pixel */
1576         int min_px_bucket[2], max_px_bucket[2]; /* Bucket Bounds converted to int's for pixel */
1577         float *v1coSS, *v2coSS, *v3coSS; /* vert co screen-space, these will be assigned to mf->v1,2,3 or mf->v1,3,4 */
1578         
1579         float *vCo[4]; /* vertex screenspace coords */
1580         
1581         float *uv1co, *uv2co, *uv3co; /* for convenience only, these will be assigned to tf->uv[0],1,2 or tf->uv[0],2,3 */
1582         float pixelScreenCo[4];
1583         int i;
1584         
1585         /* vars for getting uvspace bounds */
1586         float bucket_bounds_uv[4][2]; /* bucket bounds in UV space so we can init pixels only for this face,  */
1587         
1588         int i1,i2,i3;
1589         
1590         /* scanlines since quads can have 2 triangles intersecting the same vertical location */
1591 #ifndef PROJ_DEBUG_NOSCANLINE 
1592         ProjectScanline scanlines[2];
1593         ProjectScanline *sc;
1594         int totscanlines; /* can only be 1 or 2, oh well */
1595 #endif
1596
1597         vCo[0] = ps->dm_mvert[ (*(&mf->v1    )) ].co;
1598         vCo[1] = ps->dm_mvert[ (*(&mf->v1 + 1)) ].co;
1599         vCo[2] = ps->dm_mvert[ (*(&mf->v1 + 2)) ].co;
1600         if (mf->v4)
1601                 vCo[3] = ps->dm_mvert[ (*(&mf->v1 + 3)) ].co;
1602
1603         i = mf->v4 ? 1:0;
1604         do {
1605                 if (i==1) {
1606                         i1=0; i2=2; i3=3;
1607                 } else {
1608                         i1=0; i2=1; i3=2;
1609                 }
1610                 
1611                 uv1co = tf->uv[i1];
1612                 uv2co = tf->uv[i2];
1613                 uv3co = tf->uv[i3];
1614
1615                 v1coSS = ps->projectVertScreenCos[ (*(&mf->v1 + i1)) ];
1616                 v2coSS = ps->projectVertScreenCos[ (*(&mf->v1 + i2)) ];
1617                 v3coSS = ps->projectVertScreenCos[ (*(&mf->v1 + i3)) ];
1618                 
1619                 rect_to_uvspace(ps, bucket_bounds, v1coSS, v2coSS, v3coSS, uv1co, uv2co, uv3co, bucket_bounds_uv);
1620                 
1621                 //printf("Bounds: %f | %f | %f | %f\n", bucket_bounds[0], bucket_bounds[1], bucket_bounds[2], bucket_bounds[3]);
1622
1623                 if (    uv_image_rect(uv1co, uv2co, uv3co, NULL, min_px_tf, max_px_tf, ibuf->x, ibuf->y, 0) && 
1624                                 uv_image_rect(bucket_bounds_uv[0], bucket_bounds_uv[1], bucket_bounds_uv[2], bucket_bounds_uv[3], min_px_bucket, max_px_bucket, ibuf->x, ibuf->y, 1) )
1625                 {
1626                         
1627                         uvpixel_rect_intersect(min_px, max_px, min_px_bucket, max_px_bucket, min_px_tf, max_px_tf);
1628                         
1629                         /* clip face and */
1630
1631
1632                         for (y = min_px[1]; y < max_px[1]; y++) {
1633                                 uv[1] = (((float)y)+0.5) / (float)ibuf->y; /* TODO - this is not pixel aligned correctly */
1634                                 for (x = min_px[0]; x < max_px[0]; x++) {
1635                                         uv[0] = (((float)x)+0.5) / (float)ibuf->x;
1636                                         
1637                                         /* test we're inside uvspace bucket and triangle bounds */
1638                                         if (    IsectPQ2Df(uv, bucket_bounds_uv[0], bucket_bounds_uv[1], bucket_bounds_uv[2], bucket_bounds_uv[3]) &&
1639                                                         IsectPT2Df(uv, uv1co, uv2co, uv3co) ) {
1640                                                 
1641                                                 if (ps->projectIsOrtho) {
1642                                                         screen_px_from_ortho(ps, uv, v1coSS,v2coSS,v3coSS, uv1co,uv2co,uv3co, pixelScreenCo);
1643                                                 } else {
1644                                                         screen_px_from_persp(ps, uv, vCo[i1],vCo[i2],vCo[i3], uv1co,uv2co,uv3co, pixelScreenCo);
1645                                                 }
1646                                                 
1647                                                 project_paint_uvpixel_init(ps, thread_index, ibuf, x,y, bucket_index, face_index, image_index, pixelScreenCo);
1648                                         }
1649                                 }
1650                         }
1651                 }
1652         } while(i--);
1653 #ifndef PROJ_DEBUG_NOSEAMBLEED
1654         if (ps->projectSeamBleed > 0.0) {
1655                 int face_seam_flag;
1656                 
1657                 if (ps->thread_tot > 1)
1658                         BLI_lock_thread(LOCK_CUSTOM1); /* Other threads could be modifying these vars */
1659                 
1660                 face_seam_flag = ps->projectFaceSeamFlags[face_index];
1661                 
1662                 /* are any of our edges un-initialized? */
1663                 if ((face_seam_flag & (PROJ_FACE_SEAM1|PROJ_FACE_NOSEAM1))==0 || 
1664                         (face_seam_flag & (PROJ_FACE_SEAM2|PROJ_FACE_NOSEAM2))==0 || 
1665                         (face_seam_flag & (PROJ_FACE_SEAM3|PROJ_FACE_NOSEAM3))==0 || 
1666                         (face_seam_flag & (PROJ_FACE_SEAM4|PROJ_FACE_NOSEAM4))==0
1667                 ) {
1668                         project_face_seams_init(ps, face_index, mf->v4);
1669                         face_seam_flag = ps->projectFaceSeamFlags[face_index];
1670                         //printf("seams - %d %d %d %d\n", flag&PROJ_FACE_SEAM1, flag&PROJ_FACE_SEAM2, flag&PROJ_FACE_SEAM3, flag&PROJ_FACE_SEAM4);
1671                 }
1672                 
1673                 if ((face_seam_flag & (PROJ_FACE_SEAM1|PROJ_FACE_SEAM2|PROJ_FACE_SEAM3|PROJ_FACE_SEAM4))==0) {
1674                         
1675                         if (ps->thread_tot > 1)
1676                                 BLI_unlock_thread(LOCK_CUSTOM1); /* Other threads could be modifying these vars */
1677                         
1678                 } else {
1679                         /* we have a seam - deal with it! */
1680                         
1681                         /* Now create new UV's for the seam face */
1682                         float (*outset_uv)[2];
1683                         float insetCos[4][3]; /* inset face coords.  NOTE!!! ScreenSace for ortho, Worldspace in prespective view */
1684
1685                         float *uv_seam_quad[4];
1686                         float fac;
1687                         float *vCoSS[4]; /* vertex screenspace coords */
1688                         
1689                         float bucket_clip_edges[2][2]; /* store the screenspace coords of the face, clipped by the bucket's screen aligned rectangle */
1690                         float edge_verts_inset_clip[2][3];
1691                         int fidx1, fidx2; /* face edge pairs - loop throuh these ((0,1), (1,2), (2,3), (3,0)) or ((0,1), (1,2), (2,0)) for a tri */
1692                         
1693                         float seam_subsection[4][2];
1694                         float fac1, fac2, ftot;
1695                         
1696                         outset_uv = ps->projectFaceSeamUVs[face_index];
1697                         
1698                         if (outset_uv[0][0]==MAXFLOAT) /* first time initialize */
1699                                 uv_image_outset(tf->uv, outset_uv, ps->projectSeamBleed, ibuf->x, ibuf->y, mf->v4);
1700                         
1701                         /* ps->projectFaceSeamUVs cant be modified when threading, now this is done we can unlock */
1702                         if (ps->thread_tot > 1)
1703                                 BLI_unlock_thread(LOCK_CUSTOM1); /* Other threads could be modifying these vars */
1704                         
1705                         vCoSS[0] = ps->projectVertScreenCos[ mf->v1 ];
1706                         vCoSS[1] = ps->projectVertScreenCos[ mf->v2 ];
1707                         vCoSS[2] = ps->projectVertScreenCos[ mf->v3 ];
1708                         if (mf->v4)
1709                                 vCoSS[3] = ps->projectVertScreenCos[ mf->v4 ];
1710                         
1711                         if (ps->projectIsOrtho) {
1712                                 if (mf->v4)     scale_quad(vCoSS, insetCos, 0.99999);
1713                                 else            scale_tri(vCoSS, insetCos, 0.99999);
1714                         } else {
1715                                 if (mf->v4)     scale_quad(vCo, insetCos, 0.99999);
1716                                 else            scale_tri(vCo, insetCos, 0.99999);
1717                         }
1718                         
1719                         for (fidx1 = 0; fidx1 < (mf->v4 ? 4 : 3); fidx1++) {
1720                                 if (mf->v4)             fidx2 = (fidx1==3) ? 0 : fidx1+1; /* next fidx in the face (0,1,2,3) -> (1,2,3,0) */
1721                                 else                    fidx2 = (fidx1==2) ? 0 : fidx1+1; /* next fidx in the face (0,1,2) -> (1,2,0) */
1722                                 
1723                                 if (    (face_seam_flag & (1<<fidx1) ) && /* 1<<fidx1 -> PROJ_FACE_SEAM# */
1724                                                 line_clip_rect2f(bucket_bounds, vCoSS[fidx1], vCoSS[fidx2], bucket_clip_edges[0], bucket_clip_edges[1])
1725                                 ) {
1726
1727                                         ftot = Vec2Lenf(vCoSS[fidx1], vCoSS[fidx2]); /* screenspace edge length */
1728                                         
1729                                         if (ftot > 0.0) { /* avoid div by zero */
1730                                                 
1731                                                 fac1 = Vec2Lenf(vCoSS[fidx1], bucket_clip_edges[0]) / ftot;
1732                                                 fac2 = Vec2Lenf(vCoSS[fidx1], bucket_clip_edges[1]) / ftot;
1733                                                 
1734                                                 uv_seam_quad[0] = tf->uv[fidx1];
1735                                                 uv_seam_quad[1] = tf->uv[fidx2];
1736                                                 uv_seam_quad[2] = outset_uv[fidx2];
1737                                                 uv_seam_quad[3] = outset_uv[fidx1];
1738                                                 
1739                                                 Vec2Lerpf(seam_subsection[0], uv_seam_quad[0], uv_seam_quad[1], fac1);
1740                                                 Vec2Lerpf(seam_subsection[1], uv_seam_quad[0], uv_seam_quad[1], fac2);
1741                                                 
1742                                                 Vec2Lerpf(seam_subsection[2], uv_seam_quad[3], uv_seam_quad[2], fac2);
1743                                                 Vec2Lerpf(seam_subsection[3], uv_seam_quad[3], uv_seam_quad[2], fac1);
1744                                                 
1745                                                 /* if the bucket_clip_edges values Z values was kept we could avoid this
1746                                                  * Inset needs to be added so occlusiuon tests wont hit adjacent faces */
1747                                                 VecLerpf(edge_verts_inset_clip[0], insetCos[fidx1], insetCos[fidx2], fac1);
1748                                                 VecLerpf(edge_verts_inset_clip[1], insetCos[fidx1], insetCos[fidx2], fac2);
1749                                                 
1750
1751                                                 if (uv_image_rect(seam_subsection[0], seam_subsection[1], seam_subsection[2], seam_subsection[3], min_px, max_px, ibuf->x, ibuf->y, 1)) {
1752                                                         /* bounds between the seam rect and the uvspace bucket pixels */
1753                                                 
1754                                                         for (y = min_px[1]; y < max_px[1]; y++) {
1755                                                                 
1756                                                                 uv[1] = (((float)y)+0.5) / (float)ibuf->y; /* TODO - this is not pixel aligned correctly */
1757                                                                 for (x = min_px[0]; x < max_px[0]; x++) {
1758                                                                         
1759                                                                         uv[0] = (((float)x)+0.5) / (float)ibuf->x;
1760                                                                         
1761                                                                         /* test we're inside uvspace bucket and triangle bounds */
1762                                                                         if (    IsectPQ2Df(uv, seam_subsection[0], seam_subsection[1], seam_subsection[2], seam_subsection[3]) ) {
1763                                                                                 
1764                                                                                 /* We need to find the closest point allong the face edge,
1765                                                                                  * getting the screen_px_from_*** wont work because our actual location
1766                                                                                  * is not relevent, since we are outside the face, Use VecLerpf to find
1767                                                                                  * our location on the side of the face's UV */
1768                                                                                 /*
1769                                                                                 if (ps->projectIsOrtho) screen_px_from_ortho(ps, uv, v1co,v2co,v3co, uv1co,uv2co,uv3co, pixelScreenCo);
1770                                                                                 else                                    screen_px_from_persp(ps, uv, v1co,v2co,v3co, uv1co,uv2co,uv3co, pixelScreenCo);
1771                                                                                 */
1772                                                                                 
1773                                                                                 /* Since this is a seam we need to work out where on the line this pixel is */
1774                                                                                 //fac = lambda_cp_line2(uv, uv_seam_quad[0], uv_seam_quad[1]);
1775                                                                                 
1776                                                                                 fac = lambda_cp_line2(uv, seam_subsection[0], seam_subsection[1]);
1777                                                                                 if (fac < 0.0)          { VECCOPY(pixelScreenCo, edge_verts_inset_clip[0]); }
1778                                                                                 else if (fac > 1.0)     { VECCOPY(pixelScreenCo, edge_verts_inset_clip[1]); }
1779                                                                                 else                            { VecLerpf(pixelScreenCo, edge_verts_inset_clip[0], edge_verts_inset_clip[1], fac); }
1780                                                                                 
1781                                                                                 if (!ps->projectIsOrtho) {
1782                                                                                         pixelScreenCo[3] = 1.0;
1783                                                                                         Mat4MulVec4fl(ps->projectMat, pixelScreenCo);
1784                                                                                         pixelScreenCo[0] = (float)(curarea->winx/2.0)+(curarea->winx/2.0)*pixelScreenCo[0]/pixelScreenCo[3];    
1785                                                                                         pixelScreenCo[1] = (float)(curarea->winy/2.0)+(curarea->winy/2.0)*pixelScreenCo[1]/pixelScreenCo[3];
1786                                                                                         pixelScreenCo[2] = pixelScreenCo[2]/pixelScreenCo[3]; /* Use the depth for bucket point occlusion */
1787                                                                                 }
1788                                                                                 
1789                                                                                 project_paint_uvpixel_init(ps, thread_index, ibuf, x, y, bucket_index, face_index, image_index, pixelScreenCo);
1790                                                                         }
1791                                                                 }
1792                                                         }
1793                                                 }
1794                                         }
1795                                 }
1796                         }
1797                 }
1798         }
1799 #endif // PROJ_DEBUG_NOSEAMBLEED
1800 }
1801
1802
1803 /* takes floating point screenspace min/max and returns int min/max to be used as indicies for ps->projectBuckets, ps->projectBucketFlags */
1804 static void project_paint_rect(ProjectPaintState *ps, float min[2], float max[2], int bucket_min[2], int bucket_max[2])
1805 {
1806         /* divide by bucketWidth & bucketHeight so the bounds are offset in bucket grid units */
1807         bucket_min[0] = (int)(((float)(min[0] - ps->viewMin2D[0]) / ps->viewWidth) * ps->bucketsX) + 0.5; /* these offsets of 0.5 and 1.5 seem odd but they are correct */
1808         bucket_min[1] = (int)(((float)(min[1] - ps->viewMin2D[1]) / ps->viewHeight) * ps->bucketsY) + 0.5;
1809         
1810         bucket_max[0] = (int)(((float)(max[0] - ps->viewMin2D[0]) / ps->viewWidth) * ps->bucketsX) + 1.5;
1811         bucket_max[1] = (int)(((float)(max[1] - ps->viewMin2D[1]) / ps->viewHeight) * ps->bucketsY) + 1.5;      
1812         
1813         /* incase the rect is outside the mesh 2d bounds */
1814         CLAMP(bucket_min[0], 0, ps->bucketsX);
1815         CLAMP(bucket_min[1], 0, ps->bucketsY);
1816         
1817         CLAMP(bucket_max[0], 0, ps->bucketsX);
1818         CLAMP(bucket_max[1], 0, ps->bucketsY);
1819 }
1820
1821 static void project_bucket_bounds(ProjectPaintState *ps, int bucket_x, int bucket_y, float bucket_bounds[4])
1822 {
1823         bucket_bounds[ PROJ_BUCKET_LEFT ] =             ps->viewMin2D[0]+((bucket_x)*(ps->viewWidth / ps->bucketsX));           /* left */
1824         bucket_bounds[ PROJ_BUCKET_RIGHT ] =    ps->viewMin2D[0]+((bucket_x+1)*(ps->viewWidth / ps->bucketsX)); /* right */
1825         
1826         bucket_bounds[ PROJ_BUCKET_BOTTOM ] =   ps->viewMin2D[1]+((bucket_y)*(ps->viewHeight / ps->bucketsY));          /* bottom */
1827         bucket_bounds[ PROJ_BUCKET_TOP ] =              ps->viewMin2D[1]+((bucket_y+1)*(ps->viewHeight  / ps->bucketsY));       /* top */
1828 }
1829
1830 /* have bucket_bounds as an arg so we dont need to give bucket_x/y the rect function need */
1831 static void project_paint_bucket_init(ProjectPaintState *ps, int thread_index, int bucket_index, float bucket_bounds[4])
1832 {
1833         LinkNode *node;
1834         int face_index, image_index;
1835         ImBuf *ibuf;
1836         MTFace *tf;
1837         
1838         Image *tpage_last = NULL;
1839         int tpage_index;
1840         
1841         if ((node = ps->projectFaces[bucket_index])) {
1842                 do {
1843                         face_index = (int)node->link;
1844                                 
1845                         /* Image context switching */
1846                         tf = ps->dm_mtface+face_index;
1847                         if (tpage_last != tf->tpage) {
1848                                 tpage_last = tf->tpage;
1849                                 
1850                                 image_index = -1; /* sanity check */
1851                                 
1852                                 for (tpage_index=0; tpage_index < ps->projectImageTotal; tpage_index++) {
1853                                         if (ps->projectImages[tpage_index] == tpage_last) {
1854                                                 image_index = tpage_index;
1855                                                 break;
1856                                         }
1857                                 }
1858                                 
1859                                 if (image_index==-1) {
1860                                         printf("Error, should never happen!\n");
1861                                         return;
1862                                 }
1863                                 
1864                                 ibuf = BKE_image_get_ibuf(tpage_last, NULL); /* TODO - this may be slow */
1865                         }
1866                         project_paint_face_init(ps, thread_index, bucket_index, face_index, image_index, bucket_bounds, ibuf);
1867                         
1868                         node = node->next;
1869                 } while (node);
1870         }
1871         
1872         ps->projectBucketFlags[bucket_index] |= PROJ_BUCKET_INIT; 
1873 }
1874
1875
1876 /* We want to know if a bucket and a face overlap in screenspace
1877  * 
1878  * Note, if this ever returns false positives its not that bad, since a face in the bounding area will have its pixels
1879  * calculated when it might not be needed later, (at the moment at least)
1880  * obviously it shouldnt have bugs though */
1881
1882 static int project_bucket_face_isect(ProjectPaintState *ps, float min[2], float max[2], int bucket_x, int bucket_y, int bucket_index, MFace *mf)
1883 {
1884         /* TODO - replace this with a tricker method that uses sideofline for all projectVertScreenCos's edges against the closest bucket corner */
1885         float bucket_bounds[4];
1886         float p1[2], p2[2], p3[2], p4[2];
1887         float *v, *v1,*v2,*v3,*v4;
1888         int i;
1889         
1890         project_bucket_bounds(ps, bucket_x, bucket_y, bucket_bounds);
1891         
1892         /* Is one of the faces verts in the bucket bounds? */
1893         
1894         i = mf->v4 ? 3:2;
1895         do {
1896                 v = ps->projectVertScreenCos[ (*(&mf->v1 + i)) ];
1897                 
1898                 if ((v[0] > bucket_bounds[PROJ_BUCKET_LEFT]) &&
1899                         (v[0] < bucket_bounds[PROJ_BUCKET_RIGHT]) &&
1900                         (v[1] > bucket_bounds[PROJ_BUCKET_BOTTOM]) &&
1901                         (v[1] < bucket_bounds[PROJ_BUCKET_TOP]) )
1902                 {
1903                         return 1;
1904                 }
1905         } while (i--);
1906                 
1907         v1 = ps->projectVertScreenCos[mf->v1];
1908         v2 = ps->projectVertScreenCos[mf->v2];
1909         v3 = ps->projectVertScreenCos[mf->v3];
1910         if (mf->v4) {
1911                 v4 = ps->projectVertScreenCos[mf->v4];
1912         }
1913         
1914         p1[0] = bucket_bounds[PROJ_BUCKET_LEFT];        p1[1] = bucket_bounds[PROJ_BUCKET_BOTTOM];
1915         p2[0] = bucket_bounds[PROJ_BUCKET_LEFT];        p2[1] = bucket_bounds[PROJ_BUCKET_TOP];
1916         p3[0] = bucket_bounds[PROJ_BUCKET_RIGHT];       p3[1] = bucket_bounds[PROJ_BUCKET_TOP];
1917         p4[0] = bucket_bounds[PROJ_BUCKET_RIGHT];       p4[1] = bucket_bounds[PROJ_BUCKET_BOTTOM];
1918                 
1919         if (mf->v4) {
1920                 if(     IsectPQ2Df(p1, v1, v2, v3, v4) || IsectPQ2Df(p2, v1, v2, v3, v4) || IsectPQ2Df(p3, v1, v2, v3, v4) || IsectPQ2Df(p4, v1, v2, v3, v4) ||
1921                         /* we can avoid testing v3,v1 because another intersection MUST exist if this intersects */
1922                         (IsectLL2Df(p1,p2, v1, v2) || IsectLL2Df(p1,p2, v2, v3) || IsectLL2Df(p1,p2, v3, v4)) ||
1923                         (IsectLL2Df(p2,p3, v1, v2) || IsectLL2Df(p2,p3, v2, v3) || IsectLL2Df(p2,p3, v3, v4)) ||
1924                         (IsectLL2Df(p3,p4, v1, v2) || IsectLL2Df(p3,p4, v2, v3) || IsectLL2Df(p3,p4, v3, v4)) ||
1925                         (IsectLL2Df(p4,p1, v1, v2) || IsectLL2Df(p4,p1, v2, v3) || IsectLL2Df(p4,p1, v3, v4))
1926                 ) {
1927                         return 1;
1928                 }
1929         } else {
1930                 if(     IsectPT2Df(p1, v1, v2, v3) || IsectPT2Df(p2, v1, v2, v3) || IsectPT2Df(p3, v1, v2, v3) || IsectPT2Df(p4, v1, v2, v3) ||
1931                         /* we can avoid testing v3,v1 because another intersection MUST exist if this intersects */
1932                         (IsectLL2Df(p1,p2, v1, v2) || IsectLL2Df(p1,p2, v2, v3)) ||
1933                         (IsectLL2Df(p2,p3, v1, v2) || IsectLL2Df(p2,p3, v2, v3)) ||
1934                         (IsectLL2Df(p3,p4, v1, v2) || IsectLL2Df(p3,p4, v2, v3)) ||
1935                         (IsectLL2Df(p4,p1, v1, v2) || IsectLL2Df(p4,p1, v2, v3))
1936                 ) {
1937                         return 1;
1938                 }
1939         }
1940
1941         return 0;
1942 }
1943
1944 static void project_paint_delayed_face_init(ProjectPaintState *ps, MFace *mf, MTFace *tf, int face_index)
1945 {
1946         float min[2], max[2];
1947         int bucket_min[2], bucket_max[2]; /* for  ps->projectBuckets indexing */
1948         int i, a, bucket_x, bucket_y, bucket_index;
1949         
1950         int has_x_isect = -1, has_isect = -1; /* for early loop exit */
1951         
1952         INIT_MINMAX2(min,max);
1953         
1954         i = mf->v4 ? 3:2;
1955         do {
1956                 a = (*(&mf->v1 + i)); /* vertex index */
1957                 
1958                 DO_MINMAX2(ps->projectVertScreenCos[ a ], min, max);
1959                 
1960 #ifndef PROJ_DEBUG_NOSEAMBLEED
1961                 /* add face user if we have bleed enabled, set the UV seam flags later */
1962                 if (ps->projectSeamBleed > 0.0) {
1963                         BLI_linklist_prepend_arena(
1964                                 &ps->projectVertFaces[ a ],
1965                                 (void *)face_index, /* cast to a pointer to shut up the compiler */
1966                                 ps->projectArena
1967                         );
1968                 }
1969 #endif
1970         } while (i--);
1971         
1972         project_paint_rect(ps, min, max, bucket_min, bucket_max);
1973         
1974         for (bucket_y = bucket_min[1]; bucket_y < bucket_max[1]; bucket_y++) {
1975                 has_x_isect = 0;
1976                 for (bucket_x = bucket_min[0]; bucket_x < bucket_max[0]; bucket_x++) {
1977                         
1978                         bucket_index = bucket_x + (bucket_y * ps->bucketsX);
1979                         
1980                         if (project_bucket_face_isect(ps, min, max, bucket_x, bucket_y, bucket_index, mf)) {
1981                                 BLI_linklist_prepend_arena(
1982                                         &ps->projectFaces[ bucket_index ],
1983                                         (void *)face_index, /* cast to a pointer to shut up the compiler */
1984                                         ps->projectArena
1985                                 );
1986                                 
1987                                 has_x_isect = has_isect = 1;
1988                         } else if (has_x_isect) {
1989                                 /* assuming the face is not a bow-tie - we know we cant intersect again on the X */
1990                                 break;
1991                         }
1992                 }
1993                 
1994                 /* no intersection for this entire row, after some intersection above means we can quit now */
1995                 if (has_x_isect==0 && has_isect) { 
1996                         break;
1997                 }
1998         }
1999         
2000 #ifndef PROJ_DEBUG_NOSEAMBLEED
2001         if (ps->projectSeamBleed > 0.0) {
2002                 if (!mf->v4) {
2003                         ps->projectFaceSeamFlags[face_index] |= PROJ_FACE_NOSEAM4; /* so this wont show up as an untagged egde */
2004                 }
2005                 **ps->projectFaceSeamUVs[face_index] = MAXFLOAT; /* set as uninitialized */
2006         }
2007 #endif
2008 }
2009
2010 static void project_paint_begin( ProjectPaintState *ps, short mval[2])
2011 {       
2012         /* Viewport vars */
2013         float mat[3][3];
2014         float f_no[3];
2015         float viewPos[3]; /* for projection only - view location */
2016         
2017         float (*projScreenCo)[4]; /* Note, we could have 4D vectors are only needed for */
2018         float projMargin;
2019         /* Image Vars - keep track of images we have used */
2020         LinkNode *image_LinkList = NULL;
2021         LinkNode *node;
2022         
2023         Image *tpage_last = NULL;
2024         ImBuf *ibuf = NULL;
2025         
2026         /* Face vars */
2027         MFace *mf;
2028         MTFace *tf;
2029         
2030         int a, i; /* generic looping vars */
2031         int image_index;
2032         
2033         /* memory sized to add to arena size */
2034         int tot_bucketMem=0;
2035         int tot_faceSeamFlagMem=0;
2036         int tot_faceSeamUVMem=0;
2037         int tot_faceListMem=0;
2038         int tot_bucketFlagMem=0;
2039         int tot_bucketVertFacesMem=0;
2040
2041         /* ---- end defines ---- */
2042         
2043         /* paint onto the derived mesh
2044          * note get_viewedit_datamask checks for paint mode and will always give UVs */
2045         ps->dm = mesh_get_derived_final(ps->ob, get_viewedit_datamask());
2046         
2047         ps->dm_mvert = ps->dm->getVertArray( ps->dm );
2048         ps->dm_mface = ps->dm->getFaceArray( ps->dm );
2049         ps->dm_mtface= ps->dm->getFaceDataArray( ps->dm, CD_MTFACE );
2050         
2051         ps->dm_totvert = ps->dm->getNumVerts( ps->dm );
2052         ps->dm_totface = ps->dm->getNumFaces( ps->dm );
2053         
2054         ps->bucketsX = G.rt ? G.rt : PROJ_BUCKET_DIV;
2055         ps->bucketsY = G.rt ? G.rt : PROJ_BUCKET_DIV;
2056         
2057         ps->viewDir[0] = 0.0;
2058         ps->viewDir[1] = 0.0;
2059         ps->viewDir[2] = 1.0;
2060         
2061         view3d_get_object_project_mat(curarea, ps->ob, ps->projectMat, ps->viewMat);
2062         
2063         tot_bucketMem =                         sizeof(LinkNode *) * ps->bucketsX * ps->bucketsY;
2064         tot_faceListMem =                       sizeof(LinkNode *) * ps->bucketsX * ps->bucketsY;
2065         
2066         tot_bucketFlagMem =                     sizeof(char) * ps->bucketsX * ps->bucketsY;
2067 #ifndef PROJ_DEBUG_NOSEAMBLEED
2068         if (ps->projectSeamBleed > 0.0) { /* UV Seams for bleeding */
2069                 tot_bucketVertFacesMem =        sizeof(LinkNode *) * ps->dm_totvert;
2070                 tot_faceSeamFlagMem =           sizeof(char) * ps->dm_totface;
2071                 tot_faceSeamUVMem =                     sizeof(float) * ps->dm_totface * 8;
2072         }
2073 #endif
2074         
2075         /* BLI_memarena_new uses calloc */
2076         ps->projectArena =
2077                 BLI_memarena_new(       tot_bucketMem +
2078                                                         tot_faceListMem +
2079                                                         tot_faceSeamFlagMem +
2080                                                         tot_faceSeamUVMem +
2081                                                         tot_bucketVertFacesMem + (1<<18));
2082         
2083         BLI_memarena_use_calloc(ps->projectArena);
2084         
2085         ps->projectBuckets = (LinkNode **)BLI_memarena_alloc( ps->projectArena, tot_bucketMem);
2086         ps->projectFaces= (LinkNode **)BLI_memarena_alloc( ps->projectArena, tot_faceListMem);
2087         
2088         ps->projectBucketFlags= (char *)BLI_memarena_alloc( ps->projectArena, tot_bucketFlagMem);
2089 #ifndef PROJ_DEBUG_NOSEAMBLEED
2090         if (ps->projectSeamBleed > 0.0) {
2091                 ps->projectVertFaces= (LinkNode **)BLI_memarena_alloc( ps->projectArena, tot_bucketVertFacesMem);
2092                 ps->projectFaceSeamFlags = (char *)BLI_memarena_alloc( ps->projectArena, tot_faceSeamFlagMem);
2093                 ps->projectFaceSeamUVs= BLI_memarena_alloc( ps->projectArena, tot_faceSeamUVMem);
2094         }
2095 #endif
2096         
2097         // calloced - memset(ps->projectBuckets,                0, tot_bucketMem);
2098         // calloced -  memset(ps->projectFaces,         0, tot_faceListMem);
2099         // calloced - memset(ps->projectBucketFlags,    0, tot_bucketFlagMem);
2100 #ifndef PROJ_DEBUG_NOSEAMBLEED
2101         // calloced - memset(ps->projectFaceSeamFlags,0, tot_faceSeamFlagMem);
2102         
2103         // calloced - if (ps->projectSeamBleed > 0.0) {
2104                 // calloced - memset(ps->projectVertFaces,      0, tot_bucketVertFacesMem);
2105                 /* TODO dosnt need zeroing? */
2106                 // calloced - memset(ps->projectFaceSeamUVs,    0, tot_faceSeamUVMem);
2107         // calloced - }
2108 #endif
2109         
2110         /* Thread stuff */
2111         if (G.scene->r.mode & R_FIXED_THREADS) {
2112                 ps->thread_tot = G.scene->r.threads;
2113         } else {
2114                 ps->thread_tot = BLI_system_thread_count();
2115         }
2116         
2117         for (a=0; a<ps->thread_tot; a++) {
2118                 ps->projectArena_mt[a] = BLI_memarena_new(1<<16);
2119         }
2120         
2121         Mat4Invert(ps->ob->imat, ps->ob->obmat);
2122         
2123         Mat3CpyMat4(mat, G.vd->viewinv);
2124         Mat3MulVecfl(mat, ps->viewDir);
2125         Mat3CpyMat4(mat, ps->ob->imat);
2126         Mat3MulVecfl(mat, ps->viewDir);
2127         
2128         /* calculate vert screen coords */
2129         ps->projectVertScreenCos = BLI_memarena_alloc( ps->projectArena, sizeof(float) * ps->dm_totvert * 4);
2130         projScreenCo = ps->projectVertScreenCos;
2131         
2132         /* TODO - check cameras mode too */
2133         if (G.vd->persp == V3D_ORTHO) {
2134                 ps->projectIsOrtho = 1;
2135         }
2136         
2137         INIT_MINMAX2(ps->viewMin2D, ps->viewMax2D);
2138         
2139         for(a=0; a < ps->dm_totvert; a++, projScreenCo++) {
2140                 VECCOPY((*projScreenCo), ps->dm_mvert[a].co);           
2141                 (*projScreenCo)[3] = 1.0;
2142                 
2143                 Mat4MulVec4fl(ps->projectMat, (*projScreenCo));
2144                 
2145                 if( (*projScreenCo)[3] > 0.001 ) {
2146                         /* screen space, not clamped */
2147                         (*projScreenCo)[0] = (float)(curarea->winx/2.0)+(curarea->winx/2.0)*(*projScreenCo)[0]/(*projScreenCo)[3];      
2148                         (*projScreenCo)[1] = (float)(curarea->winy/2.0)+(curarea->winy/2.0)*(*projScreenCo)[1]/(*projScreenCo)[3];
2149                         (*projScreenCo)[2] = (*projScreenCo)[2]/(*projScreenCo)[3]; /* Use the depth for bucket point occlusion */
2150                         DO_MINMAX2((*projScreenCo), ps->viewMin2D, ps->viewMax2D);
2151                 } else {
2152                         /* TODO - deal with cases where 1 side of a face goes behind the view ? */
2153                         (*projScreenCo)[0] = (*projScreenCo)[1] = MAXFLOAT;
2154                 }
2155         }
2156         
2157         /* setup clone offset */
2158         if (ps->tool == PAINT_TOOL_CLONE) {
2159                 float projCo[4];
2160                 float *curs= give_cursor();
2161                 VECCOPY(projCo, curs);
2162                 Mat4MulVecfl(ps->ob->imat, projCo);
2163                 VecSubf(projCo, projCo, ps->ob->obmat[3]);
2164                 projCo[3] = 1.0;
2165                 Mat4MulVec4fl(ps->projectMat, projCo);
2166                 ps->cloneOfs[0] = mval[0] - ((float)(curarea->winx/2.0)+(curarea->winx/2.0)*projCo[0]/projCo[3]);
2167                 ps->cloneOfs[1] = mval[1] - ((float)(curarea->winy/2.0)+(curarea->winy/2.0)*projCo[1]/projCo[3]);
2168                 
2169                 // printf("%f %f   %f %f %f\n", ps->cloneOfs[0], ps->cloneOfs[1], curs[0], curs[1], curs[2]);
2170                 
2171         }
2172         
2173         /* If this border is not added we get artifacts for faces that
2174          * have a paralelle edge and at the bounds of the the 2D projected verts eg
2175          * - a simgle screen aligned quad */
2176         projMargin = (ps->viewMax2D[0] - ps->viewMin2D[0]) * 0.000001;
2177         ps->viewMax2D[0] += projMargin;
2178         ps->viewMin2D[0] -= projMargin;
2179         projMargin = (ps->viewMax2D[1] - ps->viewMin2D[1]) * 0.000001;
2180         ps->viewMax2D[1] += projMargin;
2181         ps->viewMin2D[1] -= projMargin;
2182         
2183         
2184         /* only for convenience */
2185         ps->viewWidth  = ps->viewMax2D[0] - ps->viewMin2D[0];
2186         ps->viewHeight = ps->viewMax2D[1] - ps->viewMin2D[1];
2187         
2188         if (!ps->projectIsOrtho) {
2189                 /* get the view direction relative to the objects matrix */
2190                 float imat[3][3];
2191                 VECCOPY(viewPos, G.vd->viewinv[3]);
2192                 Mat3CpyMat4(imat, ps->ob->imat);
2193                 Mat3MulVecfl(imat, viewPos);
2194                 VecAddf(viewPos, viewPos, ps->ob->imat[3]);
2195         }
2196         
2197         for( a = 0, tf = ps->dm_mtface, mf = ps->dm_mface; a < ps->dm_totface; mf++, tf++, a++ ) {
2198                 if (tf->tpage && ((G.f & G_FACESELECT)==0 || mf->flag & ME_FACE_SEL)) {
2199                         
2200                         if (ps->projectIsBackfaceCull) {
2201                                 /* TODO - we dont really need the normal, just the direction, save a sqrt? */
2202                                 if (mf->v4)     CalcNormFloat4(ps->dm_mvert[mf->v1].co, ps->dm_mvert[mf->v2].co, ps->dm_mvert[mf->v3].co, ps->dm_mvert[mf->v4].co, f_no);
2203                                 else            CalcNormFloat(ps->dm_mvert[mf->v1].co, ps->dm_mvert[mf->v2].co, ps->dm_mvert[mf->v3].co, f_no);
2204                                 
2205                                 if (ps->projectIsOrtho) {
2206                                         if (Inpf(f_no, ps->viewDir) < 0) {
2207                                                 continue;
2208                                         }
2209                                 } else {
2210                                         float faceDir[3] = {0,0,0};
2211                                         if (mf->v4)     {
2212                                                 VecAddf(faceDir, faceDir, ps->dm_mvert[mf->v1].co);
2213                                                 VecAddf(faceDir, faceDir, ps->dm_mvert[mf->v2].co);
2214                                                 VecAddf(faceDir, faceDir, ps->dm_mvert[mf->v3].co);
2215                                                 VecAddf(faceDir, faceDir, ps->dm_mvert[mf->v4].co);
2216                                                 VecMulf(faceDir, 1.0/4.0);
2217                                         } else {
2218                                                 VecAddf(faceDir, faceDir, ps->dm_mvert[mf->v1].co);
2219                                                 VecAddf(faceDir, faceDir, ps->dm_mvert[mf->v2].co);
2220                                                 VecAddf(faceDir, faceDir, ps->dm_mvert[mf->v3].co);
2221                                                 VecMulf(faceDir, 1.0/3.0);
2222                                         }
2223                                         
2224                                         VecSubf(faceDir, viewPos, faceDir);
2225                                         
2226                                         if (Inpf(f_no, faceDir) < 0) {
2227                                                 continue;
2228                                         }
2229                                 }
2230                         }
2231                         
2232                         if (tpage_last != tf->tpage) {
2233                                 ibuf= BKE_image_get_ibuf((Image *)tf->tpage, NULL);
2234                                 
2235                                 if (ibuf) {
2236                                         image_index = BLI_linklist_index(image_LinkList, tf->tpage);
2237                                         
2238                                         if (image_index==-1) { /* MemArena dosnt have an append func */
2239                                                 BLI_linklist_append(&image_LinkList, tf->tpage);
2240                                                 image_index = ps->projectImageTotal;
2241                                                 ps->projectImageTotal++;
2242                                         }
2243                                 }
2244                                 
2245                                 tpage_last = tf->tpage;
2246                         }
2247                         
2248                         if (ibuf) {
2249                                 /* Initialize the faces screen pixels */
2250                                 /* Add this to a list to initialize later */
2251                                 project_paint_delayed_face_init(ps, mf, tf, a);
2252                         }
2253                 }
2254         }
2255         
2256         /* build an array of images we use*/
2257         ps->projectImages = BLI_memarena_alloc( ps->projectArena, sizeof(Image *) * ps->projectImageTotal);
2258         ps->projectImBufs = BLI_memarena_alloc( ps->projectArena, sizeof(ImBuf *) * ps->projectImageTotal);
2259         *(ps->projectPartialRedraws) = BLI_memarena_alloc( ps->projectArena, sizeof(ImagePaintPartialRedraw) * ps->projectImageTotal * PROJ_BOUNDBOX_SQUARED);
2260         
2261         for (a=1; a< ps->projectImageTotal; a++) {
2262                 ps->projectPartialRedraws[a] = *(ps->projectPartialRedraws) + (a * PROJ_BOUNDBOX_SQUARED);
2263         }
2264         // calloced - memset(ps->projectPartialRedraws, 0, sizeof(ImagePaintPartialRedraw) * ps->projectImageTotal);
2265         
2266         for (node= image_LinkList, i=0; node; node= node->next, i++) {
2267                 ps->projectImages[i] = node->link;
2268                 ps->projectImages[i]->id.flag &= ~LIB_DOIT;
2269                 ps->projectImBufs[i] = BKE_image_get_ibuf(ps->projectImages[i], NULL);
2270         }
2271         /* we have built the array, discard the linked list */
2272         BLI_linklist_free(image_LinkList, NULL);
2273 }
2274
2275 static void project_paint_end( ProjectPaintState *ps )
2276 {
2277         int a;
2278         BLI_memarena_free(ps->projectArena);
2279         
2280         for (a=0; a<ps->thread_tot; a++) {
2281                 BLI_memarena_free(ps->projectArena_mt[a]);
2282         }
2283         
2284         ps->dm->release(ps->dm);
2285 }
2286
2287
2288 /* external functions */
2289
2290 /* 1= an undo, -1 is a redo. */
2291 void undo_imagepaint_step(int step)
2292 {
2293         UndoElem *undo;
2294
2295         if(step==1) {
2296                 if(curundo==NULL) error("No more steps to undo");
2297                 else {
2298                         if(G.f & G_DEBUG) printf("undo %s\n", curundo->name);
2299                         undo_restore(curundo);
2300                         curundo= curundo->prev;
2301                 }
2302         }
2303         else if(step==-1) {
2304                 if((curundo!=NULL && curundo->next==NULL) || undobase.first==NULL) error("No more steps to redo");
2305                 else {
2306                         undo= (curundo && curundo->next)? curundo->next: undobase.first;
2307                         undo_restore(undo);
2308                         curundo= undo;
2309                         if(G.f & G_DEBUG) printf("redo %s\n", undo->name);
2310                 }
2311         }
2312
2313         allqueue(REDRAWVIEW3D, 0);
2314         allqueue(REDRAWIMAGE, 0);
2315 }
2316
2317 void undo_imagepaint_clear(void)
2318 {
2319         UndoElem *uel;
2320         
2321         uel= undobase.first;
2322         while(uel) {
2323                 undo_free(uel);
2324                 uel= uel->next;
2325         }
2326
2327         BLI_freelistN(&undobase);
2328         curundo= NULL;
2329 }
2330
2331 /* Imagepaint Partial Redraw & Dirty Region */
2332
2333 static void imapaint_clear_partial_redraw()
2334 {
2335         memset(&imapaintpartial, 0, sizeof(imapaintpartial));
2336 }
2337
2338 static void imapaint_dirty_region(Image *ima, ImBuf *ibuf, int x, int y, int w, int h)
2339 {
2340         ImBuf *tmpibuf = NULL;
2341         UndoTile *tile;
2342         int srcx= 0, srcy= 0, origx, allocsize;
2343
2344         IMB_rectclip(ibuf, NULL, &x, &y, &srcx, &srcy, &w, &h);
2345
2346         if (w == 0 || h == 0)
2347                 return;
2348         
2349         if (!imapaintpartial.enabled) {
2350                 imapaintpartial.x1 = x;
2351                 imapaintpartial.y1 = y;
2352                 imapaintpartial.x2 = x+w;
2353                 imapaintpartial.y2 = y+h;
2354                 imapaintpartial.enabled = 1;
2355         }
2356         else {
2357                 imapaintpartial.x1 = MIN2(imapaintpartial.x1, x);
2358                 imapaintpartial.y1 = MIN2(imapaintpartial.y1, y);
2359                 imapaintpartial.x2 = MAX2(imapaintpartial.x2, x+w);
2360                 imapaintpartial.y2 = MAX2(imapaintpartial.y2, y+h);
2361         }
2362
2363         w = ((x + w - 1) >> IMAPAINT_TILE_BITS);
2364         h = ((y + h - 1) >> IMAPAINT_TILE_BITS);
2365         origx = (x >> IMAPAINT_TILE_BITS);
2366         y = (y >> IMAPAINT_TILE_BITS);
2367         
2368         for (; y <= h; y++) {
2369                 for (x=origx; x <= w; x++) {
2370                         for(tile=curundo->tiles.first; tile; tile=tile->next)
2371                                 if(tile->x == x && tile->y == y && strcmp(tile->id.name, ima->id.name)==0)
2372                                         break;
2373
2374                         if(!tile) {
2375                                 if (tmpibuf==NULL)
2376                                         tmpibuf = IMB_allocImBuf(IMAPAINT_TILE_SIZE, IMAPAINT_TILE_SIZE, 32, IB_rectfloat|IB_rect, 0);
2377                                 
2378                                 tile= MEM_callocN(sizeof(UndoTile), "ImaUndoTile");
2379                                 tile->id= ima->id;
2380                                 tile->x= x;
2381                                 tile->y= y;
2382
2383                                 allocsize= IMAPAINT_TILE_SIZE*IMAPAINT_TILE_SIZE*4;
2384                                 allocsize *= (ibuf->rect_float)? sizeof(float): sizeof(char);
2385                                 tile->rect= MEM_mapallocN(allocsize, "ImaUndoRect");
2386
2387                                 undo_copy_tile(tile, tmpibuf, ibuf, 0);
2388                                 curundo->undosize += allocsize;
2389
2390                                 BLI_addtail(&curundo->tiles, tile);
2391                         }
2392                 }
2393         }
2394
2395         ibuf->userflags |= IB_BITMAPDIRTY;
2396         
2397         if (tmpibuf)
2398                 IMB_freeImBuf(tmpibuf);
2399 }
2400
2401 static void imapaint_image_update(Image *image, ImBuf *ibuf, short texpaint)
2402 {
2403         if(ibuf->rect_float)
2404                 imb_freerectImBuf(ibuf); /* force recreate of char rect */ /* TODO - should just update a portion from imapaintpartial! */
2405         if(ibuf->mipmap[0])
2406                 imb_freemipmapImBuf(ibuf);
2407
2408         /* todo: should set_tpage create ->rect? */
2409         if(texpaint || G.sima->lock) {
2410                 int w = imapaintpartial.x2 - imapaintpartial.x1;
2411                 int h = imapaintpartial.y2 - imapaintpartial.y1;
2412                 // printf("%d, %d, \n", w, h);
2413                 GPU_paint_update_image(image, imapaintpartial.x1, imapaintpartial.y1, w, h);
2414         }
2415 }
2416
2417 /* note; gets called for both 2d image paint and 3d texture paint. in the
2418    latter case image may be NULL and G.sima may not exist */
2419 static void imapaint_redraw(int final, int texpaint, Image *image)
2420 {
2421         if(final) {
2422                 if(texpaint)
2423                         allqueue(REDRAWIMAGE, 0);
2424                 else if(!G.sima->lock) {
2425                         if(image)
2426                                 GPU_free_image(image); /* force OpenGL reload */
2427                         allqueue(REDRAWVIEW3D, 0);
2428                 }
2429                 allqueue(REDRAWHEADERS, 0);
2430                 
2431                 if(!texpaint && image) {
2432                         /* after paint, tag Image or RenderResult nodes changed */
2433                         if(G.scene->nodetree) {
2434                                 imagepaint_composite_tags(G.scene->nodetree, image, &G.sima->iuser);
2435                         }
2436                         /* signal composite (hurmf, need an allqueue?) */
2437                         if(G.sima->lock) {
2438                                 ScrArea *sa;
2439                                 for(sa=G.curscreen->areabase.first; sa; sa= sa->next) {
2440                                         if(sa->spacetype==SPACE_NODE) {
2441                                                 if(((SpaceNode *)sa->spacedata.first)->treetype==NTREE_COMPOSIT) {
2442                                                         addqueue(sa->win, UI_BUT_EVENT, B_NODE_TREE_EXEC);
2443                                                         break;
2444                                                 }
2445                                         }
2446                                 }
2447                         }
2448                 }               
2449         }
2450         else if(!texpaint && G.sima->lock)
2451                 force_draw_plus(SPACE_VIEW3D, 0);
2452         else
2453                 force_draw(0);
2454 }
2455
2456 /* Image Paint Operations */
2457
2458 static void imapaint_ibuf_get_set_rgb(ImBuf *ibuf, int x, int y, short torus, short set, float *rgb)
2459 {
2460         if (torus) {
2461                 x %= ibuf->x;
2462                 if (x < 0) x += ibuf->x;
2463                 y %= ibuf->y;
2464                 if (y < 0) y += ibuf->y;
2465         }
2466
2467         if (ibuf->rect_float) {
2468                 float *rrgbf = ibuf->rect_float + (ibuf->x*y + x)*4;
2469
2470                 if (set) IMAPAINT_FLOAT_RGB_COPY(rrgbf, rgb)
2471                 else IMAPAINT_FLOAT_RGB_COPY(rgb, rrgbf)
2472         }
2473         else {
2474                 char *rrgb = (char*)ibuf->rect + (ibuf->x*y + x)*4;
2475
2476                 if (set) IMAPAINT_FLOAT_RGB_TO_CHAR(rrgb, rgb)
2477                 else IMAPAINT_CHAR_RGB_TO_FLOAT(rgb, rrgb)
2478         }
2479 }
2480
2481 static int imapaint_ibuf_add_if(ImBuf *ibuf, unsigned int x, unsigned int y, float *outrgb, short torus)
2482 {
2483         float inrgb[3];
2484
2485         if ((x >= ibuf->x) || (y >= ibuf->y)) {
2486                 if (torus) imapaint_ibuf_get_set_rgb(ibuf, x, y, 1, 0, inrgb);
2487                 else return 0;
2488         }
2489         else imapaint_ibuf_get_set_rgb(ibuf, x, y, 0, 0, inrgb);
2490
2491         outrgb[0] += inrgb[0];
2492         outrgb[1] += inrgb[1];
2493         outrgb[2] += inrgb[2];
2494
2495         return 1;
2496 }
2497
2498 static void imapaint_lift_soften(ImBuf *ibuf, ImBuf *ibufb, int *pos, short torus)
2499 {
2500         int x, y, count, xi, yi, xo, yo;
2501         int out_off[2], in_off[2], dim[2];
2502         float outrgb[3];
2503
2504         dim[0] = ibufb->x;
2505         dim[1] = ibufb->y;
2506         in_off[0] = pos[0];
2507         in_off[1] = pos[1];
2508         out_off[0] = out_off[1] = 0;
2509
2510         if (!torus) {
2511                 IMB_rectclip(ibuf, ibufb, &in_off[0], &in_off[1], &out_off[0],
2512                         &out_off[1], &dim[0], &dim[1]);
2513
2514                 if ((dim[0] == 0) || (dim[1] == 0))
2515                         return;
2516         }
2517
2518         for (y=0; y < dim[1]; y++) {
2519                 for (x=0; x < dim[0]; x++) {
2520                         /* get input pixel */
2521                         xi = in_off[0] + x;
2522                         yi = in_off[1] + y;
2523
2524                         count = 1;
2525                         imapaint_ibuf_get_set_rgb(ibuf, xi, yi, torus, 0, outrgb);
2526
2527                         count += imapaint_ibuf_add_if(ibuf, xi-1, yi-1, outrgb, torus);
2528                         count += imapaint_ibuf_add_if(ibuf, xi-1, yi  , outrgb, torus);
2529                         count += imapaint_ibuf_add_if(ibuf, xi-1, yi+1, outrgb, torus);
2530
2531                         count += imapaint_ibuf_add_if(ibuf, xi  , yi-1, outrgb, torus);
2532                         count += imapaint_ibuf_add_if(ibuf, xi  , yi+1, outrgb, torus);
2533
2534                         count += imapaint_ibuf_add_if(ibuf, xi+1, yi-1, outrgb, torus);
2535                         count += imapaint_ibuf_add_if(ibuf, xi+1, yi  , outrgb, torus);
2536                         count += imapaint_ibuf_add_if(ibuf, xi+1, yi+1, outrgb, torus);
2537
2538                         outrgb[0] /= count;
2539                         outrgb[1] /= count;
2540                         outrgb[2] /= count;
2541
2542                         /* write into brush buffer */
2543                         xo = out_off[0] + x;
2544                         yo = out_off[1] + y;
2545                         imapaint_ibuf_get_set_rgb(ibufb, xo, yo, 0, 1, outrgb);
2546                 }
2547         }
2548 }
2549
2550 static void imapaint_lift_smear(ImBuf *ibuf, ImBuf *ibufb, int *pos)
2551 {
2552         IMB_rectblend_torus(ibufb, ibuf, 0, 0, pos[0], pos[1],
2553                 ibufb->x, ibufb->y, IMB_BLEND_COPY_RGB);
2554 }
2555
2556 static ImBuf *imapaint_lift_clone(ImBuf *ibuf, ImBuf *ibufb, int *pos)
2557 {
2558         /* note: allocImbuf returns zero'd memory, so regions outside image will
2559            have zero alpha, and hence not be blended onto the image */
2560         int w=ibufb->x, h=ibufb->y, destx=0, desty=0, srcx=pos[0], srcy=pos[1];
2561         ImBuf *clonebuf= IMB_allocImBuf(w, h, ibufb->depth, ibufb->flags, 0);
2562
2563         IMB_rectclip(clonebuf, ibuf, &destx, &desty, &srcx, &srcy, &w, &h);
2564         IMB_rectblend(clonebuf, ibuf, destx, desty, srcx, srcy, w, h,
2565                 IMB_BLEND_COPY_RGB);
2566         IMB_rectblend(clonebuf, ibufb, destx, desty, destx, desty, w, h,
2567                 IMB_BLEND_COPY_ALPHA);
2568
2569         return clonebuf;
2570 }
2571
2572 static void imapaint_convert_brushco(ImBuf *ibufb, float *pos, int *ipos)
2573 {
2574         ipos[0]= (int)(pos[0] - ibufb->x/2);
2575         ipos[1]= (int)(pos[1] - ibufb->y/2);
2576 }
2577
2578 /* dosnt run for projection painting
2579  * only the old style painting in the 3d view */
2580 static int imapaint_paint_op(void *state, ImBuf *ibufb, float *lastpos, float *pos)
2581 {
2582         ImagePaintState *s= ((ImagePaintState*)state);
2583         ImBuf *clonebuf= NULL;
2584         short torus= s->brush->flag & BRUSH_TORUS;
2585         short blend= s->blend;
2586         float *offset= s->brush->clone.offset;
2587         float liftpos[2];
2588         int bpos[2], blastpos[2], bliftpos[2];
2589
2590         imapaint_convert_brushco(ibufb, pos, bpos);
2591
2592         /* lift from canvas */
2593         if(s->tool == PAINT_TOOL_SOFTEN) {
2594                 imapaint_lift_soften(s->canvas, ibufb, bpos, torus);
2595         }
2596         else if(s->tool == PAINT_TOOL_SMEAR) {
2597                 if (lastpos[0]==pos[0] && lastpos[1]==pos[1])
2598                         return 0;
2599
2600                 imapaint_convert_brushco(ibufb, lastpos, blastpos);
2601                 imapaint_lift_smear(s->canvas, ibufb, blastpos);
2602         }
2603         else if(s->tool == PAINT_TOOL_CLONE && s->clonecanvas) {
2604                 liftpos[0]= pos[0] - offset[0]*s->canvas->x;
2605                 liftpos[1]= pos[1] - offset[1]*s->canvas->y;
2606
2607                 imapaint_convert_brushco(ibufb, liftpos, bliftpos);
2608                 clonebuf= imapaint_lift_clone(s->clonecanvas, ibufb, bliftpos);
2609         }
2610
2611         imapaint_dirty_region(s->image, s->canvas, bpos[0], bpos[1], ibufb->x, ibufb->y);
2612
2613         /* blend into canvas */
2614         if(torus)
2615                 IMB_rectblend_torus(s->canvas, (clonebuf)? clonebuf: ibufb,
2616                         bpos[0], bpos[1], 0, 0, ibufb->x, ibufb->y, blend);
2617         else
2618                 IMB_rectblend(s->canvas, (clonebuf)? clonebuf: ibufb,
2619                         bpos[0], bpos[1], 0, 0, ibufb->x, ibufb->y, blend);
2620                         
2621         if(clonebuf) IMB_freeImBuf(clonebuf);
2622
2623         return 1;
2624 }
2625
2626 /* 2D ImagePaint */
2627
2628 static void imapaint_compute_uvco(short *mval, float *uv)
2629 {
2630         areamouseco_to_ipoco(G.v2d, mval, &uv[0], &uv[1]);
2631 }
2632
2633 /* 3D TexturePaint */
2634
2635 int facesel_face_pick(Mesh *me, short *mval, unsigned int *index, short rect);
2636 void texpaint_pick_uv(Object *ob, Mesh *mesh, unsigned int faceindex, short *xy, float *mousepos);
2637
2638 static int texpaint_break_stroke(float *prevuv, float *fwuv, float *bkuv, float *uv)
2639 {
2640         float d1[2], d2[2];
2641         float mismatch = Vec2Lenf(fwuv, uv);
2642         float len1 = Vec2Lenf(prevuv, fwuv);
2643         float len2 = Vec2Lenf(bkuv, uv);
2644
2645         Vec2Subf(d1, fwuv, prevuv);
2646         Vec2Subf(d2, uv, bkuv);
2647
2648         return ((Inp2f(d1, d2) < 0.0f) || (mismatch > MAX2(len1, len2)*2));
2649 }
2650
2651 /* ImagePaint Common */
2652
2653 static int imapaint_canvas_set(ImagePaintState *s, Image *ima)
2654 {
2655         ImBuf *ibuf= BKE_image_get_ibuf(ima, G.sima?&G.sima->iuser:NULL);
2656         
2657         /* verify that we can paint and set canvas */
2658         if(ima->packedfile && ima->rr) {
2659                 s->warnpackedfile = ima->id.name + 2;
2660                 return 0;
2661         }       
2662         else if(ibuf && ibuf->channels!=4) {
2663                 s->warnmultifile = ima->id.name + 2;
2664                 return 0;
2665         }
2666         else if(!ima || !ibuf || !(ibuf->rect || ibuf->rect_float))
2667                 return 0;
2668
2669         s->image= ima;
2670         s->canvas= ibuf;
2671
2672         /* set clone canvas */
2673         if(s->tool == PAINT_TOOL_CLONE) {
2674                 ima= s->brush->clone.image;
2675                 ibuf= BKE_image_get_ibuf(ima, G.sima?&G.sima->iuser:NULL);
2676                 
2677                 if(!ima || !ibuf || !(ibuf->rect || ibuf->rect_float))
2678                         return 0;
2679
2680                 s->clonecanvas= ibuf;
2681
2682                 if(s->canvas->rect_float && !s->clonecanvas->rect_float) {
2683                         /* temporarily add float rect for cloning */
2684                         IMB_float_from_rect(s->clonecanvas);
2685                         s->clonefreefloat= 1;
2686                 }
2687                 else if(!s->canvas->rect_float && !s->clonecanvas->rect)
2688                         IMB_rect_from_float(s->clonecanvas);
2689         }
2690
2691         return 1;
2692 }
2693
2694 static void imapaint_canvas_free(ImagePaintState *s)
2695 {
2696         if (s->clonefreefloat)
2697                 imb_freerectfloatImBuf(s->clonecanvas);
2698 }
2699
2700 static int imapaint_paint_sub_stroke(ImagePaintState *s, BrushPainter *painter, Image *image, short texpaint, float *uv, double time, int update, float pressure)
2701 {
2702         ImBuf *ibuf= BKE_image_get_ibuf(image, G.sima?&G.sima->iuser:NULL);
2703         float pos[2];
2704
2705         if(!ibuf)
2706                 return 0;
2707
2708         pos[0] = uv[0]*ibuf->x;
2709         pos[1] = uv[1]*ibuf->y;
2710
2711         brush_painter_require_imbuf(painter, ((ibuf->rect_float)? 1: 0), 0, 0);
2712
2713         if (brush_painter_paint(painter, imapaint_paint_op, pos, time, pressure, s)) {
2714                 if (update)
2715                         imapaint_image_update(image, ibuf, texpaint);
2716                 return 1;
2717         }
2718         else return 0;
2719 }
2720
2721 static float Vec2Lenf_nosqrt(float *v1, float *v2)
2722 {
2723         float x, y;
2724
2725         x = v1[0]-v2[0];
2726         y = v1[1]-v2[1];
2727         return x*x+y*y;
2728 }
2729
2730 static float Vec2Lenf_nosqrt_other(float *v1, float v2_1, float v2_2)
2731 {
2732         float x, y;
2733
2734         x = v1[0]-v2_1;
2735         y = v1[1]-v2_2;
2736         return x*x+y*y;
2737 }
2738
2739 /* note, use a squared value so we can use Vec2Lenf_nosqrt
2740  * be sure that you have done a bounds check first or this may fail */
2741 /* only give bucket_bounds as an arg because we need it elsewhere */
2742 static int project_bucket_circle_isect(ProjectPaintState *ps, int bucket_x, int bucket_y, float cent[2], float radius_squared, float bucket_bounds[4])
2743 {
2744         // printf("%d %d - %f %f %f %f - %f %f \n", bucket_x, bucket_y, bucket_bounds[0], bucket_bounds[1], bucket_bounds[2], bucket_bounds[3], cent[0], cent[1]);
2745
2746         /* first check if we are INSIDE the bucket */
2747         /* if ( bucket_bounds[PROJ_BUCKET_LEFT] <=      cent[0] &&
2748                         bucket_bounds[PROJ_BUCKET_RIGHT] >=     cent[0] &&
2749                         bucket_bounds[PROJ_BUCKET_BOTTOM] <=    cent[1] &&
2750                         bucket_bounds[PROJ_BUCKET_TOP] >=       cent[1] )
2751         {
2752                 return 1;
2753         }*/
2754         
2755         /* Would normally to a simple intersection test, however we know the bounds of these 2 alredy intersect 
2756          * so we only need to test if the center is inside the vertical or horizontal bounds on either axis,
2757          * this is even less work then an intersection test */
2758         if (  ( bucket_bounds[PROJ_BUCKET_LEFT] <=              cent[0] &&
2759                         bucket_bounds[PROJ_BUCKET_RIGHT] >=             cent[0]) ||
2760                   (     bucket_bounds[PROJ_BUCKET_BOTTOM] <=    cent[1] &&
2761                         bucket_bounds[PROJ_BUCKET_TOP] >=               cent[1]) )
2762         {
2763                 return 1;
2764         }
2765          
2766         /* out of bounds left */
2767         if (cent[0] < bucket_bounds[PROJ_BUCKET_LEFT]) {
2768                 /* lower left out of radius test */
2769                 if (cent[1] < bucket_bounds[PROJ_BUCKET_BOTTOM]) {
2770                         return (Vec2Lenf_nosqrt_other(cent, bucket_bounds[PROJ_BUCKET_LEFT], bucket_bounds[PROJ_BUCKET_BOTTOM]) < radius_squared) ? 1 : 0;
2771                 } 
2772                 /* top left test */
2773                 else if (cent[1] > bucket_bounds[PROJ_BUCKET_TOP]) {
2774                         return (Vec2Lenf_nosqrt_other(cent, bucket_bounds[PROJ_BUCKET_LEFT], bucket_bounds[PROJ_BUCKET_TOP]) < radius_squared) ? 1 : 0;
2775                 }
2776         }
2777         else if (cent[0] > bucket_bounds[PROJ_BUCKET_RIGHT]) {
2778                 /* lower right out of radius test */
2779                 if (cent[1] < bucket_bounds[PROJ_BUCKET_BOTTOM]) {
2780                         return (Vec2Lenf_nosqrt_other(cent, bucket_bounds[PROJ_BUCKET_RIGHT], bucket_bounds[PROJ_BUCKET_BOTTOM]) < radius_squared) ? 1 : 0;
2781                 } 
2782                 /* top right test */
2783                 else if (cent[1] > bucket_bounds[PROJ_BUCKET_TOP]) {
2784                         return (Vec2Lenf_nosqrt_other(cent, bucket_bounds[PROJ_BUCKET_RIGHT], bucket_bounds[PROJ_BUCKET_TOP]) < radius_squared) ? 1 : 0;
2785                 }
2786         }
2787         
2788         return 0;
2789 }
2790
2791 static void partial_redraw_array_init(ImagePaintPartialRedraw *pr, int tot)
2792 {
2793         while (tot--) {
2794                 pr->x1 = 10000000;
2795                 pr->y1 = 10000000;
2796                 
2797                 pr->x2 = -1;
2798                 pr->y2 = -1;
2799                 
2800                 pr->enabled = 1;
2801                 
2802                 pr++;
2803         }
2804 }
2805
2806 static void partial_redraw_array_merge(ImagePaintPartialRedraw *pr, ImagePaintPartialRedraw *pr_other, int tot)
2807 {
2808         while (tot--) {
2809                 pr->x1 = MIN2(pr->x1, pr_other->x1);
2810                 pr->y1 = MIN2(pr->y1, pr_other->y1);
2811                 
2812                 pr->x2 = MAX2(pr->x2, pr_other->x2);
2813                 pr->y2 = MAX2(pr->y2, pr_other->y2);
2814                 
2815                 pr++; pr_other++;
2816         }
2817 }
2818
2819 /* Loop over all images on this mesh and update any we have touched */
2820 static int imapaint_refresh_tagged(ProjectPaintState *ps)
2821 {
2822         ImagePaintPartialRedraw *pr;
2823         int a,i;
2824         int redraw = 0;
2825         
2826         
2827         for (a=0; a < ps->projectImageTotal; a++) {
2828                 Image *ima = ps->projectImages[a];
2829                 if (ima->id.flag & LIB_DOIT) {
2830                         /* look over each bound cell */
2831                         for (i=0; i<PROJ_BOUNDBOX_SQUARED; i++) {
2832                                 pr = &(ps->projectPartialRedraws[a][i]);
2833                                 if (pr->x2 != -1) { /* TODO - use 'enabled' ? */
2834                                         /*
2835                                         if(U.uiflag & USER_GLOBALUNDO) {
2836                                                 // Dont need to set "imapaintpartial" This is updated from imapaint_dirty_region args.
2837                                                 imapaint_dirty_region(ima, ps->projectImBufs[a], pr->x1, pr->y1, pr->x2 - pr->x1, pr->y2 - pr->y1);
2838                                         } else {
2839                                         */
2840                                                 imapaintpartial = *pr;
2841                                         /*
2842                                         }
2843                                         */
2844                                         
2845                                         imapaint_image_update(ima, ps->projectImBufs[a], 1 /*texpaint*/ );
2846                                         
2847                                         redraw = 1;
2848                                 }
2849                         }
2850                         
2851                         ima->id.flag &= ~LIB_DOIT; /* clear for reuse */
2852                 }
2853         }
2854         
2855         return redraw;
2856 }
2857
2858 static int bucket_iter_init(ProjectPaintState *ps, float mval_f[2])
2859 {
2860         float min_brush[2], max_brush[2];
2861         
2862         min_brush[0] = mval_f[0] - (ps->brush->size/2);
2863         min_brush[1] = mval_f[1] - (ps->brush->size/2);
2864         
2865         max_brush[0] = mval_f[0] + (ps->brush->size/2);
2866         max_brush[1] = mval_f[1] + (ps->brush->size/2);
2867         
2868         /* offset to make this a valid bucket index */
2869         project_paint_rect(ps, min_brush, max_brush, ps->min_bucket, ps->max_bucket);
2870         
2871         /* mouse outside the model areas? */
2872         if (ps->min_bucket[0]==ps->max_bucket[0] || ps->min_bucket[1]==ps->max_bucket[1]) {
2873                 return 0;
2874         }
2875         
2876         ps->context_bucket_x = ps->min_bucket[0];
2877         ps->context_bucket_y = ps->min_bucket[1];
2878 #ifdef PROJ_DEBUG_PRINT_THREADS
2879         printf("Initializing Values %d %d!", ps->min_bucket[0], ps->min_bucket[1]);
2880 #endif
2881         return 1;
2882 }
2883
2884 static int bucket_iter_next(ProjectPaintState *ps, int *bucket_index, float bucket_bounds[4], float mval_f[2])
2885 {
2886         if (ps->thread_tot > 1)
2887                 BLI_lock_thread(LOCK_CUSTOM1);
2888         
2889         //printf("%d %d \n", ps->context_bucket_x, ps->context_bucket_y);
2890         
2891         for ( ; ps->context_bucket_y < ps->max_bucket[1]; ps->context_bucket_y++) {
2892                 for ( ; ps->context_bucket_x < ps->max_bucket[0]; ps->context_bucket_x++) {
2893                         
2894                         /* use bucket_bounds for project_bucket_circle_isect and project_paint_bucket_init*/
2895                         project_bucket_bounds(ps, ps->context_bucket_x, ps->context_bucket_y, bucket_bounds);
2896                         
2897                         if (project_bucket_circle_isect(ps, ps->context_bucket_x, ps->context_bucket_y, mval_f, ps->brush->size * ps->brush->size, bucket_bounds)) {
2898                                 *bucket_index = ps->context_bucket_x + (ps->context_bucket_y * ps->bucketsX);
2899 #ifdef PROJ_DEBUG_PRINT_THREADS
2900                                 printf(" --- %d %d \n", ps->context_bucket_x, ps->context_bucket_y);
2901 #endif
2902                                 ps->context_bucket_x++;
2903                                 
2904                                 if (ps->thread_tot > 1)
2905                                         BLI_unlock_thread(LOCK_CUSTOM1);
2906                                 
2907                                 return 1;
2908                         }
2909                 }
2910                 ps->context_bucket_x = ps->min_bucket[0];
2911         }
2912         
2913         if (ps->thread_tot > 1)
2914                 BLI_unlock_thread(LOCK_CUSTOM1);
2915         return 0;
2916 }
2917
2918 static int imapaint_paint_sub_stroke_project(
2919                                 ProjectPaintState *ps,
2920                                 BrushPainter *painter,
2921                                 short prevmval[2],
2922                                 short mval[2],
2923                                 double time,
2924                                 float pressure,
2925                                 ImagePaintPartialRedraw *projectPartialRedraws[PROJ_BOUNDBOX_SQUARED],
2926                                 int thread_index)
2927 {
2928         LinkNode *node;
2929         float mval_f[2];
2930         ProjectPixel *projPixel;
2931         int redraw = 0;
2932         
2933         int last_index = -1;
2934         ImagePaintPartialRedraw *last_partial_redraw;
2935         ImagePaintPartialRedraw *last_partial_redraw_cell;
2936         
2937         float rgba[4], alpha, dist, dist_nosqrt;
2938         char rgba_ub[4];
2939         float rgba_fp[4];
2940         float brush_size_sqared;
2941         int bucket_index;
2942         int is_floatbuf = 0;
2943         short blend= ps->blend;
2944
2945         /* pixel values */
2946         char *cp;
2947         float *fp;
2948         
2949         float bucket_bounds[4];
2950         
2951         /* for smear only */
2952         float mval_ofs[2]; 
2953         float co[2];
2954         LinkNode *smearPixels = NULL;
2955         LinkNode *smearPixels_float = NULL;
2956         MemArena *smearArena = NULL; /* mem arena for this brush projection only */
2957         
2958         
2959         
2960         mval_f[0] = mval[0]; mval_f[1] = mval[1];
2961         
2962         if (ps->tool==PAINT_TOOL_SMEAR) {
2963                 mval_ofs[0] = (float)(mval[0] - prevmval[0]);
2964                 mval_ofs[1] = (float)(mval[1] - prevmval[1]);
2965                 
2966                 smearArena = BLI_memarena_new(1<<16);
2967         }
2968         
2969         /* avoid a square root with every dist comparison */
2970         brush_size_sqared = ps->brush->size * ps->brush->size; 
2971         
2972         /* printf("brush bounds %d %d %d %d\n", bucket_min[0], bucket_min[1], bucket_max[0], bucket_max[1]); */
2973         
2974         /* If there is ever problems with getting the bounds for the brush, set the bounds to include all */
2975         /*bucket_min[0] = 0; bucket_min[1] = 0; bucket_max[0] = ps->bucketsX; bucket_max[1] = ps->bucketsY;*/
2976         
2977         /* no clamping needed, dont use screen bounds, use vert bounds  */
2978         
2979         //for (bucket_y = bucket_min[1]; bucket_y < bucket_max[1]; bucket_y++) {
2980 #ifdef PROJ_DEBUG_PRINT_THREADS
2981         printf("THREAD %d %d %d\n", ps->thread_tot, thread_index,  (ps->max_bucket[0] - ps->min_bucket[0]) * (ps->max_bucket[1] - ps->min_bucket[1]) );
2982 #endif
2983         while (bucket_iter_next(ps, &bucket_index, bucket_bounds, mval_f)) {                            
2984                 
2985 #ifdef PROJ_DEBUG_PRINT_THREADS
2986                 printf("\t%d %d\n", thread_index, bucket_index);
2987 #endif
2988                 
2989                 /* Check this bucket and its faces are initialized */
2990                 if (ps->projectBucketFlags[bucket_index] == PROJ_BUCKET_NULL) {
2991                         /* No pixels initialized */
2992                         project_paint_bucket_init(ps, thread_index, bucket_index, bucket_bounds);
2993                 }
2994                 
2995                 /* TODO - we may want to init clone data in a seperate to project_paint_bucket_init
2996                  * so we dont go overboard and init too many clone pixels  */
2997
2998                 if ((node = ps->projectBuckets[bucket_index])) {
2999                 
3000                         do {
3001                                 projPixel = (ProjectPixel *)node->link;
3002                                 
3003                                 /*dist = Vec2Lenf(projPixel->projCo2D, mval_f);*/ /* correct but uses a sqrt */
3004                                 dist_nosqrt = Vec2Lenf_nosqrt(projPixel->projCo2D, mval_f);
3005                                 
3006                                 /*if (dist < s->brush->size) {*/ /* correct but uses a sqrt */
3007                                 if (dist_nosqrt < brush_size_sqared) {
3008                                         
3009                                         if (last_index != projPixel->image_index) {
3010                                                 last_index = projPixel->image_index;
3011                                                 last_partial_redraw = projectPartialRedraws[last_index];
3012                                                 
3013                                                 ps->projectImages[last_index]->id.flag |= LIB_DOIT; /* halgrind complains this is not threadsafe but probably ok? - we are only setting this flag anyway */
3014                                                 is_floatbuf = ps->projectImBufs[last_index]->rect_float ? 1 : 0;
3015                                         }
3016                                         
3017                                         last_partial_redraw_cell = last_partial_redraw + projPixel->bb_cell_index;
3018                                         last_partial_redraw_cell->x1 = MIN2( last_partial_redraw_cell->x1, projPixel->x_px );
3019                                         last_partial_redraw_cell->y1 = MIN2( last_partial_redraw_cell->y1, projPixel->y_px );
3020                                         
3021                                         last_partial_redraw_cell->x2 = MAX2( last_partial_redraw_cell->x2, projPixel->x_px+1 );
3022                                         last_partial_redraw_cell->y2 = MAX2( last_partial_redraw_cell->y2, projPixel->y_px+1 );
3023                                         
3024                                         dist = (float)sqrt(dist_nosqrt);
3025                                         
3026                                         switch(ps->tool) {
3027                                         case PAINT_TOOL_CLONE:
3028                                                 if (is_floatbuf) {
3029                                                         if (((ProjectPixelCloneFloat*)projPixel)->clonepx[3]) {
3030                                                                 alpha = brush_sample_falloff(ps->brush, dist);
3031                                                                 fp = ((ProjectPixelCloneFloat *)projPixel)->clonepx;
3032                                                                 if (alpha >= 1.0) {
3033                                                                         VECCOPY((float *)projPixel->pixel, ((ProjectPixelCloneFloat *)projPixel)->clonepx );
3034                                                                 } else {
3035                                                                         ((float *)projPixel->pixel)[0] = (fp[0] * alpha) + ((((float *)projPixel->pixel)[0])*(1.0-alpha));
3036                                                                         ((float *)projPixel->pixel)[1] = (fp[1] * alpha) + ((((float *)projPixel->pixel)[1])*(1.0-alpha));
3037                                                                         ((float *)projPixel->pixel)[2] = (fp[2] * alpha) + ((((float *)projPixel->pixel)[2])*(1.0-alpha));
3038                                                                 }
3039                                                         }
3040                                                 } else {
3041                                                         if (((ProjectPixelClone*)projPixel)->clonepx[3]) {
3042                                                                 alpha = brush_sample_falloff(ps->brush, dist);
3043                                                                 cp = (char *)((ProjectPixelClone*)projPixel)->clonepx;
3044                                                                 if (alpha >= 1.0) {
3045                                                                         ((char *)projPixel->pixel)[0] = cp[0];
3046                                                                         ((char *)projPixel->pixel)[1] = cp[1];
3047                                                                         ((char *)projPixel->pixel)[2] = cp[2];
3048                                                                 } else {
3049                                                                         ((char *)projPixel->pixel)[0] = FTOCHAR( (((cp[0]/255.0) * alpha) + (((((char *)projPixel->pixel)[0])/255.0)*(1.0-alpha))) );
3050                                                                         ((char *)projPixel->pixel)[1] = FTOCHAR( (((cp[1]/255.0) * alpha) + (((((char *)projPixel->pixel)[1])/255.0)*(1.0-alpha))) );
3051                                                                         ((char *)projPixel->pixel)[2] = FTOCHAR( (((cp[2]/255.0) * alpha) + (((((char *)projPixel->pixel)[2])/255.0)*(1.0-alpha))) );
3052