move ED_image functions into their own file.
[blender.git] / source / blender / editors / space_image / image_edit.c
1 /*
2  * ***** BEGIN GPL LICENSE BLOCK *****
3  *
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.
8  *
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.
13  *
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.
17  *
18  * The Original Code is Copyright (C) 2008 Blender Foundation.
19  * All rights reserved.
20  *
21  *
22  * Contributor(s): Blender Foundation
23  *
24  * ***** END GPL LICENSE BLOCK *****
25  */
26
27 /** \file blender/editors/space_image/image_editor.c
28  *  \ingroup spimage
29  */
30
31 #include "DNA_object_types.h"
32 #include "DNA_scene_types.h"
33
34 #include "BLI_math.h"
35
36 #include "BKE_image.h"
37 #include "BKE_global.h"
38 #include "BKE_main.h"
39 #include "BKE_tessmesh.h"
40
41 #include "IMB_imbuf_types.h"
42
43 #include "ED_image.h"  /* own include */
44 #include "ED_mesh.h"
45 #include "ED_screen.h"
46 #include "ED_uvedit.h"
47
48 #include "WM_api.h"
49 #include "WM_types.h"
50
51 /* note; image_panel_properties() uses pointer to sima->image directly */
52 Image *ED_space_image(SpaceImage *sima)
53 {
54         return sima->image;
55 }
56
57 /* called to assign images to UV faces */
58 void ED_space_image_set(SpaceImage *sima, Scene *scene, Object *obedit, Image *ima)
59 {
60         /* context may be NULL, so use global */
61         ED_uvedit_assign_image(G.main, scene, obedit, ima, sima->image);
62
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 */
65         sima->image = ima;
66
67         if (ima == NULL || ima->type == IMA_TYPE_R_RESULT || ima->type == IMA_TYPE_COMPOSITE)
68                 sima->flag &= ~SI_DRAWTOOL;
69
70         if (sima->image)
71                 BKE_image_signal(sima->image, &sima->iuser, IMA_SIGNAL_USER_NEW_IMAGE);
72
73         if (sima->image && sima->image->id.us == 0)
74                 sima->image->id.us = 1;
75
76         if (obedit)
77                 WM_main_add_notifier(NC_GEOM | ND_DATA, obedit->data);
78
79         WM_main_add_notifier(NC_SPACE | ND_SPACE_IMAGE, NULL);
80 }
81
82 ImBuf *ED_space_image_acquire_buffer(SpaceImage *sima, void **lock_r)
83 {
84         ImBuf *ibuf;
85
86         if (sima && sima->image) {
87 #if 0
88                 if (sima->image->type == IMA_TYPE_R_RESULT && BIF_show_render_spare())
89                         return BIF_render_spare_imbuf();
90                 else
91 #endif
92                 ibuf = BKE_image_acquire_ibuf(sima->image, &sima->iuser, lock_r);
93
94                 if (ibuf && (ibuf->rect || ibuf->rect_float))
95                         return ibuf;
96         }
97
98         return NULL;
99 }
100
101 void ED_space_image_release_buffer(SpaceImage *sima, void *lock)
102 {
103         if (sima && sima->image)
104                 BKE_image_release_ibuf(sima->image, lock);
105 }
106
107 int ED_space_image_has_buffer(SpaceImage *sima)
108 {
109         ImBuf *ibuf;
110         void *lock;
111         int has_buffer;
112
113         ibuf = ED_space_image_acquire_buffer(sima, &lock);
114         has_buffer = (ibuf != NULL);
115         ED_space_image_release_buffer(sima, lock);
116
117         return has_buffer;
118 }
119
120 void ED_image_size(Image *ima, int *width, int *height)
121 {
122         ImBuf *ibuf = NULL;
123         void *lock;
124
125         if (ima)
126                 ibuf = BKE_image_acquire_ibuf(ima, NULL, &lock);
127
128         if (ibuf && ibuf->x > 0 && ibuf->y > 0) {
129                 *width = ibuf->x;
130                 *height = ibuf->y;
131         }
132         else {
133                 *width = 256;
134                 *height = 256;
135         }
136
137         if (ima)
138                 BKE_image_release_ibuf(ima, lock);
139 }
140
141 void ED_space_image_size(SpaceImage *sima, int *width, int *height)
142 {
143         Scene *scene = sima->iuser.scene;
144         ImBuf *ibuf;
145         void *lock;
146
147         ibuf = ED_space_image_acquire_buffer(sima, &lock);
148
149         if (ibuf && ibuf->x > 0 && ibuf->y > 0) {
150                 *width = ibuf->x;
151                 *height = ibuf->y;
152         }
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;
157
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);
161                 }
162
163         }
164         /* I know a bit weak... but preview uses not actual image size */
165         // XXX else if (image_preview_active(sima, width, height));
166         else {
167                 *width = 256;
168                 *height = 256;
169         }
170
171         ED_space_image_release_buffer(sima, lock);
172 }
173
174 void ED_image_aspect(Image *ima, float *aspx, float *aspy)
175 {
176         *aspx = *aspy = 1.0;
177
178         if ((ima == NULL) || (ima->type == IMA_TYPE_R_RESULT) || (ima->type == IMA_TYPE_COMPOSITE) ||
179             (ima->aspx == 0.0f || ima->aspy == 0.0f))
180         {
181                 return;
182         }
183
184         /* x is always 1 */
185         *aspy = ima->aspy / ima->aspx;
186 }
187
188 void ED_space_image_aspect(SpaceImage *sima, float *aspx, float *aspy)
189 {
190         ED_image_aspect(ED_space_image(sima), aspx, aspy);
191 }
192
193 void ED_space_image_zoom(SpaceImage *sima, ARegion *ar, float *zoomx, float *zoomy)
194 {
195         int width, height;
196
197         ED_space_image_size(sima, &width, &height);
198
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);
201 }
202
203 void ED_space_image_uv_aspect(SpaceImage *sima, float *aspx, float *aspy)
204 {
205         int w, h;
206
207         ED_space_image_aspect(sima, aspx, aspy);
208         ED_space_image_size(sima, &w, &h);
209
210         *aspx *= (float)w;
211         *aspy *= (float)h;
212
213         if (*aspx < *aspy) {
214                 *aspy = *aspy / *aspx;
215                 *aspx = 1.0f;
216         }
217         else {
218                 *aspx = *aspx / *aspy;
219                 *aspy = 1.0f;
220         }
221 }
222
223 void ED_image_uv_aspect(Image *ima, float *aspx, float *aspy)
224 {
225         int w, h;
226
227         ED_image_aspect(ima, aspx, aspy);
228         ED_image_size(ima, &w, &h);
229
230         *aspx *= (float)w;
231         *aspy *= (float)h;
232 }
233
234 int ED_space_image_show_render(SpaceImage *sima)
235 {
236         return (sima->image && ELEM(sima->image->type, IMA_TYPE_R_RESULT, IMA_TYPE_COMPOSITE));
237 }
238
239 int ED_space_image_show_paint(SpaceImage *sima)
240 {
241         if (ED_space_image_show_render(sima))
242                 return 0;
243
244         return (sima->flag & SI_DRAWTOOL);
245 }
246
247 int ED_space_image_show_uvedit(SpaceImage *sima, Object *obedit)
248 {
249         if (sima && (ED_space_image_show_render(sima) || ED_space_image_show_paint(sima)))
250                 return 0;
251
252         if (obedit && obedit->type == OB_MESH) {
253                 struct BMEditMesh *em = BMEdit_FromObject(obedit);
254                 int ret;
255
256                 ret = EDBM_mtexpoly_check(em);
257
258                 return ret;
259         }
260
261         return 0;
262 }
263
264 int ED_space_image_show_uvshadow(SpaceImage *sima, Object *obedit)
265 {
266         if (ED_space_image_show_render(sima))
267                 return 0;
268
269         if (ED_space_image_show_paint(sima))
270                 if (obedit && obedit->type == OB_MESH) {
271                         struct BMEditMesh *em = BMEdit_FromObject(obedit);
272                         int ret;
273
274                         ret = EDBM_mtexpoly_check(em);
275
276                         return ret;
277                 }
278
279         return 0;
280 }
281
282 /******************** TODO ********************/
283
284 /* XXX notifier? */
285
286 /* goes over all ImageUsers, and sets frame numbers if auto-refresh is set */
287
288 static void image_update_frame(struct Image *UNUSED(ima), struct ImageUser *iuser, void *customdata)
289 {
290         int cfra = *(int *)customdata;
291
292         BKE_image_user_check_frame_calc(iuser, cfra, 0);
293 }
294
295 void ED_image_update_frame(const Main *mainp, int cfra)
296 {
297         BKE_image_walk_all_users(mainp, &cfra, image_update_frame);
298 }