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_edit.c
31 #include "DNA_brush_types.h"
32 #include "DNA_mask_types.h"
33 #include "DNA_object_types.h"
34 #include "DNA_scene_types.h"
38 #include "BKE_colortools.h"
39 #include "BKE_context.h"
40 #include "BKE_global.h"
41 #include "BKE_image.h"
42 #include "BKE_editmesh.h"
43 #include "BKE_library.h"
45 #include "IMB_imbuf_types.h"
47 #include "ED_image.h" /* own include */
49 #include "ED_screen.h"
50 #include "ED_uvedit.h"
52 #include "UI_view2d.h"
57 /* note; image_panel_properties() uses pointer to sima->image directly */
58 Image *ED_space_image(SpaceImage *sima)
63 /* called to assign images to UV faces */
64 void ED_space_image_set(SpaceImage *sima, Scene *scene, Object *obedit, Image *ima)
66 /* context may be NULL, so use global */
67 ED_uvedit_assign_image(G.main, scene, obedit, ima, sima->image);
69 /* change the space ima after because uvedit_face_visible_test uses the space ima
70 * to check if the face is displayed in UV-localview */
73 if (ima == NULL || ima->type == IMA_TYPE_R_RESULT || ima->type == IMA_TYPE_COMPOSITE) {
74 if (sima->mode == SI_MODE_PAINT) {
75 sima->mode = SI_MODE_VIEW;
80 BKE_image_signal(sima->image, &sima->iuser, IMA_SIGNAL_USER_NEW_IMAGE);
82 id_us_ensure_real((ID *)sima->image);
85 WM_main_add_notifier(NC_GEOM | ND_DATA, obedit->data);
87 WM_main_add_notifier(NC_SPACE | ND_SPACE_IMAGE, NULL);
90 Mask *ED_space_image_get_mask(SpaceImage *sima)
92 return sima->mask_info.mask;
95 void ED_space_image_set_mask(bContext *C, SpaceImage *sima, Mask *mask)
97 sima->mask_info.mask = mask;
99 /* weak, but same as image/space */
100 id_us_ensure_real((ID *)sima->mask_info.mask);
103 WM_event_add_notifier(C, NC_MASK | NA_SELECTED, mask);
107 ImBuf *ED_space_image_acquire_buffer(SpaceImage *sima, void **r_lock)
111 if (sima && sima->image) {
113 if (sima->image->type == IMA_TYPE_R_RESULT && BIF_show_render_spare())
114 return BIF_render_spare_imbuf();
117 ibuf = BKE_image_acquire_ibuf(sima->image, &sima->iuser, r_lock);
120 if (ibuf->rect || ibuf->rect_float)
130 void ED_space_image_release_buffer(SpaceImage *sima, ImBuf *ibuf, void *lock)
132 if (sima && sima->image)
133 BKE_image_release_ibuf(sima->image, ibuf, lock);
136 bool ED_space_image_has_buffer(SpaceImage *sima)
142 ibuf = ED_space_image_acquire_buffer(sima, &lock);
143 has_buffer = (ibuf != NULL);
144 ED_space_image_release_buffer(sima, ibuf, lock);
149 void ED_space_image_get_size(SpaceImage *sima, int *width, int *height)
151 Scene *scene = sima->iuser.scene;
155 ibuf = ED_space_image_acquire_buffer(sima, &lock);
157 if (ibuf && ibuf->x > 0 && ibuf->y > 0) {
161 else if (sima->image && sima->image->type == IMA_TYPE_R_RESULT && scene) {
162 /* not very important, just nice */
163 *width = (scene->r.xsch * scene->r.size) / 100;
164 *height = (scene->r.ysch * scene->r.size) / 100;
166 if ((scene->r.mode & R_BORDER) && (scene->r.mode & R_CROP)) {
167 *width *= BLI_rctf_size_x(&scene->r.border);
168 *height *= BLI_rctf_size_y(&scene->r.border);
172 /* I know a bit weak... but preview uses not actual image size */
173 // XXX else if (image_preview_active(sima, width, height));
175 *width = IMG_SIZE_FALLBACK;
176 *height = IMG_SIZE_FALLBACK;
179 ED_space_image_release_buffer(sima, ibuf, lock);
182 void ED_space_image_get_size_fl(SpaceImage *sima, float size[2])
185 ED_space_image_get_size(sima, &size_i[0], &size_i[1]);
191 void ED_space_image_get_aspect(SpaceImage *sima, float *aspx, float *aspy)
193 Image *ima = sima->image;
194 if ((ima == NULL) || (ima->aspx == 0.0f || ima->aspy == 0.0f)) {
198 BKE_image_get_aspect(ima, aspx, aspy);
202 void ED_space_image_get_zoom(SpaceImage *sima, ARegion *ar, float *zoomx, float *zoomy)
206 ED_space_image_get_size(sima, &width, &height);
208 *zoomx = (float)(BLI_rcti_size_x(&ar->winrct) + 1) / (float)(BLI_rctf_size_x(&ar->v2d.cur) * width);
209 *zoomy = (float)(BLI_rcti_size_y(&ar->winrct) + 1) / (float)(BLI_rctf_size_y(&ar->v2d.cur) * height);
212 void ED_space_image_get_uv_aspect(SpaceImage *sima, float *aspx, float *aspy)
216 ED_space_image_get_aspect(sima, aspx, aspy);
217 ED_space_image_get_size(sima, &w, &h);
223 *aspy = *aspy / *aspx;
227 *aspx = *aspx / *aspy;
232 void ED_image_get_uv_aspect(Image *ima, ImageUser *iuser, float *aspx, float *aspy)
237 BKE_image_get_aspect(ima, aspx, aspy);
238 BKE_image_get_size(ima, iuser, &w, &h);
249 /* takes event->mval */
250 void ED_image_mouse_pos(SpaceImage *sima, ARegion *ar, const int mval[2], float co[2])
252 int sx, sy, width, height;
255 ED_space_image_get_zoom(sima, ar, &zoomx, &zoomy);
256 ED_space_image_get_size(sima, &width, &height);
258 UI_view2d_view_to_region(&ar->v2d, 0.0f, 0.0f, &sx, &sy);
260 co[0] = ((mval[0] - sx) / zoomx) / width;
261 co[1] = ((mval[1] - sy) / zoomy) / height;
264 void ED_image_point_pos(SpaceImage *sima, ARegion *ar, float x, float y, float *xr, float *yr)
266 int sx, sy, width, height;
269 ED_space_image_get_zoom(sima, ar, &zoomx, &zoomy);
270 ED_space_image_get_size(sima, &width, &height);
272 UI_view2d_view_to_region(&ar->v2d, 0.0f, 0.0f, &sx, &sy);
274 *xr = ((x - sx) / zoomx) / width;
275 *yr = ((y - sy) / zoomy) / height;
278 void ED_image_point_pos__reverse(SpaceImage *sima, ARegion *ar, const float co[2], float r_co[2])
284 UI_view2d_view_to_region(&ar->v2d, 0.0f, 0.0f, &sx, &sy);
285 ED_space_image_get_size(sima, &width, &height);
286 ED_space_image_get_zoom(sima, ar, &zoomx, &zoomy);
288 r_co[0] = (co[0] * width * zoomx) + (float)sx;
289 r_co[1] = (co[1] * height * zoomy) + (float)sy;
293 * This is more a user-level functionality, for going to next/prev used slot,
294 * Stepping onto the last unused slot too.
296 bool ED_image_slot_cycle(struct Image *image, int direction)
298 const int cur = image->render_slot;
301 BLI_assert(ELEM(direction, -1, 1));
303 for (i = 1; i < IMA_MAX_RENDER_SLOT; i++) {
304 slot = (cur + ((direction == -1) ? -i : i)) % IMA_MAX_RENDER_SLOT;
305 if (slot < 0) slot += IMA_MAX_RENDER_SLOT;
307 if (image->renders[slot] || slot == image->last_render_slot) {
308 image->render_slot = slot;
313 if (i == IMA_MAX_RENDER_SLOT) {
314 image->render_slot = ((cur == 1) ? 0 : 1);
317 return (cur != image->render_slot);
320 void ED_space_image_scopes_update(const struct bContext *C, struct SpaceImage *sima, struct ImBuf *ibuf, bool use_view_settings)
322 Scene *scene = CTX_data_scene(C);
323 Object *ob = CTX_data_active_object(C);
325 /* scope update can be expensive, don't update during paint modes */
326 if (sima->mode == SI_MODE_PAINT)
328 if (ob && ((ob->mode & (OB_MODE_TEXTURE_PAINT | OB_MODE_EDIT)) != 0))
331 scopes_update(&sima->scopes, ibuf, use_view_settings ? &scene->view_settings : NULL, &scene->display_settings);
334 bool ED_space_image_show_render(SpaceImage *sima)
336 return (sima->image && ELEM(sima->image->type, IMA_TYPE_R_RESULT, IMA_TYPE_COMPOSITE));
339 bool ED_space_image_show_paint(SpaceImage *sima)
341 if (ED_space_image_show_render(sima))
344 return (sima->mode == SI_MODE_PAINT);
347 bool ED_space_image_show_uvedit(SpaceImage *sima, Object *obedit)
349 if (sima && (ED_space_image_show_render(sima) || ED_space_image_show_paint(sima)))
352 if (obedit && obedit->type == OB_MESH) {
353 struct BMEditMesh *em = BKE_editmesh_from_object(obedit);
356 ret = EDBM_mtexpoly_check(em);
364 /* matches clip function */
365 bool ED_space_image_check_show_maskedit(Scene *scene, SpaceImage *sima)
367 /* check editmode - this is reserved for UV editing */
369 if (ob && ob->mode & OB_MODE_EDIT && ED_space_image_show_uvedit(sima, ob)) {
373 return (sima->mode == SI_MODE_MASK);
376 int ED_space_image_maskedit_poll(bContext *C)
378 SpaceImage *sima = CTX_wm_space_image(C);
381 Scene *scene = CTX_data_scene(C);
382 return ED_space_image_check_show_maskedit(scene, sima);
388 bool ED_space_image_paint_curve(const bContext *C)
390 SpaceImage *sima = CTX_wm_space_image(C);
392 if (sima && sima->mode == SI_MODE_PAINT) {
393 Brush *br = CTX_data_tool_settings(C)->imapaint.paint.brush;
395 if (br && (br->flag & BRUSH_CURVE))
403 int ED_space_image_maskedit_mask_poll(bContext *C)
405 if (ED_space_image_maskedit_poll(C)) {
406 SpaceImage *sima = CTX_wm_space_image(C);
407 return sima->mask_info.mask != NULL;