2 * ***** BEGIN GPL LICENSE BLOCK *****
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software Foundation,
16 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * The Original Code is Copyright (C) 2008 Blender Foundation.
19 * All rights reserved.
22 * Contributor(s): Blender Foundation
24 * ***** END GPL LICENSE BLOCK *****
27 /** \file blender/editors/space_image/image_editor.c
31 #include "DNA_mask_types.h"
32 #include "DNA_object_types.h"
33 #include "DNA_scene_types.h"
37 #include "BKE_context.h"
38 #include "BKE_global.h"
39 #include "BKE_image.h"
41 #include "BKE_tessmesh.h"
43 #include "IMB_imbuf_types.h"
45 #include "ED_image.h" /* own include */
47 #include "ED_screen.h"
48 #include "ED_uvedit.h"
50 #include "UI_view2d.h"
55 /* note; image_panel_properties() uses pointer to sima->image directly */
56 Image *ED_space_image(SpaceImage *sima)
61 /* called to assign images to UV faces */
62 void ED_space_image_set(SpaceImage *sima, Scene *scene, Object *obedit, Image *ima)
64 /* context may be NULL, so use global */
65 ED_uvedit_assign_image(G.main, scene, obedit, ima, sima->image);
67 /* change the space ima after because uvedit_face_visible_test uses the space ima
68 * to check if the face is displayed in UV-localview */
71 if (ima == NULL || ima->type == IMA_TYPE_R_RESULT || ima->type == IMA_TYPE_COMPOSITE) {
72 if (sima->mode == SI_MODE_PAINT) {
73 sima->mode = SI_MODE_VIEW;
78 BKE_image_signal(sima->image, &sima->iuser, IMA_SIGNAL_USER_NEW_IMAGE);
80 if (sima->image && sima->image->id.us == 0)
81 sima->image->id.us = 1;
84 WM_main_add_notifier(NC_GEOM | ND_DATA, obedit->data);
86 WM_main_add_notifier(NC_SPACE | ND_SPACE_IMAGE, NULL);
89 Mask *ED_space_image_get_mask(SpaceImage *sima)
91 return sima->mask_info.mask;
94 void ED_space_image_set_mask(bContext *C, SpaceImage *sima, Mask *mask)
96 sima->mask_info.mask = mask;
99 WM_event_add_notifier(C, NC_MASK | NA_SELECTED, mask);
103 ImBuf *ED_space_image_acquire_buffer(SpaceImage *sima, void **lock_r)
107 if (sima && sima->image) {
109 if (sima->image->type == IMA_TYPE_R_RESULT && BIF_show_render_spare())
110 return BIF_render_spare_imbuf();
113 ibuf = BKE_image_acquire_ibuf(sima->image, &sima->iuser, lock_r);
115 if (ibuf && (ibuf->rect || ibuf->rect_float))
122 void ED_space_image_release_buffer(SpaceImage *sima, void *lock)
124 if (sima && sima->image)
125 BKE_image_release_ibuf(sima->image, lock);
128 int ED_space_image_has_buffer(SpaceImage *sima)
134 ibuf = ED_space_image_acquire_buffer(sima, &lock);
135 has_buffer = (ibuf != NULL);
136 ED_space_image_release_buffer(sima, lock);
141 void ED_image_get_size(Image *ima, int *width, int *height)
147 ibuf = BKE_image_acquire_ibuf(ima, NULL, &lock);
149 if (ibuf && ibuf->x > 0 && ibuf->y > 0) {
154 *width = IMG_SIZE_FALLBACK;
155 *height = IMG_SIZE_FALLBACK;
159 BKE_image_release_ibuf(ima, lock);
162 void ED_space_image_get_size(SpaceImage *sima, int *width, int *height)
164 Scene *scene = sima->iuser.scene;
168 ibuf = ED_space_image_acquire_buffer(sima, &lock);
170 if (ibuf && ibuf->x > 0 && ibuf->y > 0) {
174 else if (sima->image && sima->image->type == IMA_TYPE_R_RESULT && scene) {
175 /* not very important, just nice */
176 *width = (scene->r.xsch * scene->r.size) / 100;
177 *height = (scene->r.ysch * scene->r.size) / 100;
179 if ((scene->r.mode & R_BORDER) && (scene->r.mode & R_CROP)) {
180 *width *= (scene->r.border.xmax - scene->r.border.xmin);
181 *height *= (scene->r.border.ymax - scene->r.border.ymin);
185 /* I know a bit weak... but preview uses not actual image size */
186 // XXX else if (image_preview_active(sima, width, height));
188 *width = IMG_SIZE_FALLBACK;
189 *height = IMG_SIZE_FALLBACK;
192 ED_space_image_release_buffer(sima, lock);
195 void ED_image_get_aspect(Image *ima, float *aspx, float *aspy)
199 if ((ima == NULL) || (ima->type == IMA_TYPE_R_RESULT) || (ima->type == IMA_TYPE_COMPOSITE) ||
200 (ima->aspx == 0.0f || ima->aspy == 0.0f))
206 *aspy = ima->aspy / ima->aspx;
209 void ED_space_image_get_aspect(SpaceImage *sima, float *aspx, float *aspy)
211 ED_image_get_aspect(ED_space_image(sima), aspx, aspy);
214 void ED_space_image_get_zoom(SpaceImage *sima, ARegion *ar, float *zoomx, float *zoomy)
218 ED_space_image_get_size(sima, &width, &height);
220 *zoomx = (float)(ar->winrct.xmax - ar->winrct.xmin + 1) / (float)((ar->v2d.cur.xmax - ar->v2d.cur.xmin) * width);
221 *zoomy = (float)(ar->winrct.ymax - ar->winrct.ymin + 1) / (float)((ar->v2d.cur.ymax - ar->v2d.cur.ymin) * height);
224 void ED_space_image_get_uv_aspect(SpaceImage *sima, float *aspx, float *aspy)
228 ED_space_image_get_aspect(sima, aspx, aspy);
229 ED_space_image_get_size(sima, &w, &h);
235 *aspy = *aspy / *aspx;
239 *aspx = *aspx / *aspy;
244 void ED_image_get_uv_aspect(Image *ima, float *aspx, float *aspy)
248 ED_image_get_aspect(ima, aspx, aspy);
249 ED_image_get_size(ima, &w, &h);
255 void ED_image_mouse_pos(SpaceImage *sima, ARegion *ar, wmEvent *event, float co[2])
257 int sx, sy, width, height;
260 ED_space_image_get_zoom(sima, ar, &zoomx, &zoomy);
261 ED_space_image_get_size(sima, &width, &height);
263 UI_view2d_to_region_no_clip(&ar->v2d, 0.0f, 0.0f, &sx, &sy);
265 co[0] = ((event->mval[0] - sx) / zoomx) / width;
266 co[1] = ((event->mval[1] - sy) / zoomy) / height;
269 void ED_image_point_pos(SpaceImage *sima, ARegion *ar, float x, float y, float *xr, float *yr)
271 int sx, sy, width, height;
274 ED_space_image_get_zoom(sima, ar, &zoomx, &zoomy);
275 ED_space_image_get_size(sima, &width, &height);
277 UI_view2d_to_region_no_clip(&ar->v2d, 0.0f, 0.0f, &sx, &sy);
279 *xr = ((x - sx) / zoomx) / width;
280 *yr = ((y - sy) / zoomy) / height;
283 void ED_image_point_pos__reverse(SpaceImage *sima, ARegion *ar, const float co[2], float r_co[2])
289 UI_view2d_to_region_no_clip(&ar->v2d, 0.0f, 0.0f, &sx, &sy);
290 ED_space_image_get_size(sima, &width, &height);
291 ED_space_image_get_zoom(sima, ar, &zoomx, &zoomy);
293 r_co[0] = (co[0] * width * zoomx) + (float)sx;
294 r_co[1] = (co[1] * height * zoomy) + (float)sy;
297 int ED_space_image_show_render(SpaceImage *sima)
299 return (sima->image && ELEM(sima->image->type, IMA_TYPE_R_RESULT, IMA_TYPE_COMPOSITE));
302 int ED_space_image_show_paint(SpaceImage *sima)
304 if (ED_space_image_show_render(sima))
307 return (sima->mode == SI_MODE_PAINT);
310 int ED_space_image_show_uvedit(SpaceImage *sima, Object *obedit)
312 if (sima && (ED_space_image_show_render(sima) || ED_space_image_show_paint(sima)))
315 if (obedit && obedit->type == OB_MESH) {
316 struct BMEditMesh *em = BMEdit_FromObject(obedit);
319 ret = EDBM_mtexpoly_check(em);
327 int ED_space_image_show_uvshadow(SpaceImage *sima, Object *obedit)
329 if (ED_space_image_show_render(sima))
332 if (ED_space_image_show_paint(sima))
333 if (obedit && obedit->type == OB_MESH) {
334 struct BMEditMesh *em = BMEdit_FromObject(obedit);
337 ret = EDBM_mtexpoly_check(em);
345 /* matches clip function */
346 int ED_space_image_check_show_maskedit(SpaceImage *sima)
348 return (sima->mode == SI_MODE_MASK);
351 int ED_space_image_maskedit_poll(bContext *C)
353 SpaceImage *sima = CTX_wm_space_image(C);
355 if (sima && sima->image) {
356 return ED_space_image_check_show_maskedit(sima);
362 int ED_space_image_maskedit_mask_poll(bContext *C)
364 if (ED_space_image_maskedit_poll(C)) {
365 Image *ima = CTX_data_edit_image(C);
368 SpaceImage *sima = CTX_wm_space_image(C);
370 return sima->mask_info.mask != NULL;
377 /******************** TODO ********************/
381 /* goes over all ImageUsers, and sets frame numbers if auto-refresh is set */
383 static void image_update_frame(struct Image *UNUSED(ima), struct ImageUser *iuser, void *customdata)
385 int cfra = *(int *)customdata;
387 BKE_image_user_check_frame_calc(iuser, cfra, 0);
390 void ED_image_update_frame(const Main *mainp, int cfra)
392 BKE_image_walk_all_users(mainp, &cfra, image_update_frame);