2.5: Image window, some small changes:
[blender.git] / source / blender / editors / space_image / space_image.c
1 /**
2  * $Id:
3  *
4  * ***** BEGIN GPL LICENSE BLOCK *****
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version. 
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software Foundation,
18  * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
19  *
20  * The Original Code is Copyright (C) 2008 Blender Foundation.
21  * All rights reserved.
22  *
23  * 
24  * Contributor(s): Blender Foundation
25  *
26  * ***** END GPL LICENSE BLOCK *****
27  */
28
29 #include <string.h>
30 #include <stdio.h>
31
32 #include "DNA_image_types.h"
33 #include "DNA_mesh_types.h"
34 #include "DNA_meshdata_types.h"
35 #include "DNA_object_types.h"
36 #include "DNA_space_types.h"
37 #include "DNA_scene_types.h"
38 #include "DNA_screen_types.h"
39
40 #include "MEM_guardedalloc.h"
41
42 #include "BLI_blenlib.h"
43 #include "BLI_arithb.h"
44 #include "BLI_editVert.h"
45 #include "BLI_rand.h"
46
47 #include "BKE_colortools.h"
48 #include "BKE_context.h"
49 #include "BKE_image.h"
50 #include "BKE_screen.h"
51 #include "BKE_utildefines.h"
52
53 #include "IMB_imbuf.h"
54 #include "IMB_imbuf_types.h"
55
56 #include "ED_image.h"
57 #include "ED_mesh.h"
58 #include "ED_space_api.h"
59 #include "ED_screen.h"
60 #include "ED_uvedit.h"
61
62 #include "BIF_gl.h"
63 #include "BIF_glutil.h"
64
65 #include "RNA_access.h"
66
67 #include "WM_api.h"
68 #include "WM_types.h"
69
70 #include "UI_interface.h"
71 #include "UI_resources.h"
72 #include "UI_view2d.h"
73
74 #include "image_intern.h"
75
76 /* ******************** default callbacks for image space ***************** */
77
78 static SpaceLink *image_new(const bContext *C)
79 {
80         ARegion *ar;
81         SpaceImage *simage;
82         
83         simage= MEM_callocN(sizeof(SpaceImage), "initimage");
84         simage->spacetype= SPACE_IMAGE;
85         simage->zoom= 1;
86         
87         simage->iuser.ok= 1;
88         simage->iuser.fie_ima= 2;
89         simage->iuser.frames= 100;
90         
91         /* header */
92         ar= MEM_callocN(sizeof(ARegion), "header for image");
93         
94         BLI_addtail(&simage->regionbase, ar);
95         ar->regiontype= RGN_TYPE_HEADER;
96         ar->alignment= RGN_ALIGN_BOTTOM;
97         
98         /* main area */
99         ar= MEM_callocN(sizeof(ARegion), "main area for image");
100         
101         BLI_addtail(&simage->regionbase, ar);
102         ar->regiontype= RGN_TYPE_WINDOW;
103         
104         return (SpaceLink *)simage;
105 }
106
107 /* not spacelink itself */
108 static void image_free(SpaceLink *sl)
109 {       
110         SpaceImage *simage= (SpaceImage*) sl;
111         
112         if(simage->cumap)
113                 curvemapping_free(simage->cumap);
114 //      if(simage->gpd)
115 // XXX          free_gpencil_data(simage->gpd);
116         
117 }
118
119
120 /* spacetype; init callback */
121 static void image_init(struct wmWindowManager *wm, ScrArea *sa)
122 {
123
124 }
125
126 static SpaceLink *image_duplicate(SpaceLink *sl)
127 {
128         SpaceImage *simagen= MEM_dupallocN(sl);
129         
130         /* clear or remove stuff from old */
131         if(simagen->cumap)
132                 simagen->cumap= curvemapping_copy(simagen->cumap);
133         
134         return (SpaceLink *)simagen;
135 }
136
137 void image_operatortypes(void)
138 {
139         WM_operatortype_append(IMAGE_OT_view_all);
140         WM_operatortype_append(IMAGE_OT_view_pan);
141         WM_operatortype_append(IMAGE_OT_view_selected);
142         WM_operatortype_append(IMAGE_OT_view_zoom);
143         WM_operatortype_append(IMAGE_OT_view_zoom_in);
144         WM_operatortype_append(IMAGE_OT_view_zoom_out);
145         WM_operatortype_append(IMAGE_OT_view_zoom_ratio);
146
147         WM_operatortype_append(IMAGE_OT_new);
148         WM_operatortype_append(IMAGE_OT_open);
149         WM_operatortype_append(IMAGE_OT_replace);
150         WM_operatortype_append(IMAGE_OT_reload);
151         WM_operatortype_append(IMAGE_OT_save);
152         WM_operatortype_append(IMAGE_OT_save_as);
153         WM_operatortype_append(IMAGE_OT_save_sequence);
154         WM_operatortype_append(IMAGE_OT_pack);
155         WM_operatortype_append(IMAGE_OT_unpack);
156
157         WM_operatortype_append(IMAGE_OT_sample);
158         WM_operatortype_append(IMAGE_OT_set_curves_point);
159
160         WM_operatortype_append(IMAGE_OT_record_composite);
161
162         WM_operatortype_append(IMAGE_OT_toolbox);
163 }
164
165 void image_keymap(struct wmWindowManager *wm)
166 {
167         ListBase *keymap= WM_keymap_listbase(wm, "Image", SPACE_IMAGE, 0);
168         
169         WM_keymap_add_item(keymap, "IMAGE_OT_view_all", HOMEKEY, KM_PRESS, 0, 0);
170         WM_keymap_add_item(keymap, "IMAGE_OT_view_selected", PADPERIOD, KM_PRESS, 0, 0);
171         WM_keymap_add_item(keymap, "IMAGE_OT_view_pan", MIDDLEMOUSE, KM_PRESS, 0, 0);
172
173         WM_keymap_add_item(keymap, "IMAGE_OT_view_zoom_in", WHEELINMOUSE, KM_PRESS, 0, 0);
174         WM_keymap_add_item(keymap, "IMAGE_OT_view_zoom_out", WHEELOUTMOUSE, KM_PRESS, 0, 0);
175         WM_keymap_add_item(keymap, "IMAGE_OT_view_zoom_in", PADPLUSKEY, KM_PRESS, 0, 0);
176         WM_keymap_add_item(keymap, "IMAGE_OT_view_zoom_out", PADMINUS, KM_PRESS, 0, 0);
177         WM_keymap_add_item(keymap, "IMAGE_OT_view_zoom", MIDDLEMOUSE, KM_PRESS, KM_CTRL, 0);
178
179         RNA_float_set(WM_keymap_add_item(keymap, "IMAGE_OT_view_zoom_ratio", PAD8, KM_PRESS, KM_SHIFT, 0)->ptr, "ratio", 8.0f);
180         RNA_float_set(WM_keymap_add_item(keymap, "IMAGE_OT_view_zoom_ratio", PAD4, KM_PRESS, KM_SHIFT, 0)->ptr, "ratio", 4.0f);
181         RNA_float_set(WM_keymap_add_item(keymap, "IMAGE_OT_view_zoom_ratio", PAD2, KM_PRESS, KM_SHIFT, 0)->ptr, "ratio", 2.0f);
182         RNA_float_set(WM_keymap_add_item(keymap, "IMAGE_OT_view_zoom_ratio", PAD1, KM_PRESS, 0, 0)->ptr, "ratio", 1.0f);
183         RNA_float_set(WM_keymap_add_item(keymap, "IMAGE_OT_view_zoom_ratio", PAD2, KM_PRESS, 0, 0)->ptr, "ratio", 0.5f);
184         RNA_float_set(WM_keymap_add_item(keymap, "IMAGE_OT_view_zoom_ratio", PAD4, KM_PRESS, 0, 0)->ptr, "ratio", 0.25f);
185         RNA_float_set(WM_keymap_add_item(keymap, "IMAGE_OT_view_zoom_ratio", PAD8, KM_PRESS, 0, 0)->ptr, "ratio", 0.125f);
186
187         WM_keymap_add_item(keymap, "IMAGE_OT_new", NKEY, KM_PRESS, KM_ALT, 0);
188         WM_keymap_add_item(keymap, "IMAGE_OT_open", OKEY, KM_PRESS, KM_ALT, 0);
189         WM_keymap_add_item(keymap, "IMAGE_OT_reload", RKEY, KM_PRESS, KM_ALT, 0);
190         WM_keymap_add_item(keymap, "IMAGE_OT_save", SKEY, KM_PRESS, KM_ALT, 0);
191
192         WM_keymap_add_item(keymap, "IMAGE_OT_sample", ACTIONMOUSE, KM_PRESS, 0, 0);
193         RNA_enum_set(WM_keymap_add_item(keymap, "IMAGE_OT_set_curves_point", ACTIONMOUSE, KM_PRESS, KM_CTRL, 0)->ptr, "point", 0);
194         RNA_enum_set(WM_keymap_add_item(keymap, "IMAGE_OT_set_curves_point", ACTIONMOUSE, KM_PRESS, KM_SHIFT, 0)->ptr, "point", 1);
195
196         WM_keymap_add_item(keymap, "IMAGE_OT_toolbox", SPACEKEY, KM_PRESS, 0, 0);
197 }
198
199 static void image_refresh(const bContext *C, ScrArea *sa)
200 {
201         SpaceImage *sima= (SpaceImage*)CTX_wm_space_data(C);
202         Object *obedit= CTX_data_edit_object(C);
203         Image *ima;
204
205         ima= ED_space_image(sima);
206
207         /* check if we have to set the image from the editmesh */
208         if(ima && (ima->source==IMA_SRC_VIEWER || sima->pin));
209         else if(obedit && obedit->type == OB_MESH) {
210                 Mesh *me= (Mesh*)obedit->data;
211                 EditMesh *em= me->edit_mesh;
212                 MTFace *tf;
213                 
214                 if(em && EM_texFaceCheck(em)) {
215                         sima->image= ima= NULL;
216                         
217                         tf = EM_get_active_mtface(em, NULL, NULL, 1); /* partially selected face is ok */
218                         
219                         if(tf && (tf->mode & TF_TEX)) {
220                                 /* don't need to check for pin here, see above */
221                                 sima->image= ima= tf->tpage;
222                                 
223                                 if(sima->flag & SI_EDITTILE);
224                                 else sima->curtile= tf->tile;
225                                 
226                                 if(ima) {
227                                         if(tf->mode & TF_TILES)
228                                                 ima->tpageflag |= IMA_TILES;
229                                         else
230                                                 ima->tpageflag &= ~IMA_TILES;
231                                 }
232                         }
233                 }
234         }
235 }
236
237 static void image_listener(ScrArea *sa, wmNotifier *wmn)
238 {
239         SpaceImage *sima= sa->spacedata.first;
240
241         /* context changes */
242         switch(wmn->category) {
243                 case NC_SCENE:
244                         switch(wmn->data) {
245                                 case ND_MODE:
246                                 case ND_RENDER_RESULT:
247                                 case ND_COMPO_RESULT:
248                                         ED_area_tag_refresh(sa);
249                                         ED_area_tag_redraw(sa);
250                                         break;
251                         }
252                         break;
253                 case NC_IMAGE:  
254                         if(!wmn->reference || wmn->reference == sima->image)
255                                 ED_area_tag_redraw(sa);
256                         break;
257         }
258 }
259
260 static int image_context(const bContext *C, bContextDataMember member, bContextDataResult *result)
261 {
262         SpaceImage *sima= (SpaceImage*)CTX_wm_space_data(C);
263
264         if(member == CTX_DATA_EDIT_IMAGE) {
265                 CTX_data_pointer_set(result, ED_space_image(sima));
266                 return 1;
267         }
268         else if(member == CTX_DATA_EDIT_IMAGE_BUFFER) {
269                 CTX_data_pointer_set(result, ED_space_image_buffer(sima));
270                 return 1;
271         }
272
273         return 0;
274 }
275
276 /************************** main region ***************************/
277
278 /* sets up the fields of the View2D from zoom and offset */
279 static void image_main_area_set_view2d(SpaceImage *sima, ARegion *ar)
280 {
281         Image *ima= ED_space_image(sima);
282         float x1, y1, w, h;
283         int width, height, winx, winy;
284         
285 #if 0
286         if(image_preview_active(curarea, &xim, &yim));
287         else if(sima->image) {
288                 ImBuf *ibuf= ED_space_image_buffer(sima);
289                 float xuser_asp, yuser_asp;
290                 
291                 ED_image_aspect(sima->image, &xuser_asp, &yuser_asp);
292                 if(ibuf) {
293                         xim= ibuf->x * xuser_asp;
294                         yim= ibuf->y * yuser_asp;
295                 }
296                 else if( sima->image->type==IMA_TYPE_R_RESULT ) {
297                         /* not very important, just nice */
298                         xim= (G.scene->r.xsch*G.scene->r.size)/100;
299                         yim= (G.scene->r.ysch*G.scene->r.size)/100;
300                 }
301         }
302 #endif
303
304         ED_space_image_size(sima, &width, &height);
305
306         w= width;
307         h= height;
308         
309         if(ima)
310                 h *= ima->aspy/ima->aspx;
311
312         winx= ar->winrct.xmax - ar->winrct.xmin + 1;
313         winy= ar->winrct.ymax - ar->winrct.ymin + 1;
314                 
315         ar->v2d.tot.xmin= 0;
316         ar->v2d.tot.ymin= 0;
317         ar->v2d.tot.xmax= w;
318         ar->v2d.tot.ymax= h;
319         
320         ar->v2d.mask.xmin= ar->v2d.mask.ymin= 0;
321         ar->v2d.mask.xmax= winx;
322         ar->v2d.mask.ymax= winy;
323
324         /* which part of the image space do we see? */
325         /* same calculation as in lrectwrite: area left and down*/
326         x1= ar->winrct.xmin+(winx-sima->zoom*w)/2;
327         y1= ar->winrct.ymin+(winy-sima->zoom*h)/2;
328
329         x1-= sima->zoom*sima->xof;
330         y1-= sima->zoom*sima->yof;
331         
332         /* relative display right */
333         ar->v2d.cur.xmin= ((ar->winrct.xmin - (float)x1)/sima->zoom);
334         ar->v2d.cur.xmax= ar->v2d.cur.xmin + ((float)winx/sima->zoom);
335         
336         /* relative display left */
337         ar->v2d.cur.ymin= ((ar->winrct.ymin-(float)y1)/sima->zoom);
338         ar->v2d.cur.ymax= ar->v2d.cur.ymin + ((float)winy/sima->zoom);
339         
340         /* normalize 0.0..1.0 */
341         ar->v2d.cur.xmin /= w;
342         ar->v2d.cur.xmax /= w;
343         ar->v2d.cur.ymin /= h;
344         ar->v2d.cur.ymax /= h;
345 }
346
347 /* add handlers, stuff you only do once or on area/region changes */
348 static void image_main_area_init(wmWindowManager *wm, ARegion *ar)
349 {
350         ListBase *keymap;
351         
352         // image space manages own v2d
353         // UI_view2d_region_reinit(&ar->v2d, V2D_COMMONVIEW_STANDARD, ar->winx, ar->winy);
354         
355         /* own keymap */
356         keymap= WM_keymap_listbase(wm, "Image", SPACE_IMAGE, 0);
357         WM_event_add_keymap_handler_bb(&ar->handlers, keymap, &ar->v2d.mask, &ar->winrct);
358 }
359
360 static void image_main_area_draw(const bContext *C, ARegion *ar)
361 {
362         /* draw entirely, view changes should be handled here */
363         SpaceImage *sima= (SpaceImage*)CTX_wm_space_data(C);
364         Object *obedit= CTX_data_edit_object(C);
365         Scene *scene= CTX_data_scene(C);
366         View2D *v2d= &ar->v2d;
367         //View2DScrollers *scrollers;
368         float col[3];
369         
370         /* clear and setup matrix */
371         UI_GetThemeColor3fv(TH_BACK, col);
372         glClearColor(col[0], col[1], col[2], 0.0);
373         glClear(GL_COLOR_BUFFER_BIT);
374         
375         /* we set view2d from own zoom and offset each time */
376         image_main_area_set_view2d(sima, ar);
377                 
378         /* we draw image in pixelspace */
379         draw_image_main(sima, ar, scene);
380
381         /* and uvs in 0.0-1.0 space */
382         UI_view2d_view_ortho(C, v2d);
383         draw_uvedit_main(sima, ar, scene, obedit);
384         ED_region_draw_cb_draw(C, ar, REGION_DRAW_POST);
385         UI_view2d_view_restore(C);
386         
387         /* scrollers? */
388         /*scrollers= UI_view2d_scrollers_calc(C, v2d, V2D_UNIT_VALUES, V2D_GRID_CLAMP, V2D_ARG_DUMMY, V2D_ARG_DUMMY);
389         UI_view2d_scrollers_draw(C, v2d, scrollers);
390         UI_view2d_scrollers_free(scrollers);*/
391 }
392
393 static void image_modal_keymaps(wmWindowManager *wm, ARegion *ar, int stype)
394 {
395         ListBase *keymap;
396         
397         keymap= WM_keymap_listbase(wm, "UVEdit", 0, 0);
398
399         if(stype==NS_EDITMODE_MESH)
400                 WM_event_add_keymap_handler(&ar->handlers, keymap);
401         else
402                 WM_event_remove_keymap_handler(&ar->handlers, keymap);
403 }
404
405 static void image_main_area_listener(ARegion *ar, wmNotifier *wmn)
406 {
407         /* context changes */
408         switch(wmn->category) {
409                 case NC_SCENE:
410                         switch(wmn->data) {
411                                 case ND_MODE:
412                                         image_modal_keymaps(wmn->wm, ar, wmn->subtype);
413                                         break;
414                         }
415                         break;
416                 case NC_OBJECT:
417                         switch(wmn->data) {
418                                 case ND_GEOM_SELECT:
419                                 case ND_GEOM_DATA:
420                                         ED_region_tag_redraw(ar);
421                                         break;
422                         }
423         }
424 }
425
426 /************************* header region **************************/
427
428 /* add handlers, stuff you only do once or on area/region changes */
429 static void image_header_area_init(wmWindowManager *wm, ARegion *ar)
430 {
431         UI_view2d_region_reinit(&ar->v2d, V2D_COMMONVIEW_HEADER, ar->winx, ar->winy);
432 }
433
434 static void image_header_area_draw(const bContext *C, ARegion *ar)
435 {
436         float col[3];
437         
438         /* clear */
439         if(ED_screen_area_active(C))
440                 UI_GetThemeColor3fv(TH_HEADER, col);
441         else
442                 UI_GetThemeColor3fv(TH_HEADERDESEL, col);
443         
444         glClearColor(col[0], col[1], col[2], 0.0);
445         glClear(GL_COLOR_BUFFER_BIT);
446         
447         /* set view2d view matrix for scrolling (without scrollers) */
448         UI_view2d_view_ortho(C, &ar->v2d);
449         
450         image_header_buttons(C, ar);
451         
452         /* restore view matrix? */
453         UI_view2d_view_restore(C);
454 }
455
456 /**************************** spacetype *****************************/
457
458 /* only called once, from space/spacetypes.c */
459 void ED_spacetype_image(void)
460 {
461         SpaceType *st= MEM_callocN(sizeof(SpaceType), "spacetype image");
462         ARegionType *art;
463         
464         st->spaceid= SPACE_IMAGE;
465         
466         st->new= image_new;
467         st->free= image_free;
468         st->init= image_init;
469         st->duplicate= image_duplicate;
470         st->operatortypes= image_operatortypes;
471         st->keymap= image_keymap;
472         st->refresh= image_refresh;
473         st->listener= image_listener;
474         st->context= image_context;
475         
476         /* regions: main window */
477         art= MEM_callocN(sizeof(ARegionType), "spacetype image region");
478         art->regionid = RGN_TYPE_WINDOW;
479         art->keymapflag= ED_KEYMAP_FRAMES;
480         art->init= image_main_area_init;
481         art->draw= image_main_area_draw;
482         art->listener= image_main_area_listener;
483         art->keymapflag= 0;
484
485         BLI_addhead(&st->regiontypes, art);
486         
487         /* regions: header */
488         art= MEM_callocN(sizeof(ARegionType), "spacetype image region");
489         art->regionid = RGN_TYPE_HEADER;
490         art->minsizey= HEADERY;
491         art->keymapflag= ED_KEYMAP_UI|ED_KEYMAP_VIEW2D|ED_KEYMAP_FRAMES;
492         
493         art->init= image_header_area_init;
494         art->draw= image_header_area_draw;
495         
496         BLI_addhead(&st->regiontypes, art);
497         
498         
499         BKE_spacetype_register(st);
500 }
501
502 /**************************** common state *****************************/
503
504 Image *ED_space_image(SpaceImage *sima)
505 {
506         return sima->image;
507 }
508
509 /* called to assign images to UV faces */
510 void ED_space_image_set(bContext *C, SpaceImage *sima, Scene *scene, Object *obedit, Image *ima)
511 {
512         ED_uvedit_assign_image(scene, obedit, ima, sima->image);
513
514         /* change the space ima after because uvedit_face_visible uses the space ima
515          * to check if the face is displayed in UV-localview */
516         sima->image= ima;
517
518         if(ima == NULL || ima->type==IMA_TYPE_R_RESULT || ima->type==IMA_TYPE_COMPOSITE)
519                 sima->flag &= ~SI_DRAWTOOL;
520
521         if(sima->image)
522                 BKE_image_signal(sima->image, &sima->iuser, IMA_SIGNAL_USER_NEW_IMAGE);
523
524         if(sima->image && sima->image->id.us==0)
525                 sima->image->id.us= 1;
526
527         if(obedit)
528                 WM_event_add_notifier(C, NC_OBJECT|ND_GEOM_DATA, obedit);
529
530         ED_area_tag_redraw(CTX_wm_area(C));
531 }
532
533 ImBuf *ED_space_image_buffer(SpaceImage *sima)
534 {
535         ImBuf *ibuf;
536
537         if(sima->image) {
538 #if 0
539                 if(sima->image->type==IMA_TYPE_R_RESULT && BIF_show_render_spare())
540                         return BIF_render_spare_imbuf();
541                 else
542 #endif
543                         ibuf= BKE_image_get_ibuf(sima->image, &sima->iuser);
544
545                 if(ibuf && (ibuf->rect || ibuf->rect_float))
546                         return ibuf;
547         }
548
549         return NULL;
550 }
551
552 void ED_image_size(Image *ima, int *width, int *height)
553 {
554         ImBuf *ibuf;
555
556         ibuf= (ima)? BKE_image_get_ibuf(ima, NULL): NULL;
557
558         if(ibuf && ibuf->x > 0 && ibuf->y > 0) {
559                 *width= ibuf->x;
560                 *height= ibuf->y;
561         }
562         else {
563                 *width= 256;
564                 *height= 256;
565         }
566 }
567
568 void ED_space_image_size(SpaceImage *sima, int *width, int *height)
569 {
570         ImBuf *ibuf;
571
572         ibuf= ED_space_image_buffer(sima);
573
574         if(ibuf && ibuf->x > 0 && ibuf->y > 0) {
575                 *width= ibuf->x;
576                 *height= ibuf->y;
577         }
578         /* I know a bit weak... but preview uses not actual image size */
579         // XXX else if(image_preview_active(sima, width, height));
580         else {
581                 *width= 256;
582                 *height= 256;
583         }
584 }
585
586 void ED_image_aspect(Image *ima, float *aspx, float *aspy)
587 {
588         *aspx= *aspy= 1.0;
589
590         if((ima == NULL) || (ima->type == IMA_TYPE_R_RESULT) || (ima->type == IMA_TYPE_COMPOSITE) ||
591            (ima->tpageflag & IMA_TILES) || (ima->aspx==0.0 || ima->aspy==0.0))
592                 return;
593
594         /* x is always 1 */
595         *aspy = ima->aspy/ima->aspx;
596 }
597
598 void ED_space_image_aspect(SpaceImage *sima, float *aspx, float *aspy)
599 {
600         ED_image_aspect(ED_space_image(sima), aspx, aspy);
601 }
602
603 void ED_space_image_zoom(SpaceImage *sima, ARegion *ar, float *zoomx, float *zoomy)
604 {
605         int width, height;
606
607         ED_space_image_size(sima, &width, &height);
608
609         *zoomx= (float)(ar->winrct.xmax - ar->winrct.xmin)/(float)((ar->v2d.cur.xmax - ar->v2d.cur.xmin)*width);
610         *zoomy= (float)(ar->winrct.ymax - ar->winrct.ymin)/(float)((ar->v2d.cur.ymax - ar->v2d.cur.ymin)*height);
611 }
612
613 void ED_space_image_uv_aspect(SpaceImage *sima, float *aspx, float *aspy)
614 {
615         int w, h;
616
617         ED_space_image_aspect(sima, aspx, aspy);
618         ED_space_image_size(sima, &w, &h);
619
620         *aspx *= (float)w/256.0f;
621         *aspy *= (float)h/256.0f;
622 }
623
624 void ED_image_uv_aspect(Image *ima, float *aspx, float *aspy)
625 {
626         int w, h;
627
628         ED_image_aspect(ima, aspx, aspy);
629         ED_image_size(ima, &w, &h);
630
631         *aspx *= (float)w;
632         *aspy *= (float)h;
633 }
634
635 int ED_space_image_show_render(SpaceImage *sima)
636 {
637         return (sima->image && ELEM(sima->image->type, IMA_TYPE_R_RESULT, IMA_TYPE_COMPOSITE));
638 }
639
640 int ED_space_image_show_paint(SpaceImage *sima)
641 {
642         if(ED_space_image_show_render(sima))
643                 return 0;
644
645         return (sima->flag & SI_DRAWTOOL);
646 }
647
648 int ED_space_image_show_uvedit(SpaceImage *sima, Object *obedit)
649 {
650         if(ED_space_image_show_render(sima))
651                 return 0;
652         if(ED_space_image_show_paint(sima))
653                 return 0;
654
655         if(obedit && obedit->type == OB_MESH)
656                 return EM_texFaceCheck(((Mesh*)obedit->data)->edit_mesh);
657
658         return 0;
659 }
660
661 int ED_space_image_show_uvshadow(SpaceImage *sima, Object *obedit)
662 {
663         if(ED_space_image_show_render(sima))
664                 return 0;
665
666         if(ED_space_image_show_paint(sima))
667                 if(obedit && obedit->type == OB_MESH)
668                         return EM_texFaceCheck(((Mesh*)obedit->data)->edit_mesh);
669
670         return 0;
671 }
672