Color management refactoiring and some extra options
[blender.git] / source / blender / editors / space_image / space_image.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/space_image.c
28  *  \ingroup spimage
29  */
30
31
32 #include <string.h>
33 #include <stdio.h>
34
35 #include "DNA_mesh_types.h"
36 #include "DNA_meshdata_types.h"
37 #include "DNA_object_types.h"
38 #include "DNA_scene_types.h"
39
40 #include "MEM_guardedalloc.h"
41
42 #include "BLI_blenlib.h"
43 #include "BLI_math.h"
44 #include "BLI_rand.h"
45 #include "BLI_utildefines.h"
46
47 #include "BKE_colortools.h"
48 #include "BKE_context.h"
49 #include "BKE_image.h"
50 #include "BKE_global.h"
51 #include "BKE_main.h"
52 #include "BKE_mesh.h"
53 #include "BKE_scene.h"
54 #include "BKE_screen.h"
55 #include "BKE_tessmesh.h"
56
57 #include "IMB_imbuf_types.h"
58
59 #include "ED_image.h"
60 #include "ED_mesh.h"
61 #include "ED_space_api.h"
62 #include "ED_screen.h"
63 #include "ED_uvedit.h"
64
65 #include "BIF_gl.h"
66
67 #include "RNA_access.h"
68
69 #include "WM_api.h"
70 #include "WM_types.h"
71
72 #include "UI_resources.h"
73 #include "UI_view2d.h"
74
75 #include "image_intern.h"
76
77 /**************************** common state *****************************/
78
79 /* note; image_panel_properties() uses pointer to sima->image directly */
80 Image *ED_space_image(SpaceImage *sima)
81 {
82         return sima->image;
83 }
84
85 /* called to assign images to UV faces */
86 void ED_space_image_set(SpaceImage *sima, Scene *scene, Object *obedit, Image *ima)
87 {
88         /* context may be NULL, so use global */
89         ED_uvedit_assign_image(G.main, scene, obedit, ima, sima->image);
90         
91         /* change the space ima after because uvedit_face_visible_test uses the space ima
92          * to check if the face is displayed in UV-localview */
93         sima->image = ima;
94         
95         if (ima == NULL || ima->type == IMA_TYPE_R_RESULT || ima->type == IMA_TYPE_COMPOSITE)
96                 sima->flag &= ~SI_DRAWTOOL;
97         
98         if (sima->image)
99                 BKE_image_signal(sima->image, &sima->iuser, IMA_SIGNAL_USER_NEW_IMAGE);
100         
101         if (sima->image && sima->image->id.us == 0)
102                 sima->image->id.us = 1;
103         
104         if (obedit)
105                 WM_main_add_notifier(NC_GEOM | ND_DATA, obedit->data);
106
107         WM_main_add_notifier(NC_SPACE | ND_SPACE_IMAGE, NULL);
108 }
109
110 ImBuf *ED_space_image_acquire_buffer(SpaceImage *sima, void **lock_r)
111 {
112         ImBuf *ibuf;
113         
114         if (sima && sima->image) {
115 #if 0
116                 if (sima->image->type == IMA_TYPE_R_RESULT && BIF_show_render_spare())
117                         return BIF_render_spare_imbuf();
118                 else
119 #endif
120                 ibuf = BKE_image_acquire_ibuf(sima->image, &sima->iuser, lock_r);
121                 
122                 if (ibuf && (ibuf->rect || ibuf->rect_float))
123                         return ibuf;
124         }
125         
126         return NULL;
127 }
128
129 void ED_space_image_release_buffer(SpaceImage *sima, void *lock)
130 {
131         if (sima && sima->image)
132                 BKE_image_release_ibuf(sima->image, lock);
133 }
134
135 int ED_space_image_has_buffer(SpaceImage *sima)
136 {
137         ImBuf *ibuf;
138         void *lock;
139         int has_buffer;
140         
141         ibuf = ED_space_image_acquire_buffer(sima, &lock);
142         has_buffer = (ibuf != NULL);
143         ED_space_image_release_buffer(sima, lock);
144         
145         return has_buffer;
146 }
147
148 void ED_image_size(Image *ima, int *width, int *height)
149 {
150         ImBuf *ibuf = NULL;
151         void *lock;
152         
153         if (ima)
154                 ibuf = BKE_image_acquire_ibuf(ima, NULL, &lock);
155         
156         if (ibuf && ibuf->x > 0 && ibuf->y > 0) {
157                 *width = ibuf->x;
158                 *height = ibuf->y;
159         }
160         else {
161                 *width = 256;
162                 *height = 256;
163         }
164         
165         if (ima)
166                 BKE_image_release_ibuf(ima, lock);
167 }
168
169 void ED_space_image_size(SpaceImage *sima, int *width, int *height)
170 {
171         Scene *scene = sima->iuser.scene;
172         ImBuf *ibuf;
173         void *lock;
174         
175         ibuf = ED_space_image_acquire_buffer(sima, &lock);
176         
177         if (ibuf && ibuf->x > 0 && ibuf->y > 0) {
178                 *width = ibuf->x;
179                 *height = ibuf->y;
180         }
181         else if (sima->image && sima->image->type == IMA_TYPE_R_RESULT && scene) {
182                 /* not very important, just nice */
183                 *width = (scene->r.xsch * scene->r.size) / 100;
184                 *height = (scene->r.ysch * scene->r.size) / 100;
185
186                 if ((scene->r.mode & R_BORDER) && (scene->r.mode & R_CROP)) {
187                         *width *= (scene->r.border.xmax - scene->r.border.xmin);
188                         *height *= (scene->r.border.ymax - scene->r.border.ymin);
189                 }
190
191         }
192         /* I know a bit weak... but preview uses not actual image size */
193         // XXX else if (image_preview_active(sima, width, height));
194         else {
195                 *width = 256;
196                 *height = 256;
197         }
198         
199         ED_space_image_release_buffer(sima, lock);
200 }
201
202 void ED_image_aspect(Image *ima, float *aspx, float *aspy)
203 {
204         *aspx = *aspy = 1.0;
205         
206         if ((ima == NULL) || (ima->type == IMA_TYPE_R_RESULT) || (ima->type == IMA_TYPE_COMPOSITE) ||
207             (ima->aspx == 0.0f || ima->aspy == 0.0f))
208         {
209                 return;
210         }
211         
212         /* x is always 1 */
213         *aspy = ima->aspy / ima->aspx;
214 }
215
216 void ED_space_image_aspect(SpaceImage *sima, float *aspx, float *aspy)
217 {
218         ED_image_aspect(ED_space_image(sima), aspx, aspy);
219 }
220
221 void ED_space_image_zoom(SpaceImage *sima, ARegion *ar, float *zoomx, float *zoomy)
222 {
223         int width, height;
224         
225         ED_space_image_size(sima, &width, &height);
226         
227         *zoomx = (float)(ar->winrct.xmax - ar->winrct.xmin + 1) / (float)((ar->v2d.cur.xmax - ar->v2d.cur.xmin) * width);
228         *zoomy = (float)(ar->winrct.ymax - ar->winrct.ymin + 1) / (float)((ar->v2d.cur.ymax - ar->v2d.cur.ymin) * height);
229 }
230
231 void ED_space_image_uv_aspect(SpaceImage *sima, float *aspx, float *aspy)
232 {
233         int w, h;
234         
235         ED_space_image_aspect(sima, aspx, aspy);
236         ED_space_image_size(sima, &w, &h);
237
238         *aspx *= (float)w;
239         *aspy *= (float)h;
240         
241         if (*aspx < *aspy) {
242                 *aspy = *aspy / *aspx;
243                 *aspx = 1.0f;
244         }
245         else {
246                 *aspx = *aspx / *aspy;
247                 *aspy = 1.0f;
248         }
249 }
250
251 void ED_image_uv_aspect(Image *ima, float *aspx, float *aspy)
252 {
253         int w, h;
254         
255         ED_image_aspect(ima, aspx, aspy);
256         ED_image_size(ima, &w, &h);
257         
258         *aspx *= (float)w;
259         *aspy *= (float)h;
260 }
261
262 int ED_space_image_show_render(SpaceImage *sima)
263 {
264         return (sima->image && ELEM(sima->image->type, IMA_TYPE_R_RESULT, IMA_TYPE_COMPOSITE));
265 }
266
267 int ED_space_image_show_paint(SpaceImage *sima)
268 {
269         if (ED_space_image_show_render(sima))
270                 return 0;
271         
272         return (sima->flag & SI_DRAWTOOL);
273 }
274
275 int ED_space_image_show_uvedit(SpaceImage *sima, Object *obedit)
276 {
277         if (sima && (ED_space_image_show_render(sima) || ED_space_image_show_paint(sima)))
278                 return 0;
279
280         if (obedit && obedit->type == OB_MESH) {
281                 struct BMEditMesh *em = BMEdit_FromObject(obedit);
282                 int ret;
283                 
284                 ret = EDBM_mtexpoly_check(em);
285                 
286                 return ret;
287         }
288         
289         return 0;
290 }
291
292 int ED_space_image_show_uvshadow(SpaceImage *sima, Object *obedit)
293 {
294         if (ED_space_image_show_render(sima))
295                 return 0;
296         
297         if (ED_space_image_show_paint(sima))
298                 if (obedit && obedit->type == OB_MESH) {
299                         struct BMEditMesh *em = BMEdit_FromObject(obedit);
300                         int ret;
301                         
302                         ret = EDBM_mtexpoly_check(em);
303                         
304                         return ret;
305                 }
306         
307         return 0;
308 }
309
310
311 static void image_scopes_tag_refresh(ScrArea *sa)
312 {
313         SpaceImage *sima = (SpaceImage *)sa->spacedata.first;
314         ARegion *ar;
315
316         /* only while histogram is visible */
317         for (ar = sa->regionbase.first; ar; ar = ar->next) {
318                 if (ar->regiontype == RGN_TYPE_PREVIEW && ar->flag & RGN_FLAG_HIDDEN)
319                         return;
320         }
321
322         sima->scopes.ok = 0;
323 }
324
325
326 /* ******************** manage regions ********************* */
327
328 ARegion *image_has_buttons_region(ScrArea *sa)
329 {
330         ARegion *ar, *arnew;
331
332         ar = BKE_area_find_region_type(sa, RGN_TYPE_UI);
333         if (ar) return ar;
334         
335         /* add subdiv level; after header */
336         ar = BKE_area_find_region_type(sa, RGN_TYPE_HEADER);
337
338         /* is error! */
339         if (ar == NULL) return NULL;
340         
341         arnew = MEM_callocN(sizeof(ARegion), "buttons for image");
342         
343         BLI_insertlinkafter(&sa->regionbase, ar, arnew);
344         arnew->regiontype = RGN_TYPE_UI;
345         arnew->alignment = RGN_ALIGN_LEFT;
346         
347         arnew->flag = RGN_FLAG_HIDDEN;
348         
349         return arnew;
350 }
351
352 ARegion *image_has_scope_region(ScrArea *sa)
353 {
354         ARegion *ar, *arnew;
355
356         ar = BKE_area_find_region_type(sa, RGN_TYPE_PREVIEW);
357         if (ar) return ar;
358
359         /* add subdiv level; after buttons */
360         ar = BKE_area_find_region_type(sa, RGN_TYPE_UI);
361
362         /* is error! */
363         if (ar == NULL) return NULL;
364         
365         arnew = MEM_callocN(sizeof(ARegion), "scopes for image");
366         
367         BLI_insertlinkafter(&sa->regionbase, ar, arnew);
368         arnew->regiontype = RGN_TYPE_PREVIEW;
369         arnew->alignment = RGN_ALIGN_RIGHT;
370         
371         arnew->flag = RGN_FLAG_HIDDEN;
372
373         image_scopes_tag_refresh(sa);
374         
375         return arnew;
376 }
377
378 /* ******************** default callbacks for image space ***************** */
379
380 static SpaceLink *image_new(const bContext *UNUSED(C))
381 {
382         ARegion *ar;
383         SpaceImage *simage;
384         
385         simage = MEM_callocN(sizeof(SpaceImage), "initimage");
386         simage->spacetype = SPACE_IMAGE;
387         simage->zoom = 1;
388         simage->lock = 1;
389
390         BKE_color_managed_view_settings_init(&simage->view_settings);
391
392         simage->iuser.ok = 1;
393         simage->iuser.fie_ima = 2;
394         simage->iuser.frames = 100;
395         
396         scopes_new(&simage->scopes);
397         simage->sample_line_hist.height = 100;
398
399         /* header */
400         ar = MEM_callocN(sizeof(ARegion), "header for image");
401         
402         BLI_addtail(&simage->regionbase, ar);
403         ar->regiontype = RGN_TYPE_HEADER;
404         ar->alignment = RGN_ALIGN_BOTTOM;
405         
406         /* buttons/list view */
407         ar = MEM_callocN(sizeof(ARegion), "buttons for image");
408         
409         BLI_addtail(&simage->regionbase, ar);
410         ar->regiontype = RGN_TYPE_UI;
411         ar->alignment = RGN_ALIGN_LEFT;
412         ar->flag = RGN_FLAG_HIDDEN;
413         
414         /* scopes */
415         ar = MEM_callocN(sizeof(ARegion), "buttons for image");
416         
417         BLI_addtail(&simage->regionbase, ar);
418         ar->regiontype = RGN_TYPE_PREVIEW;
419         ar->alignment = RGN_ALIGN_RIGHT;
420         ar->flag = RGN_FLAG_HIDDEN;
421
422         /* main area */
423         ar = MEM_callocN(sizeof(ARegion), "main area for image");
424         
425         BLI_addtail(&simage->regionbase, ar);
426         ar->regiontype = RGN_TYPE_WINDOW;
427         
428         return (SpaceLink *)simage;
429 }
430
431 /* not spacelink itself */
432 static void image_free(SpaceLink *sl)
433 {       
434         SpaceImage *simage = (SpaceImage *) sl;
435         
436         if (simage->cumap)
437                 curvemapping_free(simage->cumap);
438         scopes_free(&simage->scopes);
439 }
440
441
442 /* spacetype; init callback, add handlers */
443 static void image_init(struct wmWindowManager *UNUSED(wm), ScrArea *sa)
444 {
445         ListBase *lb = WM_dropboxmap_find("Image", SPACE_IMAGE, 0);
446
447         /* add drop boxes */
448         WM_event_add_dropbox_handler(&sa->handlers, lb);
449         
450 }
451
452 static SpaceLink *image_duplicate(SpaceLink *sl)
453 {
454         SpaceImage *simage = (SpaceImage *) sl;
455         SpaceImage *simagen = MEM_dupallocN(sl);
456         
457         /* clear or remove stuff from old */
458         if (simagen->cumap)
459                 simagen->cumap = curvemapping_copy(simagen->cumap);
460
461         scopes_new(&simagen->scopes);
462
463         BKE_color_managed_view_settings_copy(&simagen->view_settings, &simage->view_settings);
464
465         return (SpaceLink *)simagen;
466 }
467
468 static void image_operatortypes(void)
469 {
470         WM_operatortype_append(IMAGE_OT_view_all);
471         WM_operatortype_append(IMAGE_OT_view_pan);
472         WM_operatortype_append(IMAGE_OT_view_selected);
473         WM_operatortype_append(IMAGE_OT_view_zoom);
474         WM_operatortype_append(IMAGE_OT_view_zoom_in);
475         WM_operatortype_append(IMAGE_OT_view_zoom_out);
476         WM_operatortype_append(IMAGE_OT_view_zoom_ratio);
477         WM_operatortype_append(IMAGE_OT_view_ndof);
478
479         WM_operatortype_append(IMAGE_OT_new);
480         WM_operatortype_append(IMAGE_OT_open);
481         WM_operatortype_append(IMAGE_OT_match_movie_length);
482         WM_operatortype_append(IMAGE_OT_replace);
483         WM_operatortype_append(IMAGE_OT_reload);
484         WM_operatortype_append(IMAGE_OT_save);
485         WM_operatortype_append(IMAGE_OT_save_as);
486         WM_operatortype_append(IMAGE_OT_save_sequence);
487         WM_operatortype_append(IMAGE_OT_pack);
488         WM_operatortype_append(IMAGE_OT_unpack);
489         
490         WM_operatortype_append(IMAGE_OT_invert);
491
492         WM_operatortype_append(IMAGE_OT_cycle_render_slot);
493
494         WM_operatortype_append(IMAGE_OT_sample);
495         WM_operatortype_append(IMAGE_OT_sample_line);
496         WM_operatortype_append(IMAGE_OT_curves_point_set);
497
498         WM_operatortype_append(IMAGE_OT_record_composite);
499
500         WM_operatortype_append(IMAGE_OT_properties);
501         WM_operatortype_append(IMAGE_OT_scopes);
502 }
503
504 static void image_keymap(struct wmKeyConfig *keyconf)
505 {
506         wmKeyMap *keymap = WM_keymap_find(keyconf, "Image Generic", SPACE_IMAGE, 0);
507         wmKeyMapItem *kmi;
508         int i;
509         
510         WM_keymap_add_item(keymap, "IMAGE_OT_new", NKEY, KM_PRESS, KM_ALT, 0);
511         WM_keymap_add_item(keymap, "IMAGE_OT_open", OKEY, KM_PRESS, KM_ALT, 0);
512         WM_keymap_add_item(keymap, "IMAGE_OT_reload", RKEY, KM_PRESS, KM_ALT, 0);
513         WM_keymap_add_item(keymap, "IMAGE_OT_save", SKEY, KM_PRESS, KM_ALT, 0);
514         WM_keymap_add_item(keymap, "IMAGE_OT_save_as", F3KEY, KM_PRESS, 0, 0);
515         WM_keymap_add_item(keymap, "IMAGE_OT_properties", NKEY, KM_PRESS, 0, 0);
516         WM_keymap_add_item(keymap, "IMAGE_OT_scopes", TKEY, KM_PRESS, 0, 0);
517
518         WM_keymap_add_item(keymap, "IMAGE_OT_cycle_render_slot", JKEY, KM_PRESS, 0, 0);
519         RNA_boolean_set(WM_keymap_add_item(keymap, "IMAGE_OT_cycle_render_slot", JKEY, KM_PRESS, KM_ALT, 0)->ptr, "reverse", TRUE);
520         
521         keymap = WM_keymap_find(keyconf, "Image", SPACE_IMAGE, 0);
522         
523         WM_keymap_add_item(keymap, "IMAGE_OT_view_all", HOMEKEY, KM_PRESS, 0, 0);
524         WM_keymap_add_item(keymap, "IMAGE_OT_view_selected", PADPERIOD, KM_PRESS, 0, 0);
525         WM_keymap_add_item(keymap, "IMAGE_OT_view_pan", MIDDLEMOUSE, KM_PRESS, 0, 0);
526         WM_keymap_add_item(keymap, "IMAGE_OT_view_pan", MIDDLEMOUSE, KM_PRESS, KM_SHIFT, 0);
527         WM_keymap_add_item(keymap, "IMAGE_OT_view_pan", MOUSEPAN, 0, 0, 0);
528
529         WM_keymap_add_item(keymap, "IMAGE_OT_view_all", NDOF_BUTTON_FIT, KM_PRESS, 0, 0); // or view selected?
530         WM_keymap_add_item(keymap, "IMAGE_OT_view_ndof", NDOF_MOTION, 0, 0, 0);
531
532         WM_keymap_add_item(keymap, "IMAGE_OT_view_zoom_in", WHEELINMOUSE, KM_PRESS, 0, 0);
533         WM_keymap_add_item(keymap, "IMAGE_OT_view_zoom_out", WHEELOUTMOUSE, KM_PRESS, 0, 0);
534         WM_keymap_add_item(keymap, "IMAGE_OT_view_zoom_in", PADPLUSKEY, KM_PRESS, 0, 0);
535         WM_keymap_add_item(keymap, "IMAGE_OT_view_zoom_out", PADMINUS, KM_PRESS, 0, 0);
536         WM_keymap_add_item(keymap, "IMAGE_OT_view_zoom", MIDDLEMOUSE, KM_PRESS, KM_CTRL, 0);
537         WM_keymap_add_item(keymap, "IMAGE_OT_view_zoom", MOUSEZOOM, 0, 0, 0);
538
539         RNA_float_set(WM_keymap_add_item(keymap, "IMAGE_OT_view_zoom_ratio", PAD8, KM_PRESS, KM_SHIFT, 0)->ptr, "ratio", 8.0f);
540         RNA_float_set(WM_keymap_add_item(keymap, "IMAGE_OT_view_zoom_ratio", PAD4, KM_PRESS, KM_SHIFT, 0)->ptr, "ratio", 4.0f);
541         RNA_float_set(WM_keymap_add_item(keymap, "IMAGE_OT_view_zoom_ratio", PAD2, KM_PRESS, KM_SHIFT, 0)->ptr, "ratio", 2.0f);
542         RNA_float_set(WM_keymap_add_item(keymap, "IMAGE_OT_view_zoom_ratio", PAD1, KM_PRESS, 0, 0)->ptr, "ratio", 1.0f);
543         RNA_float_set(WM_keymap_add_item(keymap, "IMAGE_OT_view_zoom_ratio", PAD2, KM_PRESS, 0, 0)->ptr, "ratio", 0.5f);
544         RNA_float_set(WM_keymap_add_item(keymap, "IMAGE_OT_view_zoom_ratio", PAD4, KM_PRESS, 0, 0)->ptr, "ratio", 0.25f);
545         RNA_float_set(WM_keymap_add_item(keymap, "IMAGE_OT_view_zoom_ratio", PAD8, KM_PRESS, 0, 0)->ptr, "ratio", 0.125f);
546
547         WM_keymap_add_item(keymap, "IMAGE_OT_sample", ACTIONMOUSE, KM_PRESS, 0, 0);
548         RNA_enum_set(WM_keymap_add_item(keymap, "IMAGE_OT_curves_point_set", ACTIONMOUSE, KM_PRESS, KM_CTRL, 0)->ptr, "point", 0);
549         RNA_enum_set(WM_keymap_add_item(keymap, "IMAGE_OT_curves_point_set", ACTIONMOUSE, KM_PRESS, KM_SHIFT, 0)->ptr, "point", 1);
550
551         /* toggle editmode is handy to have while UV unwrapping */
552         kmi = WM_keymap_add_item(keymap, "OBJECT_OT_mode_set", TABKEY, KM_PRESS, 0, 0);
553         RNA_enum_set(kmi->ptr, "mode", OB_MODE_EDIT);
554         RNA_boolean_set(kmi->ptr, "toggle", TRUE);
555
556         /* fast switch to render slots */
557         for (i = 0; i < MAX2(IMA_MAX_RENDER_SLOT, 9); i++) {
558                 kmi = WM_keymap_add_item(keymap, "WM_OT_context_set_int", ONEKEY + i, KM_PRESS, 0, 0);
559                 RNA_string_set(kmi->ptr, "data_path", "space_data.image.render_slot");
560                 RNA_int_set(kmi->ptr, "value", i);
561         }
562 }
563
564 /* dropboxes */
565 static int image_drop_poll(bContext *UNUSED(C), wmDrag *drag, wmEvent *UNUSED(event))
566 {
567         if (drag->type == WM_DRAG_PATH)
568                 if (ELEM3(drag->icon, 0, ICON_FILE_IMAGE, ICON_FILE_BLANK)) /* rule might not work? */
569                         return 1;
570         return 0;
571 }
572
573 static void image_drop_copy(wmDrag *drag, wmDropBox *drop)
574 {
575         /* copy drag path to properties */
576         RNA_string_set(drop->ptr, "filepath", drag->path);
577 }
578
579 /* area+region dropbox definition */
580 static void image_dropboxes(void)
581 {
582         ListBase *lb = WM_dropboxmap_find("Image", SPACE_IMAGE, 0);
583         
584         WM_dropbox_add(lb, "IMAGE_OT_open", image_drop_poll, image_drop_copy);
585 }
586
587
588
589 static void image_refresh(const bContext *C, ScrArea *UNUSED(sa))
590 {
591         Scene *scene = CTX_data_scene(C);
592         SpaceImage *sima = CTX_wm_space_image(C);
593         Object *obedit = CTX_data_edit_object(C);
594         Image *ima;
595
596         ima = ED_space_image(sima);
597
598         BKE_image_user_check_frame_calc(&sima->iuser, scene->r.cfra, 0);
599         
600         /* check if we have to set the image from the editmesh */
601         if (ima && (ima->source == IMA_SRC_VIEWER || sima->pin)) ;
602         else if (obedit && obedit->type == OB_MESH) {
603                 Mesh *me = (Mesh *)obedit->data;
604                 struct BMEditMesh *em = me->edit_btmesh;
605                 int sloppy = 1; /* partially selected face is ok */
606
607                 if (BKE_scene_use_new_shading_nodes(scene)) {
608                         /* new shading system, get image from material */
609                         BMFace *efa = BM_active_face_get(em->bm, sloppy);
610
611                         if (efa) {
612                                 Image *node_ima;
613                                 ED_object_get_active_image(obedit, efa->mat_nr + 1, &node_ima, NULL, NULL);
614
615                                 if (node_ima)
616                                         sima->image = node_ima;
617                         }
618                 }
619                 else {
620                         /* old shading system, we set texface */
621                         MTexPoly *tf;
622                         
623                         if (em && EDBM_mtexpoly_check(em)) {
624                                 sima->image = NULL;
625                                 
626                                 tf = EDBM_mtexpoly_active_get(em, NULL, TRUE); /* partially selected face is ok */
627                                 
628                                 if (tf) {
629                                         /* don't need to check for pin here, see above */
630                                         sima->image = tf->tpage;
631                                         
632                                         if (sima->flag & SI_EDITTILE) ;
633                                         else sima->curtile = tf->tile;
634                                 }
635                         }
636                 }
637         }
638 }
639
640 static void image_listener(ScrArea *sa, wmNotifier *wmn)
641 {
642         SpaceImage *sima = (SpaceImage *)sa->spacedata.first;
643         
644         /* context changes */
645         switch (wmn->category) {
646                 case NC_SCENE:
647                         switch (wmn->data) {
648                                 case ND_FRAME:
649                                         image_scopes_tag_refresh(sa);
650                                         ED_area_tag_refresh(sa);
651                                         ED_area_tag_redraw(sa);                                 
652                                         break;
653                                 case ND_MODE:
654                                 case ND_RENDER_RESULT:
655                                 case ND_COMPO_RESULT:
656                                         if (ED_space_image_show_render(sima))
657                                                 image_scopes_tag_refresh(sa);
658                                         ED_area_tag_refresh(sa);
659                                         ED_area_tag_redraw(sa);                                 
660                                         break;
661                         }
662                         break;
663                 case NC_IMAGE:
664                         if (wmn->reference == sima->image || !wmn->reference) {
665                                 image_scopes_tag_refresh(sa);
666                                 ED_area_tag_refresh(sa);
667                                 ED_area_tag_redraw(sa);
668                         }
669                         break;
670                 case NC_SPACE:  
671                         if (wmn->data == ND_SPACE_IMAGE) {
672                                 image_scopes_tag_refresh(sa);
673                                 ED_area_tag_redraw(sa);
674                         }
675                         break;
676                 case NC_GEOM:
677                         switch (wmn->data) {
678                                 case ND_DATA:
679                                 case ND_SELECT:
680                                         image_scopes_tag_refresh(sa);
681                                         ED_area_tag_refresh(sa);
682                                         ED_area_tag_redraw(sa);
683                                         break;
684                         }
685                 case NC_OBJECT:
686                 {
687                         Object *ob = (Object *)wmn->reference;
688                         switch (wmn->data) {
689                                 case ND_TRANSFORM:
690                                 case ND_MODIFIER:
691                                         if (ob && (ob->mode & OB_MODE_EDIT) && sima->lock && (sima->flag & SI_DRAWSHADOW)) {
692                                                 ED_area_tag_refresh(sa);
693                                                 ED_area_tag_redraw(sa);
694                                         }
695                                         break;
696                         }
697                 }
698         }
699 }
700
701 const char *image_context_dir[] = {"edit_image", NULL};
702
703 static int image_context(const bContext *C, const char *member, bContextDataResult *result)
704 {
705         SpaceImage *sima = CTX_wm_space_image(C);
706
707         if (CTX_data_dir(member)) {
708                 CTX_data_dir_set(result, image_context_dir);
709         }
710         else if (CTX_data_equals(member, "edit_image")) {
711                 CTX_data_id_pointer_set(result, (ID *)ED_space_image(sima));
712                 return 1;
713         }
714
715         return 0;
716 }
717
718 /************************** main region ***************************/
719
720 /* sets up the fields of the View2D from zoom and offset */
721 static void image_main_area_set_view2d(SpaceImage *sima, ARegion *ar)
722 {
723         Image *ima = ED_space_image(sima);
724         float x1, y1, w, h;
725         int width, height, winx, winy;
726         
727 #if 0
728         if (image_preview_active(curarea, &width, &height)) ;
729         else
730 #endif
731         ED_space_image_size(sima, &width, &height);
732
733         w = width;
734         h = height;
735         
736         if (ima)
737                 h *= ima->aspy / ima->aspx;
738
739         winx = ar->winrct.xmax - ar->winrct.xmin + 1;
740         winy = ar->winrct.ymax - ar->winrct.ymin + 1;
741                 
742         ar->v2d.tot.xmin = 0;
743         ar->v2d.tot.ymin = 0;
744         ar->v2d.tot.xmax = w;
745         ar->v2d.tot.ymax = h;
746         
747         ar->v2d.mask.xmin = ar->v2d.mask.ymin = 0;
748         ar->v2d.mask.xmax = winx;
749         ar->v2d.mask.ymax = winy;
750
751         /* which part of the image space do we see? */
752         x1 = ar->winrct.xmin + (winx - sima->zoom * w) / 2.0f;
753         y1 = ar->winrct.ymin + (winy - sima->zoom * h) / 2.0f;
754
755         x1 -= sima->zoom * sima->xof;
756         y1 -= sima->zoom * sima->yof;
757         
758         /* relative display right */
759         ar->v2d.cur.xmin = ((ar->winrct.xmin - (float)x1) / sima->zoom);
760         ar->v2d.cur.xmax = ar->v2d.cur.xmin + ((float)winx / sima->zoom);
761         
762         /* relative display left */
763         ar->v2d.cur.ymin = ((ar->winrct.ymin - (float)y1) / sima->zoom);
764         ar->v2d.cur.ymax = ar->v2d.cur.ymin + ((float)winy / sima->zoom);
765         
766         /* normalize 0.0..1.0 */
767         ar->v2d.cur.xmin /= w;
768         ar->v2d.cur.xmax /= w;
769         ar->v2d.cur.ymin /= h;
770         ar->v2d.cur.ymax /= h;
771 }
772
773 /* add handlers, stuff you only do once or on area/region changes */
774 static void image_main_area_init(wmWindowManager *wm, ARegion *ar)
775 {
776         wmKeyMap *keymap;
777         
778         // image space manages own v2d
779         // UI_view2d_region_reinit(&ar->v2d, V2D_COMMONVIEW_STANDARD, ar->winx, ar->winy);
780
781         /* image paint polls for mode */
782         keymap = WM_keymap_find(wm->defaultconf, "Image Paint", 0, 0);
783         WM_event_add_keymap_handler_bb(&ar->handlers, keymap, &ar->v2d.mask, &ar->winrct);
784
785         keymap = WM_keymap_find(wm->defaultconf, "UV Editor", 0, 0);
786         WM_event_add_keymap_handler(&ar->handlers, keymap);
787         
788         keymap = WM_keymap_find(wm->defaultconf, "UV Sculpt", 0, 0);
789         WM_event_add_keymap_handler(&ar->handlers, keymap);
790
791         /* own keymaps */
792         keymap = WM_keymap_find(wm->defaultconf, "Image Generic", SPACE_IMAGE, 0);
793         WM_event_add_keymap_handler(&ar->handlers, keymap);
794         keymap = WM_keymap_find(wm->defaultconf, "Image", SPACE_IMAGE, 0);
795         WM_event_add_keymap_handler_bb(&ar->handlers, keymap, &ar->v2d.mask, &ar->winrct);
796
797 }
798
799 static void image_main_area_draw(const bContext *C, ARegion *ar)
800 {
801         /* draw entirely, view changes should be handled here */
802         SpaceImage *sima = CTX_wm_space_image(C);
803         Object *obact = CTX_data_active_object(C);
804         Object *obedit = CTX_data_edit_object(C);
805         Scene *scene = CTX_data_scene(C);
806         View2D *v2d = &ar->v2d;
807         //View2DScrollers *scrollers;
808         float col[3];
809         
810         /* XXX not supported yet, disabling for now */
811         scene->r.scemode &= ~R_COMP_CROP;
812         
813         /* clear and setup matrix */
814         UI_GetThemeColor3fv(TH_BACK, col);
815         glClearColor(col[0], col[1], col[2], 0.0);
816         glClear(GL_COLOR_BUFFER_BIT);
817
818         /* put scene context variable in iuser */
819         sima->iuser.scene = scene;
820
821         /* we set view2d from own zoom and offset each time */
822         image_main_area_set_view2d(sima, ar);
823         
824         /* we draw image in pixelspace */
825         draw_image_main(C, ar);
826
827         /* and uvs in 0.0-1.0 space */
828         UI_view2d_view_ortho(v2d);
829         draw_uvedit_main(sima, ar, scene, obedit, obact);
830
831         ED_region_draw_cb_draw(C, ar, REGION_DRAW_POST_VIEW);
832
833         /* Grease Pencil too (in addition to UV's) */
834         draw_image_grease_pencil((bContext *)C, 1); 
835
836         /* sample line */
837         draw_image_sample_line(sima);
838
839         UI_view2d_view_restore(C);
840
841         /* draw Grease Pencil - screen space only */
842         draw_image_grease_pencil((bContext *)C, 0);
843
844         /* scrollers? */
845 #if 0
846         scrollers = UI_view2d_scrollers_calc(C, v2d, V2D_UNIT_VALUES, V2D_GRID_CLAMP, V2D_ARG_DUMMY, V2D_ARG_DUMMY);
847         UI_view2d_scrollers_draw(C, v2d, scrollers);
848         UI_view2d_scrollers_free(scrollers);
849 #endif
850 }
851
852 static void image_main_area_listener(ARegion *ar, wmNotifier *wmn)
853 {
854         /* context changes */
855         switch (wmn->category) {
856                 case NC_SCREEN:
857                         if (wmn->data == ND_GPENCIL)
858                                 ED_region_tag_redraw(ar);
859                         break;
860         }
861 }
862
863 /* *********************** buttons region ************************ */
864
865 /* add handlers, stuff you only do once or on area/region changes */
866 static void image_buttons_area_init(wmWindowManager *wm, ARegion *ar)
867 {
868         wmKeyMap *keymap;
869
870         ED_region_panels_init(wm, ar);
871         
872         keymap = WM_keymap_find(wm->defaultconf, "Image Generic", SPACE_IMAGE, 0);
873         WM_event_add_keymap_handler(&ar->handlers, keymap);
874 }
875
876 static void image_buttons_area_draw(const bContext *C, ARegion *ar)
877 {
878         ED_region_panels(C, ar, 1, NULL, -1);
879 }
880
881 static void image_buttons_area_listener(ARegion *ar, wmNotifier *wmn)
882 {
883         /* context changes */
884         switch (wmn->category) {
885                 case NC_SCREEN:
886                         if (wmn->data == ND_GPENCIL)
887                                 ED_region_tag_redraw(ar);
888                         break;
889                 case NC_BRUSH:
890                         if (wmn->action == NA_EDITED)
891                                 ED_region_tag_redraw(ar);
892                         break;
893                 case NC_TEXTURE:
894                 case NC_MATERIAL:
895                         /* sending by texture render job and needed to properly update displaying
896                          * brush texture icon */
897                         ED_region_tag_redraw(ar);
898                         break;
899         }
900 }
901
902 /* *********************** scopes region ************************ */
903
904 /* add handlers, stuff you only do once or on area/region changes */
905 static void image_scope_area_init(wmWindowManager *wm, ARegion *ar)
906 {
907         wmKeyMap *keymap;
908         
909         ED_region_panels_init(wm, ar);
910         
911         keymap = WM_keymap_find(wm->defaultconf, "Image Generic", SPACE_IMAGE, 0);
912         WM_event_add_keymap_handler(&ar->handlers, keymap);
913 }
914
915 static void image_scope_area_draw(const bContext *C, ARegion *ar)
916 {
917         SpaceImage *sima = CTX_wm_space_image(C);
918         Scene *scene = CTX_data_scene(C);
919         void *lock;
920         ImBuf *ibuf = ED_space_image_acquire_buffer(sima, &lock);
921         if (ibuf) {
922                 if (!sima->scopes.ok) {
923                         BKE_histogram_update_sample_line(&sima->sample_line_hist, ibuf, scene->r.color_mgt_flag & R_COLOR_MANAGEMENT);
924                 }
925                 scopes_update(&sima->scopes, ibuf, scene->r.color_mgt_flag & R_COLOR_MANAGEMENT);
926         }
927         ED_space_image_release_buffer(sima, lock);
928         
929         ED_region_panels(C, ar, 1, NULL, -1);
930 }
931
932 static void image_scope_area_listener(ARegion *ar, wmNotifier *wmn)
933 {
934         /* context changes */
935         switch (wmn->category) {
936                 case NC_SCENE:
937                         switch (wmn->data) {
938                                 case ND_MODE:
939                                 case ND_RENDER_RESULT:
940                                 case ND_COMPO_RESULT:
941                                         ED_region_tag_redraw(ar);
942                                         break;
943                         }
944                         break;
945                 case NC_IMAGE:
946                         ED_region_tag_redraw(ar);
947                         break;
948                 case NC_NODE:
949                         ED_region_tag_redraw(ar);
950                         break;
951                         
952         }
953 }
954
955 /************************* header region **************************/
956
957 /* add handlers, stuff you only do once or on area/region changes */
958 static void image_header_area_init(wmWindowManager *UNUSED(wm), ARegion *ar)
959 {
960         ED_region_header_init(ar);
961 }
962
963 static void image_header_area_draw(const bContext *C, ARegion *ar)
964 {
965         ED_region_header(C, ar);
966 }
967
968 static void image_header_area_listener(ARegion *ar, wmNotifier *wmn)
969 {
970         /* context changes */
971         switch (wmn->category) {
972                 case NC_SCENE:
973                         switch (wmn->data) {
974                                 case ND_MODE:
975                                 case ND_TOOLSETTINGS:
976                                         ED_region_tag_redraw(ar);
977                                         break;
978                         }
979                         break;
980                 case NC_GEOM:
981                         switch (wmn->data) {
982                                 case ND_DATA:
983                                 case ND_SELECT:
984                                         ED_region_tag_redraw(ar);
985                                         break;
986                         }
987                         
988         }
989 }
990
991 /**************************** spacetype *****************************/
992
993 /* only called once, from space/spacetypes.c */
994 void ED_spacetype_image(void)
995 {
996         SpaceType *st = MEM_callocN(sizeof(SpaceType), "spacetype image");
997         ARegionType *art;
998         
999         st->spaceid = SPACE_IMAGE;
1000         strncpy(st->name, "Image", BKE_ST_MAXNAME);
1001         
1002         st->new = image_new;
1003         st->free = image_free;
1004         st->init = image_init;
1005         st->duplicate = image_duplicate;
1006         st->operatortypes = image_operatortypes;
1007         st->keymap = image_keymap;
1008         st->dropboxes = image_dropboxes;
1009         st->refresh = image_refresh;
1010         st->listener = image_listener;
1011         st->context = image_context;
1012         
1013         /* regions: main window */
1014         art = MEM_callocN(sizeof(ARegionType), "spacetype image region");
1015         art->regionid = RGN_TYPE_WINDOW;
1016         art->keymapflag = ED_KEYMAP_FRAMES | ED_KEYMAP_GPENCIL;
1017         art->init = image_main_area_init;
1018         art->draw = image_main_area_draw;
1019         art->listener = image_main_area_listener;
1020
1021         BLI_addhead(&st->regiontypes, art);
1022         
1023         /* regions: listview/buttons */
1024         art = MEM_callocN(sizeof(ARegionType), "spacetype image region");
1025         art->regionid = RGN_TYPE_UI;
1026         art->prefsizex = 220; // XXX
1027         art->keymapflag = ED_KEYMAP_UI | ED_KEYMAP_FRAMES;
1028         art->listener = image_buttons_area_listener;
1029         art->init = image_buttons_area_init;
1030         art->draw = image_buttons_area_draw;
1031         BLI_addhead(&st->regiontypes, art);
1032
1033         image_buttons_register(art);
1034         ED_uvedit_buttons_register(art);
1035         
1036         /* regions: statistics/scope buttons */
1037         art = MEM_callocN(sizeof(ARegionType), "spacetype image region");
1038         art->regionid = RGN_TYPE_PREVIEW;
1039         art->prefsizex = 220; // XXX
1040         art->keymapflag = ED_KEYMAP_UI | ED_KEYMAP_FRAMES;
1041         art->listener = image_scope_area_listener;
1042         art->init = image_scope_area_init;
1043         art->draw = image_scope_area_draw;
1044         BLI_addhead(&st->regiontypes, art);
1045
1046         /* regions: header */
1047         art = MEM_callocN(sizeof(ARegionType), "spacetype image region");
1048         art->regionid = RGN_TYPE_HEADER;
1049         art->prefsizey = HEADERY;
1050         art->keymapflag = ED_KEYMAP_UI | ED_KEYMAP_VIEW2D | ED_KEYMAP_FRAMES | ED_KEYMAP_HEADER;
1051         art->listener = image_header_area_listener;
1052         art->init = image_header_area_init;
1053         art->draw = image_header_area_draw;
1054         
1055         BLI_addhead(&st->regiontypes, art);
1056         
1057         BKE_spacetype_register(st);
1058 }
1059