2D Cursor in UV window
[blender.git] / source / blender / src / editsima.c
1 /**
2  * $Id$
3  *
4  * ***** BEGIN GPL/BL DUAL LICENSE BLOCK *****
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version. The Blender
10  * Foundation also sells licenses for use in proprietary software under
11  * the Blender License.  See http://www.blender.org/BL/ for information
12  * about this.
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: all of this file.
27  *
28  * Contributor(s): none yet.
29  *
30  * ***** END GPL/BL DUAL LICENSE BLOCK *****
31  */
32
33 #include <stdlib.h>
34 #include <string.h>
35 #include <math.h>
36
37 #ifdef HAVE_CONFIG_H
38 #include <config.h>
39 #endif
40
41 #ifndef WIN32
42 #include <unistd.h>
43 #else
44 #include <io.h>
45 #endif   
46 #include "MEM_guardedalloc.h"
47
48 #include "BLI_blenlib.h"
49 #include "BLI_arithb.h"
50 #include "BLI_editVert.h"
51
52 #include "IMB_imbuf_types.h"
53 #include "IMB_imbuf.h"
54
55 #include "DNA_image_types.h"
56 #include "DNA_mesh_types.h"
57 #include "DNA_meshdata_types.h"
58 #include "DNA_node_types.h"
59 #include "DNA_object_types.h" // only for uvedit_selectionCB() (struct Object)
60 #include "DNA_packedFile_types.h"
61 #include "DNA_scene_types.h"
62 #include "DNA_space_types.h"
63 #include "DNA_screen_types.h"
64 #include "DNA_texture_types.h"
65 #include "DNA_userdef_types.h"
66 #include "DNA_view3d_types.h"
67
68 #include "BKE_colortools.h"
69 #include "BKE_depsgraph.h"
70 #include "BKE_displist.h"
71 #include "BKE_image.h"
72 #include "BKE_global.h"
73 #include "BKE_library.h"
74 #include "BKE_main.h"
75 #include "BKE_mesh.h"
76 #include "BKE_node.h"
77 #include "BKE_object.h"
78 #include "BKE_packedFile.h"
79 #include "BKE_utildefines.h"
80
81 #include "BIF_gl.h"
82 #include "BIF_glutil.h"
83 #include "BIF_imasel.h"
84 #include "BIF_interface.h"
85 #include "BIF_drawimage.h"
86 #include "BIF_editview.h"
87 #include "BIF_editsima.h"
88 #include "BIF_mywindow.h"
89 #include "BIF_previewrender.h"
90 #include "BIF_screen.h"
91 #include "BIF_space.h"
92 #include "BIF_toolbox.h"
93 #include "BIF_transform.h"
94 #include "BIF_writeimage.h"
95 #include "BIF_editmesh.h"
96
97 #include "BSE_drawipo.h"
98 #include "BSE_edit.h"
99 #include "BSE_filesel.h"
100 #include "BSE_trans_types.h"
101
102 #include "BDR_editobject.h"
103 #include "BDR_unwrapper.h"
104
105 #include "BMF_Api.h"
106
107 #include "RE_pipeline.h"
108
109 #include "blendef.h"
110 #include "multires.h"
111 #include "mydevice.h"
112 #include "editmesh.h"
113
114 /* local prototypes */
115 void sel_uvco_inside_radius(short , MTFace *, int , float *, float *, short);
116 void uvedit_selectionCB(short , Object *, short *, float ); /* used in edit.c*/ 
117
118 void object_uvs_changed(Object *ob)
119 {
120         DAG_object_flush_update(G.scene, ob, OB_RECALC_DATA);
121
122         allqueue(REDRAWVIEW3D, 0);
123         allqueue(REDRAWIMAGE, 0);
124 }
125
126 void object_tface_flags_changed(Object *ob, int updateButtons)
127 {
128         if (updateButtons) allqueue(REDRAWBUTSEDIT, 0);
129         allqueue(REDRAWVIEW3D, 0);
130         allqueue(REDRAWIMAGE, 0);
131 }
132
133 int is_uv_tface_editing_allowed_silent(void)
134 {
135         if(!EM_texFaceCheck()) return 0;
136         if(G.sima->mode!=SI_TEXTURE) return 0;
137         if(multires_level1_test()) return 0;    
138         return 1;
139 }
140
141 int is_uv_tface_editing_allowed(void)
142 {
143         if(!G.obedit) error("Enter Edit Mode to perform this action");
144
145         return is_uv_tface_editing_allowed_silent();
146 }
147
148 void get_connected_limit_tface_uv(float *limit)
149 {
150         ImBuf *ibuf= BKE_image_get_ibuf(G.sima->image, &G.sima->iuser);
151         if(ibuf && ibuf->x > 0 && ibuf->y > 0) {
152                 limit[0]= 0.05/(float)ibuf->x;
153                 limit[1]= 0.05/(float)ibuf->y;
154         }
155         else
156                 limit[0]= limit[1]= 0.05/256.0;
157 }
158
159 void be_square_tface_uv(EditMesh *em)
160 {
161         EditFace *efa;
162         MTFace *tface;
163         /* if 1 vertex selected: doit (with the selected vertex) */
164         for (efa= em->faces.first; efa; efa= efa->next) {
165                 if (efa->v4) {
166                         if (efa->f & SELECT) {
167                                 tface= CustomData_em_get(&em->fdata, efa->data, CD_MTFACE);
168                                 if(tface->flag & TF_SEL1) {
169                                         if( tface->uv[1][0] == tface->uv[2][0] ) {
170                                                 tface->uv[1][1]= tface->uv[0][1];
171                                                 tface->uv[3][0]= tface->uv[0][0];
172                                         }
173                                         else {  
174                                                 tface->uv[1][0]= tface->uv[0][0];
175                                                 tface->uv[3][1]= tface->uv[0][1];
176                                         }
177                                         
178                                 }
179                                 if(tface->flag & TF_SEL2) {
180                                         if( tface->uv[2][1] == tface->uv[3][1] ) {
181                                                 tface->uv[2][0]= tface->uv[1][0];
182                                                 tface->uv[0][1]= tface->uv[1][1];
183                                         }
184                                         else {
185                                                 tface->uv[2][1]= tface->uv[1][1];
186                                                 tface->uv[0][0]= tface->uv[1][0];
187                                         }
188
189                                 }
190                                 if(tface->flag & TF_SEL3) {
191                                         if( tface->uv[3][0] == tface->uv[0][0] ) {
192                                                 tface->uv[3][1]= tface->uv[2][1];
193                                                 tface->uv[1][0]= tface->uv[2][0];
194                                         }
195                                         else {
196                                                 tface->uv[3][0]= tface->uv[2][0];
197                                                 tface->uv[1][1]= tface->uv[2][1];
198                                         }
199                                 }
200                                 if(tface->flag & TF_SEL4) {
201                                         if( tface->uv[0][1] == tface->uv[1][1] ) {
202                                                 tface->uv[0][0]= tface->uv[3][0];
203                                                 tface->uv[2][1]= tface->uv[3][1];
204                                         }
205                                         else  {
206                                                 tface->uv[0][1]= tface->uv[3][1];
207                                                 tface->uv[2][0]= tface->uv[3][0];
208                                         }
209
210                                 }
211                         }
212                 }
213         }
214 }
215
216 void transform_aspect_ratio_tface_uv(float *aspx, float *aspy)
217 {
218         int w, h;
219
220         transform_width_height_tface_uv(&w, &h);
221         *aspx= (float)w/256.0f;
222         *aspy= (float)h/256.0f;
223 }
224
225 void transform_width_height_tface_uv(int *width, int *height)
226 {
227         ImBuf *ibuf= BKE_image_get_ibuf(G.sima->image, &G.sima->iuser);
228
229         if(ibuf) {
230                 *width= ibuf->x;
231                 *height= ibuf->y;
232         }
233         else {
234                 *width= 256;
235                 *height= 256;
236         }
237 }
238
239 void mirror_tface_uv(char mirroraxis)
240 {
241         if (mirroraxis == 'x')
242                 Mirror(1); /* global x */
243         else if (mirroraxis == 'y')
244                 Mirror(2); /* global y */
245 }
246
247 void mirrormenu_tface_uv(void)
248 {
249         short mode= 0;
250
251         if( is_uv_tface_editing_allowed()==0 ) return;
252
253         mode= pupmenu("Mirror%t|X Axis%x1|Y Axis%x2|");
254
255         if(mode==-1) return;
256
257         if(mode==1) mirror_tface_uv('x');
258         else if(mode==2) mirror_tface_uv('y');
259
260         BIF_undo_push("Mirror UV");
261 }
262
263 void weld_align_tface_uv(char tool)
264 {
265         EditMesh *em = G.editMesh;
266         EditFace *efa;
267         MTFace *tface;
268         float min[2], max[2], cent[2];
269         int a;
270         
271         if( is_uv_tface_editing_allowed()==0 ) return;
272
273         if (!minmax_tface_uv(min, max))
274                 return;
275
276         cent[0]= (min[0]+max[0])/2.0;
277         cent[1]= (min[1]+max[1])/2.0;
278
279         if(tool == 'x' || tool == 'w') {
280                 for (a=0, efa= em->faces.first; efa; efa= efa->next, a++) {
281                         if(efa->f & SELECT) {
282                                 tface = CustomData_em_get(&em->fdata, efa->data, CD_MTFACE);
283                                 if(tface->flag & TF_SEL1)
284                                         tface->uv[0][0]= cent[0];
285                                 if(tface->flag & TF_SEL2)
286                                         tface->uv[1][0]= cent[0];
287                                 if(tface->flag & TF_SEL3)
288                                         tface->uv[2][0]= cent[0];
289                                 if(efa->v4 && (tface->flag & TF_SEL4))
290                                         tface->uv[3][0]= cent[0];
291                         }
292                 }
293         }
294
295         if(tool == 'y' || tool == 'w') {
296                 for (a=0, efa= em->faces.first; efa; efa= efa->next, a++) {
297                         if(efa->f & SELECT) {
298                                 tface = CustomData_em_get(&em->fdata, efa->data, CD_MTFACE);
299                                 if(tface->flag & TF_SEL1)
300                                         tface->uv[0][1]= cent[1];
301                                 if(tface->flag & TF_SEL2)
302                                         tface->uv[1][1]= cent[1];
303                                 if(tface->flag & TF_SEL3)
304                                         tface->uv[2][1]= cent[1];
305                                 if(efa->v4 && (tface->flag & TF_SEL4))
306                                         tface->uv[3][1]= cent[1];
307                         }
308                 }
309         }
310
311         object_uvs_changed(OBACT);
312 }
313
314 void weld_align_menu_tface_uv(void)
315 {
316         short mode= 0;
317
318         if( is_uv_tface_editing_allowed()==0 ) return;
319
320         mode= pupmenu("Weld/Align%t|Weld%x1|Align X%x2|Align Y%x3|");
321
322         if(mode==-1) return;
323
324         if(mode==1) weld_align_tface_uv('w');
325         else if(mode==2) weld_align_tface_uv('x');
326         else if(mode==3) weld_align_tface_uv('y');
327
328         if(mode==1) BIF_undo_push("Weld UV");
329         else if(mode==2 || mode==3) BIF_undo_push("Align UV");
330 }
331
332 void select_invert_tface_uv(void)
333 {
334         EditMesh *em = G.editMesh;
335         EditFace *efa;
336         MTFace *tface;
337         MFace *mface;
338         int a;
339         
340         if( is_uv_tface_editing_allowed()==0 ) return;
341
342         for (a=0, efa= em->faces.first; efa; efa= efa->next, a++) {
343                 if(efa->f & SELECT) {
344                         tface = CustomData_em_get(&G.editMesh->fdata, efa->data, CD_MTFACE);
345                         tface->flag ^= TF_SEL1;
346                         tface->flag ^= TF_SEL2;
347                         tface->flag ^= TF_SEL3;
348                         if(mface->v4) tface->flag ^= TF_SEL4;
349                 }
350         }
351
352         BIF_undo_push("Select Inverse UV");
353
354         allqueue(REDRAWIMAGE, 0);
355 }
356
357 void select_swap_tface_uv(void)
358 {
359         EditMesh *em = G.editMesh;
360         EditFace *efa;
361         MTFace *tface;
362         int sel=0;
363         
364         if( is_uv_tface_editing_allowed()==0 ) return;
365         
366         for (efa= em->faces.first; efa; efa= efa->next) {
367                 if (efa->f & SELECT) {
368                         tface = CustomData_em_get(&G.editMesh->fdata, efa->data, CD_MTFACE);
369                         if(tface->flag & (TF_SEL1+TF_SEL2+TF_SEL3+TF_SEL4)) {
370                                 sel= 1;
371                                 break;
372                         }
373                 }
374         }
375
376         for (efa= em->faces.first; efa; efa= efa->next) {
377                 if (efa->f & SELECT) {
378                         tface = CustomData_em_get(&G.editMesh->fdata, efa->data, CD_MTFACE);
379                         if(efa->v4) {
380                                 if(sel) tface->flag &= ~(TF_SEL1+TF_SEL2+TF_SEL3+TF_SEL4);
381                                 else tface->flag |= (TF_SEL1+TF_SEL2+TF_SEL3+TF_SEL4);
382                         }
383                         else {
384                                 if(sel) tface->flag &= ~(TF_SEL1+TF_SEL2+TF_SEL3+TF_SEL4);
385                                 else tface->flag |= (TF_SEL1+TF_SEL2+TF_SEL3);
386                         }
387                 }
388         }
389         
390         BIF_undo_push("Select swap");
391
392         allqueue(REDRAWIMAGE, 0);
393 }
394
395 static int msel_hit(float *limit, unsigned int *hitarray, unsigned int vertexid, float **uv, float *uv2, int sticky)
396 {
397         int i;
398         for(i=0; i< 4; i++) {
399                 if(hitarray[i] == vertexid) {
400                         if(sticky == 2) {
401                                 if(fabs(uv[i][0]-uv2[0]) < limit[0] &&
402                             fabs(uv[i][1]-uv2[1]) < limit[1])
403                                         return 1;
404                         }
405                         else return 1;
406                 }
407         }
408         return 0;
409 }
410
411 static void find_nearest_tface(MTFace **nearesttf, EditFace **nearestefa)
412 {
413         EditMesh *em= G.editMesh;
414         MTFace *tf;
415         EditFace *efa;
416         /*MFace *mf;*/
417         int i, nverts, mindist, dist, fcenter[2], uval[2];
418         short mval[2];
419
420         getmouseco_areawin(mval);       
421
422         mindist= 0x7FFFFFF;
423         *nearesttf= NULL;
424         *nearestefa= NULL;
425         
426         for (efa= em->faces.first; efa; efa= efa->next) {
427                 if (efa->f & SELECT) {
428                         tf= CustomData_em_get(&em->fdata, efa->data, CD_MTFACE);
429                         fcenter[0]= fcenter[1]= 0;
430                         nverts= efa->v4? 4: 3;
431                         for(i=0; i<nverts; i++) {
432                                 uvco_to_areaco_noclip(tf->uv[i], uval);
433                                 fcenter[0] += uval[0];
434                                 fcenter[1] += uval[1];
435                         }
436
437                         fcenter[0] /= nverts;
438                         fcenter[1] /= nverts;
439
440                         dist= abs(mval[0]- fcenter[0])+ abs(mval[1]- fcenter[1]);
441                         if (dist < mindist) {
442                                 *nearesttf= tf;
443                                 *nearestefa= efa;
444                                 mindist= dist;
445                         }
446                 }
447         }
448 }
449
450 static int nearest_uv_between(MTFace *tf, int nverts, int id, short *mval, int *uval)
451 {
452         float m[3], v1[3], v2[3], c1, c2;
453         int id1, id2;
454
455         id1= (id+nverts-1)%nverts;
456         id2= (id+nverts+1)%nverts;
457
458         m[0] = (float)(mval[0]-uval[0]);
459         m[1] = (float)(mval[1]-uval[1]);
460         Vec2Subf(v1, tf->uv[id1], tf->uv[id]);
461         Vec2Subf(v2, tf->uv[id2], tf->uv[id]);
462
463         /* m and v2 on same side of v-v1? */
464         c1= v1[0]*m[1] - v1[1]*m[0];
465         c2= v1[0]*v2[1] - v1[1]*v2[0];
466
467         if (c1*c2 < 0.0f)
468                 return 0;
469
470         /* m and v1 on same side of v-v2? */
471         c1= v2[0]*m[1] - v2[1]*m[0];
472         c2= v2[0]*v1[1] - v2[1]*v1[0];
473
474         return (c1*c2 >= 0.0f);
475 }
476
477 static void find_nearest_uv(MTFace **nearesttf, unsigned int *nearestv, int *nearestuv)
478 {
479         EditMesh *em= G.editMesh;
480         EditFace *efa;
481         MTFace *tf;
482         /*MFace *mf;*/
483         int i, nverts, mindist, dist, uval[2];
484         short mval[2];
485
486         getmouseco_areawin(mval);       
487
488         mindist= 0x7FFFFFF;
489         *nearesttf= NULL;
490         
491         for (efa= em->faces.first; efa; efa= efa->next) {
492                 if (efa->f & SELECT) {
493                         tf= CustomData_em_get(&em->fdata, efa->data, CD_MTFACE);
494                         nverts= efa->v4? 4: 3;
495                         for(i=0; i<nverts; i++) {
496                                 uvco_to_areaco_noclip(tf->uv[i], uval);
497                                 dist= abs(mval[0]-uval[0]) + abs(mval[1]-uval[1]);
498
499                                 if(tf->flag & TF_SEL_MASK(i))
500                                         dist += 5;
501
502                                 if(dist<=mindist) {
503                                         if(dist==mindist)
504                                                 if (!nearest_uv_between(tf, nverts, i, mval, uval))
505                                                         continue;
506
507                                         mindist= dist; 
508
509                                         *nearesttf= tf;
510                                         *nearestuv= i;
511
512                                         if (i==0) *nearestv=  efa->v1->tmp.l;
513                                         else if (i==1) *nearestv=  efa->v2->tmp.l;
514                                         else if (i==2) *nearestv=  efa->v3->tmp.l;
515                                         else *nearestv=  efa->v4->tmp.l;
516                                 }
517                         }
518                 }
519         }
520 }
521
522 void mouse_select_sima(void)
523 {
524         EditMesh *em = G.editMesh;
525         EditFace *efa;
526         MTFace *tf, *nearesttf;
527         EditFace *nearestefa=NULL;
528         int a, selectsticky, sticky, actface, nearestuv, i;
529         unsigned int hitv[4], nearestv;
530         float *hituv[4], limit[2];
531         
532         if( is_uv_tface_editing_allowed()==0 ) return;
533
534         get_connected_limit_tface_uv(limit);
535         actface= (G.qual & LR_ALTKEY || G.sima->flag & SI_SELACTFACE);
536
537         if(G.qual & LR_CTRLKEY) {
538                 if(G.sima->flag & SI_STICKYUVS) sticky= 0;
539                 else sticky= 1;
540         }
541         else {
542                 if(G.sima->flag & SI_STICKYUVS) sticky= 1;
543                 else if(G.sima->flag & SI_LOCALSTICKY) sticky= 2;
544                 else sticky= 0;
545         }
546
547         if(actface) {
548                 find_nearest_tface(&nearesttf, &nearestefa);
549                 if(nearesttf==NULL)
550                         return;
551
552                 nearesttf->flag |= TF_ACTIVE;
553
554                 for (i=0; i<4; i++)
555                         hituv[i]= nearesttf->uv[i];
556
557                 hitv[0]= nearestefa->v1->tmp.l;
558                 hitv[1]= nearestefa->v2->tmp.l;
559                 hitv[2]= nearestefa->v3->tmp.l;
560                 
561                 if (nearestefa->v4)     hitv[3]= nearestefa->v4->tmp.l;
562                 else                            hitv[3]= 0xFFFFFFFF;
563         }
564         else {
565                 find_nearest_uv(&nearesttf, &nearestv, &nearestuv);
566                 if(nearesttf==NULL)
567                         return;
568
569                 if(sticky) {
570                         for(i=0; i<4; i++)
571                                 hitv[i]= 0xFFFFFFFF;
572                         hitv[nearestuv]= nearestv;
573                         hituv[nearestuv]= nearesttf->uv[nearestuv];
574                 }
575         }
576
577         if(G.qual & LR_SHIFTKEY) {
578                 /* (de)select face */
579                 if(actface) {
580                         if(!(~nearesttf->flag & (TF_SEL1|TF_SEL2|TF_SEL3))
581                            && (!nearestefa->v4 || nearesttf->flag & TF_SEL4)) {
582                                 nearesttf->flag &= ~(TF_SEL1|TF_SEL2|TF_SEL3|TF_SEL4);
583                                 selectsticky= 0;
584                         }
585                         else {
586                                 nearesttf->flag |= TF_SEL1|TF_SEL2|TF_SEL3|TF_SEL4;
587                                 selectsticky= 1;
588                         }
589                 }
590                 /* (de)select uv node */
591                 else {
592                         if(nearesttf->flag & TF_SEL_MASK(nearestuv)) {
593                                 nearesttf->flag &= ~TF_SEL_MASK(nearestuv);
594                                 selectsticky= 0;
595                         }
596                         else {
597                                 nearesttf->flag |= TF_SEL_MASK(nearestuv);
598                                 selectsticky= 1;
599                         }
600                 }
601
602                 /* (de)select sticky uv nodes */
603                 if(sticky || actface) {
604                         EditVert *ev;
605                         
606                         for (a=0, ev=em->verts.first; ev; ev = ev->next, a++)
607                                 ev->tmp.l = a;
608                         
609                         /* deselect */
610                         if(selectsticky==0) {
611                                 for (efa= em->faces.first; efa; efa= efa->next) {
612                                         if(!(efa->f & SELECT)) continue;
613                                         tf= CustomData_em_get(&em->fdata, efa->data, CD_MTFACE);
614                                         if(nearesttf && tf!=nearesttf) tf->flag &=~ TF_ACTIVE;
615                                         if (!sticky) continue;
616
617                                         if(msel_hit(limit, hitv, efa->v1->tmp.l, hituv, tf->uv[0], sticky))
618                                                 tf->flag &= ~TF_SEL1;
619                                         if(msel_hit(limit, hitv, efa->v2->tmp.l, hituv, tf->uv[1], sticky))
620                                                 tf->flag &= ~TF_SEL2;
621                                         if(msel_hit(limit, hitv, efa->v3->tmp.l, hituv, tf->uv[2], sticky))
622                                                 tf->flag &= ~TF_SEL3;
623                                         if (efa->v4)
624                                                 if(msel_hit(limit, hitv, efa->v4->tmp.l, hituv, tf->uv[3], sticky))
625                                                         tf->flag &= ~TF_SEL4;
626                                 }
627                         }
628                         /* select */
629                         else {
630                                 for (efa= em->faces.first; efa; efa= efa->next) {
631                                         if(!(efa->f & SELECT)) continue;
632                                         tf= CustomData_em_get(&em->fdata, efa->data, CD_MTFACE);
633                                         if(nearesttf && tf!=nearesttf)
634                                                 tf->flag &=~ TF_ACTIVE;
635                                         if (!sticky) continue;
636
637                                         if(msel_hit(limit, hitv, efa->v1->tmp.l, hituv, tf->uv[0], sticky))
638                                                 tf->flag |= TF_SEL1;
639                                         if(msel_hit(limit, hitv, efa->v2->tmp.l, hituv, tf->uv[1], sticky))
640                                                 tf->flag |= TF_SEL2;
641                                         if(msel_hit(limit, hitv, efa->v3->tmp.l, hituv, tf->uv[2], sticky))
642                                                 tf->flag |= TF_SEL3;
643                                         if (efa->v4)
644                                                 if(msel_hit(limit, hitv, efa->v4->tmp.l, hituv, tf->uv[3], sticky))
645                                                         tf->flag |= TF_SEL4;
646                                 }
647                         }
648                 }
649         }
650         else {
651                 /* select face and deselect other faces */ 
652                 if(actface) {
653                         for (efa= em->faces.first; efa; efa= efa->next) {
654                                 tf= CustomData_em_get(&em->fdata, efa->data, CD_MTFACE);
655                                 tf->flag &= ~(TF_SEL1|TF_SEL2|TF_SEL3|TF_SEL4);
656                                 if(nearesttf && tf!=nearesttf)
657                                         tf->flag &= ~TF_ACTIVE;
658                         }
659                         if(nearesttf)
660                                 nearesttf->flag |= (TF_SEL1|TF_SEL2|TF_SEL3|TF_SEL4);
661                 }
662
663                 /* deselect uvs, and select sticky uvs */
664                 for (efa= em->faces.first; efa; efa= efa->next) {
665                         if(efa->f & SELECT) {
666                                 tf= CustomData_em_get(&em->fdata, efa->data, CD_MTFACE);
667                                 if(!actface) tf->flag &= ~(TF_SEL1|TF_SEL2|TF_SEL3|TF_SEL4);
668                                 if(!sticky) continue;
669
670                                 if(msel_hit(limit, hitv, efa->v1->tmp.l, hituv, tf->uv[0], sticky))
671                                         tf->flag |= TF_SEL1;
672                                 if(msel_hit(limit, hitv, efa->v2->tmp.l, hituv, tf->uv[1], sticky))
673                                         tf->flag |= TF_SEL2;
674                                 if(msel_hit(limit, hitv, efa->v3->tmp.l, hituv, tf->uv[2], sticky))
675                                         tf->flag |= TF_SEL3;
676                                 if(efa->v4)
677                                         if(msel_hit(limit, hitv, efa->v4->tmp.l, hituv, tf->uv[3], sticky))
678                                                 tf->flag |= TF_SEL4;
679                         }
680                 }
681                 
682                 if(!actface) 
683                         nearesttf->flag |= TF_SEL_MASK(nearestuv);
684         }
685         
686         force_draw(1);
687         
688         BIF_undo_push("Select UV");
689         rightmouse_transform();
690 }
691
692 void borderselect_sima(short whichuvs)
693 {
694         EditMesh *em = G.editMesh;
695         EditFace *efa;
696         MTFace *tface;
697         rcti rect;
698         rctf rectf;
699         int val;
700         short mval[2];
701
702         if( is_uv_tface_editing_allowed()==0) return;
703
704         val= get_border(&rect, 3);
705
706         if(val) {
707                 mval[0]= rect.xmin;
708                 mval[1]= rect.ymin;
709                 areamouseco_to_ipoco(G.v2d, mval, &rectf.xmin, &rectf.ymin);
710                 mval[0]= rect.xmax;
711                 mval[1]= rect.ymax;
712                 areamouseco_to_ipoco(G.v2d, mval, &rectf.xmax, &rectf.ymax);
713
714                 for (efa= em->faces.first; efa; efa= efa->next) {
715                         if(efa->f & SELECT) {
716                                 tface= CustomData_em_get(&em->fdata, efa->data, CD_MTFACE);
717                                 
718                                 if (whichuvs == UV_SELECT_ALL) {
719                                 
720                                         if(BLI_in_rctf(&rectf, (float)tface->uv[0][0], (float)tface->uv[0][1])) {
721                                                 if(val==LEFTMOUSE) tface->flag |= TF_SEL1;
722                                                 else tface->flag &= ~TF_SEL1;
723                                         }
724                                         if(BLI_in_rctf(&rectf, (float)tface->uv[1][0], (float)tface->uv[1][1])) {
725                                                 if(val==LEFTMOUSE) tface->flag |= TF_SEL2;
726                                                 else tface->flag &= ~TF_SEL2;
727                                         }
728                                         if(BLI_in_rctf(&rectf, (float)tface->uv[2][0], (float)tface->uv[2][1])) {
729                                                 if(val==LEFTMOUSE) tface->flag |= TF_SEL3;
730                                                 else tface->flag &= ~TF_SEL3;
731                                         }
732                                         if(efa->v4 && BLI_in_rctf(&rectf, (float)tface->uv[3][0], (float)tface->uv[3][1])) {
733                                                 if(val==LEFTMOUSE) tface->flag |= TF_SEL4;
734                                                 else tface->flag &= ~TF_SEL4;
735                                         }
736                                 } else if (whichuvs == UV_SELECT_PINNED) {
737                                         if ((tface->unwrap & TF_PIN1) && 
738                                                 BLI_in_rctf(&rectf, (float)tface->uv[0][0], (float)tface->uv[0][1])) {
739                                                 
740                                                 if(val==LEFTMOUSE) tface->flag |= TF_SEL1;
741                                                 else tface->flag &= ~TF_SEL1;
742                                         }
743                                         if ((tface->unwrap & TF_PIN2) && 
744                                                 BLI_in_rctf(&rectf, (float)tface->uv[1][0], (float)tface->uv[1][1])) {
745                                                 
746                                                 if(val==LEFTMOUSE) tface->flag |= TF_SEL2;
747                                                 else tface->flag &= ~TF_SEL2;
748                                         }
749                                         if ((tface->unwrap & TF_PIN3) && 
750                                                 BLI_in_rctf(&rectf, (float)tface->uv[2][0], (float)tface->uv[2][1])) {
751                                                 
752                                                 if(val==LEFTMOUSE) tface->flag |= TF_SEL3;
753                                                 else tface->flag &= ~TF_SEL3;
754                                         }
755                                         if ((efa->v4) && (tface->unwrap & TF_PIN4) && BLI_in_rctf(&rectf, (float)tface->uv[3][0], (float)tface->uv[3][1])) {
756                                                 if(val==LEFTMOUSE) tface->flag |= TF_SEL4;
757                                                 else tface->flag &= ~TF_SEL4;
758                                         }
759                                 }
760                         }
761                                                         
762                 }
763                 BIF_undo_push("Border select UV");
764                 scrarea_queue_winredraw(curarea);
765         }
766 }
767
768 /** This is an ugly function to set the Tface selection flags depending
769   * on whether its UV coordinates are inside the normalized 
770   * area with radius rad and offset offset. These coordinates must be
771   * normalized to 1.0 
772   * Just for readability...
773   */
774
775 void sel_uvco_inside_radius(short sel, MTFace *tface, int index, float *offset, float *ell, short select_mask)
776 {
777         // normalized ellipse: ell[0] = scaleX,
778         //                        [1] = scaleY
779
780         float *uv = tface->uv[index];
781         float x, y, r2;
782
783         x = (uv[0] - offset[0]) * ell[0];
784         y = (uv[1] - offset[1]) * ell[1];
785
786         r2 = x * x + y * y;
787         if (r2 < 1.0) {
788                 if (sel == LEFTMOUSE) tface->flag |= select_mask;
789                 else tface->flag &= ~select_mask;
790         }
791 }
792
793 // see below:
794 /** gets image dimensions of the 2D view 'v' */
795 static void getSpaceImageDimension(SpaceImage *sima, float *xy)
796 {
797         ImBuf *ibuf= BKE_image_get_ibuf(sima->image, &sima->iuser);
798         float z;
799
800         z = sima->zoom;
801
802         if (ibuf) {
803                 xy[0] = ibuf->x * z;
804                 xy[1] = ibuf->y * z;
805         } else {
806                 xy[0] = 256 * z;
807                 xy[1] = 256 * z;
808         }
809 }
810
811 /** Callback function called by circle_selectCB to enable 
812   * brush select in UV editor.
813   */
814
815 void uvedit_selectionCB(short selecting, Object *editobj, short *mval, float rad) 
816 {
817         EditMesh *em = G.editMesh;
818         EditFace *efa;
819         float offset[2];
820         MTFace *tface;
821
822         float ellipse[2]; // we need to deal with ellipses, as
823                           // non square textures require for circle
824                                           // selection. this ellipse is normalized; r = 1.0
825
826         getSpaceImageDimension(curarea->spacedata.first, ellipse);
827         ellipse[0] /= rad;
828         ellipse[1] /= rad;
829
830         areamouseco_to_ipoco(G.v2d, mval, &offset[0], &offset[1]);
831         
832         if (selecting) {
833                 for (efa= em->faces.first; efa; efa= efa->next) {
834                         tface= CustomData_em_get(&em->fdata, efa->data, CD_MTFACE);
835                         sel_uvco_inside_radius(selecting, tface, 0, offset, ellipse, TF_SEL1);
836                         sel_uvco_inside_radius(selecting, tface, 1, offset, ellipse, TF_SEL2);
837                         sel_uvco_inside_radius(selecting, tface, 2, offset, ellipse, TF_SEL3);
838                         if (efa->v4)
839                                 sel_uvco_inside_radius(selecting, tface, 3, offset, ellipse, TF_SEL4);
840                 }
841
842                 if(G.f & G_DRAWFACES) { /* full redraw only if necessary */
843                         draw_sel_circle(0, 0, 0, 0, 0); /* signal */
844                         force_draw(0);
845                 }
846                 else { /* force_draw() is no good here... */
847                         glDrawBuffer(GL_FRONT);
848                         draw_tfaces();
849                         bglFlush();
850                         glDrawBuffer(GL_BACK);
851                 }
852         }       
853 }
854
855
856 void mouseco_to_curtile(void)
857 {
858         float fx, fy;
859         short mval[2];
860         
861         if( is_uv_tface_editing_allowed()==0) return;
862
863         if(G.sima->image && G.sima->image->tpageflag & IMA_TILES) {
864                 
865                 G.sima->flag |= SI_EDITTILE;
866                 
867                 while(get_mbut()&L_MOUSE) {
868                         
869                         calc_image_view(G.sima, 'f');
870                         
871                         getmouseco_areawin(mval);
872                         areamouseco_to_ipoco(G.v2d, mval, &fx, &fy);
873
874                         if(fx>=0.0 && fy>=0.0 && fx<1.0 && fy<1.0) {
875                         
876                                 fx= (fx)*G.sima->image->xrep;
877                                 fy= (fy)*G.sima->image->yrep;
878                                 
879                                 mval[0]= fx;
880                                 mval[1]= fy;
881                                 
882                                 G.sima->curtile= mval[1]*G.sima->image->xrep + mval[0];
883                         }
884
885                         scrarea_do_windraw(curarea);
886                         screen_swapbuffers();
887                 }
888                 
889                 G.sima->flag &= ~SI_EDITTILE;
890
891                 image_changed(G.sima, 1);
892
893                 allqueue(REDRAWVIEW3D, 0);
894                 scrarea_queue_winredraw(curarea);
895         }
896 }
897
898 /* Could be used for other 2D views also */
899 void mouseco_to_cursor_sima(void)
900 {
901         short mval[2];
902         getmouseco_areawin(mval);
903         areamouseco_to_ipoco(G.v2d, mval, &G.v2d->cursor[0], &G.v2d->cursor[1]);
904         scrarea_queue_winredraw(curarea);
905 }
906
907 void stitch_uv_tface(int mode)
908 {
909         MTFace *tf;
910         int a, vtot;
911         float newuv[2], limit[2];
912         UvMapVert *vlist, *iterv, *v;
913         EditMesh *em = G.editMesh;
914         EditVert *ev;
915         EditFace *efa;
916         
917         struct UvVertMap *vmap;
918         
919         
920         if(is_uv_tface_editing_allowed()==0)
921                 return;
922
923         limit[0]= limit[1]= 20.0;
924         if(mode==1) {
925                 add_numbut(0, NUM|FLO, "Limit:", 0.1, 1000.0, &limit[0], NULL);
926                 if (!do_clever_numbuts("Stitch UVs", 1, REDRAW))
927                         return;
928         }
929
930         limit[0]= limit[1]= limit[0]/256.0;
931         if(G.sima->image) {
932                 ImBuf *ibuf= BKE_image_get_ibuf(G.sima->image, &G.sima->iuser);
933
934                 if(ibuf && ibuf->x > 0 && ibuf->y > 0) {
935                         limit[1]= limit[0]/(float)ibuf->y;
936                         limit[0]= limit[0]/(float)ibuf->x;
937                 }
938         }
939
940         /*vmap= make_uv_vert_map(me->mface, tf, me->totface, me->totvert, 1, limit);*/
941         EM_init_index_arrays(0, 0, 1);
942         vmap= make_uv_vert_map_EM(1, 0, limit);
943         if(vmap == NULL)
944                 return;
945
946         if(mode==0) {
947                 for(a=0, ev= em->verts.first; ev; a++, ev= ev->next) {
948                         v = get_uv_map_vert_EM(vmap, a);
949
950                         if(v == NULL)
951                                 continue;
952
953                         newuv[0]= 0; newuv[1]= 0;
954                         vtot= 0;
955
956                         for(iterv=v; iterv; iterv=iterv->next) {
957                                 efa = EM_get_face_for_index(iterv->f);
958                                 tf = CustomData_em_get(&em->fdata, efa->data, CD_MTFACE);
959                                 if (tf->flag & TF_SEL_MASK(iterv->tfindex)) {
960                                         newuv[0] += tf->uv[iterv->tfindex][0];
961                                         newuv[1] += tf->uv[iterv->tfindex][1];
962                                         vtot++;
963                                 }
964                         }
965
966                         if (vtot > 1) {
967                                 newuv[0] /= vtot; newuv[1] /= vtot;
968
969                                 for(iterv=v; iterv; iterv=iterv->next) {
970                                         efa = EM_get_face_for_index(iterv->f);
971                                         tf = CustomData_em_get(&em->fdata, efa->data, CD_MTFACE);
972                                         if (tf->flag & TF_SEL_MASK(iterv->tfindex)) {
973                                                 tf->uv[iterv->tfindex][0]= newuv[0];
974                                                 tf->uv[iterv->tfindex][1]= newuv[1];
975                                         }
976                                 }
977                         }
978                 }
979         } else if(mode==1) {
980                 for(a=0, ev= em->verts.first; ev; a++, ev= ev->next) {
981                         vlist= get_uv_map_vert_EM(vmap, a);
982
983                         while(vlist) {
984                                 newuv[0]= 0; newuv[1]= 0;
985                                 vtot= 0;
986
987                                 for(iterv=vlist; iterv; iterv=iterv->next) {
988                                         if((iterv != vlist) && iterv->separate)
989                                                 break;
990                                         efa = EM_get_face_for_index(iterv->f);
991                                         tf = CustomData_em_get(&em->fdata, efa->data, CD_MTFACE);
992                                         
993                                         if (tf[iterv->f].flag & TF_SEL_MASK(iterv->tfindex)) {
994                                                 newuv[0] += tf->uv[iterv->tfindex][0];
995                                                 newuv[1] += tf->uv[iterv->tfindex][1];
996                                                 vtot++;
997                                         }
998                                 }
999
1000                                 if (vtot > 1) {
1001                                         newuv[0] /= vtot; newuv[1] /= vtot;
1002
1003                                         for(iterv=vlist; iterv; iterv=iterv->next) {
1004                                                 if((iterv != vlist) && iterv->separate)
1005                                                         break;
1006                                                 efa = EM_get_face_for_index(iterv->f);
1007                                                 tf = CustomData_em_get(&em->fdata, efa->data, CD_MTFACE);
1008                                                 if (tf->flag & TF_SEL_MASK(iterv->tfindex)) {
1009                                                         tf->uv[iterv->tfindex][0]= newuv[0];
1010                                                         tf->uv[iterv->tfindex][1]= newuv[1];
1011                                                 }
1012                                         }
1013                                 }
1014                                 vlist= iterv;
1015                         }
1016                 }
1017         }
1018
1019         free_uv_vert_map_EM(vmap);
1020         EM_free_index_arrays();
1021         
1022         if(G.sima->flag & SI_BE_SQUARE) be_square_tface_uv(em);
1023
1024         BIF_undo_push("Stitch UV");
1025
1026         object_uvs_changed(OBACT);
1027 }
1028
1029 void select_linked_tface_uv(int mode)
1030 {
1031         EditMesh *em= G.editMesh;
1032         EditFace *efa;
1033         MTFace *tf, *nearesttf=NULL;
1034         UvVertMap *vmap;
1035         UvMapVert *vlist, *iterv, *startv;
1036         unsigned int *stack, stacksize= 0, nearestv;
1037         char *flag;
1038         int a, nearestuv, i, nverts, j;
1039         float limit[2];
1040         if(is_uv_tface_editing_allowed()==0)
1041                 return;
1042
1043         if (mode == 2) {
1044                 nearesttf= NULL;
1045                 nearestuv= 0;
1046         }
1047         if (mode!=2) {
1048                 find_nearest_uv(&nearesttf, &nearestv, &nearestuv);
1049                 if(nearesttf==NULL)
1050                         return;
1051         }
1052
1053         get_connected_limit_tface_uv(limit);
1054         vmap= make_uv_vert_map_EM(1, 1, limit);
1055         if(vmap == NULL)
1056                 return;
1057
1058         stack= MEM_mallocN(sizeof(*stack)* BLI_countlist(&em->faces), "UvLinkStack");
1059         flag= MEM_callocN(sizeof(*flag)*BLI_countlist(&em->faces), "UvLinkFlag");
1060
1061         if (mode == 2) {
1062                 for (a=0, efa= em->faces.first; efa; efa= efa->next, a++) {
1063                         if(!(efa->h) && (efa->f & SELECT)) {
1064                                 tf = CustomData_em_get(&em->fdata, efa->data, CD_MTFACE);
1065                                 if(tf->flag & (TF_SEL1|TF_SEL2|TF_SEL3|TF_SEL4)) {
1066                                         stack[stacksize]= a;
1067                                         stacksize++;
1068                                         flag[a]= 1;
1069                                 }
1070                         }
1071                 }
1072         } else {
1073                 for (a=0, efa= em->faces.first; efa; efa= efa->next, a++) {
1074                         tf = CustomData_em_get(&em->fdata, efa->data, CD_MTFACE);
1075                         if(tf == nearesttf) {
1076                                 stack[stacksize]= a;
1077                                 stacksize++;
1078                                 flag[a]= 1;
1079                                 break;
1080                         }
1081                 }
1082         }
1083
1084         while(stacksize > 0) {
1085                 stacksize--;
1086                 a= stack[stacksize];
1087                 
1088                 for (j=0, efa= em->faces.first; efa; efa= efa->next, j++) {
1089                         if (j==a) {
1090                                 tf = CustomData_em_get(&em->fdata, efa->data, CD_MTFACE);
1091                                 break;
1092                         }
1093                 }
1094
1095                 nverts= efa->v4? 4: 3;
1096
1097                 for(i=0; i<nverts; i++) {
1098                         /* make_uv_vert_map_EM sets verts tmp.l to the indicies */
1099                         vlist= get_uv_map_vert_EM(vmap, (*(&efa->v1 + i))->tmp.l);
1100                         
1101                         startv= vlist;
1102
1103                         for(iterv=vlist; iterv; iterv=iterv->next) {
1104                                 if(iterv->separate)
1105                                         startv= iterv;
1106                                 if(iterv->f == a)
1107                                         break;
1108                         }
1109
1110                         for(iterv=startv; iterv; iterv=iterv->next) {
1111                                 if((startv != iterv) && (iterv->separate))
1112                                         break;
1113                                 else if(!flag[iterv->f]) {
1114                                         flag[iterv->f]= 1;
1115                                         stack[stacksize]= iterv->f;;
1116                                         stacksize++;
1117                                 }
1118                         }
1119                 }
1120         }
1121
1122         if(mode==0 || mode==2) {
1123                 for (a=0, efa= em->faces.first; efa; efa= efa->next, a++) {
1124                         tf = CustomData_em_get(&em->fdata, efa->data, CD_MTFACE);
1125                         if(flag[a])
1126                                 tf->flag |= (TF_SEL1|TF_SEL2|TF_SEL3|TF_SEL4);
1127                         else
1128                                 tf->flag &= ~(TF_SEL1|TF_SEL2|TF_SEL3|TF_SEL4);
1129                 }
1130         }
1131         else if(mode==1) {
1132                 for (a=0, efa= em->faces.first; efa; efa= efa->next, a++) {
1133                         if(flag[a]) {
1134                                 tf = CustomData_em_get(&em->fdata, efa->data, CD_MTFACE);
1135                                 if (efa->v4) {
1136                                         if((tf->flag & (TF_SEL1|TF_SEL2|TF_SEL3|TF_SEL4)))
1137                                                 break;
1138                                 }
1139                                 else if(tf->flag & (TF_SEL1|TF_SEL2|TF_SEL3))
1140                                         break;
1141                         }
1142                 }
1143
1144                 /*if (a<me->totface) {*/
1145                 if (efa) {
1146                         for (a=0, efa= em->faces.first; efa; efa= efa->next, a++) {
1147                                 if(flag[a]) {
1148                                         tf = CustomData_em_get(&em->fdata, efa->data, CD_MTFACE);
1149                                         tf->flag &= ~(TF_SEL1|TF_SEL2|TF_SEL3|TF_SEL4);
1150                                 }
1151                         }
1152                 }
1153                 else {
1154                         for (a=0, efa= em->faces.first; efa; efa= efa->next, a++) {
1155                                 if(flag[a]) {
1156                                         tf = CustomData_em_get(&em->fdata, efa->data, CD_MTFACE);
1157                                         tf->flag |= (TF_SEL1|TF_SEL2|TF_SEL3|TF_SEL4);
1158                                 }
1159                         }
1160                 }
1161         }
1162         
1163         MEM_freeN(stack);
1164         MEM_freeN(flag);
1165         free_uv_vert_map_EM(vmap);
1166
1167         BIF_undo_push("Select linked UV");
1168         scrarea_queue_winredraw(curarea);
1169 }
1170
1171 void unlink_selection(void)
1172 {
1173         EditMesh *em= G.editMesh;
1174         EditFace *efa;
1175         MTFace *tface;
1176         int a;
1177
1178         if( is_uv_tface_editing_allowed()==0 ) return;
1179
1180         for (a=0, efa= em->faces.first; efa; efa= efa->next, a++) {
1181                 if(efa->f & SELECT) {
1182                         tface = CustomData_em_get(&em->fdata, efa->data, CD_MTFACE);
1183                         if(efa->v4) {
1184                                 if(~tface->flag & (TF_SEL1|TF_SEL2|TF_SEL3|TF_SEL4))
1185                                         tface->flag &= ~(TF_SEL1|TF_SEL2|TF_SEL3|TF_SEL4);
1186                         } else {
1187                                 if(~tface->flag & (TF_SEL1|TF_SEL2|TF_SEL3))
1188                                         tface->flag &= ~(TF_SEL1|TF_SEL2|TF_SEL3);
1189                         }
1190                 }
1191         }
1192         
1193         BIF_undo_push("Unlink UV selection");
1194         scrarea_queue_winredraw(curarea);
1195 }
1196
1197 void toggle_uv_select(int mode)
1198 {
1199         switch(mode){
1200         case 'f':
1201                 G.sima->flag ^= SI_SELACTFACE;
1202                 break;
1203         case 's':
1204                 G.sima->flag ^= SI_STICKYUVS; 
1205                 if (G.sima->flag & SI_STICKYUVS) G.sima->flag &= ~SI_LOCALSTICKY;
1206                 else G.sima->flag |= SI_LOCALSTICKY;
1207                 break;
1208         case 'l': 
1209                  G.sima->flag ^= SI_LOCALSTICKY;
1210                  if (G.sima->flag & SI_LOCALSTICKY) G.sima->flag &= ~SI_STICKYUVS;
1211                 break;
1212         case 'o':
1213                 G.sima->flag &= ~SI_STICKYUVS; 
1214                 G.sima->flag &= ~SI_LOCALSTICKY;
1215                 break;
1216         }
1217         allqueue(REDRAWIMAGE, 0);
1218 }
1219
1220 void pin_tface_uv(int mode)
1221 {
1222         EditMesh *em = G.editMesh;
1223         EditFace *efa;
1224         MTFace *tface;
1225         int a;
1226         
1227         if( is_uv_tface_editing_allowed()==0 ) return;
1228         
1229         for (a=0, efa= em->faces.first; efa; efa= efa->next, a++) {
1230                 if(efa->f & SELECT) {
1231                         tface = CustomData_em_get(&em->fdata, efa->data, CD_MTFACE);
1232                         if(mode ==1){
1233                                 if(tface->flag & TF_SEL1) tface->unwrap |= TF_PIN1;
1234                                 if(tface->flag & TF_SEL2) tface->unwrap |= TF_PIN2;
1235                                 if(tface->flag & TF_SEL3) tface->unwrap |= TF_PIN3;
1236                                 if(efa->v4)
1237                                         if(tface->flag & TF_SEL4) tface->unwrap |= TF_PIN4;
1238                         }
1239                         else if (mode ==0){
1240                                 if(tface->flag & TF_SEL1) tface->unwrap &= ~TF_PIN1;
1241                                 if(tface->flag & TF_SEL2) tface->unwrap &= ~TF_PIN2;
1242                                 if(tface->flag & TF_SEL3) tface->unwrap &= ~TF_PIN3;
1243                                 if(efa->v4)
1244                                         if(tface->flag & TF_SEL4) tface->unwrap &= ~TF_PIN4;
1245                         }
1246                 }
1247         }
1248         
1249         BIF_undo_push("Pin UV");
1250         scrarea_queue_winredraw(curarea);
1251 }
1252
1253 void select_pinned_tface_uv(void)
1254 {
1255         EditMesh *em= G.editMesh;
1256         EditFace *efa;
1257         MTFace *tface;
1258         int a;
1259         
1260         if( is_uv_tface_editing_allowed()==0 ) return;
1261         
1262         for (a=0, efa= em->faces.first; efa; efa= efa->next, a++) {
1263                 if(efa->f & SELECT) {
1264                         tface = CustomData_em_get(&em->fdata, efa->data, CD_MTFACE);
1265                         if (tface->unwrap & TF_PIN1) tface->flag |= TF_SEL1;
1266                         if (tface->unwrap & TF_PIN2) tface->flag |= TF_SEL2;
1267                         if (tface->unwrap & TF_PIN3) tface->flag |= TF_SEL3;
1268                         if(efa->v4) {
1269                                 if (tface->unwrap & TF_PIN4) tface->flag |= TF_SEL4;
1270                         }
1271                         
1272                 }
1273         }
1274         
1275         BIF_undo_push("Select Pinned UVs");
1276         scrarea_queue_winredraw(curarea);
1277 }
1278
1279 int minmax_tface_uv(float *min, float *max)
1280 {
1281         EditMesh *em= G.editMesh;
1282         EditFace *efa;
1283         MTFace *tf;
1284         int sel;
1285         
1286         if( is_uv_tface_editing_allowed()==0 ) return 0;
1287
1288         INIT_MINMAX2(min, max);
1289
1290         sel= 0;
1291         for (efa= em->faces.first; efa; efa= efa->next) {
1292                 if (efa->h);
1293                 else if(efa->f & SELECT) {
1294                         tf = CustomData_em_get(&em->fdata, efa->data, CD_MTFACE);
1295                         if (tf->flag & TF_SEL1) DO_MINMAX2(tf->uv[0], min, max);
1296                         if (tf->flag & TF_SEL2) DO_MINMAX2(tf->uv[1], min, max);
1297                         if (tf->flag & TF_SEL3) DO_MINMAX2(tf->uv[2], min, max);
1298                         if (efa->v4 && tf->flag & TF_SEL4)      DO_MINMAX2(tf->uv[3], min, max);
1299                         sel = 1;
1300                 }
1301         }
1302
1303         return sel;
1304 }
1305
1306 static void sima_show_info(int channels, int x, int y, char *cp, float *fp, int *zp, float *zpf)
1307 {
1308         short ofs;
1309         char str[256];
1310         
1311         ofs= sprintf(str, "X: %d Y: %d ", x, y);
1312         if(cp)
1313                 ofs+= sprintf(str+ofs, "| R: %d G: %d B: %d A: %d ", cp[0], cp[1], cp[2], cp[3]);
1314         if(fp) {
1315                 if(channels==4)
1316                         ofs+= sprintf(str+ofs, "| R: %.3f G: %.3f B: %.3f A: %.3f ", fp[0], fp[1], fp[2], fp[3]);
1317                 else if(channels==1)
1318                         ofs+= sprintf(str+ofs, "| Val: %.3f ", fp[0]);
1319                 else if(channels==3)
1320                         ofs+= sprintf(str+ofs, "| R: %.3f G: %.3f B: %.3f ", fp[0], fp[1], fp[2]);
1321         }
1322         if(zp)
1323                 ofs+= sprintf(str+ofs, "| Z: %.4f ", 0.5+0.5*( ((float)*zp)/(float)0x7fffffff));
1324         if(zpf)
1325                 ofs+= sprintf(str+ofs, "| Z: %.3f ", *zpf);
1326         
1327         glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
1328         glEnable(GL_BLEND);
1329         
1330         glColor4f(.0,.0,.0,.25);
1331         glRectf(0.0, 0.0, curarea->winx, 30.0);
1332         glDisable(GL_BLEND);
1333         
1334         glColor3ub(255, 255, 255);
1335         glRasterPos2i(10, 10);
1336         
1337         BMF_DrawString(G.fonts, str);
1338
1339 }
1340
1341 void sima_sample_color(void)
1342 {
1343         ImBuf *ibuf= BKE_image_get_ibuf(G.sima->image, &G.sima->iuser);
1344         float fx, fy;
1345         short mval[2], mvalo[2], firsttime=1;
1346         
1347         if(ibuf==NULL)
1348                 return;
1349         
1350         calc_image_view(G.sima, 'f');
1351         getmouseco_areawin(mvalo);
1352         
1353         while(get_mbut() & L_MOUSE) {
1354                 
1355                 getmouseco_areawin(mval);
1356                 if(mval[0]!=mvalo[0] || mval[1]!=mvalo[1] || firsttime) {
1357                         firsttime= 0;
1358                         areamouseco_to_ipoco(G.v2d, mval, &fx, &fy);
1359                         
1360                         if(fx>=0.0 && fy>=0.0 && fx<1.0 && fy<1.0) {
1361                                 float *fp= NULL, *zpf= NULL;
1362                                 int *zp= NULL;
1363                                 char *cp= NULL;
1364                                 
1365                                 int x= (int) (fx*ibuf->x);
1366                                 int y= (int) (fy*ibuf->y);
1367                                 
1368                                 if(x>=ibuf->x) x= ibuf->x-1;
1369                                 if(y>=ibuf->y) y= ibuf->y-1;
1370                                 
1371                                 if(ibuf->rect)
1372                                         cp= (char *)(ibuf->rect + y*ibuf->x + x);
1373                                 if(ibuf->zbuf)
1374                                         zp= ibuf->zbuf + y*ibuf->x + x;
1375                                 if(ibuf->zbuf_float)
1376                                         zpf= ibuf->zbuf_float + y*ibuf->x + x;
1377                                 if(ibuf->rect_float)
1378                                         fp= (ibuf->rect_float + (ibuf->channels)*(y*ibuf->x + x));
1379                                         
1380                                 if(G.sima->cumap) {
1381                                         float vec[3];
1382                                         if(fp==NULL) {
1383                                                 fp= vec;
1384                                                 vec[0]= (float)cp[0]/255.0f;
1385                                                 vec[1]= (float)cp[1]/255.0f;
1386                                                 vec[2]= (float)cp[2]/255.0f;
1387                                         }
1388                                         
1389                                         if(ibuf->channels==4) {
1390                                                 if(G.qual & LR_CTRLKEY) {
1391                                                         curvemapping_set_black_white(G.sima->cumap, NULL, fp);
1392                                                         curvemapping_do_ibuf(G.sima->cumap, ibuf);
1393                                                 }
1394                                                 else if(G.qual & LR_SHIFTKEY) {
1395                                                         curvemapping_set_black_white(G.sima->cumap, fp, NULL);
1396                                                         curvemapping_do_ibuf(G.sima->cumap, ibuf);
1397                                                 }
1398                                         }
1399                                 }
1400                                 
1401                                 scrarea_do_windraw(curarea);
1402                                 myortho2(-0.375, curarea->winx-0.375, -0.375, curarea->winy-0.375);
1403                                 glLoadIdentity();
1404                                 sima_show_info(ibuf->channels, x, y, cp, fp, zp, zpf);
1405                                 screen_swapbuffers();
1406                         }
1407                         
1408                 }
1409                 BIF_wait_for_statechange();
1410         }
1411         
1412         scrarea_queue_winredraw(curarea);
1413 }
1414
1415 /* Image functions */
1416
1417 static void load_image_filesel(char *str)       /* called from fileselect */
1418 {
1419         Image *ima= NULL;
1420
1421         ima= BKE_add_image_file(str);
1422         if(ima) {
1423
1424                 G.sima->image= ima;
1425
1426                 BKE_image_signal(ima, &G.sima->iuser, IMA_SIGNAL_RELOAD);
1427                 image_changed(G.sima, 0);
1428
1429         }
1430         BIF_undo_push("Load image UV");
1431         allqueue(REDRAWIMAGE, 0);
1432 }
1433
1434 static void image_replace(Image *old, Image *new)
1435 {
1436         MTFace *tface;
1437         Mesh *me;
1438         int a, rep=0;
1439
1440         new->tpageflag= old->tpageflag;
1441         new->twsta= old->twsta;
1442         new->twend= old->twend;
1443         new->xrep= old->xrep;
1444         new->yrep= old->yrep;
1445  
1446         /* TODO - This is incorrect!! -
1447          * replace should take all layers into account,
1448          * should also work with editmode */
1449         me= G.main->mesh.first;
1450         while(me) {
1451
1452                 if(me->id.lib==NULL && me->mtface) {
1453                         tface= me->mtface;
1454                         a= me->totface;
1455                         while(a--) {
1456                                 if(tface->tpage==old) {
1457                                         tface->tpage= new;
1458                                         rep++;
1459                                 }
1460                                 tface++;
1461                         }
1462                 }
1463                 me= me->id.next;
1464  
1465         }
1466         if(rep) {
1467                 if(new->id.us==0) id_us_plus(&new->id);
1468                 else id_lib_extern(&new->id);
1469                 
1470         }
1471         else error("Nothing replaced");
1472 }
1473
1474 static void replace_image_filesel(char *str)            /* called from fileselect */
1475 {
1476         Image *ima=0;
1477         
1478         ima= BKE_add_image_file(str);
1479         if(ima) {
1480  
1481                 if(G.sima->image && G.sima->image != ima) {
1482                         image_replace(G.sima->image, ima);
1483                 }
1484  
1485                 G.sima->image= ima;
1486
1487                 BKE_image_signal(ima, &G.sima->iuser, IMA_SIGNAL_RELOAD);
1488
1489                 /* replace also assigns: */
1490                 image_changed(G.sima, 0);
1491
1492         }
1493         BIF_undo_push("Replace image UV");
1494         allqueue(REDRAWIMAGE, 0);
1495 }
1496
1497
1498 static void save_image_doit(char *name)
1499 {
1500         Image *ima= G.sima->image;
1501         ImBuf *ibuf= BKE_image_get_ibuf(ima, &G.sima->iuser);
1502         int len;
1503         char str[FILE_MAXDIR+FILE_MAXFILE];
1504
1505         if (ibuf) {
1506                 BLI_strncpy(str, name, sizeof(str));
1507
1508                 BLI_convertstringcode(str, G.sce, G.scene->r.cfra);
1509                 
1510                 if(G.scene->r.scemode & R_EXTENSION) 
1511                         BKE_add_image_extension(str, G.sima->imtypenr);
1512                 
1513                 if (saveover(str)) {
1514                         
1515                         /* enforce user setting for RGB or RGBA, but skip BW */
1516                         if(G.scene->r.planes==32)
1517                                 ibuf->depth= 32;
1518                         else if(G.scene->r.planes==24)
1519                                 ibuf->depth= 24;
1520                         
1521                         waitcursor(1);
1522                         if(G.sima->imtypenr==R_MULTILAYER) {
1523                                 RenderResult *rr= BKE_image_get_renderresult(ima);
1524                                 if(rr) {
1525                                         RE_WriteRenderResult(rr, str, G.scene->r.quality);
1526                                         
1527                                         BLI_strncpy(ima->name, name, sizeof(ima->name));
1528                                         BLI_strncpy(ibuf->name, str, sizeof(ibuf->name));
1529                                         
1530                                         /* should be function? nevertheless, saving only happens here */
1531                                         for(ibuf= ima->ibufs.first; ibuf; ibuf= ibuf->next)
1532                                                 ibuf->userflags &= ~IB_BITMAPDIRTY;
1533                                         
1534                                 }
1535                                 else error("Did not write, no Multilayer Image");
1536                         }
1537                         else if (BKE_write_ibuf(ibuf, str, G.sima->imtypenr, G.scene->r.subimtype, G.scene->r.quality)) {
1538                                 BLI_strncpy(ima->name, name, sizeof(ima->name));
1539                                 BLI_strncpy(ibuf->name, str, sizeof(ibuf->name));
1540                                 
1541                                 ibuf->userflags &= ~IB_BITMAPDIRTY;
1542                                 
1543                                 /* change type? */
1544                                 if( ELEM(ima->source, IMA_SRC_GENERATED, IMA_SRC_VIEWER)) {
1545                                         ima->source= IMA_SRC_FILE;
1546                                         ima->type= IMA_TYPE_IMAGE;
1547                                 }
1548                                 if(ima->type==IMA_TYPE_R_RESULT)
1549                                         ima->type= IMA_TYPE_IMAGE;
1550                                 
1551                                 /* name image as how we saved it */
1552                                 len= strlen(str);
1553                                 while (len > 0 && str[len - 1] != '/' && str[len - 1] != '\\') len--;
1554                                 rename_id(&ima->id, str+len);
1555                         } 
1556                         else {
1557                                 error("Couldn't write image: %s", str);
1558                         }
1559
1560                         allqueue(REDRAWHEADERS, 0);
1561                         allqueue(REDRAWBUTSSHADING, 0);
1562
1563                         waitcursor(0);
1564                 }
1565         }
1566 }
1567
1568 void open_image_sima(short imageselect)
1569 {
1570         char name[FILE_MAXDIR+FILE_MAXFILE];
1571
1572         if(G.sima->image)
1573                 strcpy(name, G.sima->image->name);
1574         else
1575                 strcpy(name, U.textudir);
1576
1577         if(imageselect)
1578                 activate_imageselect(FILE_SPECIAL, "Open Image", name, load_image_filesel);
1579         else
1580                 activate_fileselect(FILE_SPECIAL, "Open Image", name, load_image_filesel);
1581 }
1582
1583 void replace_image_sima(short imageselect)
1584 {
1585         char name[FILE_MAXDIR+FILE_MAXFILE];
1586
1587         if(G.sima->image)
1588                 strcpy(name, G.sima->image->name);
1589         else
1590                 strcpy(name, U.textudir);
1591         
1592         if(imageselect)
1593                 activate_imageselect(FILE_SPECIAL, "Replace Image", name, replace_image_filesel);
1594         else
1595                 activate_fileselect(FILE_SPECIAL, "Replace Image", name, replace_image_filesel);
1596 }
1597
1598
1599 static char *filesel_imagetype_string(Image *ima)
1600 {
1601         char *strp, *str= MEM_callocN(14*32, "menu for filesel");
1602         
1603         strp= str;
1604         str += sprintf(str, "Save Image as: %%t|");
1605         str += sprintf(str, "Targa %%x%d|", R_TARGA);
1606         str += sprintf(str, "Targa Raw %%x%d|", R_RAWTGA);
1607         str += sprintf(str, "PNG %%x%d|", R_PNG);
1608         str += sprintf(str, "BMP %%x%d|", R_BMP);
1609         str += sprintf(str, "Jpeg %%x%d|", R_JPEG90);
1610         str += sprintf(str, "Iris %%x%d|", R_IRIS);
1611         if(G.have_libtiff)
1612                 str += sprintf(str, "Tiff %%x%d|", R_TIFF);
1613         str += sprintf(str, "Radiance HDR %%x%d|", R_RADHDR);
1614         str += sprintf(str, "Cineon %%x%d|", R_CINEON);
1615         str += sprintf(str, "DPX %%x%d|", R_DPX);
1616 #ifdef WITH_OPENEXR
1617         str += sprintf(str, "OpenEXR %%x%d|", R_OPENEXR);
1618         /* saving sequences of multilayer won't work, they copy buffers  */
1619         if(ima->source==IMA_SRC_SEQUENCE && ima->type==IMA_TYPE_MULTILAYER);
1620         else str += sprintf(str, "MultiLayer %%x%d|", R_MULTILAYER);
1621 #endif  
1622         return strp;
1623 }
1624
1625 /* always opens fileselect */
1626 void save_as_image_sima(void)
1627 {
1628         Image *ima = G.sima->image;
1629         ImBuf *ibuf= BKE_image_get_ibuf(ima, &G.sima->iuser);
1630         char name[FILE_MAXDIR+FILE_MAXFILE];
1631
1632         if (ima) {
1633                 strcpy(name, ima->name);
1634
1635                 if (ibuf) {
1636                         char *strp;
1637                         
1638                         strp= filesel_imagetype_string(ima);
1639                         
1640                         /* cant save multilayer sequence, ima->rr isn't valid for a specific frame */
1641                         if(ima->rr && !(ima->source==IMA_SRC_SEQUENCE && ima->type==IMA_TYPE_MULTILAYER))
1642                                 G.sima->imtypenr= R_MULTILAYER;
1643                         else if(ima->type==IMA_TYPE_R_RESULT)
1644                                 G.sima->imtypenr= G.scene->r.imtype;
1645                         else G.sima->imtypenr= BKE_ftype_to_imtype(ibuf->ftype);
1646                         
1647                         activate_fileselect_menu(FILE_SPECIAL, "Save Image", name, strp, &G.sima->imtypenr, save_image_doit);
1648                 }
1649         }
1650 }
1651
1652 /* if exists, saves over without fileselect */
1653 void save_image_sima(void)
1654 {
1655         Image *ima = G.sima->image;
1656         ImBuf *ibuf= BKE_image_get_ibuf(ima, &G.sima->iuser);
1657         char name[FILE_MAXDIR+FILE_MAXFILE];
1658
1659         if (ima) {
1660                 strcpy(name, ima->name);
1661
1662                 if (ibuf) {
1663                         if (BLI_exists(ibuf->name)) {
1664                                 if(BKE_image_get_renderresult(ima)) 
1665                                         G.sima->imtypenr= R_MULTILAYER;
1666                                 else 
1667                                         G.sima->imtypenr= BKE_ftype_to_imtype(ibuf->ftype);
1668                                 
1669                                 save_image_doit(ibuf->name);
1670                         }
1671                         else
1672                                 save_as_image_sima();
1673                 }
1674         }
1675 }
1676
1677 void save_image_sequence_sima(void)
1678 {
1679         ImBuf *ibuf;
1680         int tot= 0;
1681         char di[FILE_MAX], fi[FILE_MAX];
1682         
1683         if(G.sima->image==NULL)
1684                 return;
1685         if(G.sima->image->source!=IMA_SRC_SEQUENCE)
1686                 return;
1687         if(G.sima->image->type==IMA_TYPE_MULTILAYER) {
1688                 error("Cannot save Multilayer Sequences");
1689                 return;
1690         }
1691         
1692         /* get total */
1693         for(ibuf= G.sima->image->ibufs.first; ibuf; ibuf= ibuf->next) 
1694                 if(ibuf->userflags & IB_BITMAPDIRTY)
1695                         tot++;
1696         
1697         if(tot==0) {
1698                 notice("No Images have been changed");
1699                 return;
1700         }
1701         /* get a filename for menu */
1702         for(ibuf= G.sima->image->ibufs.first; ibuf; ibuf= ibuf->next) 
1703                 if(ibuf->userflags & IB_BITMAPDIRTY)
1704                         break;
1705         
1706         BLI_strncpy(di, ibuf->name, FILE_MAX);
1707         BLI_splitdirstring(di, fi);
1708         
1709         sprintf(fi, "%d Image(s) will be saved in %s", tot, di);
1710         if(okee(fi)) {
1711                 
1712                 for(ibuf= G.sima->image->ibufs.first; ibuf; ibuf= ibuf->next) {
1713                         if(ibuf->userflags & IB_BITMAPDIRTY) {
1714                                 char name[FILE_MAX];
1715                                 BLI_strncpy(name, ibuf->name, sizeof(name));
1716                                 
1717                                 BLI_convertstringcode(name, G.sce, 0);
1718
1719                                 if(0 == IMB_saveiff(ibuf, name, IB_rect | IB_zbuf | IB_zbuffloat)) {
1720                                         error("Could not write image", name);
1721                                         break;
1722                                 }
1723                                 printf("Saved: %s\n", ibuf->name);
1724                                 ibuf->userflags &= ~IB_BITMAPDIRTY;
1725                         }
1726                 }
1727         }
1728 }
1729
1730 void reload_image_sima(void)
1731 {
1732
1733         if (G.sima ) {
1734                 BKE_image_signal(G.sima->image, &G.sima->iuser, IMA_SIGNAL_RELOAD);
1735                 image_changed(G.sima, 0);
1736         }
1737
1738         allqueue(REDRAWIMAGE, 0);
1739         allqueue(REDRAWVIEW3D, 0);
1740         BIF_preview_changed(ID_TE);
1741 }
1742
1743 void new_image_sima(void)
1744 {
1745         static int width= 256, height= 256;
1746         static short uvtestgrid= 0;
1747         static float color[] = {0, 0, 0, 1};
1748         char name[22];
1749
1750         strcpy(name, "Untitled");
1751
1752         add_numbut(0, TEX, "Name:", 0, 21, name, NULL);
1753         add_numbut(1, NUM|INT, "Width:", 1, 5000, &width, NULL);
1754         add_numbut(2, NUM|INT, "Height:", 1, 5000, &height, NULL);
1755         add_numbut(3, COL, "", 0, 0, &color, NULL);
1756         add_numbut(4, NUM|FLO, "Alpha:", 0.0, 1.0, &color[3], NULL);
1757         add_numbut(5, TOG|SHO, "UV Test Grid", 0, 0, &uvtestgrid, NULL);
1758         if (!do_clever_numbuts("New Image", 6, REDRAW))
1759                 return;
1760
1761         G.sima->image= BKE_add_image_size(width, height, name, uvtestgrid, color);
1762         BKE_image_signal(G.sima->image, &G.sima->iuser, IMA_SIGNAL_USER_NEW_IMAGE);
1763         image_changed(G.sima, 0);
1764
1765         BIF_undo_push("Add image");
1766
1767         allqueue(REDRAWIMAGE, 0);
1768         allqueue(REDRAWVIEW3D, 0);
1769 }
1770
1771 void pack_image_sima()
1772 {
1773         Image *ima = G.sima->image;
1774
1775         if (ima) {
1776                 if(ima->source!=IMA_SRC_SEQUENCE && ima->source!=IMA_SRC_MOVIE) {
1777                         if (ima->packedfile) {
1778                                 if (G.fileflags & G_AUTOPACK)
1779                                         if (okee("Disable AutoPack?"))
1780                                                 G.fileflags &= ~G_AUTOPACK;
1781                                 
1782                                 if ((G.fileflags & G_AUTOPACK) == 0) {
1783                                         unpackImage(ima, PF_ASK);
1784                                         BIF_undo_push("Unpack image");
1785                                 }
1786                         }
1787                         else {
1788                                 ImBuf *ibuf= BKE_image_get_ibuf(ima, &G.sima->iuser);
1789                                 if (ibuf && (ibuf->userflags & IB_BITMAPDIRTY)) {
1790                                         if(okee("Can't pack painted image. Use Repack as PNG?"))
1791                                                 BKE_image_memorypack(ima);
1792                                 }
1793                                 else {
1794                                         ima->packedfile = newPackedFile(ima->name);
1795                                         BIF_undo_push("Pack image");
1796                                 }
1797                         }
1798
1799                         allqueue(REDRAWBUTSSHADING, 0);
1800                         allqueue(REDRAWHEADERS, 0);
1801                 }
1802         }
1803 }
1804
1805
1806
1807 /* goes over all ImageUsers, and sets frame numbers if auto-refresh is set */
1808 void BIF_image_update_frame(void)
1809 {
1810         Tex *tex;
1811         
1812         /* texture users */
1813         for(tex= G.main->tex.first; tex; tex= tex->id.next) {
1814                 if(tex->type==TEX_IMAGE && tex->ima)
1815                         if(ELEM(tex->ima->source, IMA_SRC_MOVIE, IMA_SRC_SEQUENCE))
1816                                 if(tex->iuser.flag & IMA_ANIM_ALWAYS)
1817                                         BKE_image_user_calc_imanr(&tex->iuser, G.scene->r.cfra, 0);
1818                 
1819         }
1820         /* image window, compo node users */
1821         if(G.curscreen) {
1822                 ScrArea *sa;
1823                 for(sa= G.curscreen->areabase.first; sa; sa= sa->next) {
1824                         if(sa->spacetype==SPACE_VIEW3D) {
1825                                 View3D *v3d= sa->spacedata.first;
1826                                 if(v3d->bgpic)
1827                                         if(v3d->bgpic->iuser.flag & IMA_ANIM_ALWAYS)
1828                                                 BKE_image_user_calc_imanr(&v3d->bgpic->iuser, G.scene->r.cfra, 0);
1829                         }
1830                         else if(sa->spacetype==SPACE_IMAGE) {
1831                                 SpaceImage *sima= sa->spacedata.first;
1832                                 if(sima->iuser.flag & IMA_ANIM_ALWAYS)
1833                                         BKE_image_user_calc_imanr(&sima->iuser, G.scene->r.cfra, 0);
1834                         }
1835                         else if(sa->spacetype==SPACE_NODE) {
1836                                 SpaceNode *snode= sa->spacedata.first;
1837                                 if((snode->treetype==NTREE_COMPOSIT) && (snode->nodetree)) {
1838                                         bNode *node;
1839                                         for(node= snode->nodetree->nodes.first; node; node= node->next) {
1840                                                 if(node->id && node->type==CMP_NODE_IMAGE) {
1841                                                         Image *ima= (Image *)node->id;
1842                                                         ImageUser *iuser= node->storage;
1843                                                         if(ELEM(ima->source, IMA_SRC_MOVIE, IMA_SRC_SEQUENCE))
1844                                                                 if(iuser->flag & IMA_ANIM_ALWAYS)
1845                                                                         BKE_image_user_calc_imanr(iuser, G.scene->r.cfra, 0);
1846                                                 }
1847                                         }
1848                                 }
1849                         }
1850                 }
1851         }
1852 }
1853