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