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