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