UV window, without an image, now draws a nice zoom-resistant grid.
[blender.git] / source / blender / src / drawimage.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 <math.h>
34 #include <string.h>
35
36 #ifdef HAVE_CONFIG_H
37 #include <config.h>
38 #endif
39
40 #ifdef WIN32
41 #include <io.h>
42 #else
43 #include <unistd.h>
44 #endif   
45 #include "MEM_guardedalloc.h"
46
47 #include "BLI_arithb.h"
48 #include "BLI_blenlib.h"
49
50 #include "IMB_imbuf_types.h"
51
52 #include "DNA_image_types.h"
53 #include "DNA_mesh_types.h"
54 #include "DNA_meshdata_types.h"
55 #include "DNA_packedFile_types.h"
56 #include "DNA_scene_types.h"
57 #include "DNA_screen_types.h"
58 #include "DNA_space_types.h"
59 #include "DNA_userdef_types.h"
60
61 #include "BKE_utildefines.h"
62 #include "BKE_global.h"
63 #include "BKE_main.h"
64 #include "BKE_mesh.h"
65 #include "BKE_image.h"
66 #include "BKE_DerivedMesh.h"
67 #include "BKE_displist.h"
68 #include "BKE_object.h"
69
70 #include "BDR_editface.h"
71 #include "BDR_drawobject.h"
72 #include "BDR_drawmesh.h"
73 #include "BDR_imagepaint.h"
74
75 #include "BIF_gl.h"
76 #include "BIF_mywindow.h"
77 #include "BIF_drawimage.h"
78 #include "BIF_resources.h"
79 #include "BIF_interface.h"
80 #include "BIF_editsima.h"
81 #include "BIF_glutil.h"
82 #include "BIF_space.h"
83 #include "BIF_screen.h"
84 #include "BIF_transform.h"
85
86 #include "BSE_headerbuttons.h"
87 #include "BSE_trans_types.h"
88 #include "BSE_view.h"
89
90 /* Modules used */
91 #include "mydevice.h"
92 #include "blendef.h"
93 #include "butspace.h"  // event codes
94
95 static unsigned char *alloc_alpha_clone_image(int *width, int *height)
96 {
97         unsigned int size, alpha;
98         unsigned char *rect, *cp;
99
100         if(!Gip.clone.image)
101                 return NULL;
102
103         if(!Gip.clone.image->ibuf)
104                 load_image(Gip.clone.image, IB_rect, G.sce, G.scene->r.cfra);
105
106         if(!Gip.clone.image->ibuf || !Gip.clone.image->ibuf->rect)
107                 return NULL;
108
109         rect= MEM_dupallocN(Gip.clone.image->ibuf->rect);
110
111         if(!rect)
112                 return NULL;
113
114         *width= Gip.clone.image->ibuf->x;
115         *height= Gip.clone.image->ibuf->y;
116
117         size= (*width)*(*height);
118         alpha= (unsigned char)255*Gip.clone.alpha;
119         cp= rect;
120
121         while(size-- > 0) {
122                 cp[3]= alpha;
123                 cp += 4;
124         }
125
126         return rect;
127 }
128
129 static void setcloneimage()
130 {
131         if(G.sima->menunr > 0) {
132                 Image *ima= (Image*)BLI_findlink(&G.main->image, G.sima->menunr-1);
133
134                 if(ima) {
135                         Gip.clone.image= ima;
136                         Gip.clone.offset[0]= Gip.clone.offset[0]= 0.0;
137                 }
138         }
139 }
140
141 /**
142  * Sets up the fields of the View2D member of the SpaceImage struct
143  * This routine can be called in two modes:
144  * mode == 'f': float mode (0.0 - 1.0)
145  * mode == 'p': pixel mode (0 - size)
146  *
147  * @param     sima  the image space to update
148  * @param     mode  the mode to use for the update
149  * @return    void
150  *   
151  */
152 void calc_image_view(SpaceImage *sima, char mode)
153 {
154         float xim=256, yim=256;
155         float x1, y1;
156         float zoom;
157         
158         if(sima->image && sima->image->ibuf) {
159                 xim= sima->image->ibuf->x;
160                 yim= sima->image->ibuf->y;
161         }
162         
163         sima->v2d.tot.xmin= 0;
164         sima->v2d.tot.ymin= 0;
165         sima->v2d.tot.xmax= xim;
166         sima->v2d.tot.ymax= yim;
167         
168         sima->v2d.mask.xmin= sima->v2d.mask.ymin= 0;
169         sima->v2d.mask.xmax= curarea->winx;
170         sima->v2d.mask.ymax= curarea->winy;
171
172
173         /* Which part of the image space do we see? */
174         /* Same calculation as in lrectwrite: area left and down*/
175         x1= curarea->winrct.xmin+(curarea->winx-sima->zoom*xim)/2;
176         y1= curarea->winrct.ymin+(curarea->winy-sima->zoom*yim)/2;
177
178         x1-= sima->zoom*sima->xof;
179         y1-= sima->zoom*sima->yof;
180
181         /* float! */
182         zoom= sima->zoom;
183         
184         /* relative display right */
185         sima->v2d.cur.xmin= ((curarea->winrct.xmin - (float)x1)/zoom);
186         sima->v2d.cur.xmax= sima->v2d.cur.xmin + ((float)curarea->winx/zoom);
187         
188         /* relative display left */
189         sima->v2d.cur.ymin= ((curarea->winrct.ymin-(float)y1)/zoom);
190         sima->v2d.cur.ymax= sima->v2d.cur.ymin + ((float)curarea->winy/zoom);
191         
192         if(mode=='f') {         
193                 sima->v2d.cur.xmin/= xim;
194                 sima->v2d.cur.xmax/= xim;
195                 sima->v2d.cur.ymin/= yim;
196                 sima->v2d.cur.ymax/= yim;
197         }
198 }
199
200 void what_image(SpaceImage *sima)
201 {
202         extern TFace *lasttface;        /* editface.c */
203         Mesh *me;
204                 
205         if(sima->mode==SI_TEXTURE) {
206                 if(G.f & G_FACESELECT) {
207
208                         sima->image= 0;
209                         me= get_mesh((G.scene->basact) ? (G.scene->basact->object) : 0);
210                         set_lasttface();
211                         
212                         if(me && me->tface && lasttface && lasttface->mode & TF_TEX) {
213                                 sima->image= lasttface->tpage;
214                                         
215                                 if(sima->flag & SI_EDITTILE);
216                                 else sima->curtile= lasttface->tile;
217                                 
218                                 if(sima->image) {
219                                         if(lasttface->mode & TF_TILES)
220                                                 sima->image->tpageflag |= IMA_TILES;
221                                         else sima->image->tpageflag &= ~IMA_TILES;
222                                 }
223                         }
224                 }
225         }
226 }
227
228 void image_changed(SpaceImage *sima, int dotile)
229 {
230         TFace *tface;
231         Mesh *me;
232         int a;
233         
234         if(sima->mode==SI_TEXTURE) {
235                 
236                 if(G.f & G_FACESELECT) {
237                         me= get_mesh(OBACT);
238                         if(me && me->tface) {
239                                 tface= me->tface;
240                                 a= me->totface;
241                                 while(a--) {
242                                         if(tface->flag & TF_SELECT) {
243                                                 
244                                                 if(dotile==2) {
245                                                         tface->mode &= ~TF_TILES;
246                                                 }
247                                                 else {
248                                                         tface->tpage= sima->image;
249                                                         tface->mode |= TF_TEX;
250                                                 
251                                                         if(dotile) tface->tile= sima->curtile;
252                                                 }
253                                                 
254                                                 if(sima->image) {
255                                                         if(sima->image->tpageflag & IMA_TILES) tface->mode |= TF_TILES;
256                                                         else tface->mode &= ~TF_TILES;
257                                                 
258                                                         if(sima->image->id.us==0) sima->image->id.us= 1;
259                                                 }
260                                         }
261                                         tface++;
262                                 }
263
264                                 object_uvs_changed(OBACT);
265                                 allqueue(REDRAWBUTSEDIT, 0);
266                         }
267                 }
268         }
269 }
270
271
272 void uvco_to_areaco(float *vec, short *mval)
273 {
274         float x, y;
275
276         mval[0]= IS_CLIPPED;
277         
278         x= (vec[0] - G.v2d->cur.xmin)/(G.v2d->cur.xmax-G.v2d->cur.xmin);
279         y= (vec[1] - G.v2d->cur.ymin)/(G.v2d->cur.ymax-G.v2d->cur.ymin);
280
281         if(x>=0.0 && x<=1.0) {
282                 if(y>=0.0 && y<=1.0) {          
283                         mval[0]= G.v2d->mask.xmin + x*(G.v2d->mask.xmax-G.v2d->mask.xmin);
284                         mval[1]= G.v2d->mask.ymin + y*(G.v2d->mask.ymax-G.v2d->mask.ymin);
285                 }
286         }
287 }
288
289 void uvco_to_areaco_noclip(float *vec, int *mval)
290 {
291         float x, y;
292
293         mval[0]= IS_CLIPPED;
294         
295         x= (vec[0] - G.v2d->cur.xmin)/(G.v2d->cur.xmax-G.v2d->cur.xmin);
296         y= (vec[1] - G.v2d->cur.ymin)/(G.v2d->cur.ymax-G.v2d->cur.ymin);
297
298         x= G.v2d->mask.xmin + x*(G.v2d->mask.xmax-G.v2d->mask.xmin);
299         y= G.v2d->mask.ymin + y*(G.v2d->mask.ymax-G.v2d->mask.ymin);
300         
301         mval[0]= x;
302         mval[1]= y;
303 }
304
305 void draw_tfaces(void)
306 {
307         TFace *tface,*activetface = NULL;
308         MFace *mface,*activemface = NULL;
309         Mesh *me;
310         int a;
311         char col1[4], col2[4];
312         float pointsize= BIF_GetThemeValuef(TH_VERTEX_SIZE);
313         
314         if(G.f & G_FACESELECT) {
315                 me= get_mesh((G.scene->basact) ? (G.scene->basact->object) : 0);
316                 if(me && me->tface) {
317                         calc_image_view(G.sima, 'f');   /* float */
318                         myortho2(G.v2d->cur.xmin, G.v2d->cur.xmax, G.v2d->cur.ymin, G.v2d->cur.ymax);
319                         glLoadIdentity();
320                         
321                         /* draw shadow mesh */
322                         if((G.sima->flag & SI_DRAWSHADOW) && !(G.obedit==OBACT)){
323                                 int dmNeedsFree;
324                                 DerivedMesh *dm = mesh_get_derived_final(OBACT, &dmNeedsFree);
325
326                                 glColor3ub(112, 112, 112);
327                                 if (dm->drawUVEdges) dm->drawUVEdges(dm);
328
329                                 if (dmNeedsFree) dm->release(dm);
330                         }
331                         else if(G.sima->flag & SI_DRAWSHADOW){          
332                                 tface= me->tface;
333                                 mface= me->mface;
334                                 a= me->totface;                 
335                                 while(a--) {
336                                         if(!(tface->flag & TF_HIDE)) {
337                                                 glColor3ub(112, 112, 112);
338                                                 glBegin(GL_LINE_LOOP);
339                                                 glVertex2fv(tface->uv[0]);
340                                                 glVertex2fv(tface->uv[1]);
341                                                 glVertex2fv(tface->uv[2]);
342                                                 if(mface->v4) glVertex2fv(tface->uv[3]);
343                                                 glEnd();
344                                         } 
345                                         tface++;
346                                         mface++;                                        
347                                 }
348                         }
349                         
350                         /* draw transparent faces */
351                         if(G.f & G_DRAWFACES) {
352                                 BIF_GetThemeColor4ubv(TH_FACE, col1);
353                                 BIF_GetThemeColor4ubv(TH_FACE_SELECT, col2);
354                                 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
355                                 glEnable(GL_BLEND);
356                                 tface= me->tface;
357                                 mface= me->mface;
358                                 a= me->totface;                 
359                                 while(a--) {
360                                         if(tface->flag & TF_SELECT) {
361                                                 if(!(~tface->flag & (TF_SEL1|TF_SEL2|TF_SEL3)) &&
362                                                    (!mface->v4 || tface->flag & TF_SEL4))
363                                                         glColor4ubv(col2);
364                                                 else
365                                                         glColor4ubv(col1);
366                                                         
367                                                 glBegin(mface->v4?GL_QUADS:GL_TRIANGLES);
368                                                         glVertex2fv(tface->uv[0]);
369                                                         glVertex2fv(tface->uv[1]);
370                                                         glVertex2fv(tface->uv[2]);
371                                                         if(mface->v4) glVertex2fv(tface->uv[3]);
372                                                 glEnd();
373                                         }
374                                         tface++;
375                                         mface++;                                        
376                                 }
377                                 glDisable(GL_BLEND);
378                         }
379
380
381                         tface= me->tface;
382                         mface= me->mface;
383                         a= me->totface;
384                         while(a--) {
385                                 if(tface->flag & TF_SELECT) {
386                                         if(tface->flag & TF_ACTIVE){
387                                                 activetface= tface; 
388                                                 activemface= mface; 
389                                         }
390
391                                         cpack(0x0);
392                                         glBegin(GL_LINE_LOOP);
393                                                 glVertex2fv(tface->uv[0]);
394                                                 glVertex2fv(tface->uv[1]);
395                                                 glVertex2fv(tface->uv[2]);
396                                                 if(mface->v4) glVertex2fv(tface->uv[3]);
397                                         glEnd();
398                                 
399                                         setlinestyle(2);
400                                         cpack(0xFFFFFF);
401                                         glBegin(GL_LINE_STRIP);
402                                                 glVertex2fv(tface->uv[0]);
403                                                 glVertex2fv(tface->uv[1]);
404                                         glEnd();
405
406                                         glBegin(GL_LINE_STRIP);
407                                                 glVertex2fv(tface->uv[0]);
408                                                 if(mface->v4) glVertex2fv(tface->uv[3]);
409                                                 else glVertex2fv(tface->uv[2]);
410                                         glEnd();
411         
412                                         glBegin(GL_LINE_STRIP);
413                                                 glVertex2fv(tface->uv[1]);
414                                                 glVertex2fv(tface->uv[2]);
415                                                 if(mface->v4) glVertex2fv(tface->uv[3]);
416                                         glEnd();
417                                         setlinestyle(0);
418                                 }
419                                         
420                                 tface++;
421                                 mface++;
422                         }
423
424                         /* draw active face edges */
425                         if (activetface){
426                                 /* colors: R=u G=v */
427
428                                 setlinestyle(2);
429                                 tface=activetface; 
430                                 mface=activemface; 
431
432                                 cpack(0x0);
433                                 glBegin(GL_LINE_LOOP);
434                                 glVertex2fv(tface->uv[0]);
435                                         glVertex2fv(tface->uv[1]);
436                                         glVertex2fv(tface->uv[2]);
437                                         if(mface->v4) glVertex2fv(tface->uv[3]);
438                                 glEnd();
439                                         
440                                 cpack(0xFF00);
441                                 glBegin(GL_LINE_STRIP);
442                                         glVertex2fv(tface->uv[0]);
443                                         glVertex2fv(tface->uv[1]);
444                                 glEnd();
445
446                                 cpack(0xFF);
447                                 glBegin(GL_LINE_STRIP);
448                                         glVertex2fv(tface->uv[0]);
449                                         if(mface->v4) glVertex2fv(tface->uv[3]);
450                                         else glVertex2fv(tface->uv[2]);
451                                 glEnd();
452
453                                 cpack(0xFFFFFF);
454                                 glBegin(GL_LINE_STRIP);
455                                         glVertex2fv(tface->uv[1]);
456                                         glVertex2fv(tface->uv[2]);
457                                         if(mface->v4) glVertex2fv(tface->uv[3]);
458                                 glEnd();
459                                 
460                                 setlinestyle(0);
461                         }
462
463             /* unselected uv's */
464                         BIF_ThemeColor(TH_VERTEX);
465                         glPointSize(pointsize);
466
467                         bglBegin(GL_POINTS);
468                         tface= me->tface;
469                         mface= me->mface;
470                         a= me->totface;
471                         while(a--) {
472                                 if(tface->flag & TF_SELECT) {
473                                         
474                                         if(tface->flag & TF_SEL1); else bglVertex2fv(tface->uv[0]);
475                                         if(tface->flag & TF_SEL2); else bglVertex2fv(tface->uv[1]);
476                                         if(tface->flag & TF_SEL3); else bglVertex2fv(tface->uv[2]);
477                                         if(mface->v4) {
478                                                 if(tface->flag & TF_SEL4); else bglVertex2fv(tface->uv[3]);
479                                         }
480                                 }
481                                 tface++;
482                                 mface++;
483                         }
484                         bglEnd();
485
486                         /* pinned uv's */
487                         /* give odd pointsizes odd pin pointsizes */
488                 glPointSize(pointsize*2 + (((int)pointsize % 2)? (-1): 0));
489                         cpack(0xFF);
490
491                         bglBegin(GL_POINTS);
492                         tface= me->tface;
493                         mface= me->mface;
494                         a= me->totface;
495                         while(a--) {
496                                 if(tface->flag & TF_SELECT) {
497                                         
498                                         if(tface->unwrap & TF_PIN1) bglVertex2fv(tface->uv[0]);
499                                         if(tface->unwrap & TF_PIN2) bglVertex2fv(tface->uv[1]);
500                                         if(tface->unwrap & TF_PIN3) bglVertex2fv(tface->uv[2]);
501                                         if(mface->v4) {
502                                                 if(tface->unwrap & TF_PIN4) bglVertex2fv(tface->uv[3]);
503                                         }
504                                 }
505                                 tface++;
506                                 mface++;
507                         }
508                         bglEnd();
509
510                         /* selected uv's */
511                         BIF_ThemeColor(TH_VERTEX_SELECT);
512                 glPointSize(pointsize);
513
514                         bglBegin(GL_POINTS);
515                         tface= me->tface;
516                         mface= me->mface;
517                         a= me->totface;
518                         while(a--) {
519                                 if(tface->flag & TF_SELECT) {
520                                         
521                                         if(tface->flag & TF_SEL1) bglVertex2fv(tface->uv[0]);
522                                         if(tface->flag & TF_SEL2) bglVertex2fv(tface->uv[1]);
523                                         if(tface->flag & TF_SEL3) bglVertex2fv(tface->uv[2]);
524                                         if(mface->v4) {
525                                                 if(tface->flag & TF_SEL4) bglVertex2fv(tface->uv[3]);
526                                         }
527                                 }
528                                 tface++;
529                                 mface++;
530                         }
531                         bglEnd();
532                 }
533         }
534         glPointSize(1.0);
535 }
536
537 static unsigned int *get_part_from_ibuf(ImBuf *ibuf, short startx, short starty, short endx, short endy)
538 {
539         unsigned int *rt, *rp, *rectmain;
540         short y, heigth, len;
541
542         /* the right offset in rectot */
543
544         rt= ibuf->rect+ (starty*ibuf->x+ startx);
545
546         len= (endx-startx);
547         heigth= (endy-starty);
548
549         rp=rectmain= MEM_mallocN(heigth*len*sizeof(int), "rect");
550         
551         for(y=0; y<heigth; y++) {
552                 memcpy(rp, rt, len*4);
553                 rt+= ibuf->x;
554                 rp+= len;
555         }
556         return rectmain;
557 }
558
559 static void draw_image_transform(ImBuf *ibuf)
560 {
561         if(G.moving) {
562                 float aspx, aspy, center[3];
563
564         BIF_drawConstraint();
565
566                 if(ibuf==0 || ibuf->rect==0 || ibuf->x==0 || ibuf->y==0) {
567                         aspx= aspy= 1.0;
568                 }
569                 else {
570                         aspx= 256.0/ibuf->x;
571                         aspy= 256.0/ibuf->y;
572                 }
573
574                 BIF_getPropCenter(center);
575
576                 /* scale and translate the circle into place and draw it */
577                 glPushMatrix();
578                 glScalef(aspx, aspy, 1.0);
579                 glTranslatef((1/aspx)*center[0] - center[0],
580                              (1/aspy)*center[1] - center[1], 0.0);
581
582                 BIF_drawPropCircle();
583
584                 glPopMatrix();
585         }
586 }
587
588 static void draw_image_view_icon(void)
589 {
590         float xPos = 5.0;
591
592         glEnable(GL_BLEND);
593         glBlendFunc(GL_SRC_ALPHA,  GL_ONE_MINUS_SRC_ALPHA); 
594         
595         if(G.sima->flag & SI_STICKYUVS) {
596                 BIF_draw_icon(xPos, 5.0, ICON_STICKY2_UVS);
597                 xPos = 25.0;
598         }
599         else if(G.sima->flag & SI_LOCALSTICKY) {
600                 BIF_draw_icon(xPos, 5.0, ICON_STICKY_UVS);
601                 xPos = 25.0;
602         }
603
604         if(G.sima->flag & SI_SELACTFACE) {
605                 BIF_draw_icon(xPos, 5.0, ICON_DRAW_UVFACES);
606         }
607         
608         glBlendFunc(GL_ONE,  GL_ZERO); 
609         glDisable(GL_BLEND);
610 }
611
612 static void draw_image_view_tool(void)
613 {
614         ImagePaintTool *tool = &Gip.tool[Gip.current];
615         short mval[2];
616         float radius;
617         int draw= 0;
618
619         if(Gip.flag & IMAGEPAINT_DRAWING) {
620                 if(Gip.flag & IMAGEPAINT_DRAW_TOOL_DRAWING)
621                         draw= 1;
622         }
623         else if(Gip.flag & IMAGEPAINT_DRAW_TOOL)
624                 draw= 1;
625         
626         if(draw) {
627                 getmouseco_areawin(mval);
628
629                 radius= tool->size*G.sima->zoom/2;
630                 fdrawXORcirc(mval[0], mval[1], radius);
631
632                 if (tool->innerradius != 1.0) {
633                         radius *= tool->innerradius;
634                         fdrawXORcirc(mval[0], mval[1], radius);
635                 }
636         }
637 }
638
639 /* ************ panel stuff ************* */
640
641 // button define is local, only events defined here possible
642 #define B_TRANS_IMAGE   1
643
644 /* is used for both read and write... */
645 static void image_editvertex_buts(uiBlock *block)
646 {
647         static float ocent[2];
648         float cent[2]= {0.0, 0.0};
649         int imx, imy;
650         int i, nactive= 0, step, digits;
651         Mesh *me;
652         
653         if( is_uv_tface_editing_allowed_silent()==0 ) return;
654         me= get_mesh(OBACT);
655         
656         if (G.sima->image && G.sima->image->ibuf) {
657                 imx= G.sima->image->ibuf->x;
658                 imy= G.sima->image->ibuf->y;
659         } else
660                 imx= imy= 256;
661         
662         for (i=0; i<me->totface; i++) {
663                 MFace *mf= &((MFace*) me->mface)[i];
664                 TFace *tf= &((TFace*) me->tface)[i];
665                 
666                 if (!(tf->flag & TF_SELECT))
667                         continue;
668                 
669                 if (tf->flag & TF_SEL1) {
670                         cent[0]+= tf->uv[0][0];
671                         cent[1]+= tf->uv[0][1];
672                         nactive++;
673                 }
674                 if (tf->flag & TF_SEL2) {
675                         cent[0]+= tf->uv[1][0];
676                         cent[1]+= tf->uv[1][1];
677                         nactive++;
678                 }
679                 if (tf->flag & TF_SEL3) {
680                         cent[0]+= tf->uv[2][0];
681                         cent[1]+= tf->uv[2][1];
682                         nactive++;
683                 }
684                 if (mf->v4 && (tf->flag & TF_SEL4)) {
685                         cent[0]+= tf->uv[3][0];
686                         cent[1]+= tf->uv[3][1];
687                         nactive++;
688                 }
689         }
690                 
691         if(block) {     // do the buttons
692                 if (nactive) {
693                         ocent[0]= cent[0]/nactive;
694                         ocent[1]= cent[1]/nactive;
695                         if (G.sima->flag & SI_COORDFLOATS) {
696                                 step= 1;
697                                 digits= 3;
698                         }
699                         else {
700                                 ocent[0] *= imx;
701                                 ocent[1] *= imy;
702                                 step= 100;
703                                 digits= 2;
704                         }
705                         
706                         uiDefBut(block, LABEL, 0, "UV Vertex:",10,55,302,19,0,0,0,0,0,"");
707                         uiBlockBeginAlign(block);
708                         if(nactive==1) {
709                                 uiDefButF(block, NUM, B_TRANS_IMAGE, "Vertex X:",       10, 35, 290, 19, &ocent[0], -10*imx, 10.0*imx, step, digits, "");
710                                 uiDefButF(block, NUM, B_TRANS_IMAGE, "Vertex Y:",       10, 15, 290, 19, &ocent[1], -10*imy, 10.0*imy, step, digits, "");
711                         }
712                         else {
713                                 uiDefButF(block, NUM, B_TRANS_IMAGE, "Median X:",       10, 35, 290, 19, &ocent[0], -10*imx, 10.0*imx, step, digits, "");
714                                 uiDefButF(block, NUM, B_TRANS_IMAGE, "Median Y:",       10, 15, 290, 19, &ocent[1], -10*imy, 10.0*imy, step, digits, "");
715                         }
716                         uiBlockEndAlign(block);
717                 }
718         }
719         else {  // apply event
720                 float delta[2];
721                 
722                 cent[0]= cent[0]/nactive;
723                 cent[1]= cent[1]/nactive;
724                         
725                 if (G.sima->flag & SI_COORDFLOATS) {
726                         delta[0]= ocent[0]-cent[0];
727                         delta[1]= ocent[1]-cent[1];
728                 }
729                 else {
730                         delta[0]= ocent[0]/imx - cent[0];
731                         delta[1]= ocent[1]/imy - cent[1];
732                 }
733
734                 for (i=0; i<me->totface; i++) {
735                         MFace *mf= &((MFace*) me->mface)[i];
736                         TFace *tf= &((TFace*) me->tface)[i];
737                 
738                         if (!(tf->flag & TF_SELECT))
739                                 continue;
740                 
741                         if (tf->flag & TF_SEL1) {
742                                 tf->uv[0][0]+= delta[0];
743                                 tf->uv[0][1]+= delta[1];
744                         }
745                         if (tf->flag & TF_SEL2) {
746                                 tf->uv[1][0]+= delta[0];
747                                 tf->uv[1][1]+= delta[1];
748                         }
749                         if (tf->flag & TF_SEL3) {
750                                 tf->uv[2][0]+= delta[0];
751                                 tf->uv[2][1]+= delta[1];
752                         }
753                         if (mf->v4 && (tf->flag & TF_SEL4)) {
754                                 tf->uv[3][0]+= delta[0];
755                                 tf->uv[3][1]+= delta[1];
756                         }
757                 }
758                         
759                 allqueue(REDRAWVIEW3D, 0);
760                 allqueue(REDRAWIMAGE, 0);
761         }
762 }
763
764
765 void do_imagebuts(unsigned short event)
766 {
767         switch(event) {
768         case B_TRANS_IMAGE:
769                 image_editvertex_buts(NULL);
770                 break;
771
772         case B_SIMAGEDRAW:
773                 if(G.f & G_FACESELECT) {
774                         make_repbind(G.sima->image);
775                         image_changed(G.sima, 1);
776                 }
777                 allqueue(REDRAWVIEW3D, 0);
778                 allqueue(REDRAWIMAGE, 0);
779                 break;
780
781         case B_SIMAGEDRAW1:
782                 image_changed(G.sima, 2);               /* 2: only tileflag */
783                 allqueue(REDRAWVIEW3D, 0);
784                 allqueue(REDRAWIMAGE, 0);
785                 break;
786                 
787         case B_TWINANIM:
788                 {
789                         Image *ima;
790                         int nr;
791
792                         ima = G.sima->image;
793                         if (ima) {
794                                 if(ima->flag & IMA_TWINANIM) {
795                                         nr= ima->xrep*ima->yrep;
796                                         if(ima->twsta>=nr) ima->twsta= 1;
797                                         if(ima->twend>=nr) ima->twend= nr-1;
798                                         if(ima->twsta>ima->twend) ima->twsta= 1;
799                                         allqueue(REDRAWIMAGE, 0);
800                                 }
801                         }
802                 }
803                 break;
804
805         case B_SIMACLONEBROWSE:
806                 setcloneimage();
807                 allqueue(REDRAWIMAGE, 0);
808                 break;
809                 
810         case B_SIMACLONEDELETE:
811                 Gip.clone.image= NULL;
812                 allqueue(REDRAWIMAGE, 0);
813                 break;
814
815         case B_SIMABRUSHCHANGE:
816                 allqueue(REDRAWIMAGE, 0);
817                 break;
818         }
819 }
820
821 static void image_panel_properties(short cntrl) // IMAGE_HANDLER_PROPERTIES
822 {
823         uiBlock *block;
824
825         block= uiNewBlock(&curarea->uiblocks, "image_panel_properties", UI_EMBOSS, UI_HELV, curarea->win);
826         uiPanelControl(UI_PNL_SOLID | UI_PNL_CLOSE | cntrl);
827         uiSetPanelHandler(IMAGE_HANDLER_PROPERTIES);  // for close and esc
828         if(uiNewPanel(curarea, block, "Properties", "Image", 10, 230, 318, 204)==0)
829                 return;
830
831         if (G.sima->image && G.sima->image->ibuf) {
832                 char str[32];
833
834                 sprintf(str, "Image: size %d x %d", G.sima->image->ibuf->x, G.sima->image->ibuf->y);
835                 uiDefBut(block, LABEL, B_NOP, str,              10,180,300,19, 0, 0, 0, 0, 0, "");
836
837                 uiBlockBeginAlign(block);
838                 uiDefButBitS(block, TOG, IMA_TWINANIM, B_TWINANIM, "Anim", 10,150,140,19, &G.sima->image->tpageflag, 0, 0, 0, 0, "Toggles use of animated texture");
839                 uiDefButS(block, NUM, B_TWINANIM, "Start:",             10,130,140,19, &G.sima->image->twsta, 0.0, 128.0, 0, 0, "Displays the start frame of an animated texture");
840                 uiDefButS(block, NUM, B_TWINANIM, "End:",               10,110,140,19, &G.sima->image->twend, 0.0, 128.0, 0, 0, "Displays the end frame of an animated texture");
841                 uiDefButS(block, NUM, B_NOP, "Speed",                           10,90,140,19, &G.sima->image->animspeed, 1.0, 100.0, 0, 0, "Displays Speed of the animation in frames per second");
842                 uiBlockEndAlign(block);
843
844                 uiBlockBeginAlign(block);
845                 uiDefButBitS(block, TOG, IMA_TILES, B_SIMAGEDRAW1, "Tiles",     160,150,140,19, &G.sima->image->tpageflag, 0, 0, 0, 0, "Toggles use of tilemode for faces");
846                 uiDefButS(block, NUM, B_SIMAGEDRAW, "X:",               160,130,70,19, &G.sima->image->xrep, 1.0, 16.0, 0, 0, "Sets the degree of repetition in the X direction");
847                 uiDefButS(block, NUM, B_SIMAGEDRAW, "Y:",               230,130,70,19, &G.sima->image->yrep, 1.0, 16.0, 0, 0, "Sets the degree of repetition in the Y direction");
848                 uiBlockBeginAlign(block);
849         }
850
851         image_editvertex_buts(block);
852 }
853
854 static void image_panel_paint(short cntrl)      // IMAGE_HANDLER_PROPERTIES
855 {
856         ImagePaintTool *tool= &Gip.tool[Gip.current];
857         uiBlock *block;
858         ID *id;
859
860         block= uiNewBlock(&curarea->uiblocks, "image_panel_paint", UI_EMBOSS, UI_HELV, curarea->win);
861         uiPanelControl(UI_PNL_SOLID | UI_PNL_CLOSE | cntrl);
862         uiSetPanelHandler(IMAGE_HANDLER_PAINT);  // for close and esc
863         if(uiNewPanel(curarea, block, "Image Paint", "Image", 10, 230, 318, 204)==0)
864                 return;
865
866         uiBlockBeginAlign(block);
867         uiDefButF(block, COL, B_VPCOLSLI, "",           979,160,230,19, tool->rgba, 0, 0, 0, 0, "");
868         uiDefButF(block, NUMSLI, 0, "Opacity ",         979,140,230,19, tool->rgba+3, 0.0, 1.0, 0, 0, "The amount of pressure on the brush");
869         uiDefButI(block, NUMSLI, 0, "Size ",            979,120,230,19, &tool->size, 2, 64, 0, 0, "The size of the brush");
870         uiDefButF(block, NUMSLI, 0, "Fall ",            979,100,230,19, &tool->innerradius, 0.0, 1.0, 0, 0, "The fall off radius of the brush");
871
872         if(Gip.current == IMAGEPAINT_BRUSH || Gip.current == IMAGEPAINT_SMEAR)
873                 uiDefButF(block, NUMSLI, 0, "Stepsize ",979,80,230,19, &tool->timing, 1.0, 100.0, 0, 0, "Repeating Paint On %of Brush diameter");
874         else
875                 uiDefButF(block, NUMSLI, 0, "Flow ",    979,80,230,19, &tool->timing, 1.0, 100.0, 0, 0, "Paint Flow for Air Brush");
876         uiBlockEndAlign(block);
877
878         uiBlockBeginAlign(block);
879         /* TODO: FLOATPANELMESSAGEEATER catching LMB on the panel buttons */
880         /* so LMB does not "GO" through the floating panel */
881         uiDefButS(block, ROW, B_SIMABRUSHCHANGE, "Brush",               890,160,80,19, &Gip.current, 7.0, IMAGEPAINT_BRUSH, 0, 0, "Brush");
882         uiDefButS(block, ROW, B_SIMABRUSHCHANGE, "AirBrush",            890,140,80,19, &Gip.current, 7.0, IMAGEPAINT_AIRBRUSH, 0, 0, "AirBrush");
883         uiDefButS(block, ROW, B_SIMABRUSHCHANGE, "Soften",              890,120,80,19, &Gip.current, 7.0, IMAGEPAINT_SOFTEN, 0, 0, "Soften");
884         uiDefButS(block, ROW, B_SIMABRUSHCHANGE, "Aux AB1",             890,100,80,19, &Gip.current, 7.0, IMAGEPAINT_AUX1, 0, 0, "Auxiliary Air Brush1");
885         uiDefButS(block, ROW, B_SIMABRUSHCHANGE, "Aux AB2",             890,80,80,19, &Gip.current, 7.0, IMAGEPAINT_AUX2, 0, 0, "Auxiliary Air Brush2");        
886         uiDefButS(block, ROW, B_SIMABRUSHCHANGE, "Smear",               890,60,80,19, &Gip.current, 7.0, IMAGEPAINT_SMEAR, 0, 0, "Smear");      
887         uiDefButS(block, ROW, B_SIMABRUSHCHANGE, "Clone",               890,40,80,19, &Gip.current, 7.0, IMAGEPAINT_CLONE, 0, 0, "Clone Brush / use RMB to drag source image"); 
888         uiBlockEndAlign(block);
889
890         uiBlockBeginAlign(block);       
891         id= (ID*)Gip.clone.image;
892         std_libbuttons(block, 979, 40, 0, NULL, B_SIMACLONEBROWSE, id, 0, &G.sima->menunr, 0, 0, B_SIMACLONEDELETE, 0, 0);
893         uiDefButF(block, NUMSLI, 0, "B ",979,20,230,19, &Gip.clone.alpha , 0.0, 1.0, 0, 0, "Blend clone image");
894         uiBlockEndAlign(block);
895
896 #if 0
897         uiDefButBitS(block, TOG|BIT, IMAGEPAINT_DRAW_TOOL_DRAWING, B_SIMABRUSHCHANGE, "TD", 890,1,50,19, &Gip.flag, 0, 0, 0, 0, "Enables tool shape while drawing");
898         uiDefButBitS(block, TOG|BIT, IMAGEPAINT_DRAW_TOOL, B_SIMABRUSHCHANGE, "TP", 940,1,50,19, &Gip.flag, 0, 0, 0, 0, "Enables tool shape while not drawing");
899 #endif
900         uiDefButBitS(block, TOG|BIT, IMAGEPAINT_TORUS, B_SIMABRUSHCHANGE, "Wrap", 890,1,50,19, &Gip.flag, 0, 0, 0, 0, "Enables torus wrapping");
901 }
902
903 static void image_blockhandlers(ScrArea *sa)
904 {
905         SpaceImage *sima= sa->spacedata.first;
906         short a;
907
908         /* warning; blocks need to be freed each time, handlers dont remove  */
909         uiFreeBlocksWin(&sa->uiblocks, sa->win);
910         
911         for(a=0; a<SPACE_MAXHANDLER; a+=2) {
912                 switch(sima->blockhandler[a]) {
913
914                 case IMAGE_HANDLER_PROPERTIES:
915                         image_panel_properties(sima->blockhandler[a+1]);
916                         break;
917                 case IMAGE_HANDLER_PAINT:
918                         image_panel_paint(sima->blockhandler[a+1]);
919                         break;          
920                 }
921                 /* clear action value for event */
922                 sima->blockhandler[a+1]= 0;
923         }
924         uiDrawBlocksPanels(sa, 0);
925 }
926
927 static void imagespace_grid(SpaceImage *sima)
928 {
929         float gridsize, gridstep= 1.0f/32.0f;
930         float fac, blendfac;
931         
932         gridsize= sima->zoom;
933         
934         if(gridsize<=0.0f) return;
935         
936         if(gridsize<1.0f) {
937                 while(gridsize<1.0f) {
938                         gridsize*= 4.0;
939                         gridstep*= 4.0;
940                 }
941         }
942         else {
943                 while(gridsize>=4.0f) {
944                         gridsize/= 4.0;
945                         gridstep/= 4.0;
946                 }
947         }
948         
949         /* the fine resolution level */
950         blendfac= 0.25*gridsize - floor(0.25*gridsize);
951         CLAMP(blendfac, 0.0, 1.0);
952         BIF_ThemeColorShade(TH_BACK, (int)(20.0*(1.0-blendfac)));
953         
954         fac= 0.0f;
955         glBegin(GL_LINES);
956         while(fac<1.0) {
957                 glVertex2f(0.0f, fac);
958                 glVertex2f(1.0f, fac);
959                 glVertex2f(fac, 0.0f);
960                 glVertex2f(fac, 1.0f);
961                 fac+= gridstep;
962         }
963         
964         /* the large resolution level */
965         BIF_ThemeColor(TH_BACK);
966         
967         fac= 0.0f;
968         while(fac<1.0) {
969                 glVertex2f(0.0f, fac);
970                 glVertex2f(1.0f, fac);
971                 glVertex2f(fac, 0.0f);
972                 glVertex2f(fac, 1.0f);
973                 fac+= 4.0*gridstep;
974         }
975         glEnd();
976         
977 }
978
979 void drawimagespace(ScrArea *sa, void *spacedata)
980 {
981         SpaceImage *sima= spacedata;
982         ImBuf *ibuf= NULL;
983         float col[3];
984         unsigned int *rect;
985         int x1, y1;
986         short sx, sy, dx, dy;
987         
988                 /* If derived data is used then make sure that object
989                  * is up-to-date... might not be the case because updates
990                  * are normally done in drawview and could get here before
991                  * drawing a View3D.
992                  */
993         if (!G.obedit && OBACT && (sima->flag & SI_DRAWSHADOW)) {
994                 object_handle_update(OBACT);
995         }
996
997         BIF_GetThemeColor3fv(TH_BACK, col);
998         glClearColor(col[0], col[1], col[2], 0.0);
999         glClear(GL_COLOR_BUFFER_BIT);
1000
1001         bwin_clear_viewmat(sa->win);    /* clear buttons view */
1002         glLoadIdentity();
1003         
1004         what_image(sima);
1005         
1006         if(sima->image) {
1007                 if(sima->image->ibuf==0) {
1008                         load_image(sima->image, IB_rect, G.sce, G.scene->r.cfra);
1009                 }       
1010                 tag_image_time(sima->image);
1011                 ibuf= sima->image->ibuf;
1012         }
1013         
1014         if(ibuf==NULL || ibuf->rect==NULL) {
1015                 calc_image_view(sima, 'f');
1016                 myortho2(G.v2d->cur.xmin, G.v2d->cur.xmax, G.v2d->cur.ymin, G.v2d->cur.ymax);
1017                 BIF_ThemeColorShade(TH_BACK, 20);
1018                 glRectf(0.0, 0.0, 1.0, 1.0);
1019                 imagespace_grid(sima);
1020                 draw_tfaces();
1021         }
1022         else {
1023                 /* calc location */
1024                 x1= (curarea->winx-sima->zoom*ibuf->x)/2;
1025                 y1= (curarea->winy-sima->zoom*ibuf->y)/2;
1026         
1027                 x1-= sima->zoom*sima->xof;
1028                 y1-= sima->zoom*sima->yof;
1029                 
1030                 /* needed for gla draw */
1031                 glaDefine2DArea(&curarea->winrct);
1032                 glPixelZoom((float)sima->zoom, (float)sima->zoom);
1033                                 
1034                 if(sima->flag & SI_EDITTILE) {
1035                         glaDrawPixelsSafe(x1, y1, ibuf->x, ibuf->y, ibuf->rect);
1036                         
1037                         glPixelZoom(1.0, 1.0);
1038                         
1039                         dx= ibuf->x/sima->image->xrep;
1040                         dy= ibuf->y/sima->image->yrep;
1041                         sy= (sima->curtile / sima->image->xrep);
1042                         sx= sima->curtile - sy*sima->image->xrep;
1043         
1044                         sx*= dx;
1045                         sy*= dy;
1046                         
1047                         calc_image_view(sima, 'p');     /* pixel */
1048                         myortho2(G.v2d->cur.xmin, G.v2d->cur.xmax, G.v2d->cur.ymin, G.v2d->cur.ymax);
1049                         
1050                         cpack(0x0);
1051                         glPolygonMode(GL_FRONT_AND_BACK, GL_LINE); glRects(sx,  sy,  sx+dx-1,  sy+dy-1); glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
1052                         cpack(0xFFFFFF);
1053                         glPolygonMode(GL_FRONT_AND_BACK, GL_LINE); glRects(sx+1,  sy+1,  sx+dx,  sy+dy); glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
1054                 }
1055                 else if(sima->mode==SI_TEXTURE) {
1056                         
1057                         if(sima->image->tpageflag & IMA_TILES) {
1058                                 
1059                                 /* just leave this a while */
1060                                 if(sima->image->xrep<1) return;
1061                                 if(sima->image->yrep<1) return;
1062                                 
1063                                 if(sima->curtile >= sima->image->xrep*sima->image->yrep) 
1064                                         sima->curtile = sima->image->xrep*sima->image->yrep - 1; 
1065                                 
1066                                 dx= ibuf->x/sima->image->xrep;
1067                                 dy= ibuf->y/sima->image->yrep;
1068                                 
1069                                 sy= (sima->curtile / sima->image->xrep);
1070                                 sx= sima->curtile - sy*sima->image->xrep;
1071                 
1072                                 sx*= dx;
1073                                 sy*= dy;
1074                                 
1075                                 rect= get_part_from_ibuf(ibuf, sx, sy, sx+dx, sy+dy);
1076                                 
1077                                 /* rect= ibuf->rect; */
1078                                 for(sy= 0; sy+dy<=ibuf->y; sy+= dy) {
1079                                         for(sx= 0; sx+dx<=ibuf->x; sx+= dx) {
1080                                                 glaDrawPixelsSafe(x1+sx*sima->zoom, y1+sy*sima->zoom, dx, dy, rect);
1081                                         }
1082                                 }
1083                                 
1084                                 MEM_freeN(rect);
1085                         }
1086                         else 
1087                                 glaDrawPixelsSafe(x1, y1, ibuf->x, ibuf->y, ibuf->rect);
1088                         
1089                         if(Gip.current == IMAGEPAINT_CLONE) {
1090                                 int w, h;
1091                                 unsigned char *clonerect;
1092
1093                                 /* this is not very efficient, but glDrawPixels doesn't allow
1094                                    drawing with alpha */
1095                                 clonerect= alloc_alpha_clone_image(&w, &h);
1096
1097                                 if(clonerect) {
1098                                         int offx, offy;
1099                                         offx = sima->zoom*ibuf->x * + Gip.clone.offset[0];
1100                                         offy = sima->zoom*ibuf->y * + Gip.clone.offset[1];
1101
1102                                         glEnable(GL_BLEND);
1103                                         glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
1104                                         glaDrawPixelsSafe(x1 + offx, y1 + offy, w, h, clonerect);
1105                                         glDisable(GL_BLEND);
1106
1107                                         MEM_freeN(clonerect);
1108                                 }
1109                         }
1110                         
1111                         glPixelZoom(1.0, 1.0);
1112                         
1113                         draw_tfaces();
1114                 }
1115         
1116                 calc_image_view(sima, 'f');     /* float */
1117         }
1118
1119         draw_image_transform(ibuf);
1120
1121         myortho2(-0.375, sa->winx-0.375, -0.375, sa->winy-0.375);
1122
1123         draw_image_view_tool();
1124         draw_image_view_icon();
1125         draw_area_emboss(sa);
1126
1127         /* it is important to end a view in a transform compatible with buttons */
1128         bwin_scalematrix(sa->win, sima->blockscale, sima->blockscale, sima->blockscale);
1129         image_blockhandlers(sa);
1130
1131         curarea->win_swap= WIN_BACK_OK;
1132 }
1133
1134 static void image_zoom_power_of_two(void)
1135 {
1136         /* Make zoom a power of 2 */
1137
1138         G.sima->zoom = 1 / G.sima->zoom;
1139         G.sima->zoom = log(G.sima->zoom) / log(2);
1140         G.sima->zoom = ceil(G.sima->zoom);
1141         G.sima->zoom = pow(2, G.sima->zoom);
1142         G.sima->zoom = 1 / G.sima->zoom;
1143 }
1144
1145 static void image_zoom_set_factor(float zoomfac)
1146 {
1147         SpaceImage *sima= curarea->spacedata.first;
1148         int width, height;
1149
1150         if (zoomfac <= 0.0f)
1151                 return;
1152
1153         sima->zoom *= zoomfac;
1154
1155         if (sima->zoom > 0.1f && sima->zoom < 4.0f)
1156                 return;
1157
1158         /* check zoom limits */
1159
1160         calc_image_view(G.sima, 'p');
1161         width= 256;
1162         height= 256;
1163         if (sima->image) {
1164                 if (sima->image->ibuf) {
1165                         width= sima->image->ibuf->x;
1166                         height= sima->image->ibuf->y;
1167                 }
1168         }
1169         width *= sima->zoom;
1170         height *= sima->zoom;
1171
1172         if ((width < 4) && (height < 4))
1173                 sima->zoom /= zoomfac;
1174         else if((curarea->winrct.xmax - curarea->winrct.xmin) <= sima->zoom)
1175                 sima->zoom /= zoomfac;
1176         else if((curarea->winrct.ymax - curarea->winrct.ymin) <= sima->zoom)
1177                 sima->zoom /= zoomfac;
1178 }
1179
1180 void image_viewmove(int mode)
1181 {
1182         short mval[2], mvalo[2], zoom0;
1183         
1184         getmouseco_sc(mvalo);
1185         zoom0= G.sima->zoom;
1186         
1187         while(get_mbut()&(L_MOUSE|M_MOUSE)) {
1188
1189                 getmouseco_sc(mval);
1190
1191                 if(mvalo[0]!=mval[0] || mvalo[1]!=mval[1]) {
1192                 
1193                         if(mode==0) {
1194                                 G.sima->xof += (mvalo[0]-mval[0])/G.sima->zoom;
1195                                 G.sima->yof += (mvalo[1]-mval[1])/G.sima->zoom;
1196                         }
1197                         else if (mode==1) {
1198                                 float factor;
1199
1200                                 factor= 1.0+(float)(mvalo[0]-mval[0]+mvalo[1]-mval[1])/300.0;
1201                                 image_zoom_set_factor(factor);
1202                         }
1203
1204                         mvalo[0]= mval[0];
1205                         mvalo[1]= mval[1];
1206                         
1207                         scrarea_do_windraw(curarea);
1208                         screen_swapbuffers();
1209                 }
1210                 else BIF_wait_for_statechange();
1211         }
1212 }
1213
1214 void image_viewzoom(unsigned short event, int invert)
1215 {
1216         SpaceImage *sima= curarea->spacedata.first;
1217
1218         if(event==WHEELDOWNMOUSE || event==PADMINUS)
1219                 image_zoom_set_factor((U.uiflag & USER_WHEELZOOMDIR)? 1.25: 0.8);
1220         else if(event==WHEELUPMOUSE || event==PADPLUSKEY)
1221                 image_zoom_set_factor((U.uiflag & USER_WHEELZOOMDIR)? 0.8: 1.25);
1222         else if(event==PAD1)
1223                 sima->zoom= 1.0;
1224         else if(event==PAD2)
1225                 sima->zoom= (invert)? 2.0: 0.5;
1226         else if(event==PAD4)
1227                 sima->zoom= (invert)? 4.0: 0.25;
1228         else if(event==PAD8)
1229                 sima->zoom= (invert)? 8.0: 0.125;
1230         else
1231                 return;
1232 }
1233
1234 /**
1235  * Updates the fields of the View2D member of the SpaceImage struct.
1236  * Default behavior is to reset the position of the image and set the zoom to 1
1237  * If the image will not fit within the window rectangle, the zoom is adjusted
1238  *
1239  * @return    void
1240  *   
1241  */
1242 void image_home(void)
1243 {
1244         int width, height;
1245         float zoomX, zoomY;
1246
1247         if (curarea->spacetype != SPACE_IMAGE) return;
1248         if ((G.sima->image == 0) || (G.sima->image->ibuf == 0)) return;
1249
1250         /* Check if the image will fit in the image with zoom==1 */
1251         width = curarea->winx;
1252         height = curarea->winy;
1253         if (((G.sima->image->ibuf->x >= width) || (G.sima->image->ibuf->y >= height)) && 
1254                 ((width > 0) && (height > 0))) {
1255                 /* Find the zoom value that will fit the image in the image space */
1256                 zoomX = ((float)width) / ((float)G.sima->image->ibuf->x);
1257                 zoomY = ((float)height) / ((float)G.sima->image->ibuf->y);
1258                 G.sima->zoom= MIN2(zoomX, zoomY);
1259
1260                 image_zoom_power_of_two();
1261         }
1262         else {
1263                 G.sima->zoom= (float)1;
1264         }
1265
1266         G.sima->xof= G.sima->yof= 0;
1267         
1268         calc_image_view(G.sima, 'p');
1269         
1270         scrarea_queue_winredraw(curarea);
1271 }
1272
1273 void image_viewcentre(void)
1274 {
1275         float size, min[2], max[2], d[2], xim=256.0f, yim=256.0f;
1276
1277         if( is_uv_tface_editing_allowed()==0 ) return;
1278
1279         if (!minmax_tface_uv(min, max)) return;
1280
1281         if(G.sima->image && G.sima->image->ibuf) {
1282                 xim= G.sima->image->ibuf->x;
1283                 yim= G.sima->image->ibuf->y;
1284         }
1285
1286         G.sima->xof= ((min[0] + max[0])*0.5f - 0.5f)*xim;
1287         G.sima->yof= ((min[1] + max[1])*0.5f - 0.5f)*yim;
1288
1289         d[0] = max[0] - min[0];
1290         d[1] = max[1] - min[1];
1291         size= 0.5*MAX2(d[0], d[1])*MAX2(xim, yim)/256.0f;
1292         
1293         if(size<=0.01) size= 0.01;
1294
1295         G.sima->zoom= 0.7/size;
1296
1297         calc_image_view(G.sima, 'p');
1298
1299         scrarea_queue_winredraw(curarea);
1300 }
1301