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