Merge branch 'master' into blender2.8
[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         const 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         struct Depsgraph *depsgraph = CTX_data_depsgraph(C);
95         Scene *scene = CTX_data_scene(C);
96         SpaceSeq *sseq = (SpaceSeq *) CTX_wm_space_data(C);
97         ARegion *ar = CTX_wm_region(C);
98         ImBuf *ibuf = sequencer_ibuf_get(bmain, depsgraph, scene, sseq, CFRA, 0, NULL);
99         ImageSampleInfo *info = op->customdata;
100         float fx, fy;
101
102         if (ibuf == NULL) {
103                 IMB_freeImBuf(ibuf);
104                 info->draw = 0;
105                 return;
106         }
107
108         UI_view2d_region_to_view(&ar->v2d, event->mval[0], event->mval[1], &fx, &fy);
109
110         fx += (float) ibuf->x / 2.0f;
111         fy += (float) ibuf->y / 2.0f;
112
113         if (fx >= 0.0f && fy >= 0.0f && fx < ibuf->x && fy < ibuf->y) {
114                 const float *fp;
115                 unsigned char *cp;
116                 int x = (int) fx, y = (int) fy;
117
118                 info->x = x;
119                 info->y = y;
120                 info->draw = 1;
121                 info->channels = ibuf->channels;
122
123                 info->colp = NULL;
124                 info->colfp = NULL;
125
126                 if (ibuf->rect) {
127                         cp = (unsigned char *)(ibuf->rect + y * ibuf->x + x);
128
129                         info->col[0] = cp[0];
130                         info->col[1] = cp[1];
131                         info->col[2] = cp[2];
132                         info->col[3] = cp[3];
133                         info->colp = info->col;
134
135                         info->colf[0] = (float)cp[0] / 255.0f;
136                         info->colf[1] = (float)cp[1] / 255.0f;
137                         info->colf[2] = (float)cp[2] / 255.0f;
138                         info->colf[3] = (float)cp[3] / 255.0f;
139                         info->colfp = info->colf;
140
141                         copy_v4_v4(info->linearcol, info->colf);
142                         IMB_colormanagement_colorspace_to_scene_linear_v4(info->linearcol, false, ibuf->rect_colorspace);
143
144                         info->color_manage = true;
145                 }
146                 if (ibuf->rect_float) {
147                         fp = (ibuf->rect_float + (ibuf->channels) * (y * ibuf->x + x));
148
149                         info->colf[0] = fp[0];
150                         info->colf[1] = fp[1];
151                         info->colf[2] = fp[2];
152                         info->colf[3] = fp[3];
153                         info->colfp = info->colf;
154
155                         /* sequencer's image buffers are in non-linear space, need to make them linear */
156                         copy_v4_v4(info->linearcol, info->colf);
157                         BKE_sequencer_pixel_from_sequencer_space_v4(scene, info->linearcol);
158
159                         info->color_manage = true;
160                 }
161         }
162         else {
163                 info->draw = 0;
164         }
165
166         IMB_freeImBuf(ibuf);
167         ED_area_tag_redraw(CTX_wm_area(C));
168 }
169
170 static void sample_exit(bContext *C, wmOperator *op)
171 {
172         ImageSampleInfo *info = op->customdata;
173
174         ED_region_draw_cb_exit(info->art, info->draw_handle);
175         ED_area_tag_redraw(CTX_wm_area(C));
176         MEM_freeN(info);
177 }
178
179 static int sample_invoke(bContext *C, wmOperator *op, const wmEvent *event)
180 {
181         ARegion *ar = CTX_wm_region(C);
182         SpaceSeq *sseq = CTX_wm_space_seq(C);
183         ImageSampleInfo *info;
184
185         if (sseq->mainb != SEQ_DRAW_IMG_IMBUF)
186                 return OPERATOR_CANCELLED;
187
188         info = MEM_callocN(sizeof(ImageSampleInfo), "ImageSampleInfo");
189         info->art = ar->type;
190         info->draw_handle = ED_region_draw_cb_activate(ar->type, sample_draw, info, REGION_DRAW_POST_PIXEL);
191         op->customdata = info;
192
193         sample_apply(C, op, event);
194
195         WM_event_add_modal_handler(C, op);
196
197         return OPERATOR_RUNNING_MODAL;
198 }
199
200 static int sample_modal(bContext *C, wmOperator *op, const wmEvent *event)
201 {
202         switch (event->type) {
203                 case LEFTMOUSE:
204                 case RIGHTMOUSE: /* XXX hardcoded */
205                         if (event->val == KM_RELEASE) {
206                                 sample_exit(C, op);
207                                 return OPERATOR_CANCELLED;
208                         }
209                         break;
210                 case MOUSEMOVE:
211                         sample_apply(C, op, event);
212                         break;
213         }
214
215         return OPERATOR_RUNNING_MODAL;
216 }
217
218 static void sample_cancel(bContext *C, wmOperator *op)
219 {
220         sample_exit(C, op);
221 }
222
223 static int sample_poll(bContext *C)
224 {
225         SpaceSeq *sseq = CTX_wm_space_seq(C);
226         return sseq && BKE_sequencer_editing_get(CTX_data_scene(C), false) != NULL;
227 }
228
229 void SEQUENCER_OT_sample(wmOperatorType *ot)
230 {
231         /* identifiers */
232         ot->name = "Sample Color";
233         ot->idname = "SEQUENCER_OT_sample";
234         ot->description = "Use mouse to sample color in current frame";
235
236         /* api callbacks */
237         ot->invoke = sample_invoke;
238         ot->modal = sample_modal;
239         ot->cancel = sample_cancel;
240         ot->poll = sample_poll;
241
242         /* flags */
243         ot->flag = OPTYPE_BLOCKING;
244 }