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)
122 BKE_image_release_ibuf(sima->image, ibuf, *r_lock);
132 void ED_space_image_release_buffer(SpaceImage *sima, ImBuf *ibuf, void *lock)
134 if (sima && sima->image)
135 BKE_image_release_ibuf(sima->image, ibuf, lock);
138 bool ED_space_image_has_buffer(SpaceImage *sima)
144 ibuf = ED_space_image_acquire_buffer(sima, &lock);
145 has_buffer = (ibuf != NULL);
146 ED_space_image_release_buffer(sima, ibuf, lock);
151 void ED_space_image_get_size(SpaceImage *sima, int *width, int *height)
153 Scene *scene = sima->iuser.scene;
157 ibuf = ED_space_image_acquire_buffer(sima, &lock);
159 if (ibuf && ibuf->x > 0 && ibuf->y > 0) {
163 else if (sima->image && sima->image->type == IMA_TYPE_R_RESULT && scene) {
164 /* not very important, just nice */
165 *width = (scene->r.xsch * scene->r.size) / 100;
166 *height = (scene->r.ysch * scene->r.size) / 100;
168 if ((scene->r.mode & R_BORDER) && (scene->r.mode & R_CROP)) {
169 *width *= BLI_rctf_size_x(&scene->r.border);
170 *height *= BLI_rctf_size_y(&scene->r.border);
174 /* I know a bit weak... but preview uses not actual image size */
175 // XXX else if (image_preview_active(sima, width, height));
177 *width = IMG_SIZE_FALLBACK;
178 *height = IMG_SIZE_FALLBACK;
181 ED_space_image_release_buffer(sima, ibuf, lock);
184 void ED_space_image_get_size_fl(SpaceImage *sima, float size[2])
187 ED_space_image_get_size(sima, &size_i[0], &size_i[1]);
193 void ED_space_image_get_aspect(SpaceImage *sima, float *aspx, float *aspy)
195 Image *ima = sima->image;
196 if ((ima == NULL) || (ima->aspx == 0.0f || ima->aspy == 0.0f)) {
200 BKE_image_get_aspect(ima, aspx, aspy);
204 void ED_space_image_get_zoom(SpaceImage *sima, ARegion *ar, float *zoomx, float *zoomy)
208 ED_space_image_get_size(sima, &width, &height);
210 *zoomx = (float)(BLI_rcti_size_x(&ar->winrct) + 1) / (float)(BLI_rctf_size_x(&ar->v2d.cur) * width);
211 *zoomy = (float)(BLI_rcti_size_y(&ar->winrct) + 1) / (float)(BLI_rctf_size_y(&ar->v2d.cur) * height);
214 void ED_space_image_get_uv_aspect(SpaceImage *sima, float *aspx, float *aspy)
218 ED_space_image_get_aspect(sima, aspx, aspy);
219 ED_space_image_get_size(sima, &w, &h);
225 *aspy = *aspy / *aspx;
229 *aspx = *aspx / *aspy;
234 void ED_image_get_uv_aspect(Image *ima, ImageUser *iuser, float *aspx, float *aspy)
239 BKE_image_get_aspect(ima, aspx, aspy);
240 BKE_image_get_size(ima, iuser, &w, &h);
251 /* takes event->mval */
252 void ED_image_mouse_pos(SpaceImage *sima, ARegion *ar, const int mval[2], float co[2])
254 int sx, sy, width, height;
257 ED_space_image_get_zoom(sima, ar, &zoomx, &zoomy);
258 ED_space_image_get_size(sima, &width, &height);
260 UI_view2d_view_to_region(&ar->v2d, 0.0f, 0.0f, &sx, &sy);
262 co[0] = ((mval[0] - sx) / zoomx) / width;
263 co[1] = ((mval[1] - sy) / zoomy) / height;
266 void ED_image_point_pos(SpaceImage *sima, ARegion *ar, float x, float y, float *xr, float *yr)
268 int sx, sy, width, height;
271 ED_space_image_get_zoom(sima, ar, &zoomx, &zoomy);
272 ED_space_image_get_size(sima, &width, &height);
274 UI_view2d_view_to_region(&ar->v2d, 0.0f, 0.0f, &sx, &sy);
276 *xr = ((x - sx) / zoomx) / width;
277 *yr = ((y - sy) / zoomy) / height;
280 void ED_image_point_pos__reverse(SpaceImage *sima, ARegion *ar, const float co[2], float r_co[2])
286 UI_view2d_view_to_region(&ar->v2d, 0.0f, 0.0f, &sx, &sy);
287 ED_space_image_get_size(sima, &width, &height);
288 ED_space_image_get_zoom(sima, ar, &zoomx, &zoomy);
290 r_co[0] = (co[0] * width * zoomx) + (float)sx;
291 r_co[1] = (co[1] * height * zoomy) + (float)sy;
295 * This is more a user-level functionality, for going to next/prev used slot,
296 * Stepping onto the last unused slot too.
298 bool ED_image_slot_cycle(struct Image *image, int direction)
300 const int cur = image->render_slot;
303 BLI_assert(ELEM(direction, -1, 1));
305 for (i = 1; i < IMA_MAX_RENDER_SLOT; i++) {
306 slot = (cur + ((direction == -1) ? -i : i)) % IMA_MAX_RENDER_SLOT;
307 if (slot < 0) slot += IMA_MAX_RENDER_SLOT;
309 if (image->renders[slot] || slot == image->last_render_slot) {
310 image->render_slot = slot;
315 if (i == IMA_MAX_RENDER_SLOT) {
316 image->render_slot = ((cur == 1) ? 0 : 1);
319 return (cur != image->render_slot);
322 void ED_space_image_scopes_update(const struct bContext *C, struct SpaceImage *sima, struct ImBuf *ibuf, bool use_view_settings)
324 Scene *scene = CTX_data_scene(C);
325 Object *ob = CTX_data_active_object(C);
327 /* scope update can be expensive, don't update during paint modes */
328 if (sima->mode == SI_MODE_PAINT)
330 if (ob && ((ob->mode & (OB_MODE_TEXTURE_PAINT | OB_MODE_EDIT)) != 0))
333 scopes_update(&sima->scopes, ibuf, use_view_settings ? &scene->view_settings : NULL, &scene->display_settings);
336 bool ED_space_image_show_render(SpaceImage *sima)
338 return (sima->image && ELEM(sima->image->type, IMA_TYPE_R_RESULT, IMA_TYPE_COMPOSITE));
341 bool ED_space_image_show_paint(SpaceImage *sima)
343 if (ED_space_image_show_render(sima))
346 return (sima->mode == SI_MODE_PAINT);
349 bool ED_space_image_show_uvedit(SpaceImage *sima, Object *obedit)
351 if (sima && (ED_space_image_show_render(sima) || ED_space_image_show_paint(sima)))
354 if (obedit && obedit->type == OB_MESH) {
355 struct BMEditMesh *em = BKE_editmesh_from_object(obedit);
358 ret = EDBM_mtexpoly_check(em);
366 /* matches clip function */
367 bool ED_space_image_check_show_maskedit(Scene *scene, SpaceImage *sima)
369 /* check editmode - this is reserved for UV editing */
371 if (ob && ob->mode & OB_MODE_EDIT && ED_space_image_show_uvedit(sima, ob)) {
375 return (sima->mode == SI_MODE_MASK);
378 int ED_space_image_maskedit_poll(bContext *C)
380 SpaceImage *sima = CTX_wm_space_image(C);
383 Scene *scene = CTX_data_scene(C);
384 return ED_space_image_check_show_maskedit(scene, sima);
390 bool ED_space_image_paint_curve(const bContext *C)
392 SpaceImage *sima = CTX_wm_space_image(C);
394 if (sima && sima->mode == SI_MODE_PAINT) {
395 Brush *br = CTX_data_tool_settings(C)->imapaint.paint.brush;
397 if (br && (br->flag & BRUSH_CURVE))
405 int ED_space_image_maskedit_mask_poll(bContext *C)
407 if (ED_space_image_maskedit_poll(C)) {
408 SpaceImage *sima = CTX_wm_space_image(C);
409 return sima->mask_info.mask != NULL;