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