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