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