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"
53 /* note; image_panel_properties() uses pointer to sima->image directly */
54 Image *ED_space_image(SpaceImage *sima)
59 /* called to assign images to UV faces */
60 void ED_space_image_set(SpaceImage *sima, Scene *scene, Object *obedit, Image *ima)
62 /* context may be NULL, so use global */
63 ED_uvedit_assign_image(G.main, scene, obedit, ima, sima->image);
65 /* change the space ima after because uvedit_face_visible_test uses the space ima
66 * to check if the face is displayed in UV-localview */
69 if (ima == NULL || ima->type == IMA_TYPE_R_RESULT || ima->type == IMA_TYPE_COMPOSITE)
70 sima->flag &= ~SI_DRAWTOOL;
73 BKE_image_signal(sima->image, &sima->iuser, IMA_SIGNAL_USER_NEW_IMAGE);
75 if (sima->image && sima->image->id.us == 0)
76 sima->image->id.us = 1;
79 WM_main_add_notifier(NC_GEOM | ND_DATA, obedit->data);
81 WM_main_add_notifier(NC_SPACE | ND_SPACE_IMAGE, NULL);
84 Mask *ED_space_image_get_mask(SpaceImage *sima)
86 return sima->mask_info.mask;
89 void ED_space_image_set_mask(bContext *C, SpaceImage *sima, Mask *mask)
91 sima->mask_info.mask = mask;
94 WM_event_add_notifier(C, NC_MASK | NA_SELECTED, mask);
98 ImBuf *ED_space_image_acquire_buffer(SpaceImage *sima, void **lock_r)
102 if (sima && sima->image) {
104 if (sima->image->type == IMA_TYPE_R_RESULT && BIF_show_render_spare())
105 return BIF_render_spare_imbuf();
108 ibuf = BKE_image_acquire_ibuf(sima->image, &sima->iuser, lock_r);
110 if (ibuf && (ibuf->rect || ibuf->rect_float))
117 void ED_space_image_release_buffer(SpaceImage *sima, void *lock)
119 if (sima && sima->image)
120 BKE_image_release_ibuf(sima->image, lock);
123 int ED_space_image_has_buffer(SpaceImage *sima)
129 ibuf = ED_space_image_acquire_buffer(sima, &lock);
130 has_buffer = (ibuf != NULL);
131 ED_space_image_release_buffer(sima, lock);
136 void ED_image_size(Image *ima, int *width, int *height)
142 ibuf = BKE_image_acquire_ibuf(ima, NULL, &lock);
144 if (ibuf && ibuf->x > 0 && ibuf->y > 0) {
154 BKE_image_release_ibuf(ima, lock);
157 void ED_space_image_size(SpaceImage *sima, int *width, int *height)
159 Scene *scene = sima->iuser.scene;
163 ibuf = ED_space_image_acquire_buffer(sima, &lock);
165 if (ibuf && ibuf->x > 0 && ibuf->y > 0) {
169 else if (sima->image && sima->image->type == IMA_TYPE_R_RESULT && scene) {
170 /* not very important, just nice */
171 *width = (scene->r.xsch * scene->r.size) / 100;
172 *height = (scene->r.ysch * scene->r.size) / 100;
174 if ((scene->r.mode & R_BORDER) && (scene->r.mode & R_CROP)) {
175 *width *= (scene->r.border.xmax - scene->r.border.xmin);
176 *height *= (scene->r.border.ymax - scene->r.border.ymin);
180 /* I know a bit weak... but preview uses not actual image size */
181 // XXX else if (image_preview_active(sima, width, height));
187 ED_space_image_release_buffer(sima, lock);
190 void ED_image_aspect(Image *ima, float *aspx, float *aspy)
194 if ((ima == NULL) || (ima->type == IMA_TYPE_R_RESULT) || (ima->type == IMA_TYPE_COMPOSITE) ||
195 (ima->aspx == 0.0f || ima->aspy == 0.0f))
201 *aspy = ima->aspy / ima->aspx;
204 void ED_space_image_aspect(SpaceImage *sima, float *aspx, float *aspy)
206 ED_image_aspect(ED_space_image(sima), aspx, aspy);
209 void ED_space_image_zoom(SpaceImage *sima, ARegion *ar, float *zoomx, float *zoomy)
213 ED_space_image_size(sima, &width, &height);
215 *zoomx = (float)(ar->winrct.xmax - ar->winrct.xmin + 1) / (float)((ar->v2d.cur.xmax - ar->v2d.cur.xmin) * width);
216 *zoomy = (float)(ar->winrct.ymax - ar->winrct.ymin + 1) / (float)((ar->v2d.cur.ymax - ar->v2d.cur.ymin) * height);
219 void ED_space_image_uv_aspect(SpaceImage *sima, float *aspx, float *aspy)
223 ED_space_image_aspect(sima, aspx, aspy);
224 ED_space_image_size(sima, &w, &h);
230 *aspy = *aspy / *aspx;
234 *aspx = *aspx / *aspy;
239 void ED_image_uv_aspect(Image *ima, float *aspx, float *aspy)
243 ED_image_aspect(ima, aspx, aspy);
244 ED_image_size(ima, &w, &h);
250 int ED_space_image_show_render(SpaceImage *sima)
252 return (sima->image && ELEM(sima->image->type, IMA_TYPE_R_RESULT, IMA_TYPE_COMPOSITE));
255 int ED_space_image_show_paint(SpaceImage *sima)
257 if (ED_space_image_show_render(sima))
260 return (sima->flag & SI_DRAWTOOL);
263 int ED_space_image_show_uvedit(SpaceImage *sima, Object *obedit)
265 if (sima && (ED_space_image_show_render(sima) || ED_space_image_show_paint(sima)))
268 if (obedit && obedit->type == OB_MESH) {
269 struct BMEditMesh *em = BMEdit_FromObject(obedit);
272 ret = EDBM_mtexpoly_check(em);
280 int ED_space_image_show_uvshadow(SpaceImage *sima, Object *obedit)
282 if (ED_space_image_show_render(sima))
285 if (ED_space_image_show_paint(sima))
286 if (obedit && obedit->type == OB_MESH) {
287 struct BMEditMesh *em = BMEdit_FromObject(obedit);
290 ret = EDBM_mtexpoly_check(em);
298 /* matches clip function */
299 int ED_space_image_check_show_maskedit(SpaceImage *sima)
301 /* MASKTODO - whem we have a mask edit option */
306 int ED_space_image_maskedit_poll(bContext *C)
308 SpaceImage *sima = CTX_wm_space_image(C);
310 if (sima && sima->image) {
311 return ED_space_image_check_show_maskedit(sima);
317 int ED_space_image_maskedit_mask_poll(bContext *C)
319 if (ED_space_image_maskedit_poll(C)) {
320 Image *ima = CTX_data_edit_image(C);
323 SpaceImage *sima = CTX_wm_space_image(C);
325 return sima->mask_info.mask != NULL;
332 /******************** TODO ********************/
336 /* goes over all ImageUsers, and sets frame numbers if auto-refresh is set */
338 static void image_update_frame(struct Image *UNUSED(ima), struct ImageUser *iuser, void *customdata)
340 int cfra = *(int *)customdata;
342 BKE_image_user_check_frame_calc(iuser, cfra, 0);
345 void ED_image_update_frame(const Main *mainp, int cfra)
347 BKE_image_walk_all_users(mainp, &cfra, image_update_frame);