4 * ***** BEGIN GPL LICENSE BLOCK *****
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.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software Foundation,
18 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20 * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
21 * All rights reserved.
23 * The Original Code is: all of this file.
25 * Contributor(s): Blender Foundation, 2002-2006
27 * ***** END GPL LICENSE BLOCK *****
42 #include "MEM_guardedalloc.h"
44 #include "BLI_arithb.h"
45 #include "BLI_blenlib.h"
46 #include "BLI_editVert.h"
48 #include "IMB_imbuf.h"
49 #include "IMB_imbuf_types.h"
51 #include "DNA_brush_types.h"
52 #include "DNA_camera_types.h"
53 #include "DNA_color_types.h"
54 #include "DNA_image_types.h"
55 #include "DNA_mesh_types.h"
56 #include "DNA_meshdata_types.h"
57 #include "DNA_node_types.h"
58 #include "DNA_object_types.h"
59 #include "DNA_packedFile_types.h"
60 #include "DNA_scene_types.h"
61 #include "DNA_screen_types.h"
62 #include "DNA_space_types.h"
63 #include "DNA_texture_types.h"
64 #include "DNA_userdef_types.h"
66 #include "BKE_brush.h"
67 #include "BKE_colortools.h"
68 #include "BKE_utildefines.h"
69 #include "BKE_global.h"
70 #include "BKE_library.h"
74 #include "BKE_image.h"
75 #include "BKE_DerivedMesh.h"
76 #include "BKE_displist.h"
77 #include "BKE_object.h"
79 #include "BDR_editface.h"
80 #include "BDR_drawobject.h"
81 #include "BDR_drawmesh.h"
82 #include "BDR_imagepaint.h"
84 #include "BIF_cursors.h"
86 #include "BIF_graphics.h"
87 #include "BIF_mywindow.h"
88 #include "BIF_drawimage.h"
89 #include "BIF_resources.h"
90 #include "BIF_interface.h"
91 #include "BIF_interface_icons.h"
92 #include "BIF_editsima.h"
93 #include "BIF_glutil.h"
94 #include "BIF_renderwin.h"
95 #include "BIF_space.h"
96 #include "BIF_screen.h"
97 #include "BIF_toolbox.h"
98 #include "BIF_transform.h"
100 #include "BSE_drawipo.h"
101 #include "BSE_drawview.h"
102 #include "BSE_filesel.h"
103 #include "BSE_headerbuttons.h"
104 #include "BSE_node.h"
105 #include "BSE_trans_types.h"
106 #include "BSE_view.h"
108 #include "RE_pipeline.h"
111 #include "PIL_time.h"
114 #include "mydevice.h"
116 #include "butspace.h" // event codes
119 #include "interface.h" /* bad.... but preview code needs UI info. Will solve... (ton) */
121 static unsigned char *alloc_alpha_clone_image(int *width, int *height)
123 Brush *brush = G.scene->toolsettings->imapaint.brush;
125 unsigned int size, alpha;
126 unsigned char *rect, *cp;
128 if(!brush || !brush->clone.image)
131 ibuf= BKE_image_get_ibuf(brush->clone.image, NULL);
133 if(!ibuf || !ibuf->rect)
136 rect= MEM_dupallocN(ibuf->rect);
143 size= (*width)*(*height);
144 alpha= (unsigned char)255*brush->clone.alpha;
155 static int image_preview_active(ScrArea *sa, float *xim, float *yim)
157 SpaceImage *sima= sa->spacedata.first;
159 /* only when compositor shows, and image handler set */
160 if(sima->image && sima->image->type==IMA_TYPE_COMPOSITE) {
163 for(a=0; a<SPACE_MAXHANDLER; a+=2) {
164 if(sima->blockhandler[a] == IMAGE_HANDLER_PREVIEW) {
165 if(xim) *xim= (G.scene->r.size*G.scene->r.xsch)/100;
166 if(yim) *yim= (G.scene->r.size*G.scene->r.ysch)/100;
176 * Sets up the fields of the View2D member of the SpaceImage struct
177 * This routine can be called in two modes:
178 * mode == 'f': float mode (0.0 - 1.0)
179 * mode == 'p': pixel mode (0 - size)
181 * @param sima the image space to update
182 * @param mode the mode to use for the update
186 void calc_image_view(SpaceImage *sima, char mode)
188 float xim=256, yim=256;
191 if(image_preview_active(curarea, &xim, &yim));
192 else if(sima->image) {
193 ImBuf *ibuf= BKE_image_get_ibuf(sima->image, &sima->iuser);
194 float xuser_asp, yuser_asp;
196 aspect_sima(sima, &xuser_asp, &yuser_asp);
198 xim= ibuf->x * xuser_asp;
199 yim= ibuf->y * yuser_asp;
201 else if( sima->image->type==IMA_TYPE_R_RESULT ) {
202 /* not very important, just nice */
203 xim= (G.scene->r.xsch*G.scene->r.size)/100;
204 yim= (G.scene->r.ysch*G.scene->r.size)/100;
208 sima->v2d.tot.xmin= 0;
209 sima->v2d.tot.ymin= 0;
210 sima->v2d.tot.xmax= xim;
211 sima->v2d.tot.ymax= yim;
213 sima->v2d.mask.xmin= sima->v2d.mask.ymin= 0;
214 sima->v2d.mask.xmax= curarea->winx;
215 sima->v2d.mask.ymax= curarea->winy;
218 /* Which part of the image space do we see? */
219 /* Same calculation as in lrectwrite: area left and down*/
220 x1= curarea->winrct.xmin+(curarea->winx-sima->zoom*xim)/2;
221 y1= curarea->winrct.ymin+(curarea->winy-sima->zoom*yim)/2;
223 x1-= sima->zoom*sima->xof;
224 y1-= sima->zoom*sima->yof;
226 /* relative display right */
227 sima->v2d.cur.xmin= ((curarea->winrct.xmin - (float)x1)/sima->zoom);
228 sima->v2d.cur.xmax= sima->v2d.cur.xmin + ((float)curarea->winx/sima->zoom);
230 /* relative display left */
231 sima->v2d.cur.ymin= ((curarea->winrct.ymin-(float)y1)/sima->zoom);
232 sima->v2d.cur.ymax= sima->v2d.cur.ymin + ((float)curarea->winy/sima->zoom);
235 sima->v2d.cur.xmin/= xim;
236 sima->v2d.cur.xmax/= xim;
237 sima->v2d.cur.ymin/= yim;
238 sima->v2d.cur.ymax/= yim;
242 /* check for facelesect, and set active image */
243 void what_image(SpaceImage *sima)
247 if( (sima->mode!=SI_TEXTURE) ||
248 (sima->image && sima->image->source==IMA_SRC_VIEWER) ||
254 /* viewer overrides faceselect */
258 activetf = get_active_mtface(NULL, NULL, 1); /* partially selected face is ok */
260 if(activetf && activetf->mode & TF_TEX) {
262 sima->image= activetf->tpage;
264 if(sima->flag & SI_EDITTILE);
265 else sima->curtile= activetf->tile;
268 if(activetf->mode & TF_TILES)
269 sima->image->tpageflag |= IMA_TILES;
270 else sima->image->tpageflag &= ~IMA_TILES;
275 /* after a what_image(), this call will give ibufs, includes the spare image */
276 ImBuf *imagewindow_get_ibuf(SpaceImage *sima)
280 /* check for spare */
281 if(sima->image->type==IMA_TYPE_R_RESULT && sima->showspare)
284 return BKE_image_get_ibuf(sima->image, &sima->iuser);
289 extern int EM_texFaceCheck(void); /* from editmesh.c */
290 /* called to assign images to UV faces */
291 void image_changed(SpaceImage *sima, Image *image)
294 EditMesh *em = G.editMesh;
298 sima->flag &= ~SI_DRAWTOOL;
300 if(sima->mode!=SI_TEXTURE || !EM_texFaceCheck())
303 /* skip assigning these procedural images... */
304 if(image && (image->type==IMA_TYPE_R_RESULT || image->type==IMA_TYPE_COMPOSITE))
307 for (efa= em->faces.first; efa; efa= efa->next) {
308 tface = CustomData_em_get(&em->fdata, efa->data, CD_MTFACE);
309 if (efa->h==0 && efa->f & SELECT) {
312 tface->mode |= TF_TEX;
314 if(image->tpageflag & IMA_TILES) tface->mode |= TF_TILES;
315 else tface->mode &= ~TF_TILES;
317 if(image->id.us==0) id_us_plus(&image->id);
318 else id_lib_extern(&image->id);
321 tface->mode &= ~TF_TEX;
325 /* change the space image after because SIMA_FACEDRAW_CHECK uses the space image
326 * to check if the face is displayed in UV-localview */
329 object_uvs_changed(OBACT);
330 allqueue(REDRAWBUTSEDIT, 0);
333 * dotile - 1, set the tile flag (from the space image)
334 * 2, set the tile index for the faces.
336 void image_set_tile(SpaceImage *sima, int dotile)
339 EditMesh *em = G.editMesh;
342 if(!sima->image || sima->mode!=SI_TEXTURE || !EM_texFaceCheck())
345 /* skip assigning these procedural images... */
346 if(sima->image && (sima->image->type==IMA_TYPE_R_RESULT || sima->image->type==IMA_TYPE_COMPOSITE))
349 for (efa= em->faces.first; efa; efa= efa->next) {
350 tface = CustomData_em_get(&em->fdata, efa->data, CD_MTFACE);
351 if (efa->h==0 && efa->f & SELECT) {
354 if (sima->image->tpageflag & IMA_TILES) {
355 tface->mode |= TF_TILES;
357 tface->mode &= ~TF_TILES;
359 } else if (dotile==2) {
361 tface->tile= sima->curtile;
365 object_uvs_changed(OBACT);
366 allqueue(REDRAWBUTSEDIT, 0);
371 void uvco_to_areaco(float *vec, short *mval)
377 x= (vec[0] - G.v2d->cur.xmin)/(G.v2d->cur.xmax-G.v2d->cur.xmin);
378 y= (vec[1] - G.v2d->cur.ymin)/(G.v2d->cur.ymax-G.v2d->cur.ymin);
380 if(x>=0.0 && x<=1.0) {
381 if(y>=0.0 && y<=1.0) {
382 mval[0]= G.v2d->mask.xmin + x*(G.v2d->mask.xmax-G.v2d->mask.xmin);
383 mval[1]= G.v2d->mask.ymin + y*(G.v2d->mask.ymax-G.v2d->mask.ymin);
388 void uvco_to_areaco_noclip(float *vec, int *mval)
394 x= (vec[0] - G.v2d->cur.xmin)/(G.v2d->cur.xmax-G.v2d->cur.xmin);
395 y= (vec[1] - G.v2d->cur.ymin)/(G.v2d->cur.ymax-G.v2d->cur.ymin);
397 x= G.v2d->mask.xmin + x*(G.v2d->mask.xmax-G.v2d->mask.xmin);
398 y= G.v2d->mask.ymin + y*(G.v2d->mask.ymax-G.v2d->mask.ymin);
404 static void drawcursor_sima(float xuser_asp, float yuser_asp)
409 transform_width_height_tface_uv(&wi, &hi);
410 w = (((float)wi)/256.0f)*G.sima->zoom * xuser_asp;
411 h = (((float)hi)/256.0f)*G.sima->zoom * yuser_asp;
414 glTranslatef(G.v2d->cursor[0], G.v2d->cursor[1], 0.0f);
415 fdrawline(-0.05/w, 0, 0, 0.05/h);
416 fdrawline(0, 0.05/h, 0.05/w, 0);
417 fdrawline(0.05/w, 0, 0, -0.05/h);
418 fdrawline(0, -0.05/h, -0.05/w, 0);
422 fdrawline(-0.05/w, 0, 0, 0.05/h);
423 fdrawline(0, 0.05/h, 0.05/w, 0);
424 fdrawline(0.05/w, 0, 0, -0.05/h);
425 fdrawline(0, -0.05/h, -0.05/w, 0);
430 fdrawline(-0.020/w, 0, -0.1/w, 0);
431 fdrawline(0.1/w, 0, .020/w, 0);
432 fdrawline(0, -0.020/h, 0, -0.1/h);
433 fdrawline(0, 0.1/h, 0, 0.020/h);
437 fdrawline(-0.020/w, 0, -0.1/w, 0);
438 fdrawline(0.1/w, 0, .020/w, 0);
439 fdrawline(0, -0.020/h, 0, -0.1/h);
440 fdrawline(0, 0.1/h, 0, 0.020/h);
442 glTranslatef(-G.v2d->cursor[0], -G.v2d->cursor[1], 0.0f);
446 /* draws uv's in the image space */
450 void draw_uvs_sima(void)
452 MTFace *tface,*activetface = NULL;
453 EditMesh *em = G.editMesh;
456 char col1[4], col2[4];
457 float pointsize= BIF_GetThemeValuef(TH_VERTEX_SIZE);
459 if (!G.obedit || !CustomData_has_layer(&em->fdata, CD_MTFACE))
462 calc_image_view(G.sima, 'f'); /* float */
463 myortho2(G.v2d->cur.xmin, G.v2d->cur.xmax, G.v2d->cur.ymin, G.v2d->cur.ymax);
467 if(G.sima->flag & SI_DRAWTOOL || G.sima->flag & SI_DRAWSHADOW) {
468 glColor3ub(112, 112, 112);
469 for (efa= em->faces.first; efa; efa= efa->next) {
470 tface= CustomData_em_get(&em->fdata, efa->data, CD_MTFACE);
471 if (SIMA_FACEDRAW_CHECK(efa, tface)) {
474 glBegin(GL_LINE_LOOP);
475 glVertex2fv(tface->uv[0]);
476 glVertex2fv(tface->uv[1]);
477 glVertex2fv(tface->uv[2]);
478 if(efa->v4) glVertex2fv(tface->uv[3]);
484 if(G.sima->flag & SI_DRAWTOOL)
485 return; /* only draw shadow mesh */
488 /* would be nice to do this within a draw loop but most below are optional, so it would involve too many checks */
489 for (efa= em->faces.first; efa; efa= efa->next) {
490 tface= CustomData_em_get(&em->fdata, efa->data, CD_MTFACE);
491 if (SIMA_FACEDRAW_CHECK(efa, tface)) {
499 /* draw transparent faces */
500 if(G.f & G_DRAWFACES) {
501 BIF_GetThemeColor4ubv(TH_FACE, col1);
502 BIF_GetThemeColor4ubv(TH_FACE_SELECT, col2);
503 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
506 for (efa= em->faces.first; efa; efa= efa->next) {
508 // tface= CustomData_em_get(&em->fdata, efa->data, CD_MTFACE);
509 // if (SIMA_FACEDRAW_CHECK(efa, tface)) {
511 /*this is a shortcut to do the same as above but a faster for drawing */
512 if ((tface=(MTFace *)efa->tmp.p)) {
514 if( SIMA_FACESEL_CHECK(efa, tface) )
515 glColor4ubv((GLubyte *)col2);
517 glColor4ubv((GLubyte *)col1);
519 glBegin(efa->v4?GL_QUADS:GL_TRIANGLES);
520 glVertex2fv(tface->uv[0]);
521 glVertex2fv(tface->uv[1]);
522 glVertex2fv(tface->uv[2]);
523 if(efa->v4) glVertex2fv(tface->uv[3]);
530 if (G.sima->flag & SI_SMOOTH_UV) {
531 glEnable( GL_LINE_SMOOTH );
533 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
536 switch (G.sima->dt_uv) {
538 for (efa= em->faces.first; efa; efa= efa->next) {
539 // tface= CustomData_em_get(&em->fdata, efa->data, CD_MTFACE);
540 // if (SIMA_FACEDRAW_CHECK(efa, tface)) {
542 /*this is a shortcut to do the same as above but a faster for drawing */
543 if ((tface=(MTFace *)efa->tmp.p)) {
546 glBegin(GL_LINE_LOOP);
547 glVertex2fv(tface->uv[0]);
548 glVertex2fv(tface->uv[1]);
549 glVertex2fv(tface->uv[2]);
550 if(efa->v4) glVertex2fv(tface->uv[3]);
555 glBegin(GL_LINE_STRIP);
556 glVertex2fv(tface->uv[0]);
557 glVertex2fv(tface->uv[1]);
560 glBegin(GL_LINE_STRIP);
561 glVertex2fv(tface->uv[0]);
562 if(efa->v4) glVertex2fv(tface->uv[3]);
563 else glVertex2fv(tface->uv[2]);
566 glBegin(GL_LINE_STRIP);
567 glVertex2fv(tface->uv[1]);
568 glVertex2fv(tface->uv[2]);
569 if(efa->v4) glVertex2fv(tface->uv[3]);
575 case 1: /* black/white */
577 cpack((G.sima->dt_uv==1) ? 0x0 : 0xFFFFFF);
578 for (efa= em->faces.first; efa; efa= efa->next) {
579 // tface= CustomData_em_get(&em->fdata, efa->data, CD_MTFACE);
580 // if (SIMA_FACEDRAW_CHECK(efa, tface)) {
582 /*this is a shortcut to do the same as above but a faster for drawing */
583 if ((tface=(MTFace *)efa->tmp.p)) {
585 glBegin(GL_LINE_LOOP);
586 glVertex2fv(tface->uv[0]);
587 glVertex2fv(tface->uv[1]);
588 glVertex2fv(tface->uv[2]);
589 if(efa->v4) glVertex2fv(tface->uv[3]);
598 for (efa= em->faces.first; efa; efa= efa->next) {
599 // tface= CustomData_em_get(&em->fdata, efa->data, CD_MTFACE);
600 // if (SIMA_FACEDRAW_CHECK(efa, tface)) {
602 /*this is a shortcut to do the same as above but a faster for drawing */
603 if ((tface=(MTFace *)efa->tmp.p)) {
605 glBegin(GL_LINE_LOOP);
606 glVertex2fv(tface->uv[0]);
607 glVertex2fv(tface->uv[1]);
608 glVertex2fv(tface->uv[2]);
609 if(efa->v4) glVertex2fv(tface->uv[3]);
616 for (efa= em->faces.first; efa; efa= efa->next) {
617 // tface= CustomData_em_get(&em->fdata, efa->data, CD_MTFACE);
618 // if (SIMA_FACEDRAW_CHECK(efa, tface)) {
620 /*this is a shortcut to do the same as above but a faster for drawing */
621 if ((tface=(MTFace *)efa->tmp.p)) {
623 glBegin(GL_LINE_LOOP);
624 glVertex2fv(tface->uv[0]);
625 glVertex2fv(tface->uv[1]);
626 glVertex2fv(tface->uv[2]);
627 if(efa->v4) glVertex2fv(tface->uv[3]);
634 if (G.sima->flag & SI_SMOOTH_UV) {
635 glDisable( GL_LINE_SMOOTH);
640 /* draw active face edges */
641 /* colors: R=u G=v */
642 activetface = get_active_mtface(&efa, NULL, 0);
648 glBegin(GL_LINE_LOOP);
649 glVertex2fv(tface->uv[0]);
650 glVertex2fv(tface->uv[1]);
651 glVertex2fv(tface->uv[2]);
652 if(efa->v4) glVertex2fv(tface->uv[3]);
656 glBegin(GL_LINE_STRIP);
657 glVertex2fv(tface->uv[0]);
658 glVertex2fv(tface->uv[1]);
662 glBegin(GL_LINE_STRIP);
663 glVertex2fv(tface->uv[0]);
664 if(efa->v4) glVertex2fv(tface->uv[3]);
665 else glVertex2fv(tface->uv[2]);
669 glBegin(GL_LINE_STRIP);
670 glVertex2fv(tface->uv[1]);
671 glVertex2fv(tface->uv[2]);
672 if(efa->v4) glVertex2fv(tface->uv[3]);
678 /* unselected uv's */
679 BIF_ThemeColor(TH_VERTEX);
680 glPointSize(pointsize);
683 for (efa= em->faces.first; efa; efa= efa->next) {
685 // tface= CustomData_em_get(&em->fdata, efa->data, CD_MTFACE);
686 // if (SIMA_FACEDRAW_CHECK(efa, tface)) {
688 /*this is a shortcut to do the same as above but a faster for drawing */
689 if ((tface=(MTFace *)efa->tmp.p)) {
691 if(SIMA_UVSEL_CHECK(efa, tface, 0)); else bglVertex2fv(tface->uv[0]);
692 if(SIMA_UVSEL_CHECK(efa, tface, 1)); else bglVertex2fv(tface->uv[1]);
693 if(SIMA_UVSEL_CHECK(efa, tface, 2)); else bglVertex2fv(tface->uv[2]);
695 if(SIMA_UVSEL_CHECK(efa, tface, 3)); else bglVertex2fv(tface->uv[3]);
702 /* give odd pointsizes odd pin pointsizes */
703 glPointSize(pointsize*2 + (((int)pointsize % 2)? (-1): 0));
707 for (efa= em->faces.first; efa; efa= efa->next) {
708 // tface= CustomData_em_get(&em->fdata, efa->data, CD_MTFACE);
709 // if (SIMA_FACEDRAW_CHECK(efa, tface)) {
711 /*this is a shortcut to do the same as above but a faster for drawing */
712 if ((tface=(MTFace *)efa->tmp.p)) {
714 if(tface->unwrap & TF_PIN1) bglVertex2fv(tface->uv[0]);
715 if(tface->unwrap & TF_PIN2) bglVertex2fv(tface->uv[1]);
716 if(tface->unwrap & TF_PIN3) bglVertex2fv(tface->uv[2]);
718 if(tface->unwrap & TF_PIN4) bglVertex2fv(tface->uv[3]);
725 BIF_ThemeColor(TH_VERTEX_SELECT);
726 glPointSize(pointsize);
729 for (efa= em->faces.first; efa; efa= efa->next) {
730 // tface= CustomData_em_get(&em->fdata, efa->data, CD_MTFACE);
731 // if (SIMA_FACEDRAW_CHECK(efa, tface)) {
733 /*this is a shortcut to do the same as above but a faster for drawing */
734 if ((tface=(MTFace *)efa->tmp.p)) {
736 if(!SIMA_UVSEL_CHECK(efa, tface, 0)); else bglVertex2fv(tface->uv[0]);
737 if(!SIMA_UVSEL_CHECK(efa, tface, 1)); else bglVertex2fv(tface->uv[1]);
738 if(!SIMA_UVSEL_CHECK(efa, tface, 2)); else bglVertex2fv(tface->uv[2]);
740 if(!SIMA_UVSEL_CHECK(efa, tface, 3)); else bglVertex2fv(tface->uv[3]);
748 static unsigned int *get_part_from_ibuf(ImBuf *ibuf, short startx, short starty, short endx, short endy)
750 unsigned int *rt, *rp, *rectmain;
751 short y, heigth, len;
753 /* the right offset in rectot */
755 rt= ibuf->rect+ (starty*ibuf->x+ startx);
758 heigth= (endy-starty);
760 rp=rectmain= MEM_mallocN(heigth*len*sizeof(int), "rect");
762 for(y=0; y<heigth; y++) {
763 memcpy(rp, rt, len*4);
770 static void draw_image_transform(ImBuf *ibuf, float xuser_asp, float yuser_asp)
773 float aspx, aspy, center[3];
775 BIF_drawConstraint();
777 if(ibuf==0 || ibuf->rect==0 || ibuf->x==0 || ibuf->y==0) {
781 aspx= (256.0/ibuf->x) * xuser_asp;
782 aspy= (256.0/ibuf->y) * yuser_asp;
784 BIF_getPropCenter(center);
786 /* scale and translate the circle into place and draw it */
788 glScalef(aspx, aspy, 1.0);
789 glTranslatef((1/aspx)*center[0] - center[0],
790 (1/aspy)*center[1] - center[1], 0.0);
792 BIF_drawPropCircle();
798 static void draw_image_view_icon(void)
803 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
806 if (G.sima->flag & SI_SYNC_UVSEL) {
807 /* take settings from the editmesh */
808 if (G.scene->selectmode == SCE_SELECT_FACE || G.sima->flag & SI_SELACTFACE) {
809 BIF_icon_draw_aspect(xPos, 5.0, ICON_FACESEL_HLT, 1.0f);
813 /* use the flags for UV mode - normal operation */
814 if(G.sima->flag & SI_SELACTFACE) {
815 BIF_icon_draw_aspect(xPos, 5.0, ICON_FACESEL_HLT, 1.0f);
819 glBlendFunc(GL_ONE, GL_ZERO);
823 static void draw_image_view_tool(void)
825 ToolSettings *settings= G.scene->toolsettings;
826 Brush *brush= settings->imapaint.brush;
832 if(settings->imapaint.flag & IMAGEPAINT_DRAWING) {
833 if(settings->imapaint.flag & IMAGEPAINT_DRAW_TOOL_DRAWING)
836 else if(settings->imapaint.flag & IMAGEPAINT_DRAW_TOOL)
840 getmouseco_areawin(mval);
842 radius= brush->size*G.sima->zoom/2;
843 fdrawXORcirc(mval[0], mval[1], radius);
845 if (brush->innerradius != 1.0) {
846 radius *= brush->innerradius;
847 fdrawXORcirc(mval[0], mval[1], radius);
853 /* ************ panel stuff ************* */
855 /* this function gets the values for cursor and vertex number buttons */
856 static void image_transform_but_attr(int *imx, int *imy, int *step, int *digits) /*, float *xcoord, float *ycoord)*/
858 ImBuf *ibuf= imagewindow_get_ibuf(G.sima);
864 if (G.sima->flag & SI_COORDFLOATS) {
875 /* is used for both read and write... */
876 void image_editvertex_buts(uiBlock *block)
878 static float ocent[2];
879 float cent[2]= {0.0, 0.0};
880 int imx= 256, imy= 256;
881 int nactive= 0, step, digits;
882 EditMesh *em = G.editMesh;
886 if( is_uv_tface_editing_allowed_silent()==0 ) return;
888 image_transform_but_attr(&imx, &imy, &step, &digits);
890 for (efa= em->faces.first; efa; efa= efa->next) {
891 tf= CustomData_em_get(&em->fdata, efa->data, CD_MTFACE);
892 if (SIMA_FACEDRAW_CHECK(efa, tf)) {
894 if (SIMA_UVSEL_CHECK(efa, tf, 0)) {
895 cent[0]+= tf->uv[0][0];
896 cent[1]+= tf->uv[0][1];
899 if (SIMA_UVSEL_CHECK(efa, tf, 1)) {
900 cent[0]+= tf->uv[1][0];
901 cent[1]+= tf->uv[1][1];
904 if (SIMA_UVSEL_CHECK(efa, tf, 2)) {
905 cent[0]+= tf->uv[2][0];
906 cent[1]+= tf->uv[2][1];
909 if (efa->v4 && SIMA_UVSEL_CHECK(efa, tf, 3)) {
910 cent[0]+= tf->uv[3][0];
911 cent[1]+= tf->uv[3][1];
917 if(block) { // do the buttons
919 ocent[0]= cent[0]/nactive;
920 ocent[1]= cent[1]/nactive;
921 if (G.sima->flag & SI_COORDFLOATS) {
927 //uiBlockBeginAlign(block);
929 uiDefButF(block, NUM, B_TRANS_IMAGE, "Vertex X:", 10, 10, 145, 19, &ocent[0], -10*imx, 10.0*imx, step, digits, "");
930 uiDefButF(block, NUM, B_TRANS_IMAGE, "Vertex Y:", 165, 10, 145, 19, &ocent[1], -10*imy, 10.0*imy, step, digits, "");
933 uiDefButF(block, NUM, B_TRANS_IMAGE, "Median X:", 10, 10, 145, 19, &ocent[0], -10*imx, 10.0*imx, step, digits, "");
934 uiDefButF(block, NUM, B_TRANS_IMAGE, "Median Y:", 165, 10, 145, 19, &ocent[1], -10*imy, 10.0*imy, step, digits, "");
936 //uiBlockEndAlign(block);
939 else { // apply event
942 cent[0]= cent[0]/nactive;
943 cent[1]= cent[1]/nactive;
945 if (G.sima->flag & SI_COORDFLOATS) {
946 delta[0]= ocent[0]-cent[0];
947 delta[1]= ocent[1]-cent[1];
950 delta[0]= ocent[0]/imx - cent[0];
951 delta[1]= ocent[1]/imy - cent[1];
954 for (efa= em->faces.first; efa; efa= efa->next) {
955 tf= CustomData_em_get(&em->fdata, efa->data, CD_MTFACE);
956 if (SIMA_FACEDRAW_CHECK(efa, tf)) {
957 if (SIMA_UVSEL_CHECK(efa, tf, 0)) {
958 tf->uv[0][0]+= delta[0];
959 tf->uv[0][1]+= delta[1];
961 if (SIMA_UVSEL_CHECK(efa, tf, 1)) {
962 tf->uv[1][0]+= delta[0];
963 tf->uv[1][1]+= delta[1];
965 if (SIMA_UVSEL_CHECK(efa, tf, 2)) {
966 tf->uv[2][0]+= delta[0];
967 tf->uv[2][1]+= delta[1];
969 if (efa->v4 && SIMA_UVSEL_CHECK(efa, tf, 3)) {
970 tf->uv[3][0]+= delta[0];
971 tf->uv[3][1]+= delta[1];
976 allqueue(REDRAWVIEW3D, 0);
977 allqueue(REDRAWIMAGE, 0);
982 /* is used for both read and write... */
983 void image_editcursor_buts(uiBlock *block)
985 static float ocent[2];
986 int imx= 256, imy= 256;
989 if( is_uv_tface_editing_allowed_silent()==0 ) return;
991 image_transform_but_attr(&imx, &imy, &step, &digits);
993 if(block) { // do the buttons
994 ocent[0]= G.v2d->cursor[0];
995 ocent[1]= G.v2d->cursor[1];
996 if (G.sima->flag & SI_COORDFLOATS) {
1002 uiBlockBeginAlign(block);
1003 uiDefButF(block, NUM, B_CURSOR_IMAGE, "Cursor X:", 165, 120, 145, 19, &ocent[0], -10*imx, 10.0*imx, step, digits, "");
1004 uiDefButF(block, NUM, B_CURSOR_IMAGE, "Cursor Y:", 165, 100, 145, 19, &ocent[1], -10*imy, 10.0*imy, step, digits, "");
1005 uiBlockEndAlign(block);
1007 else { // apply event
1008 if (G.sima->flag & SI_COORDFLOATS) {
1009 G.v2d->cursor[0]= ocent[0];
1010 G.v2d->cursor[1]= ocent[1];
1013 G.v2d->cursor[0]= ocent[0]/imx;
1014 G.v2d->cursor[1]= ocent[1]/imy;
1016 allqueue(REDRAWIMAGE, 0);
1021 void image_info(Image *ima, ImBuf *ibuf, char *str)
1027 if(ima==NULL) return;
1029 sprintf(str, "Can not get an image");
1033 if(ima->source==IMA_SRC_MOVIE) {
1034 ofs= sprintf(str, "Movie ");
1036 ofs+= sprintf(str+ofs, "%d frs", IMB_anim_get_duration(ima->anim));
1039 ofs= sprintf(str, "Image ");
1041 ofs+= sprintf(str+ofs, ": size %d x %d,", ibuf->x, ibuf->y);
1043 if(ibuf->rect_float) {
1044 if(ibuf->channels!=4) {
1045 sprintf(str+ofs, "%d float channel(s)", ibuf->channels);
1047 else if(ibuf->depth==32)
1048 strcat(str, " RGBA float");
1050 strcat(str, " RGB float");
1054 strcat(str, " RGBA byte");
1056 strcat(str, " RGB byte");
1058 if(ibuf->zbuf || ibuf->zbuf_float)
1059 strcat(str, " + Z");
1063 static void image_panel_properties(short cntrl) // IMAGE_HANDLER_PROPERTIES
1067 block= uiNewBlock(&curarea->uiblocks, "image_panel_properties", UI_EMBOSS, UI_HELV, curarea->win);
1068 uiPanelControl(UI_PNL_SOLID | UI_PNL_CLOSE | cntrl);
1069 uiSetPanelHandler(IMAGE_HANDLER_PROPERTIES); // for close and esc
1070 if(uiNewPanel(curarea, block, "Image Properties", "Image", 10, 10, 318, 204)==0)
1073 /* note, it draws no bottom half in facemode, for vertex buttons */
1074 uiblock_image_panel(block, &G.sima->image, &G.sima->iuser, B_REDR, B_REDR);
1075 image_editvertex_buts(block);
1078 static void image_panel_game_properties(short cntrl) // IMAGE_HANDLER_GAME_PROPERTIES
1080 ImBuf *ibuf= BKE_image_get_ibuf(G.sima->image, &G.sima->iuser);
1083 block= uiNewBlock(&curarea->uiblocks, "image_panel_game_properties", UI_EMBOSS, UI_HELV, curarea->win);
1084 uiPanelControl(UI_PNL_SOLID | UI_PNL_CLOSE | cntrl);
1085 uiSetPanelHandler(IMAGE_HANDLER_GAME_PROPERTIES); // for close and esc
1086 if(uiNewPanel(curarea, block, "Real-time Properties", "Image", 10, 10, 318, 204)==0)
1092 image_info(G.sima->image, ibuf, str);
1093 uiDefBut(block, LABEL, B_NOP, str, 10,180,300,19, 0, 0, 0, 0, 0, "");
1095 uiBlockBeginAlign(block);
1096 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");
1097 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");
1098 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");
1099 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");
1100 uiBlockEndAlign(block);
1102 uiBlockBeginAlign(block);
1103 uiDefButBitS(block, TOG, IMA_TILES, B_SIMAGETILE, "Tiles", 160,150,140,19, &G.sima->image->tpageflag, 0, 0, 0, 0, "Toggles use of tilemode for faces (Shift LMB to pick the tile for selected faces)");
1104 uiDefButS(block, NUM, B_SIMA_REDR_IMA_3D, "X:", 160,130,70,19, &G.sima->image->xrep, 1.0, 16.0, 0, 0, "Sets the degree of repetition in the X direction");
1105 uiDefButS(block, NUM, B_SIMA_REDR_IMA_3D, "Y:", 230,130,70,19, &G.sima->image->yrep, 1.0, 16.0, 0, 0, "Sets the degree of repetition in the Y direction");
1106 uiBlockBeginAlign(block);
1108 uiBlockBeginAlign(block);
1109 uiDefButBitS(block, TOG, IMA_CLAMP_U, B_SIMA3DVIEWDRAW, "ClampX", 160,100,70,19, &G.sima->image->tpageflag, 0, 0, 0, 0, "Disable texture repeating horizontaly");
1110 uiDefButBitS(block, TOG, IMA_CLAMP_V, B_SIMA3DVIEWDRAW, "ClampY", 230,100,70,19, &G.sima->image->tpageflag, 0, 0, 0, 0, "Disable texture repeating vertically");
1111 uiBlockEndAlign(block);
1115 //static void image_panel_transform_properties(short cntrl) // IMAGE_HANDLER_TRANSFORM_PROPERTIES
1119 // block= uiNewBlock(&curarea->uiblocks, "image_transform_properties", UI_EMBOSS, UI_HELV, curarea->win);
1120 // uiPanelControl(UI_PNL_SOLID | UI_PNL_CLOSE | cntrl);
1121 // uiSetPanelHandler(IMAGE_HANDLER_TRANSFORM_PROPERTIES); // for close and esc
1122 // if(uiNewPanel(curarea, block, "Transform Properties", "Image", 10, 10, 318, 204)==0)
1125 // image_editvertex_buts(block);
1128 static void image_panel_view_properties(short cntrl) // IMAGE_HANDLER_VIEW_PROPERTIES
1132 block= uiNewBlock(&curarea->uiblocks, "image_view_properties", UI_EMBOSS, UI_HELV, curarea->win);
1133 uiPanelControl(UI_PNL_SOLID | UI_PNL_CLOSE | cntrl);
1134 uiSetPanelHandler(IMAGE_HANDLER_VIEW_PROPERTIES); // for close and esc
1135 if(uiNewPanel(curarea, block, "View Properties", "Image", 10, 10, 318, 204)==0)
1139 uiDefButBitI(block, TOG, SI_DRAW_TILE, B_REDR, "Repeat Image", 10,160,140,19, &G.sima->flag, 0, 0, 0, 0, "Repeat/Tile the image display");
1140 uiDefButBitI(block, TOG, SI_COORDFLOATS, B_REDR, "Normalized Coords", 165,160,145,19, &G.sima->flag, 0, 0, 0, 0, "Display coords from 0.0 to 1.0 rather then in pixels");
1143 if (G.sima->image) {
1144 uiDefBut(block, LABEL, B_NOP, "Image Display:", 10,140,140,19, 0, 0, 0, 0, 0, "");
1145 uiBlockBeginAlign(block);
1146 uiDefButF(block, NUM, B_REDR, "AspX:", 10,120,140,19, &G.sima->image->aspx, 0.1, 5000.0, 100, 0, "X Display Aspect for this image, does not affect renderingm 0 disables.");
1147 uiDefButF(block, NUM, B_REDR, "AspY:", 10,100,140,19, &G.sima->image->aspy, 0.1, 5000.0, 100, 0, "X Display Aspect for this image, does not affect rendering 0 disables.");
1148 uiBlockEndAlign(block);
1152 if (EM_texFaceCheck()) {
1153 uiDefBut(block, LABEL, B_NOP, "Draw Type:", 10, 20,120,19, 0, 0, 0, 0, 0, "");
1154 uiBlockBeginAlign(block);
1155 uiDefButC(block, ROW, B_REDR, "Dash", 10, 0,58,19, &G.sima->dt_uv, 0.0, 0.0, 0, 0, "Dashed Wire UV drawtype");
1156 uiDefButC(block, ROW, B_REDR, "Black", 68, 0,58,19, &G.sima->dt_uv, 0.0, 1.0, 0, 0, "Black Wire UV drawtype");
1157 uiDefButC(block, ROW, B_REDR, "White", 126,0,58,19, &G.sima->dt_uv, 0.0, 2.0, 0, 0, "White Wire UV drawtype");
1158 uiDefButC(block, ROW, B_REDR, "Outline", 184,0,58,19, &G.sima->dt_uv, 0.0, 3.0, 0, 0, "Outline Wire UV drawtype");
1159 uiBlockBeginAlign(block);
1160 uiDefButBitI(block, TOG, SI_SMOOTH_UV, B_REDR, "Smooth", 250,0,60,19, &G.sima->flag, 0, 0, 0, 0, "Display smooth lines in the UV view");
1162 image_editcursor_buts(block);
1165 static void image_panel_paint(short cntrl) // IMAGE_HANDLER_PAINT
1167 /* B_SIMABRUSHCHANGE only redraws and eats the mouse messages */
1168 /* so that LEFTMOUSE does not 'punch' through the floating panel */
1170 ToolSettings *settings= G.scene->toolsettings;
1171 Brush *brush= settings->imapaint.brush;
1176 block= uiNewBlock(&curarea->uiblocks, "image_panel_paint", UI_EMBOSS, UI_HELV, curarea->win);
1177 uiPanelControl(UI_PNL_SOLID | UI_PNL_CLOSE | cntrl);
1178 uiSetPanelHandler(IMAGE_HANDLER_PAINT); // for close and esc
1179 if(uiNewPanel(curarea, block, "Image Paint", "Image", 10, 230, 318, 204)==0)
1184 uiBlockBeginAlign(block);
1185 uiDefButS(block, ROW, B_SIMABRUSHCHANGE, "Draw", 0 ,yco,80,19, &settings->imapaint.tool, 7.0, PAINT_TOOL_DRAW, 0, 0, "Draw brush");
1186 uiDefButS(block, ROW, B_SIMABRUSHCHANGE, "Soften", 80 ,yco,80,19, &settings->imapaint.tool, 7.0, PAINT_TOOL_SOFTEN, 0, 0, "Soften brush");
1187 uiDefButS(block, ROW, B_SIMABRUSHCHANGE, "Smear", 160,yco,80,19, &settings->imapaint.tool, 7.0, PAINT_TOOL_SMEAR, 0, 0, "Smear brush");
1188 uiDefButS(block, ROW, B_SIMABRUSHCHANGE, "Clone", 240,yco,80,19, &settings->imapaint.tool, 7.0, PAINT_TOOL_CLONE, 0, 0, "Clone brush, use RMB to drag source image");
1189 uiBlockEndAlign(block);
1192 uiBlockSetCol(block, TH_BUT_SETTING2);
1193 id= (ID*)settings->imapaint.brush;
1194 xco= std_libbuttons(block, 0, yco, 0, NULL, B_SIMABRUSHBROWSE, ID_BR, 0, id, NULL, &(G.sima->menunr), 0, B_SIMABRUSHLOCAL, B_SIMABRUSHDELETE, 0, B_KEEPDATA);
1195 uiBlockSetCol(block, TH_AUTO);
1197 if(brush && !brush->id.lib) {
1200 uiDefButS(block, MENU, B_SIMANOTHING, "Mix %x0|Add %x1|Subtract %x2|Multiply %x3|Lighten %x4|Darken %x5|Erase Alpha %x6|Add Alpha %x7", xco+10,yco,butw,19, &brush->blend, 0, 0, 0, 0, "Blending method for applying brushes");
1202 uiDefButBitS(block, TOG|BIT, BRUSH_TORUS, B_SIMABRUSHCHANGE, "Wrap", xco+10,yco-25,butw,19, &brush->flag, 0, 0, 0, 0, "Enables torus wrapping");
1204 uiBlockBeginAlign(block);
1205 uiDefButBitS(block, TOG|BIT, BRUSH_AIRBRUSH, B_SIMABRUSHCHANGE, "Airbrush", xco+10,yco-50,butw,19, &brush->flag, 0, 0, 0, 0, "Keep applying paint effect while holding mouse (spray)");
1206 uiDefButF(block, NUM, B_SIMANOTHING, "Rate ", xco+10,yco-70,butw,19, &brush->rate, 0.01, 1.0, 0, 0, "Number of paints per second for Airbrush");
1207 uiBlockEndAlign(block);
1211 uiBlockBeginAlign(block);
1212 uiDefButF(block, COL, B_VPCOLSLI, "", 0,yco,200,19, brush->rgb, 0, 0, 0, 0, "");
1213 uiDefButF(block, NUMSLI, B_SIMANOTHING, "Opacity ", 0,yco-20,180,19, &brush->alpha, 0.0, 1.0, 0, 0, "The amount of pressure on the brush");
1214 uiDefButBitS(block, TOG|BIT, BRUSH_ALPHA_PRESSURE, B_SIMANOTHING, "P", 180,yco-20,20,19, &brush->flag, 0, 0, 0, 0, "Enables pressure sensitivity for tablets");
1215 uiDefButI(block, NUMSLI, B_SIMANOTHING, "Size ", 0,yco-40,180,19, &brush->size, 1, 200, 0, 0, "The size of the brush");
1216 uiDefButBitS(block, TOG|BIT, BRUSH_SIZE_PRESSURE, B_SIMANOTHING, "P", 180,yco-40,20,19, &brush->flag, 0, 0, 0, 0, "Enables pressure sensitivity for tablets");
1217 uiDefButF(block, NUMSLI, B_SIMANOTHING, "Falloff ", 0,yco-60,180,19, &brush->innerradius, 0.0, 1.0, 0, 0, "The fall off radius of the brush");
1218 uiDefButBitS(block, TOG|BIT, BRUSH_RAD_PRESSURE, B_SIMANOTHING, "P", 180,yco-60,20,19, &brush->flag, 0, 0, 0, 0, "Enables pressure sensitivity for tablets");
1219 uiDefButF(block, NUMSLI, B_SIMANOTHING, "Spacing ",0,yco-80,180,19, &brush->spacing, 1.0, 100.0, 0, 0, "Repeating paint on %% of brush diameter");
1220 uiDefButBitS(block, TOG|BIT, BRUSH_SPACING_PRESSURE, B_SIMANOTHING, "P", 180,yco-80,20,19, &brush->flag, 0, 0, 0, 0, "Enables pressure sensitivity for tablets");
1221 uiBlockEndAlign(block);
1225 if(settings->imapaint.tool == PAINT_TOOL_CLONE) {
1226 id= (ID*)brush->clone.image;
1227 uiBlockSetCol(block, TH_BUT_SETTING2);
1228 xco= std_libbuttons(block, 0, yco, 0, NULL, B_SIMACLONEBROWSE, ID_IM, 0, id, 0, &G.sima->menunr, 0, 0, B_SIMACLONEDELETE, 0, 0);
1229 uiBlockSetCol(block, TH_AUTO);
1232 uiDefButF(block, NUMSLI, B_SIMABRUSHCHANGE, "B ",xco+5,yco,butw,19, &brush->clone.alpha , 0.0, 1.0, 0, 0, "Opacity of clone image display");
1236 MTex *mtex= brush->mtex[brush->texact];
1238 uiBlockSetCol(block, TH_BUT_SETTING2);
1239 id= (mtex)? (ID*)mtex->tex: NULL;
1240 xco= std_libbuttons(block, 0, yco, 0, NULL, B_SIMABTEXBROWSE, ID_TE, 0, id, NULL, &(G.sima->menunr), 0, 0, B_SIMABTEXDELETE, 0, 0);
1241 /*uiDefButBitS(block, TOG|BIT, BRUSH_FIXED_TEX, B_SIMABRUSHCHANGE, "Fixed", xco+5,yco,butw,19, &brush->flag, 0, 0, 0, 0, "Keep texture origin in fixed position");*/
1242 uiBlockSetCol(block, TH_AUTO);
1247 uiDefButBitS(block, TOG|BIT, IMAGEPAINT_DRAW_TOOL_DRAWING, B_SIMABRUSHCHANGE, "TD", 0,1,50,19, &settings->imapaint.flag.flag, 0, 0, 0, 0, "Enables brush shape while drawing");
1248 uiDefButBitS(block, TOG|BIT, IMAGEPAINT_DRAW_TOOL, B_SIMABRUSHCHANGE, "TP", 50,1,50,19, &settings->imapaint.flag.flag, 0, 0, 0, 0, "Enables brush shape while not drawing");
1252 static void image_panel_curves_reset(void *cumap_v, void *ibuf_v)
1254 CurveMapping *cumap = cumap_v;
1257 for(a=0; a<CM_TOT; a++)
1258 curvemap_reset(cumap->cm+a, &cumap->clipr);
1260 cumap->black[0]=cumap->black[1]=cumap->black[2]= 0.0f;
1261 cumap->white[0]=cumap->white[1]=cumap->white[2]= 1.0f;
1262 curvemapping_set_black_white(cumap, NULL, NULL);
1264 curvemapping_changed(cumap, 0);
1265 curvemapping_do_ibuf(cumap, ibuf_v);
1267 allqueue(REDRAWIMAGE, 0);
1271 static void image_panel_curves(short cntrl) // IMAGE_HANDLER_CURVES
1277 /* and we check for spare */
1278 ibuf= imagewindow_get_ibuf(G.sima);
1280 block= uiNewBlock(&curarea->uiblocks, "image_panel_curves", UI_EMBOSS, UI_HELV, curarea->win);
1281 uiPanelControl(UI_PNL_SOLID | UI_PNL_CLOSE | cntrl);
1282 uiSetPanelHandler(IMAGE_HANDLER_CURVES); // for close and esc
1283 if(uiNewPanel(curarea, block, "Curves", "Image", 10, 450, 318, 204)==0)
1289 if(G.sima->cumap==NULL)
1290 G.sima->cumap= curvemapping_add(4, 0.0f, 0.0f, 1.0f, 1.0f);
1292 rect.xmin= 110; rect.xmax= 310;
1293 rect.ymin= 10; rect.ymax= 200;
1294 curvemap_buttons(block, G.sima->cumap, 'c', B_SIMACURVES, B_REDR, &rect);
1296 bt=uiDefBut(block, BUT, B_SIMARANGE, "Reset", 10, 160, 90, 19, NULL, 0.0f, 0.0f, 0, 0, "Reset Black/White point and curves");
1297 uiButSetFunc(bt, image_panel_curves_reset, G.sima->cumap, ibuf);
1299 uiBlockBeginAlign(block);
1300 uiDefButF(block, NUM, B_SIMARANGE, "Min R:", 10, 120, 90, 19, G.sima->cumap->black, -1000.0f, 1000.0f, 10, 2, "Black level");
1301 uiDefButF(block, NUM, B_SIMARANGE, "Min G:", 10, 100, 90, 19, G.sima->cumap->black+1, -1000.0f, 1000.0f, 10, 2, "Black level");
1302 uiDefButF(block, NUM, B_SIMARANGE, "Min B:", 10, 80, 90, 19, G.sima->cumap->black+2, -1000.0f, 1000.0f, 10, 2, "Black level");
1304 uiBlockBeginAlign(block);
1305 uiDefButF(block, NUM, B_SIMARANGE, "Max R:", 10, 50, 90, 19, G.sima->cumap->white, -1000.0f, 1000.0f, 10, 2, "White level");
1306 uiDefButF(block, NUM, B_SIMARANGE, "Max G:", 10, 30, 90, 19, G.sima->cumap->white+1, -1000.0f, 1000.0f, 10, 2, "White level");
1307 uiDefButF(block, NUM, B_SIMARANGE, "Max B:", 10, 10, 90, 19, G.sima->cumap->white+2, -1000.0f, 1000.0f, 10, 2, "White level");
1312 /* are there curves? curves visible? and curves do something? */
1313 static int image_curves_active(ScrArea *sa)
1315 SpaceImage *sima= sa->spacedata.first;
1318 if(curvemapping_RGBA_does_something(sima->cumap)) {
1320 for(a=0; a<SPACE_MAXHANDLER; a+=2) {
1321 if(sima->blockhandler[a] == IMAGE_HANDLER_CURVES)
1329 /* 0: disable preview
1330 otherwise refresh preview
1332 void image_preview_event(int event)
1338 G.scene->r.scemode &= ~R_COMP_CROP;
1342 if(image_preview_active(curarea, NULL, NULL)) {
1343 G.scene->r.scemode |= R_COMP_CROP;
1347 G.scene->r.scemode &= ~R_COMP_CROP;
1350 if(exec && G.scene->nodetree) {
1351 /* should work when no node editor in screen..., so we execute right away */
1353 ntreeCompositTagGenerators(G.scene->nodetree);
1356 G.scene->nodetree->timecursor= set_timecursor;
1357 G.scene->nodetree->test_break= blender_test_break;
1361 ntreeCompositExecTree(G.scene->nodetree, &G.scene->r, 1); /* 1 is do_previews */
1363 G.scene->nodetree->timecursor= NULL;
1364 G.scene->nodetree->test_break= NULL;
1366 scrarea_do_windraw(curarea);
1369 allqueue(REDRAWNODE, 1);
1374 /* nothing drawn here, we use it to store values */
1375 static void preview_cb(struct ScrArea *sa, struct uiBlock *block)
1378 rcti *disprect= &G.scene->r.disprect;
1379 int winx= (G.scene->r.size*G.scene->r.xsch)/100;
1380 int winy= (G.scene->r.size*G.scene->r.ysch)/100;
1383 if(G.scene->r.mode & R_BORDER) {
1384 winx*= (G.scene->r.border.xmax - G.scene->r.border.xmin);
1385 winy*= (G.scene->r.border.ymax - G.scene->r.border.ymin);
1388 /* while dragging we need to update the rects, otherwise it doesn't end with correct one */
1390 BLI_init_rctf(&dispf, 15.0f, (block->maxx - block->minx)-15.0f, 15.0f, (block->maxy - block->miny)-15.0f);
1391 ui_graphics_to_window_rct(sa->win, &dispf, disprect);
1393 /* correction for gla draw */
1394 BLI_translate_rcti(disprect, -curarea->winrct.xmin, -curarea->winrct.ymin);
1396 calc_image_view(G.sima, 'p');
1397 // printf("winrct %d %d %d %d\n", disprect->xmin, disprect->ymin,disprect->xmax, disprect->ymax);
1398 /* map to image space coordinates */
1399 mval[0]= disprect->xmin; mval[1]= disprect->ymin;
1400 areamouseco_to_ipoco(G.v2d, mval, &dispf.xmin, &dispf.ymin);
1401 mval[0]= disprect->xmax; mval[1]= disprect->ymax;
1402 areamouseco_to_ipoco(G.v2d, mval, &dispf.xmax, &dispf.ymax);
1404 /* map to render coordinates */
1405 disprect->xmin= dispf.xmin;
1406 disprect->xmax= dispf.xmax;
1407 disprect->ymin= dispf.ymin;
1408 disprect->ymax= dispf.ymax;
1410 CLAMP(disprect->xmin, 0, winx);
1411 CLAMP(disprect->xmax, 0, winx);
1412 CLAMP(disprect->ymin, 0, winy);
1413 CLAMP(disprect->ymax, 0, winy);
1414 // printf("drawrct %d %d %d %d\n", disprect->xmin, disprect->ymin,disprect->xmax, disprect->ymax);
1418 static int is_preview_allowed(ScrArea *cur)
1420 SpaceImage *sima= cur->spacedata.first;
1423 /* check if another areawindow has preview set */
1424 for(sa=G.curscreen->areabase.first; sa; sa= sa->next) {
1425 if(sa!=cur && sa->spacetype==SPACE_IMAGE) {
1426 if(image_preview_active(sa, NULL, NULL))
1430 /* check image type */
1431 if(sima->image==NULL || sima->image->type!=IMA_TYPE_COMPOSITE)
1437 static void image_panel_preview(ScrArea *sa, short cntrl) // IMAGE_HANDLER_PREVIEW
1440 SpaceImage *sima= sa->spacedata.first;
1443 if(is_preview_allowed(sa)==0) {
1444 rem_blockhandler(sa, IMAGE_HANDLER_PREVIEW);
1445 G.scene->r.scemode &= ~R_COMP_CROP; /* quite weak */
1449 block= uiNewBlock(&sa->uiblocks, "image_panel_preview", UI_EMBOSS, UI_HELV, sa->win);
1450 uiPanelControl(UI_PNL_SOLID | UI_PNL_CLOSE | UI_PNL_SCALE | cntrl);
1451 uiSetPanelHandler(IMAGE_HANDLER_PREVIEW); // for close and esc
1453 ofsx= -150+(sa->winx/2)/sima->blockscale;
1454 ofsy= -100+(sa->winy/2)/sima->blockscale;
1455 if(uiNewPanel(sa, block, "Preview", "Image", ofsx, ofsy, 300, 200)==0) return;
1457 uiBlockSetDrawExtraFunc(block, preview_cb);
1461 static void image_blockhandlers(ScrArea *sa)
1463 SpaceImage *sima= sa->spacedata.first;
1466 /* warning; blocks need to be freed each time, handlers dont remove */
1467 uiFreeBlocksWin(&sa->uiblocks, sa->win);
1469 for(a=0; a<SPACE_MAXHANDLER; a+=2) {
1470 switch(sima->blockhandler[a]) {
1472 case IMAGE_HANDLER_PROPERTIES:
1473 image_panel_properties(sima->blockhandler[a+1]);
1475 case IMAGE_HANDLER_GAME_PROPERTIES:
1476 image_panel_game_properties(sima->blockhandler[a+1]);
1478 // case IMAGE_HANDLER_TRANSFORM_PROPERTIES:
1479 // if (EM_texFaceCheck())
1480 // image_panel_transform_properties(sima->blockhandler[a+1]);
1482 case IMAGE_HANDLER_VIEW_PROPERTIES:
1483 image_panel_view_properties(sima->blockhandler[a+1]);
1485 case IMAGE_HANDLER_PAINT:
1486 image_panel_paint(sima->blockhandler[a+1]);
1488 case IMAGE_HANDLER_CURVES:
1489 image_panel_curves(sima->blockhandler[a+1]);
1491 case IMAGE_HANDLER_PREVIEW:
1492 image_panel_preview(sa, sima->blockhandler[a+1]);
1495 /* clear action value for event */
1496 sima->blockhandler[a+1]= 0;
1498 uiDrawBlocksPanels(sa, 0);
1501 void imagespace_composite_flipbook(ScrArea *sa)
1503 SpaceImage *sima= sa->spacedata.first;
1505 int cfrao= G.scene->r.cfra;
1508 if(sa->spacetype!=SPACE_IMAGE)
1510 if(sima->iuser.frames<2)
1512 if(G.scene->nodetree==NULL)
1515 sfra= sima->iuser.sfra;
1516 efra= sima->iuser.sfra + sima->iuser.frames-1;
1517 G.scene->nodetree->test_break= blender_test_break;
1519 for(G.scene->r.cfra=sfra; G.scene->r.cfra<=efra; G.scene->r.cfra++) {
1521 set_timecursor(CFRA);
1523 BKE_image_all_free_anim_ibufs(CFRA);
1524 ntreeCompositTagAnimated(G.scene->nodetree);
1525 ntreeCompositExecTree(G.scene->nodetree, &G.scene->r, G.scene->r.cfra!=cfrao); /* 1 is no previews */
1529 ibuf= BKE_image_get_ibuf(sima->image, &sima->iuser);
1530 /* save memory in flipbooks */
1532 imb_freerectfloatImBuf(ibuf);
1534 if(blender_test_break())
1537 G.scene->nodetree->test_break= NULL;
1542 allqueue(REDRAWNODE, 1);
1543 allqueue(REDRAWIMAGE, 1);
1545 G.scene->r.cfra= cfrao;
1548 static void imagespace_grid(SpaceImage *sima)
1550 float gridsize, gridstep= 1.0f/32.0f;
1551 float fac, blendfac;
1553 gridsize= sima->zoom;
1555 calc_image_view(sima, 'f');
1556 myortho2(sima->v2d.cur.xmin, sima->v2d.cur.xmax, sima->v2d.cur.ymin, sima->v2d.cur.ymax);
1558 BIF_ThemeColorShade(TH_BACK, 20);
1559 glRectf(0.0, 0.0, 1.0, 1.0);
1561 if(gridsize<=0.0f) return;
1564 while(gridsize<1.0f) {
1570 while(gridsize>=4.0f) {
1576 /* the fine resolution level */
1577 blendfac= 0.25*gridsize - floor(0.25*gridsize);
1578 CLAMP(blendfac, 0.0, 1.0);
1579 BIF_ThemeColorShade(TH_BACK, (int)(20.0*(1.0-blendfac)));
1584 glVertex2f(0.0f, fac);
1585 glVertex2f(1.0f, fac);
1586 glVertex2f(fac, 0.0f);
1587 glVertex2f(fac, 1.0f);
1591 /* the large resolution level */
1592 BIF_ThemeColor(TH_BACK);
1596 glVertex2f(0.0f, fac);
1597 glVertex2f(1.0f, fac);
1598 glVertex2f(fac, 0.0f);
1599 glVertex2f(fac, 1.0f);
1606 static void sima_draw_alpha_backdrop(SpaceImage *sima, float x1, float y1, float xsize, float ysize)
1608 float tile= sima->zoom*15.0f;
1609 float x, y, maxx, maxy;
1611 glColor3ub(100, 100, 100);
1612 glRectf(x1, y1, x1 + sima->zoom*xsize, y1 + sima->zoom*ysize);
1613 glColor3ub(160, 160, 160);
1615 maxx= x1+sima->zoom*xsize;
1616 maxy= y1+sima->zoom*ysize;
1618 for(x=0; x<xsize; x+=30) {
1619 for(y=0; y<ysize; y+=30) {
1620 float fx= x1 + sima->zoom*x;
1621 float fy= y1 + sima->zoom*y;
1622 float tilex= tile, tiley= tile;
1629 glRectf(fx, fy, fx + tilex, fy + tiley);
1632 for(x=15; x<xsize; x+=30) {
1633 for(y=15; y<ysize; y+=30) {
1634 float fx= x1 + sima->zoom*x;
1635 float fy= y1 + sima->zoom*y;
1636 float tilex= tile, tiley= tile;
1643 glRectf(fx, fy, fx + tilex, fy + tiley);
1648 static void sima_draw_alpha_pixels(float x1, float y1, int rectx, int recty, unsigned int *recti)
1651 /* swap bytes, so alpha is most significant one, then just draw it as luminance int */
1652 if(G.order==B_ENDIAN)
1653 glPixelStorei(GL_UNPACK_SWAP_BYTES, 1);
1654 glaDrawPixelsSafe(x1, y1, rectx, recty, rectx, GL_LUMINANCE, GL_UNSIGNED_INT, recti);
1655 glPixelStorei(GL_UNPACK_SWAP_BYTES, 0);
1658 static void sima_draw_alpha_pixelsf(float x1, float y1, int rectx, int recty, float *rectf)
1660 float *trectf= MEM_mallocN(rectx*recty*4, "temp");
1663 for(a= rectx*recty -1, b= 4*a+3; a>=0; a--, b-=4)
1664 trectf[a]= rectf[b];
1666 glaDrawPixelsSafe(x1, y1, rectx, recty, rectx, GL_LUMINANCE, GL_FLOAT, trectf);
1668 /* ogl trick below is slower... (on ATI 9600) */
1669 // glColorMask(1, 0, 0, 0);
1670 // glaDrawPixelsSafe(x1, y1, rectx, recty, rectx, GL_RGBA, GL_FLOAT, rectf+3);
1671 // glColorMask(0, 1, 0, 0);
1672 // glaDrawPixelsSafe(x1, y1, rectx, recty, rectx, GL_RGBA, GL_FLOAT, rectf+2);
1673 // glColorMask(0, 0, 1, 0);
1674 // glaDrawPixelsSafe(x1, y1, rectx, recty, rectx, GL_RGBA, GL_FLOAT, rectf+1);
1675 // glColorMask(1, 1, 1, 1);
1678 static void sima_draw_zbuf_pixels(float x1, float y1, int rectx, int recty, int *recti)
1683 /* zbuffer values are signed, so we need to shift color range */
1684 glPixelTransferf(GL_RED_SCALE, 0.5f);
1685 glPixelTransferf(GL_GREEN_SCALE, 0.5f);
1686 glPixelTransferf(GL_BLUE_SCALE, 0.5f);
1687 glPixelTransferf(GL_RED_BIAS, 0.5f);
1688 glPixelTransferf(GL_GREEN_BIAS, 0.5f);
1689 glPixelTransferf(GL_BLUE_BIAS, 0.5f);
1691 glaDrawPixelsSafe(x1, y1, rectx, recty, rectx, GL_LUMINANCE, GL_INT, recti);
1693 glPixelTransferf(GL_RED_SCALE, 1.0f);
1694 glPixelTransferf(GL_GREEN_SCALE, 1.0f);
1695 glPixelTransferf(GL_BLUE_SCALE, 1.0f);
1696 glPixelTransferf(GL_RED_BIAS, 0.0f);
1697 glPixelTransferf(GL_GREEN_BIAS, 0.0f);
1698 glPixelTransferf(GL_BLUE_BIAS, 0.0f);
1701 static void sima_draw_zbuffloat_pixels(float x1, float y1, int rectx, int recty, float *rect_float)
1703 float bias, scale, *rectf, clipend;
1706 if(rect_float==NULL)
1709 if(G.scene->camera && G.scene->camera->type==OB_CAMERA) {
1710 bias= ((Camera *)G.scene->camera->data)->clipsta;
1711 clipend= ((Camera *)G.scene->camera->data)->clipend;
1712 scale= 1.0f/(clipend-bias);
1720 rectf= MEM_mallocN(rectx*recty*4, "temp");
1721 for(a= rectx*recty -1; a>=0; a--) {
1722 if(rect_float[a]>clipend)
1724 else if(rect_float[a]<bias)
1727 rectf[a]= 1.0f - (rect_float[a]-bias)*scale;
1728 rectf[a]*= rectf[a];
1731 glaDrawPixelsSafe(x1, y1, rectx, recty, rectx, GL_LUMINANCE, GL_FLOAT, rectf);
1736 static void imagewindow_draw_renderinfo(ScrArea *sa)
1738 SpaceImage *sima= sa->spacedata.first;
1741 char *str= sima->showspare?sima->info_spare:sima->info_str;
1747 rect.ymin= rect.ymax-RW_HEADERY;
1749 glaDefine2DArea(&rect);
1751 /* clear header rect */
1752 BIF_GetThemeColor3fv(TH_BACK, colf);
1753 glClearColor(colf[0]+0.1f, colf[1]+0.1f, colf[2]+0.1f, 1.0);
1754 glClear(GL_COLOR_BUFFER_BIT);
1756 BIF_ThemeColor(TH_TEXT_HI);
1757 glRasterPos2i(12, 5);
1758 if(sima->showspare) {
1759 BMF_DrawString(G.fonts, "(Previous)");
1760 glRasterPos2i(72, 5);
1762 BMF_DrawString(G.fonts, str);
1765 void drawimagespace(ScrArea *sa, void *spacedata)
1767 SpaceImage *sima= spacedata;
1773 short sx, sy, dx, dy, show_render= 0, show_viewer= 0;
1774 float xuser_asp, yuser_asp;
1775 /* If derived data is used then make sure that object
1776 * is up-to-date... might not be the case because updates
1777 * are normally done in drawview and could get here before
1780 if (!G.obedit && OBACT && (sima->flag & SI_DRAWSHADOW)) {
1781 object_handle_update(OBACT);
1784 BIF_GetThemeColor3fv(TH_BACK, col);
1785 glClearColor(col[0], col[1], col[2], 0.0);
1786 glClear(GL_COLOR_BUFFER_BIT);
1788 bwin_clear_viewmat(sa->win); /* clear buttons view */
1791 if(sima->image && sima->image->source==IMA_SRC_VIEWER) {
1793 if(sima->image->type==IMA_TYPE_R_RESULT)
1798 aspect_sima(sima, &xuser_asp, &yuser_asp);
1802 /* UGLY hack? until now iusers worked fine... but for flipbook viewer we need this */
1803 if(sima->image->type==IMA_TYPE_COMPOSITE) {
1804 ImageUser *iuser= ntree_get_active_iuser(G.scene->nodetree);
1806 BKE_image_user_calc_imanr(iuser, G.scene->r.cfra, 0);
1807 G.sima->iuser= *iuser;
1810 /* and we check for spare */
1811 ibuf= imagewindow_get_ibuf(sima);
1814 if(ibuf==NULL || (ibuf->rect==NULL && ibuf->rect_float==NULL)) {
1815 imagespace_grid(sima);
1816 if(show_viewer==0) {
1818 drawcursor_sima(xuser_asp, yuser_asp);
1822 float xim, yim, xoffs=0.0f, yoffs= 0.0f;
1824 if(image_preview_active(sa, &xim, &yim)) {
1825 xoffs= G.scene->r.disprect.xmin;
1826 yoffs= G.scene->r.disprect.ymin;
1828 calc_image_view(sima, 'f');
1829 myortho2(G.v2d->cur.xmin, G.v2d->cur.xmax, G.v2d->cur.ymin, G.v2d->cur.ymax);
1830 glRectf(0.0f, 0.0f, 1.0f, 1.0f);
1834 xim= ibuf->x * xuser_asp; yim= ibuf->y * yuser_asp;
1838 x1= sima->zoom*xoffs + ((float)sa->winx - sima->zoom*(float)xim)/2.0f;
1839 y1= sima->zoom*yoffs + ((float)sa->winy - sima->zoom*(float)yim)/2.0f;
1841 x1-= sima->zoom*sima->xof;
1842 y1-= sima->zoom*sima->yof;
1844 /* needed for gla draw */
1846 rcti rct= sa->winrct;
1848 imagewindow_draw_renderinfo(sa); /* calls glaDefine2DArea too */
1850 rct.ymax-=RW_HEADERY;
1851 glaDefine2DArea(&rct);
1853 else glaDefine2DArea(&sa->winrct);
1855 glPixelZoom(sima->zoom * xuser_asp, sima->zoom * yuser_asp);
1857 if(sima->flag & SI_EDITTILE) {
1858 /* create char buffer from float if needed */
1859 if(ibuf->rect_float && ibuf->rect==NULL)
1860 IMB_rect_from_float(ibuf);
1862 glaDrawPixelsSafe(x1, y1, ibuf->x, ibuf->y, ibuf->x, GL_RGBA, GL_UNSIGNED_BYTE, ibuf->rect);
1864 glPixelZoom(1.0, 1.0);
1866 dx= ibuf->x/sima->image->xrep;
1867 dy= ibuf->y/sima->image->yrep;
1868 sy= (sima->curtile / sima->image->xrep);
1869 sx= sima->curtile - sy*sima->image->xrep;
1874 calc_image_view(sima, 'p'); /* pixel */
1875 myortho2(G.v2d->cur.xmin, G.v2d->cur.xmax, G.v2d->cur.ymin, G.v2d->cur.ymax);
1878 glPolygonMode(GL_FRONT_AND_BACK, GL_LINE); glRects(sx, sy, sx+dx-1, sy+dy-1); glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
1880 glPolygonMode(GL_FRONT_AND_BACK, GL_LINE); glRects(sx+1, sy+1, sx+dx, sy+dy); glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
1882 else if(sima->mode==SI_TEXTURE) {
1884 if(sima->image->tpageflag & IMA_TILES) {
1886 /* just leave this a while */
1887 if(sima->image->xrep<1) return;
1888 if(sima->image->yrep<1) return;
1890 if(sima->curtile >= sima->image->xrep*sima->image->yrep)
1891 sima->curtile = sima->image->xrep*sima->image->yrep - 1;
1893 dx= ibuf->x/sima->image->xrep;
1894 dy= ibuf->y/sima->image->yrep;
1896 sy= (sima->curtile / sima->image->xrep);
1897 sx= sima->curtile - sy*sima->image->xrep;
1902 /* create char buffer from float if needed */
1903 if(ibuf->rect_float && ibuf->rect==NULL)
1904 IMB_rect_from_float(ibuf);
1906 rect= get_part_from_ibuf(ibuf, sx, sy, sx+dx, sy+dy);
1908 /* rect= ibuf->rect; */
1909 for(sy= 0; sy+dy<=ibuf->y; sy+= dy) {
1910 for(sx= 0; sx+dx<=ibuf->x; sx+= dx) {
1911 glaDrawPixelsSafe(x1+sx*sima->zoom, y1+sy*sima->zoom, dx, dy, dx, GL_RGBA, GL_UNSIGNED_BYTE, rect);
1918 float x1_rep, y1_rep;
1920 double time_current;
1921 short loop_draw_ok = 0;
1923 if (sima->flag & SI_DRAW_TILE) {
1927 time_current = PIL_check_seconds_timer();
1929 for (x_rep= ((int)G.v2d->cur.xmin)-1; x_rep < G.v2d->cur.xmax; x_rep++) {
1930 x1_rep=x1+ ((x_rep* ibuf->x * sima->zoom) *xuser_asp);
1931 for (y_rep= ((int)G.v2d->cur.ymin)-1; y_rep < G.v2d->cur.ymax; y_rep++) {
1932 y1_rep=y1+ ((y_rep * ibuf->y *sima->zoom) *yuser_asp);
1934 /* end repeating image loop */
1941 /*printf("Drawing %d %d zoom:%.6f (%.6f %.6f), (%.6f %.6f)\n", x_rep, y_rep, sima->zoom, G.v2d->cur.xmin, G.v2d->cur.ymin, G.v2d->cur.xmax, G.v2d->cur.ymax);*/
1943 /* this part is generic image display */
1944 if(sima->flag & SI_SHOW_ALPHA) {
1946 sima_draw_alpha_pixels(x1_rep, y1_rep, ibuf->x, ibuf->y, ibuf->rect);
1947 else if(ibuf->rect_float && ibuf->channels==4)
1948 sima_draw_alpha_pixelsf(x1_rep, y1_rep, ibuf->x, ibuf->y, ibuf->rect_float);
1950 else if(sima->flag & SI_SHOW_ZBUF && ((ibuf->zbuf || ibuf->zbuf_float || (ibuf->channels==1)) == 0)) {
1952 sima_draw_zbuf_pixels(x1_rep, y1_rep, ibuf->x, ibuf->y, ibuf->zbuf);
1953 else if(ibuf->zbuf_float)
1954 sima_draw_zbuffloat_pixels(x1_rep, y1_rep, ibuf->x, ibuf->y, ibuf->zbuf_float);
1955 else if(ibuf->channels==1)
1956 sima_draw_zbuffloat_pixels(x1_rep, y1_rep, ibuf->x, ibuf->y, ibuf->rect_float);
1959 if(sima->flag & SI_USE_ALPHA) {
1960 sima_draw_alpha_backdrop(sima, x1_rep, y1_rep, (float)ibuf->x, (float)ibuf->y);
1962 glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
1965 /* detect if we need to redo the curve map.
1966 ibuf->rect is zero for compositor and render results after change
1967 convert to 32 bits always... drawing float rects isnt supported well (atis)
1969 NOTE: if float buffer changes, we have to manually remove the rect
1972 if(ibuf->rect_float) {
1973 if(ibuf->rect==NULL) {
1974 if(image_curves_active(sa))
1975 curvemapping_do_ibuf(G.sima->cumap, ibuf);
1977 IMB_rect_from_float(ibuf);
1982 glaDrawPixelsSafe(x1_rep, y1_rep, ibuf->x, ibuf->y, ibuf->x, GL_RGBA, GL_UNSIGNED_BYTE, ibuf->rect);
1984 // glaDrawPixelsSafe(x1_rep, y1_rep, ibuf->x, ibuf->y, ibuf->x, GL_RGBA, GL_FLOAT, ibuf->rect_float);
1986 if(sima->flag & SI_USE_ALPHA)
1987 glDisable(GL_BLEND);
1990 /* only draw once */
1992 break; /* only draw once */
1993 } else if ((PIL_check_seconds_timer() - time_current) > 0.25) {
1998 /* tile draw loop */
2000 /* only draw once */
2001 if(!loop_draw_ok) break;
2003 /* tile draw loop */
2007 brush= G.scene->toolsettings->imapaint.brush;
2008 if(brush && (G.scene->toolsettings->imapaint.tool == PAINT_TOOL_CLONE)) {
2010 unsigned char *clonerect;
2012 /* this is not very efficient, but glDrawPixels doesn't allow
2013 drawing with alpha */
2014 clonerect= alloc_alpha_clone_image(&w, &h);
2018 offx = sima->zoom*ibuf->x * + brush->clone.offset[0];
2019 offy = sima->zoom*ibuf->y * + brush->clone.offset[1];
2022 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
2023 glaDrawPixelsSafe(x1 + offx, y1 + offy, w, h, w, GL_RGBA, GL_UNSIGNED_BYTE, clonerect);
2024 glDisable(GL_BLEND);
2026 MEM_freeN(clonerect);
2030 glPixelZoom(1.0, 1.0);
2032 if(show_viewer==0) {
2034 drawcursor_sima(xuser_asp, yuser_asp);
2038 glPixelZoom(1.0, 1.0);
2040 calc_image_view(sima, 'f'); /* float */
2043 draw_image_transform(ibuf, xuser_asp, yuser_asp);
2045 mywinset(sa->win); /* restore scissor after gla call... */
2046 myortho2(-0.375, sa->winx-0.375, -0.375, sa->winy-0.375);
2048 if(G.rendering==0) {
2049 draw_image_view_tool();
2050 draw_image_view_icon();
2052 draw_area_emboss(sa);
2054 /* it is important to end a view in a transform compatible with buttons */
2055 bwin_scalematrix(sa->win, sima->blockscale, sima->blockscale, sima->blockscale);
2056 if(!(G.rendering && show_render))
2057 image_blockhandlers(sa);
2059 sa->win_swap= WIN_BACK_OK;
2062 static void image_zoom_power_of_two(void)
2064 /* Make zoom a power of 2 */
2066 G.sima->zoom = 1 / G.sima->zoom;
2067 G.sima->zoom = log(G.sima->zoom) / log(2);
2068 G.sima->zoom = ceil(G.sima->zoom);
2069 G.sima->zoom = pow(2, G.sima->zoom);
2070 G.sima->zoom = 1 / G.sima->zoom;
2073 static void image_zoom_set_factor(float zoomfac)
2075 SpaceImage *sima= curarea->spacedata.first;
2078 if (zoomfac <= 0.0f)
2081 sima->zoom *= zoomfac;
2083 if (sima->zoom > 0.1f && sima->zoom < 4.0f)
2086 /* check zoom limits */
2088 calc_image_view(G.sima, 'f'); /* was 'p' are there any cases where this should be 'p'?*/
2092 ImBuf *ibuf= imagewindow_get_ibuf(sima);
2096 /* I know a bit weak... but preview uses not actual image size */
2097 if(image_preview_active(curarea, &xim, &yim)) {
2107 width *= sima->zoom;
2108 height *= sima->zoom;
2110 if ((width < 4) && (height < 4))
2111 sima->zoom /= zoomfac;
2112 else if((curarea->winrct.xmax - curarea->winrct.xmin) <= sima->zoom)
2113 sima->zoom /= zoomfac;
2114 else if((curarea->winrct.ymax - curarea->winrct.ymin) <= sima->zoom)
2115 sima->zoom /= zoomfac;
2118 void image_viewmove(int mode)
2120 short mval[2], mvalo[2], zoom0;
2124 getmouseco_sc(mvalo);
2125 zoom0= G.sima->zoom;
2127 oldcursor=get_cursor();
2128 win=winlay_get_active_window();
2130 SetBlenderCursor(BC_NSEW_SCROLLCURSOR);
2132 while(get_mbut()&(L_MOUSE|M_MOUSE)) {
2134 getmouseco_sc(mval);
2136 if(mvalo[0]!=mval[0] || mvalo[1]!=mval[1]) {
2139 G.sima->xof += (mvalo[0]-mval[0])/G.sima->zoom;
2140 G.sima->yof += (mvalo[1]-mval[1])/G.sima->zoom;
2145 factor= 1.0+(float)(mvalo[0]-mval[0]+mvalo[1]-mval[1])/300.0;
2146 image_zoom_set_factor(factor);
2152 scrarea_do_windraw(curarea);
2153 screen_swapbuffers();
2155 else BIF_wait_for_statechange();
2157 window_set_cursor(win, oldcursor);
2159 if(image_preview_active(curarea, NULL, NULL)) {
2160 /* recalculates new preview rect */
2161 scrarea_do_windraw(curarea);
2162 image_preview_event(2);
2166 void image_viewzoom(unsigned short event, int invert)
2168 SpaceImage *sima= curarea->spacedata.first;
2170 if(event==WHEELDOWNMOUSE || event==PADMINUS)
2171 image_zoom_set_factor((U.uiflag & USER_WHEELZOOMDIR)? 1.25: 0.8);
2172 else if(event==WHEELUPMOUSE || event==PADPLUSKEY)
2173 image_zoom_set_factor((U.uiflag & USER_WHEELZOOMDIR)? 0.8: 1.25);
2174 else if(event==PAD1)
2176 else if(event==PAD2)
2177 sima->zoom= (invert)? 2.0: 0.5;
2178 else if(event==PAD4)
2179 sima->zoom= (invert)? 4.0: 0.25;
2180 else if(event==PAD8)
2181 sima->zoom= (invert)? 8.0: 0.125;
2183 /* ensure pixel exact locations for draw */
2184 sima->xof= (int)sima->xof;
2185 sima->yof= (int)sima->yof;
2187 if(image_preview_active(curarea, NULL, NULL)) {
2188 /* recalculates new preview rect */
2189 scrarea_do_windraw(curarea);
2190 image_preview_event(2);
2195 * Updates the fields of the View2D member of the SpaceImage struct.
2196 * Default behavior is to reset the position of the image and set the zoom to 1
2197 * If the image will not fit within the window rectangle, the zoom is adjusted
2202 void image_home(void)
2205 int width, height, imgwidth, imgheight;
2208 if (curarea->spacetype != SPACE_IMAGE) return;
2209 ibuf= imagewindow_get_ibuf(G.sima);
2217 imgheight = ibuf->y;
2220 /* Check if the image will fit in the image with zoom==1 */
2221 width = curarea->winx;
2222 height = curarea->winy;
2223 if (((imgwidth >= width) || (imgheight >= height)) &&
2224 ((width > 0) && (height > 0))) {
2225 /* Find the zoom value that will fit the image in the image space */
2226 zoomX = ((float)width) / ((float)imgwidth);
2227 zoomY = ((float)height) / ((float)imgheight);
2228 G.sima->zoom= MIN2(zoomX, zoomY);
2230 image_zoom_power_of_two();
2236 G.sima->xof= G.sima->yof= 0.0f;
2238 calc_image_view(G.sima, 'f'); /* was 'p' are there any cases where this should be 'p'?*/
2239 /*calc_arearcts(curarea);*/
2240 scrarea_queue_winredraw(curarea);
2241 scrarea_queue_winredraw(curarea);
2244 void image_viewcenter(void)
2246 ImBuf *ibuf= BKE_image_get_ibuf(G.sima->image, &G.sima->iuser);
2247 float size, min[2], max[2], d[2], xim=256.0f, yim=256.0f;
2249 if( is_uv_tface_editing_allowed()==0 ) return;
2251 if (!minmax_tface_uv(min, max)) return;
2258 G.sima->xof= (int) (((min[0] + max[0])*0.5f - 0.5f)*xim);
2259 G.sima->yof= (int) (((min[1] + max[1])*0.5f - 0.5f)*yim);
2261 d[0] = max[0] - min[0];
2262 d[1] = max[1] - min[1];
2263 size= 0.5*MAX2(d[0], d[1])*MAX2(xim, yim)/256.0f;
2265 if(size<=0.01) size= 0.01;
2267 G.sima->zoom= 0.7/size;
2269 calc_image_view(G.sima, 'f'); /* was 'p' are there any cases where 'p' is still needed? */
2271 scrarea_queue_winredraw(curarea);
2275 /* *********************** render callbacks ***************** */
2277 /* set on initialize render, only one render output to imagewindow can exist, so the global isnt dangerous yet :) */
2278 static ScrArea *image_area= NULL;
2280 /* can get as well the full picture, as the parts while rendering */
2281 static void imagewindow_progress(ScrArea *sa, RenderResult *rr, volatile rcti *renrect)
2283 SpaceImage *sima= sa->spacedata.first;
2284 float x1, y1, *rectf= NULL;
2285 unsigned int *rect32= NULL;
2286 int ymin, ymax, xmin, xmax;
2288 /* if renrect argument, we only display scanlines */
2290 /* if ymax==recty, rendering of layer is ready, we should not draw, other things happen... */
2291 if(rr->renlay==NULL || renrect->ymax>=rr->recty)
2294 /* xmin here is first subrect x coord, xmax defines subrect width */
2295 xmin = renrect->xmin;
2296 xmax = renrect->xmax - xmin;
2299 ymin= renrect->ymin;
2300 ymax= renrect->ymax - ymin;
2303 renrect->ymin= renrect->ymax;
2307 xmax = rr->rectx - 2*rr->crop;
2308 ymax = rr->recty - 2*rr->crop;
2311 /* image window cruft */
2313 /* find current float rect for display, first case is after composit... still weak */
2318 rect32= (unsigned int *)rr->rect32;
2320 if(rr->renlay==NULL || rr->renlay->rectf==NULL) return;
2321 rectf= rr->renlay->rectf;
2325 /* if scanline updates... */
2326 rectf+= 4*(rr->rectx*ymin + xmin);
2328 /* when rendering more pixels than needed, we crop away cruft */
2330 rectf+= 4*(rr->crop*rr->rectx + rr->crop);
2333 /* tilerect defines drawing offset from (0,0) */
2334 /* however, tilerect (xmin, ymin) is first pixel */
2335 x1 = sima->centx + (rr->tilerect.xmin + rr->crop + xmin)*sima->zoom;
2336 y1 = sima->centy + (rr->tilerect.ymin + rr->crop + ymin)*sima->zoom;
2338 /* needed for gla draw */
2339 { rcti rct= sa->winrct; rct.ymax-= RW_HEADERY; glaDefine2DArea(&rct);}
2341 glPixelZoom(sima->zoom, sima->zoom);
2344 glaDrawPixelsSafe(x1, y1, xmax, ymax, rr->rectx, GL_RGBA, GL_UNSIGNED_BYTE, rect32);
2346 glaDrawPixelsSafe_to32(x1, y1, xmax, ymax, rr->rectx, rectf);
2348 glPixelZoom(1.0, 1.0);
2353 /* in render window; display a couple of scanlines of rendered image */
2354 /* NOTE: called while render, so no malloc allowed! */
2355 static void imagewindow_progress_display_cb(RenderResult *rr, volatile rcti *rect)
2360 imagewindow_progress(image_area, rr, rect);
2362 /* no screen_swapbuffers, prevent any other window to draw */
2367 /* unused, init_display_cb is called on each render */
2368 static void imagewindow_clear_display_cb(RenderResult *rr)
2374 /* returns biggest area that is not uv/image editor. Note that it uses buttons */
2375 /* window as the last possible alternative. */
2376 static ScrArea *biggest_non_image_area(void)
2378 ScrArea *sa, *big= NULL;
2379 int size, maxsize= 0, bwmaxsize= 0;
2382 for(sa= G.curscreen->areabase.first; sa; sa= sa->next) {
2383 if(sa->winx > 10 && sa->winy > 10) {
2384 size= sa->winx*sa->winy;
2385 if(sa->spacetype == SPACE_BUTS) {
2386 if(foundwin == 0 && size > bwmaxsize) {
2391 else if(sa->spacetype != SPACE_IMAGE && size > maxsize) {
2402 static ScrArea *biggest_area(void)
2404 ScrArea *sa, *big= NULL;
2405 int size, maxsize= 0;
2407 for(sa= G.curscreen->areabase.first; sa; sa= sa->next) {
2408 size= sa->winx*sa->winy;
2409 if(size > maxsize) {
2418 /* if R_DISPLAYIMAGE
2419 use Image Window showing Render Result
2420 else: turn largest non-image area into Image Window (not to frustrate texture or composite usage)
2421 else: then we use Image Window anyway...
2423 make a new temp fullscreen area with Image Window
2426 static ScrArea *find_area_showing_r_result(void)
2431 /* find an imagewindow showing render result */
2432 for(sa=G.curscreen->areabase.first; sa; sa= sa->next) {
2433 if(sa->spacetype==SPACE_IMAGE) {
2434 sima= sa->spacedata.first;
2435 if(sima->image && sima->image->type==IMA_TYPE_R_RESULT)
2442 static ScrArea *imagewindow_set_render_display(void)
2447 sa= find_area_showing_r_result();
2450 /* find largest open non-image area */
2451 sa= biggest_non_image_area();
2453 newspace(sa, SPACE_IMAGE);
2454 sima= sa->spacedata.first;
2456 /* makes ESC go back to prev space */
2457 sima->flag |= SI_PREVSPACE;
2460 /* use any area of decent size */
2462 if(sa->spacetype!=SPACE_IMAGE) {
2463 newspace(sa, SPACE_IMAGE);
2464 sima= sa->spacedata.first;
2466 /* makes ESC go back to prev space */
2467 sima->flag |= SI_PREVSPACE;
2472 sima= sa->spacedata.first;
2474 /* get the correct image, and scale it */
2475 sima->image= BKE_image_verify_viewer(IMA_TYPE_R_RESULT, "Render Result");
2477 if(G.displaymode==R_DISPLAYSCREEN) {
2479 sima->flag |= SI_FULLWINDOW;
2480 /* fullscreen works with lousy curarea */
2490 static void imagewindow_init_display_cb(RenderResult *rr)
2493 image_area= imagewindow_set_render_display();
2496 SpaceImage *sima= image_area->spacedata.first;
2498 areawinset(image_area->win);
2500 if(sima->info_str==NULL)
2501 sima->info_str= MEM_callocN(RW_MAXTEXT, "info str imagewin");
2503 /* calc location using original size (tiles don't tell) */
2504 sima->centx= (image_area->winx - sima->zoom*(float)rr->rectx)/2.0f;
2505 sima->centy= (image_area->winy - sima->zoom*(float)rr->recty)/2.0f;
2507 sima->centx-= sima->zoom*sima->xof;
2508 sima->centy-= sima->zoom*sima->yof;
2510 drawimagespace(image_area, sima);
2511 if(image_area->headertype) scrarea_do_headdraw(image_area);
2512 screen_swapbuffers();
2514 allqueue(REDRAWIMAGE, 0); /* redraw in end */
2518 /* coming from BIF_toggle_render_display() */
2519 void imagewindow_toggle_render(void)
2523 /* check if any imagewindow is showing temporal render output */
2524 for(sa=G.curscreen->areabase.first; sa; sa= sa->next) {
2525 if(sa->spacetype==SPACE_IMAGE) {
2526 SpaceImage *sima= sa->spacedata.first;
2528 if(sima->image && sima->image->type==IMA_TYPE_R_RESULT)
2529 if(sima->flag & (SI_PREVSPACE|SI_FULLWINDOW))
2534 addqueue(sa->win, ESCKEY, 1); /* also returns from fullscreen */
2537 sa= imagewindow_set_render_display();
2538 scrarea_queue_headredraw(sa);
2539 scrarea_queue_winredraw(sa);
2543 /* NOTE: called while render, so no malloc allowed! */
2544 static void imagewindow_renderinfo_cb(RenderStats *rs)
2548 SpaceImage *sima= image_area->spacedata.first;
2551 make_renderinfo_string(rs, sima->info_str);
2553 imagewindow_draw_renderinfo(image_area);
2555 /* no screen_swapbuffers, prevent any other window to draw */
2560 void imagewindow_render_callbacks(Render *re)
2562 RE_display_init_cb(re, imagewindow_init_display_cb);
2563 RE_display_draw_cb(re, imagewindow_progress_display_cb);
2564 RE_display_clear_cb(re, imagewindow_clear_display_cb);
2565 RE_stats_draw_cb(re, imagewindow_renderinfo_cb);
2568 void imagewin_store_spare(void)
2570 ScrArea *sa= find_area_showing_r_result();
2574 SpaceImage *sima= sa->spacedata.first;
2576 if(sima->spare==NULL)
2579 /* only store when it does not show spare */
2580 if(sima->showspare==0)
2585 IMB_freeImBuf(sima->spare);
2587 /* make a copy of render result */
2588 ibuf= BKE_image_get_ibuf(sima->image, &sima->iuser);
2589 sima->spare= IMB_dupImBuf(ibuf);
2592 BLI_strncpy(sima->info_spare, sima->info_str, RW_MAXTEXT);
2597 /* context: in current image window? */
2598 void imagewindow_swap_render_rects(void)
2600 ScrArea *sa= find_area_showing_r_result();
2603 SpaceImage *sima= sa->spacedata.first;
2604 ImBuf *ibuf= BKE_image_get_ibuf(sima->image, &sima->iuser);
2607 sima->showspare ^= 1;
2609 if(sima->spare==NULL)
2610 sima->spare= IMB_allocImBuf(ibuf->x, ibuf->y, 32, 0, 0);
2611 if(sima->info_spare==NULL)
2612 sima->info_spare= MEM_callocN(RW_MAXTEXT, "info str imagewin");
2614 allqueue(REDRAWIMAGE, 0);