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