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_object_types.h"
32 #include "DNA_scene_types.h"
36 #include "BKE_image.h"
37 #include "BKE_global.h"
39 #include "BKE_tessmesh.h"
41 #include "IMB_imbuf_types.h"
43 #include "ED_image.h" /* own include */
45 #include "ED_screen.h"
46 #include "ED_uvedit.h"
51 /* note; image_panel_properties() uses pointer to sima->image directly */
52 Image *ED_space_image(SpaceImage *sima)
57 /* called to assign images to UV faces */
58 void ED_space_image_set(SpaceImage *sima, Scene *scene, Object *obedit, Image *ima)
60 /* context may be NULL, so use global */
61 ED_uvedit_assign_image(G.main, scene, obedit, ima, sima->image);
63 /* change the space ima after because uvedit_face_visible_test uses the space ima
64 * to check if the face is displayed in UV-localview */
67 if (ima == NULL || ima->type == IMA_TYPE_R_RESULT || ima->type == IMA_TYPE_COMPOSITE)
68 sima->flag &= ~SI_DRAWTOOL;
71 BKE_image_signal(sima->image, &sima->iuser, IMA_SIGNAL_USER_NEW_IMAGE);
73 if (sima->image && sima->image->id.us == 0)
74 sima->image->id.us = 1;
77 WM_main_add_notifier(NC_GEOM | ND_DATA, obedit->data);
79 WM_main_add_notifier(NC_SPACE | ND_SPACE_IMAGE, NULL);
82 ImBuf *ED_space_image_acquire_buffer(SpaceImage *sima, void **lock_r)
86 if (sima && sima->image) {
88 if (sima->image->type == IMA_TYPE_R_RESULT && BIF_show_render_spare())
89 return BIF_render_spare_imbuf();
92 ibuf = BKE_image_acquire_ibuf(sima->image, &sima->iuser, lock_r);
94 if (ibuf && (ibuf->rect || ibuf->rect_float))
101 void ED_space_image_release_buffer(SpaceImage *sima, void *lock)
103 if (sima && sima->image)
104 BKE_image_release_ibuf(sima->image, lock);
107 int ED_space_image_has_buffer(SpaceImage *sima)
113 ibuf = ED_space_image_acquire_buffer(sima, &lock);
114 has_buffer = (ibuf != NULL);
115 ED_space_image_release_buffer(sima, lock);
120 void ED_image_size(Image *ima, int *width, int *height)
126 ibuf = BKE_image_acquire_ibuf(ima, NULL, &lock);
128 if (ibuf && ibuf->x > 0 && ibuf->y > 0) {
138 BKE_image_release_ibuf(ima, lock);
141 void ED_space_image_size(SpaceImage *sima, int *width, int *height)
143 Scene *scene = sima->iuser.scene;
147 ibuf = ED_space_image_acquire_buffer(sima, &lock);
149 if (ibuf && ibuf->x > 0 && ibuf->y > 0) {
153 else if (sima->image && sima->image->type == IMA_TYPE_R_RESULT && scene) {
154 /* not very important, just nice */
155 *width = (scene->r.xsch * scene->r.size) / 100;
156 *height = (scene->r.ysch * scene->r.size) / 100;
158 if ((scene->r.mode & R_BORDER) && (scene->r.mode & R_CROP)) {
159 *width *= (scene->r.border.xmax - scene->r.border.xmin);
160 *height *= (scene->r.border.ymax - scene->r.border.ymin);
164 /* I know a bit weak... but preview uses not actual image size */
165 // XXX else if (image_preview_active(sima, width, height));
171 ED_space_image_release_buffer(sima, lock);
174 void ED_image_aspect(Image *ima, float *aspx, float *aspy)
178 if ((ima == NULL) || (ima->type == IMA_TYPE_R_RESULT) || (ima->type == IMA_TYPE_COMPOSITE) ||
179 (ima->aspx == 0.0f || ima->aspy == 0.0f))
185 *aspy = ima->aspy / ima->aspx;
188 void ED_space_image_aspect(SpaceImage *sima, float *aspx, float *aspy)
190 ED_image_aspect(ED_space_image(sima), aspx, aspy);
193 void ED_space_image_zoom(SpaceImage *sima, ARegion *ar, float *zoomx, float *zoomy)
197 ED_space_image_size(sima, &width, &height);
199 *zoomx = (float)(ar->winrct.xmax - ar->winrct.xmin + 1) / (float)((ar->v2d.cur.xmax - ar->v2d.cur.xmin) * width);
200 *zoomy = (float)(ar->winrct.ymax - ar->winrct.ymin + 1) / (float)((ar->v2d.cur.ymax - ar->v2d.cur.ymin) * height);
203 void ED_space_image_uv_aspect(SpaceImage *sima, float *aspx, float *aspy)
207 ED_space_image_aspect(sima, aspx, aspy);
208 ED_space_image_size(sima, &w, &h);
214 *aspy = *aspy / *aspx;
218 *aspx = *aspx / *aspy;
223 void ED_image_uv_aspect(Image *ima, float *aspx, float *aspy)
227 ED_image_aspect(ima, aspx, aspy);
228 ED_image_size(ima, &w, &h);
234 int ED_space_image_show_render(SpaceImage *sima)
236 return (sima->image && ELEM(sima->image->type, IMA_TYPE_R_RESULT, IMA_TYPE_COMPOSITE));
239 int ED_space_image_show_paint(SpaceImage *sima)
241 if (ED_space_image_show_render(sima))
244 return (sima->flag & SI_DRAWTOOL);
247 int ED_space_image_show_uvedit(SpaceImage *sima, Object *obedit)
249 if (sima && (ED_space_image_show_render(sima) || ED_space_image_show_paint(sima)))
252 if (obedit && obedit->type == OB_MESH) {
253 struct BMEditMesh *em = BMEdit_FromObject(obedit);
256 ret = EDBM_mtexpoly_check(em);
264 int ED_space_image_show_uvshadow(SpaceImage *sima, Object *obedit)
266 if (ED_space_image_show_render(sima))
269 if (ED_space_image_show_paint(sima))
270 if (obedit && obedit->type == OB_MESH) {
271 struct BMEditMesh *em = BMEdit_FromObject(obedit);
274 ret = EDBM_mtexpoly_check(em);
282 /******************** TODO ********************/
286 /* goes over all ImageUsers, and sets frame numbers if auto-refresh is set */
288 static void image_update_frame(struct Image *UNUSED(ima), struct ImageUser *iuser, void *customdata)
290 int cfra = *(int *)customdata;
292 BKE_image_user_check_frame_calc(iuser, cfra, 0);
295 void ED_image_update_frame(const Main *mainp, int cfra)
297 BKE_image_walk_all_users(mainp, &cfra, image_update_frame);