2012-08-23 16:14:52 +00:00
|
|
|
/*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version 2
|
|
|
|
* of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software Foundation,
|
|
|
|
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
*
|
|
|
|
* The Original Code is Copyright (C) 2012 Blender Foundation.
|
|
|
|
* All rights reserved.
|
|
|
|
*/
|
|
|
|
|
2019-02-18 08:08:12 +11:00
|
|
|
/** \file
|
|
|
|
* \ingroup spseq
|
2012-08-23 16:14:52 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "MEM_guardedalloc.h"
|
|
|
|
|
2013-03-18 11:34:05 +00:00
|
|
|
#include "BLI_math.h"
|
2012-08-23 16:14:52 +00:00
|
|
|
#include "BLI_utildefines.h"
|
|
|
|
|
|
|
|
#include "DNA_scene_types.h"
|
|
|
|
|
|
|
|
#include "BKE_context.h"
|
|
|
|
#include "BKE_main.h"
|
|
|
|
#include "BKE_screen.h"
|
2020-03-19 09:33:03 +01:00
|
|
|
#include "BKE_sequencer.h"
|
2012-08-23 16:14:52 +00:00
|
|
|
|
|
|
|
#include "WM_api.h"
|
|
|
|
#include "WM_types.h"
|
|
|
|
|
|
|
|
#include "ED_image.h"
|
|
|
|
#include "ED_screen.h"
|
|
|
|
#include "ED_space_api.h"
|
|
|
|
|
2020-03-19 09:33:03 +01:00
|
|
|
#include "IMB_colormanagement.h"
|
2012-08-23 16:14:52 +00:00
|
|
|
#include "IMB_imbuf.h"
|
|
|
|
#include "IMB_imbuf_types.h"
|
|
|
|
|
|
|
|
#include "UI_view2d.h"
|
|
|
|
|
|
|
|
/* own include */
|
|
|
|
#include "sequencer_intern.h"
|
|
|
|
|
|
|
|
/******************** sample backdrop operator ********************/
|
|
|
|
|
|
|
|
typedef struct ImageSampleInfo {
|
2019-04-17 06:17:24 +02:00
|
|
|
ARegionType *art;
|
|
|
|
void *draw_handle;
|
|
|
|
int x, y;
|
|
|
|
int channels;
|
2012-08-23 16:14:52 +00:00
|
|
|
|
2020-04-03 16:21:24 +11:00
|
|
|
uchar col[4];
|
2019-04-17 06:17:24 +02:00
|
|
|
float colf[4];
|
|
|
|
float linearcol[4];
|
2012-08-23 16:14:52 +00:00
|
|
|
|
2020-04-03 16:21:24 +11:00
|
|
|
uchar *colp;
|
2019-04-17 06:17:24 +02:00
|
|
|
const float *colfp;
|
2012-08-23 16:14:52 +00:00
|
|
|
|
2019-04-17 06:17:24 +02:00
|
|
|
int draw;
|
|
|
|
int color_manage;
|
2012-08-23 16:14:52 +00:00
|
|
|
} ImageSampleInfo;
|
|
|
|
|
2020-03-06 16:56:42 +01:00
|
|
|
static void sample_draw(const bContext *C, ARegion *region, void *arg_info)
|
2012-08-23 16:14:52 +00:00
|
|
|
{
|
2019-04-17 06:17:24 +02:00
|
|
|
Scene *scene = CTX_data_scene(C);
|
|
|
|
ImageSampleInfo *info = arg_info;
|
|
|
|
|
|
|
|
if (info->draw) {
|
|
|
|
ED_image_draw_info(scene,
|
2020-03-06 16:56:42 +01:00
|
|
|
region,
|
2019-04-17 06:17:24 +02:00
|
|
|
info->color_manage,
|
|
|
|
false,
|
|
|
|
info->channels,
|
|
|
|
info->x,
|
|
|
|
info->y,
|
|
|
|
info->colp,
|
|
|
|
info->colfp,
|
|
|
|
info->linearcol,
|
|
|
|
NULL,
|
|
|
|
NULL);
|
|
|
|
}
|
2012-08-23 16:14:52 +00:00
|
|
|
}
|
|
|
|
|
2013-03-13 09:03:46 +00:00
|
|
|
static void sample_apply(bContext *C, wmOperator *op, const wmEvent *event)
|
2012-08-23 16:14:52 +00:00
|
|
|
{
|
2019-04-17 06:17:24 +02:00
|
|
|
Main *bmain = CTX_data_main(C);
|
2019-07-25 16:36:22 +02:00
|
|
|
struct Depsgraph *depsgraph = CTX_data_ensure_evaluated_depsgraph(C);
|
2019-04-17 06:17:24 +02:00
|
|
|
Scene *scene = CTX_data_scene(C);
|
|
|
|
SpaceSeq *sseq = (SpaceSeq *)CTX_wm_space_data(C);
|
2020-03-06 16:56:42 +01:00
|
|
|
ARegion *region = CTX_wm_region(C);
|
2019-04-17 06:17:24 +02:00
|
|
|
ImBuf *ibuf = sequencer_ibuf_get(bmain, depsgraph, scene, sseq, CFRA, 0, NULL);
|
|
|
|
ImageSampleInfo *info = op->customdata;
|
|
|
|
float fx, fy;
|
|
|
|
|
|
|
|
if (ibuf == NULL) {
|
|
|
|
IMB_freeImBuf(ibuf);
|
|
|
|
info->draw = 0;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-03-06 16:56:42 +01:00
|
|
|
UI_view2d_region_to_view(®ion->v2d, event->mval[0], event->mval[1], &fx, &fy);
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2019-05-19 11:18:23 -07:00
|
|
|
fx /= scene->r.xasp / scene->r.yasp;
|
|
|
|
|
2019-04-17 06:17:24 +02:00
|
|
|
fx += (float)scene->r.xsch / 2.0f;
|
|
|
|
fy += (float)scene->r.ysch / 2.0f;
|
|
|
|
fx *= (float)ibuf->x / (float)scene->r.xsch;
|
|
|
|
fy *= (float)ibuf->y / (float)scene->r.ysch;
|
|
|
|
|
|
|
|
if (fx >= 0.0f && fy >= 0.0f && fx < ibuf->x && fy < ibuf->y) {
|
|
|
|
const float *fp;
|
2020-04-03 16:21:24 +11:00
|
|
|
uchar *cp;
|
2019-04-17 06:17:24 +02:00
|
|
|
int x = (int)fx, y = (int)fy;
|
|
|
|
|
|
|
|
info->x = x;
|
|
|
|
info->y = y;
|
|
|
|
info->draw = 1;
|
|
|
|
info->channels = ibuf->channels;
|
|
|
|
|
|
|
|
info->colp = NULL;
|
|
|
|
info->colfp = NULL;
|
|
|
|
|
|
|
|
if (ibuf->rect) {
|
2020-04-03 16:21:24 +11:00
|
|
|
cp = (uchar *)(ibuf->rect + y * ibuf->x + x);
|
2019-04-17 06:17:24 +02:00
|
|
|
|
|
|
|
info->col[0] = cp[0];
|
|
|
|
info->col[1] = cp[1];
|
|
|
|
info->col[2] = cp[2];
|
|
|
|
info->col[3] = cp[3];
|
|
|
|
info->colp = info->col;
|
|
|
|
|
|
|
|
info->colf[0] = (float)cp[0] / 255.0f;
|
|
|
|
info->colf[1] = (float)cp[1] / 255.0f;
|
|
|
|
info->colf[2] = (float)cp[2] / 255.0f;
|
|
|
|
info->colf[3] = (float)cp[3] / 255.0f;
|
|
|
|
info->colfp = info->colf;
|
|
|
|
|
|
|
|
copy_v4_v4(info->linearcol, info->colf);
|
|
|
|
IMB_colormanagement_colorspace_to_scene_linear_v4(
|
|
|
|
info->linearcol, false, ibuf->rect_colorspace);
|
|
|
|
|
|
|
|
info->color_manage = true;
|
|
|
|
}
|
|
|
|
if (ibuf->rect_float) {
|
|
|
|
fp = (ibuf->rect_float + (ibuf->channels) * (y * ibuf->x + x));
|
|
|
|
|
|
|
|
info->colf[0] = fp[0];
|
|
|
|
info->colf[1] = fp[1];
|
|
|
|
info->colf[2] = fp[2];
|
|
|
|
info->colf[3] = fp[3];
|
|
|
|
info->colfp = info->colf;
|
|
|
|
|
|
|
|
/* sequencer's image buffers are in non-linear space, need to make them linear */
|
|
|
|
copy_v4_v4(info->linearcol, info->colf);
|
|
|
|
BKE_sequencer_pixel_from_sequencer_space_v4(scene, info->linearcol);
|
|
|
|
|
|
|
|
info->color_manage = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
info->draw = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
IMB_freeImBuf(ibuf);
|
|
|
|
ED_area_tag_redraw(CTX_wm_area(C));
|
2012-08-23 16:14:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void sample_exit(bContext *C, wmOperator *op)
|
|
|
|
{
|
2019-04-17 06:17:24 +02:00
|
|
|
ImageSampleInfo *info = op->customdata;
|
2012-08-23 16:14:52 +00:00
|
|
|
|
2019-04-17 06:17:24 +02:00
|
|
|
ED_region_draw_cb_exit(info->art, info->draw_handle);
|
|
|
|
ED_area_tag_redraw(CTX_wm_area(C));
|
|
|
|
MEM_freeN(info);
|
2012-08-23 16:14:52 +00:00
|
|
|
}
|
|
|
|
|
2013-03-13 09:03:46 +00:00
|
|
|
static int sample_invoke(bContext *C, wmOperator *op, const wmEvent *event)
|
2012-08-23 16:14:52 +00:00
|
|
|
{
|
2020-03-06 16:56:42 +01:00
|
|
|
ARegion *region = CTX_wm_region(C);
|
2019-04-17 06:17:24 +02:00
|
|
|
SpaceSeq *sseq = CTX_wm_space_seq(C);
|
|
|
|
ImageSampleInfo *info;
|
2012-08-23 16:14:52 +00:00
|
|
|
|
2019-04-17 06:17:24 +02:00
|
|
|
if (sseq->mainb != SEQ_DRAW_IMG_IMBUF) {
|
|
|
|
return OPERATOR_CANCELLED;
|
|
|
|
}
|
2012-09-05 14:11:22 +00:00
|
|
|
|
2019-04-17 06:17:24 +02:00
|
|
|
info = MEM_callocN(sizeof(ImageSampleInfo), "ImageSampleInfo");
|
2020-03-06 16:56:42 +01:00
|
|
|
info->art = region->type;
|
2019-04-17 06:17:24 +02:00
|
|
|
info->draw_handle = ED_region_draw_cb_activate(
|
2020-03-06 16:56:42 +01:00
|
|
|
region->type, sample_draw, info, REGION_DRAW_POST_PIXEL);
|
2019-04-17 06:17:24 +02:00
|
|
|
op->customdata = info;
|
2012-08-23 16:14:52 +00:00
|
|
|
|
2019-04-17 06:17:24 +02:00
|
|
|
sample_apply(C, op, event);
|
2012-08-23 16:14:52 +00:00
|
|
|
|
2019-04-17 06:17:24 +02:00
|
|
|
WM_event_add_modal_handler(C, op);
|
2012-08-23 16:14:52 +00:00
|
|
|
|
2019-04-17 06:17:24 +02:00
|
|
|
return OPERATOR_RUNNING_MODAL;
|
2012-08-23 16:14:52 +00:00
|
|
|
}
|
|
|
|
|
2013-03-13 09:03:46 +00:00
|
|
|
static int sample_modal(bContext *C, wmOperator *op, const wmEvent *event)
|
2012-08-23 16:14:52 +00:00
|
|
|
{
|
2019-04-17 06:17:24 +02:00
|
|
|
switch (event->type) {
|
|
|
|
case LEFTMOUSE:
|
|
|
|
case RIGHTMOUSE: /* XXX hardcoded */
|
|
|
|
if (event->val == KM_RELEASE) {
|
|
|
|
sample_exit(C, op);
|
|
|
|
return OPERATOR_CANCELLED;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case MOUSEMOVE:
|
|
|
|
sample_apply(C, op, event);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return OPERATOR_RUNNING_MODAL;
|
2012-08-23 16:14:52 +00:00
|
|
|
}
|
|
|
|
|
2013-10-30 23:08:53 +00:00
|
|
|
static void sample_cancel(bContext *C, wmOperator *op)
|
2012-08-23 16:14:52 +00:00
|
|
|
{
|
2019-04-17 06:17:24 +02:00
|
|
|
sample_exit(C, op);
|
2012-08-23 16:14:52 +00:00
|
|
|
}
|
|
|
|
|
2018-07-02 11:47:00 +02:00
|
|
|
static bool sample_poll(bContext *C)
|
2012-08-23 16:14:52 +00:00
|
|
|
{
|
2019-04-17 06:17:24 +02:00
|
|
|
SpaceSeq *sseq = CTX_wm_space_seq(C);
|
|
|
|
return sseq && BKE_sequencer_editing_get(CTX_data_scene(C), false) != NULL;
|
2012-08-23 16:14:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void SEQUENCER_OT_sample(wmOperatorType *ot)
|
|
|
|
{
|
2019-04-17 06:17:24 +02:00
|
|
|
/* identifiers */
|
|
|
|
ot->name = "Sample Color";
|
|
|
|
ot->idname = "SEQUENCER_OT_sample";
|
|
|
|
ot->description = "Use mouse to sample color in current frame";
|
|
|
|
|
|
|
|
/* api callbacks */
|
|
|
|
ot->invoke = sample_invoke;
|
|
|
|
ot->modal = sample_modal;
|
|
|
|
ot->cancel = sample_cancel;
|
|
|
|
ot->poll = sample_poll;
|
|
|
|
|
|
|
|
/* flags */
|
|
|
|
ot->flag = OPTYPE_BLOCKING;
|
2012-08-23 16:14:52 +00:00
|
|
|
}
|