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 if (sima->mode == SI_MODE_PAINT) {
71 sima->mode = SI_MODE_VIEW;
76 BKE_image_signal(sima->image, &sima->iuser, IMA_SIGNAL_USER_NEW_IMAGE);
78 if (sima->image && sima->image->id.us == 0)
79 sima->image->id.us = 1;
82 WM_main_add_notifier(NC_GEOM | ND_DATA, obedit->data);
84 WM_main_add_notifier(NC_SPACE | ND_SPACE_IMAGE, NULL);
87 Mask *ED_space_image_get_mask(SpaceImage *sima)
89 return sima->mask_info.mask;
92 void ED_space_image_set_mask(bContext *C, SpaceImage *sima, Mask *mask)
94 sima->mask_info.mask = mask;
97 WM_event_add_notifier(C, NC_MASK | NA_SELECTED, mask);
101 ImBuf *ED_space_image_acquire_buffer(SpaceImage *sima, void **lock_r)
105 if (sima && sima->image) {
107 if (sima->image->type == IMA_TYPE_R_RESULT && BIF_show_render_spare())
108 return BIF_render_spare_imbuf();
111 ibuf = BKE_image_acquire_ibuf(sima->image, &sima->iuser, lock_r);
113 if (ibuf && (ibuf->rect || ibuf->rect_float))
120 void ED_space_image_release_buffer(SpaceImage *sima, void *lock)
122 if (sima && sima->image)
123 BKE_image_release_ibuf(sima->image, lock);
126 int ED_space_image_has_buffer(SpaceImage *sima)
132 ibuf = ED_space_image_acquire_buffer(sima, &lock);
133 has_buffer = (ibuf != NULL);
134 ED_space_image_release_buffer(sima, lock);
139 void ED_image_get_size(Image *ima, int *width, int *height)
145 ibuf = BKE_image_acquire_ibuf(ima, NULL, &lock);
147 if (ibuf && ibuf->x > 0 && ibuf->y > 0) {
152 *width = IMG_SIZE_FALLBACK;
153 *height = IMG_SIZE_FALLBACK;
157 BKE_image_release_ibuf(ima, lock);
160 void ED_space_image_get_size(SpaceImage *sima, int *width, int *height)
162 Scene *scene = sima->iuser.scene;
166 ibuf = ED_space_image_acquire_buffer(sima, &lock);
168 if (ibuf && ibuf->x > 0 && ibuf->y > 0) {
172 else if (sima->image && sima->image->type == IMA_TYPE_R_RESULT && scene) {
173 /* not very important, just nice */
174 *width = (scene->r.xsch * scene->r.size) / 100;
175 *height = (scene->r.ysch * scene->r.size) / 100;
177 if ((scene->r.mode & R_BORDER) && (scene->r.mode & R_CROP)) {
178 *width *= (scene->r.border.xmax - scene->r.border.xmin);
179 *height *= (scene->r.border.ymax - scene->r.border.ymin);
183 /* I know a bit weak... but preview uses not actual image size */
184 // XXX else if (image_preview_active(sima, width, height));
186 *width = IMG_SIZE_FALLBACK;
187 *height = IMG_SIZE_FALLBACK;
190 ED_space_image_release_buffer(sima, lock);
193 void ED_image_get_aspect(Image *ima, float *aspx, float *aspy)
197 if ((ima == NULL) || (ima->type == IMA_TYPE_R_RESULT) || (ima->type == IMA_TYPE_COMPOSITE) ||
198 (ima->aspx == 0.0f || ima->aspy == 0.0f))
204 *aspy = ima->aspy / ima->aspx;
207 void ED_space_image_get_aspect(SpaceImage *sima, float *aspx, float *aspy)
209 ED_image_get_aspect(ED_space_image(sima), aspx, aspy);
212 void ED_space_image_get_zoom(SpaceImage *sima, ARegion *ar, float *zoomx, float *zoomy)
216 ED_space_image_get_size(sima, &width, &height);
218 *zoomx = (float)(ar->winrct.xmax - ar->winrct.xmin + 1) / (float)((ar->v2d.cur.xmax - ar->v2d.cur.xmin) * width);
219 *zoomy = (float)(ar->winrct.ymax - ar->winrct.ymin + 1) / (float)((ar->v2d.cur.ymax - ar->v2d.cur.ymin) * height);
222 void ED_space_image_get_uv_aspect(SpaceImage *sima, float *aspx, float *aspy)
226 ED_space_image_get_aspect(sima, aspx, aspy);
227 ED_space_image_get_size(sima, &w, &h);
233 *aspy = *aspy / *aspx;
237 *aspx = *aspx / *aspy;
242 void ED_image_get_uv_aspect(Image *ima, float *aspx, float *aspy)
246 ED_image_get_aspect(ima, aspx, aspy);
247 ED_image_get_size(ima, &w, &h);
253 int ED_space_image_show_render(SpaceImage *sima)
255 return (sima->image && ELEM(sima->image->type, IMA_TYPE_R_RESULT, IMA_TYPE_COMPOSITE));
258 int ED_space_image_show_paint(SpaceImage *sima)
260 if (ED_space_image_show_render(sima))
263 return (sima->mode == SI_MODE_PAINT);
266 int ED_space_image_show_uvedit(SpaceImage *sima, Object *obedit)
268 if (sima && (ED_space_image_show_render(sima) || ED_space_image_show_paint(sima)))
271 if (obedit && obedit->type == OB_MESH) {
272 struct BMEditMesh *em = BMEdit_FromObject(obedit);
275 ret = EDBM_mtexpoly_check(em);
283 int ED_space_image_show_uvshadow(SpaceImage *sima, Object *obedit)
285 if (ED_space_image_show_render(sima))
288 if (ED_space_image_show_paint(sima))
289 if (obedit && obedit->type == OB_MESH) {
290 struct BMEditMesh *em = BMEdit_FromObject(obedit);
293 ret = EDBM_mtexpoly_check(em);
301 /* matches clip function */
302 int ED_space_image_check_show_maskedit(SpaceImage *sima)
304 return (sima->mode == SI_MODE_MASK);
307 int ED_space_image_maskedit_poll(bContext *C)
309 SpaceImage *sima = CTX_wm_space_image(C);
311 if (sima && sima->image) {
312 return ED_space_image_check_show_maskedit(sima);
318 int ED_space_image_maskedit_mask_poll(bContext *C)
320 if (ED_space_image_maskedit_poll(C)) {
321 Image *ima = CTX_data_edit_image(C);
324 SpaceImage *sima = CTX_wm_space_image(C);
326 return sima->mask_info.mask != NULL;
333 /******************** TODO ********************/
337 /* goes over all ImageUsers, and sets frame numbers if auto-refresh is set */
339 static void image_update_frame(struct Image *UNUSED(ima), struct ImageUser *iuser, void *customdata)
341 int cfra = *(int *)customdata;
343 BKE_image_user_check_frame_calc(iuser, cfra, 0);
346 void ED_image_update_frame(const Main *mainp, int cfra)
348 BKE_image_walk_all_users(mainp, &cfra, image_update_frame);