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