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