Merged changes in the trunk up to revision 50257.
[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
53 #include "UI_view2d.h"
54
55 /* own include */
56 #include "sequencer_intern.h"
57
58 /******************** sample backdrop operator ********************/
59
60 typedef struct ImageSampleInfo {
61         ARegionType *art;
62         void *draw_handle;
63         int x, y;
64         int channels;
65
66         unsigned char col[4];
67         float colf[4];
68
69         unsigned char *colp;
70         float *colfp;
71
72         int draw;
73 } ImageSampleInfo;
74
75 static void sample_draw(const bContext *C, ARegion *ar, void *arg_info)
76 {
77         Scene *scene = CTX_data_scene(C);
78         ImageSampleInfo *info = arg_info;
79
80         if (info->draw) {
81                 ED_image_draw_info(ar, (scene->r.color_mgt_flag & R_COLOR_MANAGEMENT), info->channels,
82                                    info->x, info->y, info->col, info->colf, NULL, NULL);
83         }
84 }
85
86 static void sample_apply(bContext *C, wmOperator *op, wmEvent *event)
87 {
88         Main *bmain = CTX_data_main(C);
89         Scene *scene = CTX_data_scene(C);
90         SpaceSeq *sseq = (SpaceSeq *) CTX_wm_space_data(C);
91         ARegion *ar = CTX_wm_region(C);
92         ImBuf *ibuf = sequencer_ibuf_get(bmain, scene, sseq, CFRA, 0);
93         ImageSampleInfo *info = op->customdata;
94         float fx, fy;
95         
96         if (ibuf == NULL) {
97                 IMB_freeImBuf(ibuf);
98                 info->draw = 0;
99                 return;
100         }
101
102         UI_view2d_region_to_view(&ar->v2d, event->mval[0], event->mval[1], &fx, &fy);
103
104         fx += (float) ibuf->x / 2.0f;
105         fy += (float) ibuf->y / 2.0f;
106
107         if (fx >= 0.0f && fy >= 0.0f && fx < ibuf->x && fy < ibuf->y) {
108                 float *fp;
109                 unsigned char *cp;
110                 int x = (int) fx, y = (int) fy;
111
112                 info->x = x;
113                 info->y = y;
114                 info->draw = 1;
115                 info->channels = ibuf->channels;
116
117                 info->colp = NULL;
118                 info->colfp = NULL;
119                 
120                 if (ibuf->rect) {
121                         cp = (unsigned char *)(ibuf->rect + y * ibuf->x + x);
122
123                         info->col[0] = cp[0];
124                         info->col[1] = cp[1];
125                         info->col[2] = cp[2];
126                         info->col[3] = cp[3];
127                         info->colp = info->col;
128
129                         info->colf[0] = (float)cp[0] / 255.0f;
130                         info->colf[1] = (float)cp[1] / 255.0f;
131                         info->colf[2] = (float)cp[2] / 255.0f;
132                         info->colf[3] = (float)cp[3] / 255.0f;
133                         info->colfp = info->colf;
134                 }
135                 if (ibuf->rect_float) {
136                         fp = (ibuf->rect_float + (ibuf->channels) * (y * ibuf->x + x));
137
138                         info->colf[0] = fp[0];
139                         info->colf[1] = fp[1];
140                         info->colf[2] = fp[2];
141                         info->colf[3] = fp[3];
142                         info->colfp = info->colf;
143                 }
144         }
145         else {
146                 info->draw = 0;
147         }
148
149         IMB_freeImBuf(ibuf);
150         ED_area_tag_redraw(CTX_wm_area(C));
151 }
152
153 static void sample_exit(bContext *C, wmOperator *op)
154 {
155         ImageSampleInfo *info = op->customdata;
156
157         ED_region_draw_cb_exit(info->art, info->draw_handle);
158         ED_area_tag_redraw(CTX_wm_area(C));
159         MEM_freeN(info);
160 }
161
162 static int sample_invoke(bContext *C, wmOperator *op, wmEvent *event)
163 {
164         ARegion *ar = CTX_wm_region(C);
165         ImageSampleInfo *info;
166
167         info = MEM_callocN(sizeof(ImageSampleInfo), "ImageSampleInfo");
168         info->art = ar->type;
169         info->draw_handle = ED_region_draw_cb_activate(ar->type, sample_draw, info, REGION_DRAW_POST_PIXEL);
170         op->customdata = info;
171
172         sample_apply(C, op, event);
173
174         WM_event_add_modal_handler(C, op);
175
176         return OPERATOR_RUNNING_MODAL;
177 }
178
179 static int sample_modal(bContext *C, wmOperator *op, wmEvent *event)
180 {
181         switch (event->type) {
182                 case LEFTMOUSE:
183                 case RIGHTMOUSE: /* XXX hardcoded */
184                         sample_exit(C, op);
185                         return OPERATOR_CANCELLED;
186                 case MOUSEMOVE:
187                         sample_apply(C, op, event);
188                         break;
189         }
190
191         return OPERATOR_RUNNING_MODAL;
192 }
193
194 static int sample_cancel(bContext *C, wmOperator *op)
195 {
196         sample_exit(C, op);
197
198         return OPERATOR_CANCELLED;
199 }
200
201 int sample_poll(bContext *C)
202 {
203         return BKE_sequencer_editing_get(CTX_data_scene(C), FALSE) != NULL;
204 }
205
206 void SEQUENCER_OT_sample(wmOperatorType *ot)
207 {
208         /* identifiers */
209         ot->name = "Sample Color";
210         ot->idname = "SEQUENCER_OT_sample";
211         ot->description = "Use mouse to sample color in current frame";
212
213         /* api callbacks */
214         ot->invoke = sample_invoke;
215         ot->modal = sample_modal;
216         ot->cancel = sample_cancel;
217         ot->poll = sample_poll;
218
219         /* flags */
220         ot->flag = OPTYPE_BLOCKING;
221 }