Small fix: the compositing "Preview" option in UV/Image editor should only
[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.h"
51 #include "IMB_imbuf_types.h"
52
53 #include "DNA_camera_types.h"
54 #include "DNA_color_types.h"
55 #include "DNA_image_types.h"
56 #include "DNA_mesh_types.h"
57 #include "DNA_meshdata_types.h"
58 #include "DNA_object_types.h"
59 #include "DNA_packedFile_types.h"
60 #include "DNA_scene_types.h"
61 #include "DNA_screen_types.h"
62 #include "DNA_space_types.h"
63 #include "DNA_userdef_types.h"
64
65 #include "BKE_colortools.h"
66 #include "BKE_utildefines.h"
67 #include "BKE_global.h"
68 #include "BKE_library.h"
69 #include "BKE_main.h"
70 #include "BKE_mesh.h"
71 #include "BKE_node.h"
72 #include "BKE_image.h"
73 #include "BKE_DerivedMesh.h"
74 #include "BKE_displist.h"
75 #include "BKE_object.h"
76
77 #include "BDR_editface.h"
78 #include "BDR_drawobject.h"
79 #include "BDR_drawmesh.h"
80 #include "BDR_imagepaint.h"
81
82 #include "BIF_gl.h"
83 #include "BIF_mywindow.h"
84 #include "BIF_drawimage.h"
85 #include "BIF_resources.h"
86 #include "BIF_interface.h"
87 #include "BIF_interface_icons.h"
88 #include "BIF_editsima.h"
89 #include "BIF_glutil.h"
90 #include "BIF_space.h"
91 #include "BIF_screen.h"
92 #include "BIF_transform.h"
93
94 #include "BSE_drawipo.h"
95 #include "BSE_headerbuttons.h"
96 #include "BSE_trans_types.h"
97 #include "BSE_view.h"
98
99 /* Modules used */
100 #include "mydevice.h"
101 #include "blendef.h"
102 #include "butspace.h"  // event codes
103
104 #include "interface.h"  /* bad.... but preview code needs UI info. Will solve... (ton) */
105
106 static unsigned char *alloc_alpha_clone_image(int *width, int *height)
107 {
108         unsigned int size, alpha;
109         unsigned char *rect, *cp;
110
111         if(!Gip.clone.image)
112                 return NULL;
113
114         if(!Gip.clone.image->ibuf)
115                 load_image(Gip.clone.image, IB_rect, G.sce, G.scene->r.cfra);
116
117         if(!Gip.clone.image->ibuf || !Gip.clone.image->ibuf->rect)
118                 return NULL;
119
120         rect= MEM_dupallocN(Gip.clone.image->ibuf->rect);
121
122         if(!rect)
123                 return NULL;
124
125         *width= Gip.clone.image->ibuf->x;
126         *height= Gip.clone.image->ibuf->y;
127
128         size= (*width)*(*height);
129         alpha= (unsigned char)255*Gip.clone.alpha;
130         cp= rect;
131
132         while(size-- > 0) {
133                 cp[3]= alpha;
134                 cp += 4;
135         }
136
137         return rect;
138 }
139
140 static void setcloneimage()
141 {
142         if(G.sima->menunr > 0) {
143                 Image *ima= (Image*)BLI_findlink(&G.main->image, G.sima->menunr-1);
144
145                 if(ima) {
146                         Gip.clone.image= ima;
147                         Gip.clone.offset[0]= Gip.clone.offset[0]= 0.0;
148                 }
149         }
150 }
151
152 static int image_preview_active(ScrArea *sa, float *xim, float *yim)
153 {
154         SpaceImage *sima= sa->spacedata.first;
155         
156         /* only when compositor shows, and image handler set */
157         if(sima->image == (Image *)find_id("IM", "Compositor")) {
158                 short a;
159         
160                 for(a=0; a<SPACE_MAXHANDLER; a+=2) {
161                         if(sima->blockhandler[a] == IMAGE_HANDLER_PREVIEW) {
162                                 if(xim) *xim= (G.scene->r.size*G.scene->r.xsch)/100;
163                                 if(yim) *yim= (G.scene->r.size*G.scene->r.ysch)/100;
164                                 return 1;
165                         }
166                 }
167         }
168         return 0;
169 }
170
171
172 /**
173  * Sets up the fields of the View2D member of the SpaceImage struct
174  * This routine can be called in two modes:
175  * mode == 'f': float mode (0.0 - 1.0)
176  * mode == 'p': pixel mode (0 - size)
177  *
178  * @param     sima  the image space to update
179  * @param     mode  the mode to use for the update
180  * @return    void
181  *   
182  */
183 void calc_image_view(SpaceImage *sima, char mode)
184 {
185         float xim=256, yim=256;
186         float x1, y1;
187         float zoom;
188         
189         if(image_preview_active(curarea, &xim, &yim));
190         else if(sima->image && sima->image->ibuf) {
191                 xim= sima->image->ibuf->x;
192                 yim= sima->image->ibuf->y;
193         }
194                 
195         sima->v2d.tot.xmin= 0;
196         sima->v2d.tot.ymin= 0;
197         sima->v2d.tot.xmax= xim;
198         sima->v2d.tot.ymax= yim;
199         
200         sima->v2d.mask.xmin= sima->v2d.mask.ymin= 0;
201         sima->v2d.mask.xmax= curarea->winx;
202         sima->v2d.mask.ymax= curarea->winy;
203
204
205         /* Which part of the image space do we see? */
206         /* Same calculation as in lrectwrite: area left and down*/
207         x1= curarea->winrct.xmin+(curarea->winx-sima->zoom*xim)/2;
208         y1= curarea->winrct.ymin+(curarea->winy-sima->zoom*yim)/2;
209
210         x1-= sima->zoom*sima->xof;
211         y1-= sima->zoom*sima->yof;
212
213         /* float! */
214         zoom= sima->zoom;
215         
216         /* relative display right */
217         sima->v2d.cur.xmin= ((curarea->winrct.xmin - (float)x1)/zoom);
218         sima->v2d.cur.xmax= sima->v2d.cur.xmin + ((float)curarea->winx/zoom);
219         
220         /* relative display left */
221         sima->v2d.cur.ymin= ((curarea->winrct.ymin-(float)y1)/zoom);
222         sima->v2d.cur.ymax= sima->v2d.cur.ymin + ((float)curarea->winy/zoom);
223         
224         if(mode=='f') {         
225                 sima->v2d.cur.xmin/= xim;
226                 sima->v2d.cur.xmax/= xim;
227                 sima->v2d.cur.ymin/= yim;
228                 sima->v2d.cur.ymax/= yim;
229         }
230 }
231
232 void what_image(SpaceImage *sima)
233 {
234         TFace *activetf;
235         Mesh *me;
236                 
237         if(sima->mode==SI_TEXTURE) {
238                 if(G.f & G_FACESELECT) {
239
240                         sima->image= 0;
241                         me= get_mesh(OBACT);
242                         activetf = get_active_tface();
243                         
244                         if(me && me->tface && activetf && activetf->mode & TF_TEX) {
245                                 sima->image= activetf->tpage;
246                                         
247                                 if(sima->flag & SI_EDITTILE);
248                                 else sima->curtile= activetf->tile;
249                                 
250                                 if(sima->image) {
251                                         if(activetf->mode & TF_TILES)
252                                                 sima->image->tpageflag |= IMA_TILES;
253                                         else sima->image->tpageflag &= ~IMA_TILES;
254                                 }
255                         }
256                 }
257         }
258 }
259
260 void image_changed(SpaceImage *sima, int dotile)
261 {
262         TFace *tface;
263         Mesh *me;
264         int a;
265         
266         if(sima->mode==SI_TEXTURE) {
267                 
268                 if(G.f & G_FACESELECT) {
269                         me= get_mesh(OBACT);
270                         if(me && me->tface) {
271                                 tface= me->tface;
272                                 a= me->totface;
273                                 while(a--) {
274                                         if(tface->flag & TF_SELECT) {
275                                                 
276                                                 if(dotile==2) {
277                                                         tface->mode &= ~TF_TILES;
278                                                 }
279                                                 else {
280                                                         tface->tpage= sima->image;
281                                                         tface->mode |= TF_TEX;
282                                                 
283                                                         if(dotile) tface->tile= sima->curtile;
284                                                 }
285                                                 
286                                                 if(sima->image) {
287                                                         if(sima->image->tpageflag & IMA_TILES) tface->mode |= TF_TILES;
288                                                         else tface->mode &= ~TF_TILES;
289                                                 
290                                                         if(sima->image->id.us==0) sima->image->id.us= 1;
291                                                 }
292                                         }
293                                         tface++;
294                                 }
295
296                                 object_uvs_changed(OBACT);
297                                 allqueue(REDRAWBUTSEDIT, 0);
298                         }
299                 }
300         }
301 }
302
303
304 void uvco_to_areaco(float *vec, short *mval)
305 {
306         float x, y;
307
308         mval[0]= IS_CLIPPED;
309         
310         x= (vec[0] - G.v2d->cur.xmin)/(G.v2d->cur.xmax-G.v2d->cur.xmin);
311         y= (vec[1] - G.v2d->cur.ymin)/(G.v2d->cur.ymax-G.v2d->cur.ymin);
312
313         if(x>=0.0 && x<=1.0) {
314                 if(y>=0.0 && y<=1.0) {          
315                         mval[0]= G.v2d->mask.xmin + x*(G.v2d->mask.xmax-G.v2d->mask.xmin);
316                         mval[1]= G.v2d->mask.ymin + y*(G.v2d->mask.ymax-G.v2d->mask.ymin);
317                 }
318         }
319 }
320
321 void uvco_to_areaco_noclip(float *vec, int *mval)
322 {
323         float x, y;
324
325         mval[0]= IS_CLIPPED;
326         
327         x= (vec[0] - G.v2d->cur.xmin)/(G.v2d->cur.xmax-G.v2d->cur.xmin);
328         y= (vec[1] - G.v2d->cur.ymin)/(G.v2d->cur.ymax-G.v2d->cur.ymin);
329
330         x= G.v2d->mask.xmin + x*(G.v2d->mask.xmax-G.v2d->mask.xmin);
331         y= G.v2d->mask.ymin + y*(G.v2d->mask.ymax-G.v2d->mask.ymin);
332         
333         mval[0]= x;
334         mval[1]= y;
335 }
336
337 void draw_tfaces(void)
338 {
339         TFace *tface,*activetface = NULL;
340         MFace *mface,*activemface = NULL;
341         Mesh *me;
342         int a;
343         char col1[4], col2[4];
344         float pointsize= BIF_GetThemeValuef(TH_VERTEX_SIZE);
345         
346         if(G.f & G_FACESELECT) {
347                 me= get_mesh((G.scene->basact) ? (G.scene->basact->object) : 0);
348                 if(me && me->tface) {
349                         calc_image_view(G.sima, 'f');   /* float */
350                         myortho2(G.v2d->cur.xmin, G.v2d->cur.xmax, G.v2d->cur.ymin, G.v2d->cur.ymax);
351                         glLoadIdentity();
352                         
353                         /* draw shadow mesh */
354                         if((G.sima->flag & SI_DRAWSHADOW) && !(G.obedit==OBACT)){
355                                 int dmNeedsFree;
356                                 DerivedMesh *dm = mesh_get_derived_final(OBACT, &dmNeedsFree);
357
358                                 glColor3ub(112, 112, 112);
359                                 if (dm->drawUVEdges) dm->drawUVEdges(dm);
360
361                                 if (dmNeedsFree) dm->release(dm);
362                         }
363                         else if(G.sima->flag & SI_DRAWSHADOW){          
364                                 tface= me->tface;
365                                 mface= me->mface;
366                                 a= me->totface;                 
367                                 while(a--) {
368                                         if(!(tface->flag & TF_HIDE)) {
369                                                 glColor3ub(112, 112, 112);
370                                                 glBegin(GL_LINE_LOOP);
371                                                 glVertex2fv(tface->uv[0]);
372                                                 glVertex2fv(tface->uv[1]);
373                                                 glVertex2fv(tface->uv[2]);
374                                                 if(mface->v4) glVertex2fv(tface->uv[3]);
375                                                 glEnd();
376                                         } 
377                                         tface++;
378                                         mface++;                                        
379                                 }
380                         }
381                         
382                         /* draw transparent faces */
383                         if(G.f & G_DRAWFACES) {
384                                 BIF_GetThemeColor4ubv(TH_FACE, col1);
385                                 BIF_GetThemeColor4ubv(TH_FACE_SELECT, col2);
386                                 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
387                                 glEnable(GL_BLEND);
388                                 tface= me->tface;
389                                 mface= me->mface;
390                                 a= me->totface;                 
391                                 while(a--) {
392                                         if(tface->flag & TF_SELECT) {
393                                                 if(!(~tface->flag & (TF_SEL1|TF_SEL2|TF_SEL3)) &&
394                                                    (!mface->v4 || tface->flag & TF_SEL4))
395                                                         glColor4ubv(col2);
396                                                 else
397                                                         glColor4ubv(col1);
398                                                         
399                                                 glBegin(mface->v4?GL_QUADS:GL_TRIANGLES);
400                                                         glVertex2fv(tface->uv[0]);
401                                                         glVertex2fv(tface->uv[1]);
402                                                         glVertex2fv(tface->uv[2]);
403                                                         if(mface->v4) glVertex2fv(tface->uv[3]);
404                                                 glEnd();
405                                         }
406                                         tface++;
407                                         mface++;                                        
408                                 }
409                                 glDisable(GL_BLEND);
410                         }
411
412
413                         tface= me->tface;
414                         mface= me->mface;
415                         a= me->totface;
416                         while(a--) {
417                                 if(tface->flag & TF_SELECT) {
418                                         if(tface->flag & TF_ACTIVE){
419                                                 activetface= tface; 
420                                                 activemface= mface; 
421                                         }
422
423                                         cpack(0x0);
424                                         glBegin(GL_LINE_LOOP);
425                                                 glVertex2fv(tface->uv[0]);
426                                                 glVertex2fv(tface->uv[1]);
427                                                 glVertex2fv(tface->uv[2]);
428                                                 if(mface->v4) glVertex2fv(tface->uv[3]);
429                                         glEnd();
430                                 
431                                         setlinestyle(2);
432                                         cpack(0xFFFFFF);
433                                         glBegin(GL_LINE_STRIP);
434                                                 glVertex2fv(tface->uv[0]);
435                                                 glVertex2fv(tface->uv[1]);
436                                         glEnd();
437
438                                         glBegin(GL_LINE_STRIP);
439                                                 glVertex2fv(tface->uv[0]);
440                                                 if(mface->v4) glVertex2fv(tface->uv[3]);
441                                                 else glVertex2fv(tface->uv[2]);
442                                         glEnd();
443         
444                                         glBegin(GL_LINE_STRIP);
445                                                 glVertex2fv(tface->uv[1]);
446                                                 glVertex2fv(tface->uv[2]);
447                                                 if(mface->v4) glVertex2fv(tface->uv[3]);
448                                         glEnd();
449                                         setlinestyle(0);
450                                 }
451                                         
452                                 tface++;
453                                 mface++;
454                         }
455
456                         /* draw active face edges */
457                         if (activetface){
458                                 /* colors: R=u G=v */
459
460                                 setlinestyle(2);
461                                 tface=activetface; 
462                                 mface=activemface; 
463
464                                 cpack(0x0);
465                                 glBegin(GL_LINE_LOOP);
466                                 glVertex2fv(tface->uv[0]);
467                                         glVertex2fv(tface->uv[1]);
468                                         glVertex2fv(tface->uv[2]);
469                                         if(mface->v4) glVertex2fv(tface->uv[3]);
470                                 glEnd();
471                                         
472                                 cpack(0xFF00);
473                                 glBegin(GL_LINE_STRIP);
474                                         glVertex2fv(tface->uv[0]);
475                                         glVertex2fv(tface->uv[1]);
476                                 glEnd();
477
478                                 cpack(0xFF);
479                                 glBegin(GL_LINE_STRIP);
480                                         glVertex2fv(tface->uv[0]);
481                                         if(mface->v4) glVertex2fv(tface->uv[3]);
482                                         else glVertex2fv(tface->uv[2]);
483                                 glEnd();
484
485                                 cpack(0xFFFFFF);
486                                 glBegin(GL_LINE_STRIP);
487                                         glVertex2fv(tface->uv[1]);
488                                         glVertex2fv(tface->uv[2]);
489                                         if(mface->v4) glVertex2fv(tface->uv[3]);
490                                 glEnd();
491                                 
492                                 setlinestyle(0);
493                         }
494
495             /* unselected uv's */
496                         BIF_ThemeColor(TH_VERTEX);
497                         glPointSize(pointsize);
498
499                         bglBegin(GL_POINTS);
500                         tface= me->tface;
501                         mface= me->mface;
502                         a= me->totface;
503                         while(a--) {
504                                 if(tface->flag & TF_SELECT) {
505                                         
506                                         if(tface->flag & TF_SEL1); else bglVertex2fv(tface->uv[0]);
507                                         if(tface->flag & TF_SEL2); else bglVertex2fv(tface->uv[1]);
508                                         if(tface->flag & TF_SEL3); else bglVertex2fv(tface->uv[2]);
509                                         if(mface->v4) {
510                                                 if(tface->flag & TF_SEL4); else bglVertex2fv(tface->uv[3]);
511                                         }
512                                 }
513                                 tface++;
514                                 mface++;
515                         }
516                         bglEnd();
517
518                         /* pinned uv's */
519                         /* give odd pointsizes odd pin pointsizes */
520                 glPointSize(pointsize*2 + (((int)pointsize % 2)? (-1): 0));
521                         cpack(0xFF);
522
523                         bglBegin(GL_POINTS);
524                         tface= me->tface;
525                         mface= me->mface;
526                         a= me->totface;
527                         while(a--) {
528                                 if(tface->flag & TF_SELECT) {
529                                         
530                                         if(tface->unwrap & TF_PIN1) bglVertex2fv(tface->uv[0]);
531                                         if(tface->unwrap & TF_PIN2) bglVertex2fv(tface->uv[1]);
532                                         if(tface->unwrap & TF_PIN3) bglVertex2fv(tface->uv[2]);
533                                         if(mface->v4) {
534                                                 if(tface->unwrap & TF_PIN4) bglVertex2fv(tface->uv[3]);
535                                         }
536                                 }
537                                 tface++;
538                                 mface++;
539                         }
540                         bglEnd();
541
542                         /* selected uv's */
543                         BIF_ThemeColor(TH_VERTEX_SELECT);
544                 glPointSize(pointsize);
545
546                         bglBegin(GL_POINTS);
547                         tface= me->tface;
548                         mface= me->mface;
549                         a= me->totface;
550                         while(a--) {
551                                 if(tface->flag & TF_SELECT) {
552                                         
553                                         if(tface->flag & TF_SEL1) bglVertex2fv(tface->uv[0]);
554                                         if(tface->flag & TF_SEL2) bglVertex2fv(tface->uv[1]);
555                                         if(tface->flag & TF_SEL3) bglVertex2fv(tface->uv[2]);
556                                         if(mface->v4) {
557                                                 if(tface->flag & TF_SEL4) bglVertex2fv(tface->uv[3]);
558                                         }
559                                 }
560                                 tface++;
561                                 mface++;
562                         }
563                         bglEnd();
564                 }
565         }
566         glPointSize(1.0);
567 }
568
569 static unsigned int *get_part_from_ibuf(ImBuf *ibuf, short startx, short starty, short endx, short endy)
570 {
571         unsigned int *rt, *rp, *rectmain;
572         short y, heigth, len;
573
574         /* the right offset in rectot */
575
576         rt= ibuf->rect+ (starty*ibuf->x+ startx);
577
578         len= (endx-startx);
579         heigth= (endy-starty);
580
581         rp=rectmain= MEM_mallocN(heigth*len*sizeof(int), "rect");
582         
583         for(y=0; y<heigth; y++) {
584                 memcpy(rp, rt, len*4);
585                 rt+= ibuf->x;
586                 rp+= len;
587         }
588         return rectmain;
589 }
590
591 static void draw_image_transform(ImBuf *ibuf)
592 {
593         if(G.moving) {
594                 float aspx, aspy, center[3];
595
596         BIF_drawConstraint();
597
598                 if(ibuf==0 || ibuf->rect==0 || ibuf->x==0 || ibuf->y==0) {
599                         aspx= aspy= 1.0;
600                 }
601                 else {
602                         aspx= 256.0/ibuf->x;
603                         aspy= 256.0/ibuf->y;
604                 }
605
606                 BIF_getPropCenter(center);
607
608                 /* scale and translate the circle into place and draw it */
609                 glPushMatrix();
610                 glScalef(aspx, aspy, 1.0);
611                 glTranslatef((1/aspx)*center[0] - center[0],
612                              (1/aspy)*center[1] - center[1], 0.0);
613
614                 BIF_drawPropCircle();
615
616                 glPopMatrix();
617         }
618 }
619
620 static void draw_image_view_icon(void)
621 {
622         float xPos = 5.0;
623
624         glEnable(GL_BLEND);
625         glBlendFunc(GL_SRC_ALPHA,  GL_ONE_MINUS_SRC_ALPHA); 
626         
627         if(G.sima->flag & SI_STICKYUVS) {
628                 BIF_icon_draw(xPos, 5.0, ICON_STICKY2_UVS);
629                 xPos = 25.0;
630         }
631         else if(!(G.sima->flag & SI_LOCALSTICKY)) {
632                 BIF_icon_draw(xPos, 5.0, ICON_STICKY_UVS);
633                 xPos = 25.0;
634         }
635
636         if(G.sima->flag & SI_SELACTFACE) {
637                 BIF_icon_draw(xPos, 5.0, ICON_DRAW_UVFACES);
638         }
639         
640         glBlendFunc(GL_ONE,  GL_ZERO); 
641         glDisable(GL_BLEND);
642 }
643
644 static void draw_image_view_tool(void)
645 {
646         ImagePaintTool *tool = &Gip.tool[Gip.current];
647         short mval[2];
648         float radius;
649         int draw= 0;
650
651         if(Gip.flag & IMAGEPAINT_DRAWING) {
652                 if(Gip.flag & IMAGEPAINT_DRAW_TOOL_DRAWING)
653                         draw= 1;
654         }
655         else if(Gip.flag & IMAGEPAINT_DRAW_TOOL)
656                 draw= 1;
657         
658         if(draw) {
659                 getmouseco_areawin(mval);
660
661                 radius= tool->size*G.sima->zoom/2;
662                 fdrawXORcirc(mval[0], mval[1], radius);
663
664                 if (tool->innerradius != 1.0) {
665                         radius *= tool->innerradius;
666                         fdrawXORcirc(mval[0], mval[1], radius);
667                 }
668         }
669 }
670
671 /* ************ panel stuff ************* */
672
673 // button define is local, only events defined here possible
674 #define B_TRANS_IMAGE   1
675
676 /* is used for both read and write... */
677 static void image_editvertex_buts(uiBlock *block)
678 {
679         static float ocent[2];
680         float cent[2]= {0.0, 0.0};
681         int imx, imy;
682         int i, nactive= 0, step, digits;
683         Mesh *me;
684         
685         if( is_uv_tface_editing_allowed_silent()==0 ) return;
686         me= get_mesh(OBACT);
687         
688         if (G.sima->image && G.sima->image->ibuf) {
689                 imx= G.sima->image->ibuf->x;
690                 imy= G.sima->image->ibuf->y;
691         } else
692                 imx= imy= 256;
693         
694         for (i=0; i<me->totface; i++) {
695                 MFace *mf= &((MFace*) me->mface)[i];
696                 TFace *tf= &((TFace*) me->tface)[i];
697                 
698                 if (!(tf->flag & TF_SELECT))
699                         continue;
700                 
701                 if (tf->flag & TF_SEL1) {
702                         cent[0]+= tf->uv[0][0];
703                         cent[1]+= tf->uv[0][1];
704                         nactive++;
705                 }
706                 if (tf->flag & TF_SEL2) {
707                         cent[0]+= tf->uv[1][0];
708                         cent[1]+= tf->uv[1][1];
709                         nactive++;
710                 }
711                 if (tf->flag & TF_SEL3) {
712                         cent[0]+= tf->uv[2][0];
713                         cent[1]+= tf->uv[2][1];
714                         nactive++;
715                 }
716                 if (mf->v4 && (tf->flag & TF_SEL4)) {
717                         cent[0]+= tf->uv[3][0];
718                         cent[1]+= tf->uv[3][1];
719                         nactive++;
720                 }
721         }
722                 
723         if(block) {     // do the buttons
724                 if (nactive) {
725                         ocent[0]= cent[0]/nactive;
726                         ocent[1]= cent[1]/nactive;
727                         if (G.sima->flag & SI_COORDFLOATS) {
728                                 step= 1;
729                                 digits= 3;
730                         }
731                         else {
732                                 ocent[0] *= imx;
733                                 ocent[1] *= imy;
734                                 step= 100;
735                                 digits= 2;
736                         }
737                         
738                         uiDefBut(block, LABEL, 0, "UV Vertex:",10,55,302,19,0,0,0,0,0,"");
739                         uiBlockBeginAlign(block);
740                         if(nactive==1) {
741                                 uiDefButF(block, NUM, B_TRANS_IMAGE, "Vertex X:",       10, 35, 290, 19, &ocent[0], -10*imx, 10.0*imx, step, digits, "");
742                                 uiDefButF(block, NUM, B_TRANS_IMAGE, "Vertex Y:",       10, 15, 290, 19, &ocent[1], -10*imy, 10.0*imy, step, digits, "");
743                         }
744                         else {
745                                 uiDefButF(block, NUM, B_TRANS_IMAGE, "Median X:",       10, 35, 290, 19, &ocent[0], -10*imx, 10.0*imx, step, digits, "");
746                                 uiDefButF(block, NUM, B_TRANS_IMAGE, "Median Y:",       10, 15, 290, 19, &ocent[1], -10*imy, 10.0*imy, step, digits, "");
747                         }
748                         uiBlockEndAlign(block);
749                 }
750         }
751         else {  // apply event
752                 float delta[2];
753                 
754                 cent[0]= cent[0]/nactive;
755                 cent[1]= cent[1]/nactive;
756                         
757                 if (G.sima->flag & SI_COORDFLOATS) {
758                         delta[0]= ocent[0]-cent[0];
759                         delta[1]= ocent[1]-cent[1];
760                 }
761                 else {
762                         delta[0]= ocent[0]/imx - cent[0];
763                         delta[1]= ocent[1]/imy - cent[1];
764                 }
765
766                 for (i=0; i<me->totface; i++) {
767                         MFace *mf= &((MFace*) me->mface)[i];
768                         TFace *tf= &((TFace*) me->tface)[i];
769                 
770                         if (!(tf->flag & TF_SELECT))
771                                 continue;
772                 
773                         if (tf->flag & TF_SEL1) {
774                                 tf->uv[0][0]+= delta[0];
775                                 tf->uv[0][1]+= delta[1];
776                         }
777                         if (tf->flag & TF_SEL2) {
778                                 tf->uv[1][0]+= delta[0];
779                                 tf->uv[1][1]+= delta[1];
780                         }
781                         if (tf->flag & TF_SEL3) {
782                                 tf->uv[2][0]+= delta[0];
783                                 tf->uv[2][1]+= delta[1];
784                         }
785                         if (mf->v4 && (tf->flag & TF_SEL4)) {
786                                 tf->uv[3][0]+= delta[0];
787                                 tf->uv[3][1]+= delta[1];
788                         }
789                 }
790                         
791                 allqueue(REDRAWVIEW3D, 0);
792                 allqueue(REDRAWIMAGE, 0);
793         }
794 }
795
796
797 void do_imagebuts(unsigned short event)
798 {
799         switch(event) {
800         case B_TRANS_IMAGE:
801                 image_editvertex_buts(NULL);
802                 break;
803
804         case B_SIMAGEDRAW:
805                 if(G.f & G_FACESELECT) {
806                         make_repbind(G.sima->image);
807                         image_changed(G.sima, 1);
808                 }
809                 allqueue(REDRAWVIEW3D, 0);
810                 allqueue(REDRAWIMAGE, 0);
811                 break;
812
813         case B_SIMAGEDRAW1:
814                 image_changed(G.sima, 2);               /* 2: only tileflag */
815                 allqueue(REDRAWVIEW3D, 0);
816                 allqueue(REDRAWIMAGE, 0);
817                 break;
818                 
819         case B_TWINANIM:
820                 {
821                         Image *ima;
822                         int nr;
823
824                         ima = G.sima->image;
825                         if (ima) {
826                                 if(ima->flag & IMA_TWINANIM) {
827                                         nr= ima->xrep*ima->yrep;
828                                         if(ima->twsta>=nr) ima->twsta= 1;
829                                         if(ima->twend>=nr) ima->twend= nr-1;
830                                         if(ima->twsta>ima->twend) ima->twsta= 1;
831                                         allqueue(REDRAWIMAGE, 0);
832                                 }
833                         }
834                 }
835                 break;
836
837         case B_SIMACLONEBROWSE:
838                 setcloneimage();
839                 allqueue(REDRAWIMAGE, 0);
840                 break;
841                 
842         case B_SIMACLONEDELETE:
843                 Gip.clone.image= NULL;
844                 allqueue(REDRAWIMAGE, 0);
845                 break;
846
847         case B_SIMABRUSHCHANGE:
848                 allqueue(REDRAWIMAGE, 0);
849                 break;
850                 
851         case B_SIMACURVES:
852                 curvemapping_do_image(G.sima->cumap, G.sima->image);
853                 allqueue(REDRAWIMAGE, 0);
854                 break;
855                 
856         case B_SIMARANGE:
857                 curvemapping_set_black_white(G.sima->cumap, NULL, NULL);
858                 curvemapping_do_image(G.sima->cumap, G.sima->image);
859                 allqueue(REDRAWIMAGE, 0);
860                 break;
861         }
862 }
863
864 static void image_panel_properties(short cntrl) // IMAGE_HANDLER_PROPERTIES
865 {
866         uiBlock *block;
867
868         block= uiNewBlock(&curarea->uiblocks, "image_panel_properties", UI_EMBOSS, UI_HELV, curarea->win);
869         uiPanelControl(UI_PNL_SOLID | UI_PNL_CLOSE | cntrl);
870         uiSetPanelHandler(IMAGE_HANDLER_PROPERTIES);  // for close and esc
871         if(uiNewPanel(curarea, block, "Properties", "Image", 10, 10, 318, 204)==0)
872                 return;
873
874         if (G.sima->image && G.sima->image->ibuf) {
875                 char str[64];
876
877                 sprintf(str, "Image: size %d x %d", G.sima->image->ibuf->x, G.sima->image->ibuf->y);
878                 if(G.sima->image->ibuf->rect_float)
879                         strcat(str, " 4x32 bits");
880                 else 
881                         strcat(str, " 4x8 bits");
882                 
883                 uiDefBut(block, LABEL, B_NOP, str,              10,180,300,19, 0, 0, 0, 0, 0, "");
884
885                 uiBlockBeginAlign(block);
886                 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");
887                 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");
888                 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");
889                 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");
890                 uiBlockEndAlign(block);
891
892                 uiBlockBeginAlign(block);
893                 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");
894                 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");
895                 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");
896                 uiBlockBeginAlign(block);
897         }
898
899         image_editvertex_buts(block);
900 }
901
902 static void image_panel_paint(short cntrl)      // IMAGE_HANDLER_PROPERTIES
903 {
904         /* B_SIMABRUSHCHANGE only redraws and eats the mouse messages  */
905         /* so that LEFTMOUSE does not 'punch' through the floating panel */
906         /* B_SIMANOTHING */
907         ImagePaintTool *tool= &Gip.tool[Gip.current];
908         uiBlock *block;
909         ID *id;
910
911         block= uiNewBlock(&curarea->uiblocks, "image_panel_paint", UI_EMBOSS, UI_HELV, curarea->win);
912         uiPanelControl(UI_PNL_SOLID | UI_PNL_CLOSE | cntrl);
913         uiSetPanelHandler(IMAGE_HANDLER_PAINT);  // for close and esc
914         if(uiNewPanel(curarea, block, "Image Paint", "Image", 10, 230, 318, 204)==0)
915                 return;
916
917         uiBlockBeginAlign(block);
918         uiDefButF(block, COL, B_VPCOLSLI, "",           979,160,230,19, tool->rgba, 0, 0, 0, 0, "");
919         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");
920         uiDefButI(block, NUMSLI, B_SIMANOTHING , "Size ",               979,120,230,19, &tool->size, 2, 64, 0, 0, "The size of the brush");
921         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");
922
923         if(Gip.current == IMAGEPAINT_BRUSH || Gip.current == IMAGEPAINT_SMEAR)
924                 uiDefButF(block, NUMSLI, B_SIMANOTHING , "Stepsize ",979,80,230,19, &tool->timing, 1.0, 100.0, 0, 0, "Repeating Paint On %of Brush diameter");
925         else
926                 uiDefButF(block, NUMSLI, B_SIMANOTHING , "Flow ",       979,80,230,19, &tool->timing, 1.0, 100.0, 0, 0, "Paint Flow for Air Brush");
927         uiBlockEndAlign(block);
928
929         uiBlockBeginAlign(block);
930         uiDefButS(block, ROW, B_SIMABRUSHCHANGE, "Brush",               890,160,80,19, &Gip.current, 7.0, IMAGEPAINT_BRUSH, 0, 0, "Brush");
931         uiDefButS(block, ROW, B_SIMABRUSHCHANGE, "AirBrush",            890,140,80,19, &Gip.current, 7.0, IMAGEPAINT_AIRBRUSH, 0, 0, "AirBrush");
932         uiDefButS(block, ROW, B_SIMABRUSHCHANGE, "Soften",              890,120,80,19, &Gip.current, 7.0, IMAGEPAINT_SOFTEN, 0, 0, "Soften");
933         uiDefButS(block, ROW, B_SIMABRUSHCHANGE, "Aux AB1",             890,100,80,19, &Gip.current, 7.0, IMAGEPAINT_AUX1, 0, 0, "Auxiliary Air Brush1");
934         uiDefButS(block, ROW, B_SIMABRUSHCHANGE, "Aux AB2",             890,80,80,19, &Gip.current, 7.0, IMAGEPAINT_AUX2, 0, 0, "Auxiliary Air Brush2");        
935         uiDefButS(block, ROW, B_SIMABRUSHCHANGE, "Smear",               890,60,80,19, &Gip.current, 7.0, IMAGEPAINT_SMEAR, 0, 0, "Smear");      
936         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"); 
937         uiBlockEndAlign(block);
938
939         uiBlockBeginAlign(block);       
940         id= (ID*)Gip.clone.image;
941         std_libbuttons(block, 979, 40, 0, NULL, B_SIMACLONEBROWSE, ID_IM, 0, id, 0, &G.sima->menunr, 0, 0, B_SIMACLONEDELETE, 0, 0);
942         uiDefButF(block, NUMSLI, B_SIMABRUSHCHANGE, "B ",979,20,230,19, &Gip.clone.alpha , 0.0, 1.0, 0, 0, "Blend clone image");
943         uiBlockEndAlign(block);
944
945 #if 0
946         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");
947         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");
948 #endif
949         uiDefButBitS(block, TOG|BIT, IMAGEPAINT_TORUS, B_SIMABRUSHCHANGE, "Wrap", 890,1,50,19, &Gip.flag, 0, 0, 0, 0, "Enables torus wrapping");
950 }
951
952 static void image_panel_curves_reset(void *cumap_v, void *unused)
953 {
954         CurveMapping *cumap = cumap_v;
955         int a;
956         
957         for(a=0; a<CM_TOT; a++)
958                 curvemap_reset(cumap->cm+a, &cumap->clipr);
959         
960         cumap->black[0]=cumap->black[1]=cumap->black[2]= 0.0f;
961         cumap->white[0]=cumap->white[1]=cumap->white[2]= 1.0f;
962         curvemapping_set_black_white(cumap, NULL, NULL);
963         
964         curvemapping_changed(cumap, 0);
965         curvemapping_do_image(cumap, G.sima->image);
966         
967         allqueue(REDRAWIMAGE, 0);
968 }
969
970
971 static void image_panel_curves(short cntrl)     // IMAGE_HANDLER_PROPERTIES
972 {
973         uiBlock *block;
974         uiBut *bt;
975         
976         block= uiNewBlock(&curarea->uiblocks, "image_panel_curves", UI_EMBOSS, UI_HELV, curarea->win);
977         uiPanelControl(UI_PNL_SOLID | UI_PNL_CLOSE | cntrl);
978         uiSetPanelHandler(IMAGE_HANDLER_CURVES);  // for close and esc
979         if(uiNewPanel(curarea, block, "Curves", "Image", 10, 450, 318, 204)==0)
980                 return;
981         
982         if (G.sima->image && G.sima->image->ibuf) {
983                 rctf rect;
984                 
985                 if(G.sima->cumap==NULL)
986                         G.sima->cumap= curvemapping_add(4, 0.0f, 0.0f, 1.0f, 1.0f);
987                 
988                 rect.xmin= 110; rect.xmax= 310;
989                 rect.ymin= 10; rect.ymax= 200;
990                 curvemap_buttons(block, G.sima->cumap, 'c', B_SIMACURVES, B_SIMAGEDRAW, &rect);
991                 
992                 bt=uiDefBut(block, BUT, B_SIMARANGE, "Reset",   10, 160, 90, 19, NULL, 0.0f, 0.0f, 0, 0, "Reset Black/White point and curves");
993                 uiButSetFunc(bt, image_panel_curves_reset, G.sima->cumap, NULL);
994                 
995                 uiBlockBeginAlign(block);
996                 uiDefButF(block, NUM, B_SIMARANGE, "Min R:",    10, 120, 90, 19, G.sima->cumap->black, -1000.0f, 1000.0f, 10, 2, "Black level");
997                 uiDefButF(block, NUM, B_SIMARANGE, "Min G:",    10, 100, 90, 19, G.sima->cumap->black+1, -1000.0f, 1000.0f, 10, 2, "Black level");
998                 uiDefButF(block, NUM, B_SIMARANGE, "Min B:",    10, 80, 90, 19, G.sima->cumap->black+2, -1000.0f, 1000.0f, 10, 2, "Black level");
999                 
1000                 uiBlockBeginAlign(block);
1001                 uiDefButF(block, NUM, B_SIMARANGE, "Max R:",    10, 50, 90, 19, G.sima->cumap->white, -1000.0f, 1000.0f, 10, 2, "White level");
1002                 uiDefButF(block, NUM, B_SIMARANGE, "Max G:",    10, 30, 90, 19, G.sima->cumap->white+1, -1000.0f, 1000.0f, 10, 2, "White level");
1003                 uiDefButF(block, NUM, B_SIMARANGE, "Max B:",    10, 10, 90, 19, G.sima->cumap->white+2, -1000.0f, 1000.0f, 10, 2, "White level");
1004                 
1005         }
1006 }
1007
1008 /* are there curves? curves visible? and curves do something? */
1009 static int image_curves_active(ScrArea *sa)
1010 {
1011         SpaceImage *sima= sa->spacedata.first;
1012
1013         if(sima->cumap) {
1014                 if(curvemapping_RGBA_does_something(sima->cumap)) {
1015                         short a;
1016                         for(a=0; a<SPACE_MAXHANDLER; a+=2) {
1017                                 if(sima->blockhandler[a] == IMAGE_HANDLER_CURVES)
1018                                         return 1;
1019                         }
1020                 }
1021         }
1022         return 0;
1023 }
1024
1025 void image_preview_event(int event)
1026 {
1027         int exec= 0;
1028         
1029
1030         if(event==0) {
1031                 G.scene->r.scemode &= ~R_COMP_CROP;
1032                 exec= 1;
1033         }
1034         else if(event==2 || (G.scene->r.scemode & R_COMP_CROP)==0) {
1035                 if(image_preview_active(curarea, NULL, NULL)) {
1036                         G.scene->r.scemode |= R_COMP_CROP;
1037                         exec= 1;
1038                 }
1039         }
1040         
1041         if(exec) {
1042                 ScrArea *sa;
1043                 
1044                 ntreeCompositTagGenerators(G.scene->nodetree);
1045         
1046                 for(sa=G.curscreen->areabase.first; sa; sa= sa->next) {
1047                         if(sa->spacetype==SPACE_NODE) {
1048                                 addqueue(sa->win, UI_BUT_EVENT, B_NODE_TREE_EXEC);
1049                                 break;
1050                         }
1051                 }
1052         }       
1053 }
1054
1055
1056 /* nothing drawn here, we use it to store values */
1057 static void preview_cb(struct ScrArea *sa, struct uiBlock *block)
1058 {
1059         rctf dispf;
1060         rcti *disprect= &G.scene->r.disprect;
1061         int winx= (G.scene->r.size*G.scene->r.xsch)/100;
1062         int winy= (G.scene->r.size*G.scene->r.ysch)/100;
1063         short mval[2];
1064         
1065         if(G.scene->r.mode & R_BORDER) {
1066                 winx*= (G.scene->r.border.xmax - G.scene->r.border.xmin);
1067                 winy*= (G.scene->r.border.ymax - G.scene->r.border.ymin);
1068         }
1069         
1070         /* while dragging we don't update the rects */
1071         if(block->panel->flag & PNL_SELECT)
1072                 return;
1073         if(get_mbut() & M_MOUSE)
1074                 return;
1075
1076         BLI_init_rctf(&dispf, 15.0f, (block->maxx - block->minx)-15.0f, 15.0f, (block->maxy - block->miny)-15.0f);
1077         ui_graphics_to_window_rct(sa->win, &dispf, disprect);
1078         
1079         /* correction for gla draw */
1080         BLI_translate_rcti(disprect, -curarea->winrct.xmin, -curarea->winrct.ymin);
1081         
1082         calc_image_view(G.sima, 'p');
1083 //      printf("winrct %d %d %d %d\n", disprect->xmin, disprect->ymin,disprect->xmax, disprect->ymax);
1084         /* map to image space coordinates */
1085         mval[0]= disprect->xmin; mval[1]= disprect->ymin;
1086         areamouseco_to_ipoco(G.v2d, mval, &dispf.xmin, &dispf.ymin);
1087         mval[0]= disprect->xmax; mval[1]= disprect->ymax;
1088         areamouseco_to_ipoco(G.v2d, mval, &dispf.xmax, &dispf.ymax);
1089         
1090         /* map to render coordinates */
1091         disprect->xmin= dispf.xmin;
1092         disprect->xmax= dispf.xmax;
1093         disprect->ymin= dispf.ymin;
1094         disprect->ymax= dispf.ymax;
1095         
1096         CLAMP(disprect->xmin, 0, winx);
1097         CLAMP(disprect->xmax, 0, winx);
1098         CLAMP(disprect->ymin, 0, winy);
1099         CLAMP(disprect->ymax, 0, winy);
1100 //      printf("drawrct %d %d %d %d\n", disprect->xmin, disprect->ymin,disprect->xmax, disprect->ymax);
1101         
1102         image_preview_event(1);
1103 }
1104
1105 static int is_preview_allowed(ScrArea *cur)
1106 {
1107         ScrArea *sa;
1108
1109         for(sa=G.curscreen->areabase.first; sa; sa= sa->next) {
1110                 if(sa!=cur && sa->spacetype==SPACE_IMAGE) {
1111                         if(image_preview_active(sa, NULL, NULL))
1112                            return 0;
1113                 }
1114         }
1115         return 1;
1116 }
1117
1118 static void image_panel_preview(ScrArea *sa, short cntrl)       // IMAGE_HANDLER_PREVIEW
1119 {
1120         uiBlock *block;
1121         SpaceImage *sima= sa->spacedata.first;
1122         int ofsx, ofsy;
1123         
1124         if(is_preview_allowed(sa)==0) {
1125                 rem_blockhandler(sa, IMAGE_HANDLER_PREVIEW);
1126                 return;
1127         }
1128         
1129         block= uiNewBlock(&sa->uiblocks, "image_panel_preview", UI_EMBOSS, UI_HELV, sa->win);
1130         uiPanelControl(UI_PNL_SOLID | UI_PNL_CLOSE | UI_PNL_SCALE | cntrl);
1131         uiSetPanelHandler(IMAGE_HANDLER_PREVIEW);  // for close and esc
1132         
1133         ofsx= -150+(sa->winx/2)/sima->blockscale;
1134         ofsy= -100+(sa->winy/2)/sima->blockscale;
1135         if(uiNewPanel(sa, block, "Preview", "Image", ofsx, ofsy, 300, 200)==0) return;
1136         
1137         uiBlockSetDrawExtraFunc(block, preview_cb);
1138         
1139 }
1140
1141 static void image_blockhandlers(ScrArea *sa)
1142 {
1143         SpaceImage *sima= sa->spacedata.first;
1144         short a;
1145
1146         /* warning; blocks need to be freed each time, handlers dont remove  */
1147         uiFreeBlocksWin(&sa->uiblocks, sa->win);
1148         
1149         for(a=0; a<SPACE_MAXHANDLER; a+=2) {
1150                 switch(sima->blockhandler[a]) {
1151
1152                 case IMAGE_HANDLER_PROPERTIES:
1153                         image_panel_properties(sima->blockhandler[a+1]);
1154                         break;
1155                 case IMAGE_HANDLER_PAINT:
1156                         image_panel_paint(sima->blockhandler[a+1]);
1157                         break;          
1158                 case IMAGE_HANDLER_CURVES:
1159                         image_panel_curves(sima->blockhandler[a+1]);
1160                         break;          
1161                 case IMAGE_HANDLER_PREVIEW:
1162                         image_panel_preview(sa, sima->blockhandler[a+1]);
1163                         break;          
1164                 }
1165                 /* clear action value for event */
1166                 sima->blockhandler[a+1]= 0;
1167         }
1168         uiDrawBlocksPanels(sa, 0);
1169 }
1170
1171 static void imagespace_grid(SpaceImage *sima)
1172 {
1173         float gridsize, gridstep= 1.0f/32.0f;
1174         float fac, blendfac;
1175         
1176         gridsize= sima->zoom;
1177         
1178         if(gridsize<=0.0f) return;
1179         
1180         if(gridsize<1.0f) {
1181                 while(gridsize<1.0f) {
1182                         gridsize*= 4.0;
1183                         gridstep*= 4.0;
1184                 }
1185         }
1186         else {
1187                 while(gridsize>=4.0f) {
1188                         gridsize/= 4.0;
1189                         gridstep/= 4.0;
1190                 }
1191         }
1192         
1193         /* the fine resolution level */
1194         blendfac= 0.25*gridsize - floor(0.25*gridsize);
1195         CLAMP(blendfac, 0.0, 1.0);
1196         BIF_ThemeColorShade(TH_BACK, (int)(20.0*(1.0-blendfac)));
1197         
1198         fac= 0.0f;
1199         glBegin(GL_LINES);
1200         while(fac<1.0) {
1201                 glVertex2f(0.0f, fac);
1202                 glVertex2f(1.0f, fac);
1203                 glVertex2f(fac, 0.0f);
1204                 glVertex2f(fac, 1.0f);
1205                 fac+= gridstep;
1206         }
1207         
1208         /* the large resolution level */
1209         BIF_ThemeColor(TH_BACK);
1210         
1211         fac= 0.0f;
1212         while(fac<1.0) {
1213                 glVertex2f(0.0f, fac);
1214                 glVertex2f(1.0f, fac);
1215                 glVertex2f(fac, 0.0f);
1216                 glVertex2f(fac, 1.0f);
1217                 fac+= 4.0*gridstep;
1218         }
1219         glEnd();
1220         
1221 }
1222
1223 static void sima_draw_alpha_backdrop(SpaceImage *sima, float x1, float y1, float xsize, float ysize)
1224 {
1225         float tile= sima->zoom*15.0f;
1226         float x, y, maxx, maxy;
1227         
1228         glColor3ub(100, 100, 100);
1229         glRectf(x1, y1, x1 + sima->zoom*xsize, y1 + sima->zoom*ysize);
1230         glColor3ub(160, 160, 160);
1231         
1232         maxx= x1+sima->zoom*xsize;
1233         maxy= y1+sima->zoom*ysize;
1234         
1235         for(x=0; x<xsize; x+=30) {
1236                 for(y=0; y<ysize; y+=30) {
1237                         float fx= x1 + sima->zoom*x;
1238                         float fy= y1 + sima->zoom*y;
1239                         float tilex= tile, tiley= tile;
1240                         
1241                         if(fx+tile > maxx)
1242                                 tilex= maxx-fx;
1243                         if(fy+tile > maxy)
1244                                 tiley= maxy-fy;
1245                         
1246                         glRectf(fx, fy, fx + tilex, fy + tiley);
1247                 }
1248         }
1249         for(x=15; x<xsize; x+=30) {
1250                 for(y=15; y<ysize; y+=30) {
1251                         float fx= x1 + sima->zoom*x;
1252                         float fy= y1 + sima->zoom*y;
1253                         float tilex= tile, tiley= tile;
1254                         
1255                         if(fx+tile > maxx)
1256                                 tilex= maxx-fx;
1257                         if(fy+tile > maxy)
1258                                 tiley= maxy-fy;
1259                         
1260                         glRectf(fx, fy, fx + tilex, fy + tiley);
1261                 }
1262         }
1263 }
1264
1265 static void sima_draw_alpha_pixels(float x1, float y1, int rectx, int recty, unsigned int *recti)
1266 {
1267         
1268         /* swap bytes, so alpha is most significant one, then just draw it as luminance int */
1269         glPixelStorei(GL_UNPACK_SWAP_BYTES, 1);
1270         glaDrawPixelsSafe(x1, y1, rectx, recty, rectx, GL_LUMINANCE, GL_UNSIGNED_INT, recti);
1271         glPixelStorei(GL_UNPACK_SWAP_BYTES, 0);
1272 }
1273
1274 static void sima_draw_alpha_pixelsf(float x1, float y1, int rectx, int recty, float *rectf)
1275 {
1276         float *trectf= MEM_mallocN(rectx*recty*4, "temp");
1277         int a, b;
1278         
1279         for(a= rectx*recty -1, b= 4*a+3; a>=0; a--, b-=4)
1280                 trectf[a]= rectf[b];
1281         
1282         glaDrawPixelsSafe(x1, y1, rectx, recty, rectx, GL_LUMINANCE, GL_FLOAT, trectf);
1283         MEM_freeN(trectf);
1284         /* ogl trick below is slower... (on ATI 9600) */
1285 //      glColorMask(1, 0, 0, 0);
1286 //      glaDrawPixelsSafe(x1, y1, rectx, recty, rectx, GL_RGBA, GL_FLOAT, rectf+3);
1287 //      glColorMask(0, 1, 0, 0);
1288 //      glaDrawPixelsSafe(x1, y1, rectx, recty, rectx, GL_RGBA, GL_FLOAT, rectf+2);
1289 //      glColorMask(0, 0, 1, 0);
1290 //      glaDrawPixelsSafe(x1, y1, rectx, recty, rectx, GL_RGBA, GL_FLOAT, rectf+1);
1291 //      glColorMask(1, 1, 1, 1);
1292 }
1293
1294 static void sima_draw_zbuf_pixels(float x1, float y1, int rectx, int recty, int *recti)
1295 {
1296         if(recti==NULL)
1297                 return;
1298         
1299         /* zbuffer values are signed, so we need to shift color range */
1300         glPixelTransferf(GL_RED_SCALE, 0.5f);
1301         glPixelTransferf(GL_GREEN_SCALE, 0.5f);
1302         glPixelTransferf(GL_BLUE_SCALE, 0.5f);
1303         glPixelTransferf(GL_RED_BIAS, 0.5f);
1304         glPixelTransferf(GL_GREEN_BIAS, 0.5f);
1305         glPixelTransferf(GL_BLUE_BIAS, 0.5f);
1306         
1307         glaDrawPixelsSafe(x1, y1, rectx, recty, rectx, GL_LUMINANCE, GL_INT, recti);
1308         
1309         glPixelTransferf(GL_RED_SCALE, 1.0f);
1310         glPixelTransferf(GL_GREEN_SCALE, 1.0f);
1311         glPixelTransferf(GL_BLUE_SCALE, 1.0f);
1312         glPixelTransferf(GL_RED_BIAS, 0.0f);
1313         glPixelTransferf(GL_GREEN_BIAS, 0.0f);
1314         glPixelTransferf(GL_BLUE_BIAS, 0.0f);
1315 }
1316
1317 static void sima_draw_zbuffloat_pixels(float x1, float y1, int rectx, int recty, float *rect_float)
1318 {
1319         float bias, scale, *rectf, clipend;
1320         int a;
1321         
1322         if(rect_float==NULL)
1323                 return;
1324         
1325         if(G.scene->camera && G.scene->camera->type==OB_CAMERA) {
1326                 bias= ((Camera *)G.scene->camera->data)->clipsta;
1327                 clipend= ((Camera *)G.scene->camera->data)->clipend;
1328                 scale= 1.0f/(clipend-bias);
1329         }
1330         else {
1331                 bias= 0.1f;
1332                 scale= 0.01f;
1333                 clipend= 100.0f;
1334         }
1335         
1336         rectf= MEM_mallocN(rectx*recty*4, "temp");
1337         for(a= rectx*recty -1; a>=0; a--) {
1338                 if(rect_float[a]>clipend)
1339                         rectf[a]= 0.0f;
1340                 else if(rect_float[a]<bias)
1341                         rectf[a]= 1.0f;
1342                 else {
1343                         rectf[a]= 1.0f - (rect_float[a]-bias)*scale;
1344                         rectf[a]*= rectf[a];
1345                 }
1346         }
1347         glaDrawPixelsSafe(x1, y1, rectx, recty, rectx, GL_LUMINANCE, GL_FLOAT, rectf);
1348         
1349         MEM_freeN(rectf);
1350 }
1351
1352 void drawimagespace(ScrArea *sa, void *spacedata)
1353 {
1354         SpaceImage *sima= spacedata;
1355         ImBuf *ibuf= NULL;
1356         float col[3];
1357         unsigned int *rect;
1358         int x1, y1;
1359         short sx, sy, dx, dy;
1360         
1361                 /* If derived data is used then make sure that object
1362                  * is up-to-date... might not be the case because updates
1363                  * are normally done in drawview and could get here before
1364                  * drawing a View3D.
1365                  */
1366         if (!G.obedit && OBACT && (sima->flag & SI_DRAWSHADOW)) {
1367                 object_handle_update(OBACT);
1368         }
1369
1370         BIF_GetThemeColor3fv(TH_BACK, col);
1371         glClearColor(col[0], col[1], col[2], 0.0);
1372         glClear(GL_COLOR_BUFFER_BIT);
1373
1374         bwin_clear_viewmat(sa->win);    /* clear buttons view */
1375         glLoadIdentity();
1376         
1377         what_image(sima);
1378         
1379         if(sima->image) {
1380                 if(sima->image->ibuf==NULL) {
1381                         load_image(sima->image, IB_rect, G.sce, G.scene->r.cfra);
1382                         scrarea_queue_headredraw(sa);   /* update header for image options */
1383                 }       
1384                 tag_image_time(sima->image);
1385                 ibuf= sima->image->ibuf;
1386         }
1387         
1388         if(ibuf==NULL || (ibuf->rect==NULL && ibuf->rect_float==NULL)) {
1389                 calc_image_view(sima, 'f');
1390                 myortho2(G.v2d->cur.xmin, G.v2d->cur.xmax, G.v2d->cur.ymin, G.v2d->cur.ymax);
1391                 BIF_ThemeColorShade(TH_BACK, 20);
1392                 glRectf(0.0, 0.0, 1.0, 1.0);
1393                 imagespace_grid(sima);
1394                 draw_tfaces();
1395         }
1396         else {
1397                 float xim, yim, xoffs=0.0f, yoffs= 0.0f;
1398                 
1399                 if(image_preview_active(curarea, &xim, &yim)) {
1400                         xoffs= G.scene->r.disprect.xmin;
1401                         yoffs= G.scene->r.disprect.ymin;
1402                         glColor3ub(0,0,0);
1403                         calc_image_view(sima, 'f');     
1404                         myortho2(G.v2d->cur.xmin, G.v2d->cur.xmax, G.v2d->cur.ymin, G.v2d->cur.ymax);
1405                         glRectf(0.0f, 0.0f, 1.0f, 1.0f);
1406                         glLoadIdentity();
1407                 }
1408                 else {
1409                         xim= ibuf->x; yim= ibuf->y;
1410                 }
1411                 
1412                 /* calc location */
1413                 x1= sima->zoom*xoffs + (curarea->winx-sima->zoom*xim)/2;
1414                 y1= sima->zoom*yoffs + (curarea->winy-sima->zoom*yim)/2;
1415         
1416                 x1-= sima->zoom*sima->xof;
1417                 y1-= sima->zoom*sima->yof;
1418                 
1419                 /* needed for gla draw */
1420                 glaDefine2DArea(&curarea->winrct);
1421                 glPixelZoom((float)sima->zoom, (float)sima->zoom);
1422                                 
1423                 if(sima->flag & SI_EDITTILE) {
1424                         glaDrawPixelsSafe(x1, y1, ibuf->x, ibuf->y, ibuf->x, GL_RGBA, GL_UNSIGNED_BYTE, ibuf->rect);
1425                         
1426                         glPixelZoom(1.0, 1.0);
1427                         
1428                         dx= ibuf->x/sima->image->xrep;
1429                         dy= ibuf->y/sima->image->yrep;
1430                         sy= (sima->curtile / sima->image->xrep);
1431                         sx= sima->curtile - sy*sima->image->xrep;
1432         
1433                         sx*= dx;
1434                         sy*= dy;
1435                         
1436                         calc_image_view(sima, 'p');     /* pixel */
1437                         myortho2(G.v2d->cur.xmin, G.v2d->cur.xmax, G.v2d->cur.ymin, G.v2d->cur.ymax);
1438                         
1439                         cpack(0x0);
1440                         glPolygonMode(GL_FRONT_AND_BACK, GL_LINE); glRects(sx,  sy,  sx+dx-1,  sy+dy-1); glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
1441                         cpack(0xFFFFFF);
1442                         glPolygonMode(GL_FRONT_AND_BACK, GL_LINE); glRects(sx+1,  sy+1,  sx+dx,  sy+dy); glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
1443                 }
1444                 else if(sima->mode==SI_TEXTURE) {
1445                         
1446                         if(sima->image->tpageflag & IMA_TILES) {
1447                                 
1448                                 /* just leave this a while */
1449                                 if(sima->image->xrep<1) return;
1450                                 if(sima->image->yrep<1) return;
1451                                 
1452                                 if(sima->curtile >= sima->image->xrep*sima->image->yrep) 
1453                                         sima->curtile = sima->image->xrep*sima->image->yrep - 1; 
1454                                 
1455                                 dx= ibuf->x/sima->image->xrep;
1456                                 dy= ibuf->y/sima->image->yrep;
1457                                 
1458                                 sy= (sima->curtile / sima->image->xrep);
1459                                 sx= sima->curtile - sy*sima->image->xrep;
1460                 
1461                                 sx*= dx;
1462                                 sy*= dy;
1463                                 
1464                                 rect= get_part_from_ibuf(ibuf, sx, sy, sx+dx, sy+dy);
1465                                 
1466                                 /* rect= ibuf->rect; */
1467                                 for(sy= 0; sy+dy<=ibuf->y; sy+= dy) {
1468                                         for(sx= 0; sx+dx<=ibuf->x; sx+= dx) {
1469                                                 glaDrawPixelsSafe(x1+sx*sima->zoom, y1+sy*sima->zoom, dx, dy, dx, GL_RGBA, GL_UNSIGNED_BYTE, rect);
1470                                         }
1471                                 }
1472                                 
1473                                 MEM_freeN(rect);
1474                         }
1475                         else {
1476                                 /* this part is generic image display */
1477                                 
1478                                 if(sima->flag & SI_SHOW_ALPHA) {
1479                                         if(ibuf->rect)
1480                                                 sima_draw_alpha_pixels(x1, y1, ibuf->x, ibuf->y, ibuf->rect);
1481                                         else if(ibuf->rect_float)
1482                                                 sima_draw_alpha_pixelsf(x1, y1, ibuf->x, ibuf->y, ibuf->rect_float);
1483                                 }
1484                                 else if(sima->flag & SI_SHOW_ZBUF) {
1485                                         if(ibuf->zbuf)
1486                                                 sima_draw_zbuf_pixels(x1, y1, ibuf->x, ibuf->y, ibuf->zbuf);
1487                                         else
1488                                                 sima_draw_zbuffloat_pixels(x1, y1, ibuf->x, ibuf->y, ibuf->zbuf_float);
1489                                 }
1490                                 else {
1491                                         if(sima->flag & SI_USE_ALPHA) {
1492                                                 sima_draw_alpha_backdrop(sima, x1, y1, (float)ibuf->x, (float)ibuf->y);
1493                                                 glEnable(GL_BLEND);
1494                                         }
1495                                         
1496                                         /* detect if we need to redo the curve map. 
1497                                            ibuf->rect is zero for compositor and render results after change 
1498                                            convert to 32 bits always... drawing float rects isnt supported well (atis)
1499                                         */
1500                                         if(ibuf->rect_float) {
1501                                                 if(ibuf->rect==NULL) {
1502                                                         if(image_curves_active(sa))
1503                                                                 curvemapping_do_image(G.sima->cumap, G.sima->image);
1504                                                         else 
1505                                                                 IMB_rect_from_float(ibuf);
1506                                                 }
1507                                         }
1508
1509                                         if(ibuf->rect)
1510                                                 glaDrawPixelsSafe(x1, y1, ibuf->x, ibuf->y, ibuf->x, GL_RGBA, GL_UNSIGNED_BYTE, ibuf->rect);
1511 //                                      else
1512 //                                              glaDrawPixelsSafe(x1, y1, ibuf->x, ibuf->y, ibuf->x, GL_RGBA, GL_FLOAT, ibuf->rect_float);
1513                                         
1514                                         if(sima->flag & SI_USE_ALPHA)
1515                                                 glDisable(GL_BLEND);
1516                                 }
1517                         }
1518                         
1519                         if(Gip.current == IMAGEPAINT_CLONE) {
1520                                 int w, h;
1521                                 unsigned char *clonerect;
1522
1523                                 /* this is not very efficient, but glDrawPixels doesn't allow
1524                                    drawing with alpha */
1525                                 clonerect= alloc_alpha_clone_image(&w, &h);
1526
1527                                 if(clonerect) {
1528                                         int offx, offy;
1529                                         offx = sima->zoom*ibuf->x * + Gip.clone.offset[0];
1530                                         offy = sima->zoom*ibuf->y * + Gip.clone.offset[1];
1531
1532                                         glEnable(GL_BLEND);
1533                                         glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
1534                                         glaDrawPixelsSafe(x1 + offx, y1 + offy, w, h, w, GL_RGBA, GL_UNSIGNED_BYTE, clonerect);
1535                                         glDisable(GL_BLEND);
1536
1537                                         MEM_freeN(clonerect);
1538                                 }
1539                         }
1540                         
1541                         glPixelZoom(1.0, 1.0);
1542                         
1543                         draw_tfaces();
1544                 }
1545         
1546                 calc_image_view(sima, 'f');     /* float */
1547         }
1548
1549         draw_image_transform(ibuf);
1550
1551         mywinset(sa->win);      /* restore scissor after gla call... */
1552         myortho2(-0.375, sa->winx-0.375, -0.375, sa->winy-0.375);
1553
1554         draw_image_view_tool();
1555         draw_image_view_icon();
1556         draw_area_emboss(sa);
1557
1558         /* it is important to end a view in a transform compatible with buttons */
1559         bwin_scalematrix(sa->win, sima->blockscale, sima->blockscale, sima->blockscale);
1560         image_blockhandlers(sa);
1561
1562         curarea->win_swap= WIN_BACK_OK;
1563 }
1564
1565 static void image_zoom_power_of_two(void)
1566 {
1567         /* Make zoom a power of 2 */
1568
1569         G.sima->zoom = 1 / G.sima->zoom;
1570         G.sima->zoom = log(G.sima->zoom) / log(2);
1571         G.sima->zoom = ceil(G.sima->zoom);
1572         G.sima->zoom = pow(2, G.sima->zoom);
1573         G.sima->zoom = 1 / G.sima->zoom;
1574 }
1575
1576 static void image_zoom_set_factor(float zoomfac)
1577 {
1578         SpaceImage *sima= curarea->spacedata.first;
1579         int width, height;
1580
1581         if (zoomfac <= 0.0f)
1582                 return;
1583
1584         sima->zoom *= zoomfac;
1585
1586         if (sima->zoom > 0.1f && sima->zoom < 4.0f)
1587                 return;
1588
1589         /* check zoom limits */
1590
1591         calc_image_view(G.sima, 'p');
1592         width= 256;
1593         height= 256;
1594         if (sima->image) {
1595                 if (sima->image->ibuf) {
1596                         float xim, yim;
1597                         /* I know a bit weak... but preview uses not actual image size */
1598                         if(image_preview_active(curarea, &xim, &yim)) {
1599                                 width= (int) xim;
1600                                 height= (int) yim;
1601                         }
1602                         else {
1603                                 width= sima->image->ibuf->x;
1604                                 height= sima->image->ibuf->y;
1605                         }
1606                 }
1607         }
1608         width *= sima->zoom;
1609         height *= sima->zoom;
1610
1611         if ((width < 4) && (height < 4))
1612                 sima->zoom /= zoomfac;
1613         else if((curarea->winrct.xmax - curarea->winrct.xmin) <= sima->zoom)
1614                 sima->zoom /= zoomfac;
1615         else if((curarea->winrct.ymax - curarea->winrct.ymin) <= sima->zoom)
1616                 sima->zoom /= zoomfac;
1617 }
1618
1619 void image_viewmove(int mode)
1620 {
1621         short mval[2], mvalo[2], zoom0;
1622         
1623         getmouseco_sc(mvalo);
1624         zoom0= G.sima->zoom;
1625         
1626         while(get_mbut()&(L_MOUSE|M_MOUSE)) {
1627
1628                 getmouseco_sc(mval);
1629
1630                 if(mvalo[0]!=mval[0] || mvalo[1]!=mval[1]) {
1631                 
1632                         if(mode==0) {
1633                                 G.sima->xof += (mvalo[0]-mval[0])/G.sima->zoom;
1634                                 G.sima->yof += (mvalo[1]-mval[1])/G.sima->zoom;
1635                         }
1636                         else if (mode==1) {
1637                                 float factor;
1638
1639                                 factor= 1.0+(float)(mvalo[0]-mval[0]+mvalo[1]-mval[1])/300.0;
1640                                 image_zoom_set_factor(factor);
1641                         }
1642
1643                         mvalo[0]= mval[0];
1644                         mvalo[1]= mval[1];
1645                         
1646                         scrarea_do_windraw(curarea);
1647                         screen_swapbuffers();
1648                 }
1649                 else BIF_wait_for_statechange();
1650         }
1651         
1652         if(image_preview_active(curarea, NULL, NULL)) {
1653                 /* recalculates new preview rect */
1654                 scrarea_do_windraw(curarea);
1655                 image_preview_event(2);
1656         }
1657 }
1658
1659 void image_viewzoom(unsigned short event, int invert)
1660 {
1661         SpaceImage *sima= curarea->spacedata.first;
1662
1663         if(event==WHEELDOWNMOUSE || event==PADMINUS)
1664                 image_zoom_set_factor((U.uiflag & USER_WHEELZOOMDIR)? 1.25: 0.8);
1665         else if(event==WHEELUPMOUSE || event==PADPLUSKEY)
1666                 image_zoom_set_factor((U.uiflag & USER_WHEELZOOMDIR)? 0.8: 1.25);
1667         else if(event==PAD1)
1668                 sima->zoom= 1.0;
1669         else if(event==PAD2)
1670                 sima->zoom= (invert)? 2.0: 0.5;
1671         else if(event==PAD4)
1672                 sima->zoom= (invert)? 4.0: 0.25;
1673         else if(event==PAD8)
1674                 sima->zoom= (invert)? 8.0: 0.125;
1675         
1676         if(image_preview_active(curarea, NULL, NULL)) {
1677                 /* recalculates new preview rect */
1678                 scrarea_do_windraw(curarea);
1679                 image_preview_event(2);
1680         }
1681 }
1682
1683 /**
1684  * Updates the fields of the View2D member of the SpaceImage struct.
1685  * Default behavior is to reset the position of the image and set the zoom to 1
1686  * If the image will not fit within the window rectangle, the zoom is adjusted
1687  *
1688  * @return    void
1689  *   
1690  */
1691 void image_home(void)
1692 {
1693         int width, height, imgwidth, imgheight;
1694         float zoomX, zoomY;
1695
1696         if (curarea->spacetype != SPACE_IMAGE) return;
1697
1698         if ((G.sima->image == 0) || (G.sima->image->ibuf == 0)) {
1699                 imgwidth = 256;
1700                 imgheight = 256;
1701         }
1702         else {
1703                 imgwidth = G.sima->image->ibuf->x;
1704                 imgheight = G.sima->image->ibuf->y;
1705         }
1706
1707         /* Check if the image will fit in the image with zoom==1 */
1708         width = curarea->winx;
1709         height = curarea->winy;
1710         if (((imgwidth >= width) || (imgheight >= height)) && 
1711                 ((width > 0) && (height > 0))) {
1712                 /* Find the zoom value that will fit the image in the image space */
1713                 zoomX = ((float)width) / ((float)imgwidth);
1714                 zoomY = ((float)height) / ((float)imgheight);
1715                 G.sima->zoom= MIN2(zoomX, zoomY);
1716
1717                 image_zoom_power_of_two();
1718         }
1719         else {
1720                 G.sima->zoom= (float)1;
1721         }
1722
1723         G.sima->xof= G.sima->yof= 0;
1724         
1725         calc_image_view(G.sima, 'p');
1726         
1727         scrarea_queue_winredraw(curarea);
1728 }
1729
1730 void image_viewcentre(void)
1731 {
1732         float size, min[2], max[2], d[2], xim=256.0f, yim=256.0f;
1733
1734         if( is_uv_tface_editing_allowed()==0 ) return;
1735
1736         if (!minmax_tface_uv(min, max)) return;
1737
1738         if(G.sima->image && G.sima->image->ibuf) {
1739                 xim= G.sima->image->ibuf->x;
1740                 yim= G.sima->image->ibuf->y;
1741         }
1742
1743         G.sima->xof= ((min[0] + max[0])*0.5f - 0.5f)*xim;
1744         G.sima->yof= ((min[1] + max[1])*0.5f - 0.5f)*yim;
1745
1746         d[0] = max[0] - min[0];
1747         d[1] = max[1] - min[1];
1748         size= 0.5*MAX2(d[0], d[1])*MAX2(xim, yim)/256.0f;
1749         
1750         if(size<=0.01) size= 0.01;
1751
1752         G.sima->zoom= 0.7/size;
1753
1754         calc_image_view(G.sima, 'p');
1755
1756         scrarea_queue_winredraw(curarea);
1757 }
1758