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