Color Management: add View as Render to image datablocks
[blender.git] / source / blender / editors / space_sequencer / sequencer_view.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) 2012 Blender Foundation.
19  * All rights reserved.
20  *
21  * Contributor(s): Blender Foundation,
22  *                 Sergey Sharybin
23  *
24  * ***** END GPL LICENSE BLOCK *****
25  */
26
27
28 /** \file blender/editors/space_sequencer/sequencer_modifier.c
29  *  \ingroup spseq
30  */
31
32 #include "MEM_guardedalloc.h"
33
34 #include "BLI_utildefines.h"
35
36 #include "DNA_scene_types.h"
37
38 #include "BKE_context.h"
39 #include "BKE_main.h"
40 #include "BKE_sequencer.h"
41 #include "BKE_screen.h"
42
43 #include "WM_api.h"
44 #include "WM_types.h"
45
46 #include "ED_image.h"
47 #include "ED_screen.h"
48 #include "ED_space_api.h"
49
50 #include "IMB_imbuf.h"
51 #include "IMB_imbuf_types.h"
52 #include "IMB_colormanagement.h"
53
54 #include "UI_view2d.h"
55
56 /* own include */
57 #include "sequencer_intern.h"
58
59 /******************** sample backdrop operator ********************/
60
61 typedef struct ImageSampleInfo {
62         ARegionType *art;
63         void *draw_handle;
64         int x, y;
65         int channels;
66
67         unsigned char col[4];
68         float colf[4];
69
70         unsigned char *colp;
71         float *colfp;
72
73         int draw;
74 int color_manage;
75 } ImageSampleInfo;
76
77 static void sample_draw(const bContext *C, ARegion *ar, void *arg_info)
78 {
79         Scene *scene = CTX_data_scene(C);
80         ImageSampleInfo *info = arg_info;
81
82         if (info->draw) {
83                 ED_image_draw_info(scene, ar, info->color_manage, FALSE, info->channels,
84                                    info->x, info->y, info->colp, info->colfp, NULL, NULL);
85         }
86 }
87
88 static void sample_apply(bContext *C, wmOperator *op, wmEvent *event)
89 {
90         Main *bmain = CTX_data_main(C);
91         Scene *scene = CTX_data_scene(C);
92         SpaceSeq *sseq = (SpaceSeq *) CTX_wm_space_data(C);
93         ARegion *ar = CTX_wm_region(C);
94         ImBuf *ibuf = sequencer_ibuf_get(bmain, scene, sseq, CFRA, 0);
95         ImageSampleInfo *info = op->customdata;
96         float fx, fy;
97         
98         if (ibuf == NULL) {
99                 IMB_freeImBuf(ibuf);
100                 info->draw = 0;
101                 return;
102         }
103
104         UI_view2d_region_to_view(&ar->v2d, event->mval[0], event->mval[1], &fx, &fy);
105
106         fx += (float) ibuf->x / 2.0f;
107         fy += (float) ibuf->y / 2.0f;
108
109         if (fx >= 0.0f && fy >= 0.0f && fx < ibuf->x && fy < ibuf->y) {
110                 float *fp;
111                 unsigned char *cp;
112                 int x = (int) fx, y = (int) fy;
113
114                 info->x = x;
115                 info->y = y;
116                 info->draw = 1;
117                 info->channels = ibuf->channels;
118
119                 info->colp = NULL;
120                 info->colfp = NULL;
121                 
122                 if (ibuf->rect) {
123                         cp = (unsigned char *)(ibuf->rect + y * ibuf->x + x);
124
125                         info->col[0] = cp[0];
126                         info->col[1] = cp[1];
127                         info->col[2] = cp[2];
128                         info->col[3] = cp[3];
129                         info->colp = info->col;
130
131                         info->colf[0] = (float)cp[0] / 255.0f;
132                         info->colf[1] = (float)cp[1] / 255.0f;
133                         info->colf[2] = (float)cp[2] / 255.0f;
134                         info->colf[3] = (float)cp[3] / 255.0f;
135                         info->colfp = info->colf;
136
137                         info->color_manage = FALSE;
138                 }
139                 if (ibuf->rect_float) {
140                         fp = (ibuf->rect_float + (ibuf->channels) * (y * ibuf->x + x));
141
142                         info->colf[0] = fp[0];
143                         info->colf[1] = fp[1];
144                         info->colf[2] = fp[2];
145                         info->colf[3] = fp[3];
146                         info->colfp = info->colf;
147
148                         /* sequencer's image buffers are in non-linear space, need to make them linear */
149                         BKE_sequencer_pixel_from_sequencer_space_v4(scene, info->colf);
150
151                         info->color_manage = TRUE;
152                 }
153         }
154         else {
155                 info->draw = 0;
156         }
157
158         IMB_freeImBuf(ibuf);
159         ED_area_tag_redraw(CTX_wm_area(C));
160 }
161
162 static void sample_exit(bContext *C, wmOperator *op)
163 {
164         ImageSampleInfo *info = op->customdata;
165
166         ED_region_draw_cb_exit(info->art, info->draw_handle);
167         ED_area_tag_redraw(CTX_wm_area(C));
168         MEM_freeN(info);
169 }
170
171 static int sample_invoke(bContext *C, wmOperator *op, wmEvent *event)
172 {
173         ARegion *ar = CTX_wm_region(C);
174         SpaceSeq *sseq = CTX_wm_space_seq(C);
175         ImageSampleInfo *info;
176
177         if (sseq->mainb != SEQ_DRAW_IMG_IMBUF)
178                 return OPERATOR_CANCELLED;
179
180         info = MEM_callocN(sizeof(ImageSampleInfo), "ImageSampleInfo");
181         info->art = ar->type;
182         info->draw_handle = ED_region_draw_cb_activate(ar->type, sample_draw, info, REGION_DRAW_POST_PIXEL);
183         op->customdata = info;
184
185         sample_apply(C, op, event);
186
187         WM_event_add_modal_handler(C, op);
188
189         return OPERATOR_RUNNING_MODAL;
190 }
191
192 static int sample_modal(bContext *C, wmOperator *op, wmEvent *event)
193 {
194         switch (event->type) {
195                 case LEFTMOUSE:
196                 case RIGHTMOUSE: /* XXX hardcoded */
197                         sample_exit(C, op);
198                         return OPERATOR_CANCELLED;
199                 case MOUSEMOVE:
200                         sample_apply(C, op, event);
201                         break;
202         }
203
204         return OPERATOR_RUNNING_MODAL;
205 }
206
207 static int sample_cancel(bContext *C, wmOperator *op)
208 {
209         sample_exit(C, op);
210
211         return OPERATOR_CANCELLED;
212 }
213
214 int sample_poll(bContext *C)
215 {
216         return BKE_sequencer_editing_get(CTX_data_scene(C), FALSE) != NULL;
217 }
218
219 void SEQUENCER_OT_sample(wmOperatorType *ot)
220 {
221         /* identifiers */
222         ot->name = "Sample Color";
223         ot->idname = "SEQUENCER_OT_sample";
224         ot->description = "Use mouse to sample color in current frame";
225
226         /* api callbacks */
227         ot->invoke = sample_invoke;
228         ot->modal = sample_modal;
229         ot->cancel = sample_cancel;
230         ot->poll = sample_poll;
231
232         /* flags */
233         ot->flag = OPTYPE_BLOCKING;
234 }