diff options
author | Jeroen Bakker <jeroen@blender.org> | 2021-12-03 15:47:42 +0300 |
---|---|---|
committer | Jeroen Bakker <jeroen@blender.org> | 2021-12-03 15:48:00 +0300 |
commit | ab927f5ca7a35393ea28de56e7a8d2a938c5de34 (patch) | |
tree | fe87d0a81c749c19668572e3cd072da1f3d44ac1 /source | |
parent | c4e041da23b9c45273fcd4874308c536b6a315d1 (diff) |
ImBuf: Made Wrapping and Cropping optional in IMB_transform.
`IMB_transform` is used in VSE. It had a required crop parameter
for cropping the source buffer. This is not always needed.
In the image engine we want to use the use the `IMB_transform`
with wrap repeat. Both options are mutual exclusive and due
to performance reasons the wrap repeat is only available when
performing a nearest interpolation.
Diffstat (limited to 'source')
-rw-r--r-- | source/blender/blenkernel/intern/tracking_stabilize.c | 2 | ||||
-rw-r--r-- | source/blender/imbuf/IMB_imbuf.h | 39 | ||||
-rw-r--r-- | source/blender/imbuf/intern/imageprocess.c | 147 | ||||
-rw-r--r-- | source/blender/sequencer/intern/render.c | 8 |
4 files changed, 147 insertions, 49 deletions
diff --git a/source/blender/blenkernel/intern/tracking_stabilize.c b/source/blender/blenkernel/intern/tracking_stabilize.c index d5585116f7e..a4dbebb08a9 100644 --- a/source/blender/blenkernel/intern/tracking_stabilize.c +++ b/source/blender/blenkernel/intern/tracking_stabilize.c @@ -1307,7 +1307,7 @@ void BKE_tracking_stabilization_data_get(MovieClip *clip, discard_stabilization_working_context(ctx); } -typedef void (*interpolation_func)(struct ImBuf *, struct ImBuf *, float, float, int, int); +typedef void (*interpolation_func)(const struct ImBuf *, struct ImBuf *, float, float, int, int); typedef struct TrackingStabilizeFrameInterpolationData { ImBuf *ibuf; diff --git a/source/blender/imbuf/IMB_imbuf.h b/source/blender/imbuf/IMB_imbuf.h index 7bfd1074ac6..9b4451889bc 100644 --- a/source/blender/imbuf/IMB_imbuf.h +++ b/source/blender/imbuf/IMB_imbuf.h @@ -613,22 +613,22 @@ void IMB_convert_rgba_to_abgr(struct ImBuf *ibuf); * \attention defined in imageprocess.c */ void bicubic_interpolation( - struct ImBuf *in, struct ImBuf *out, float u, float v, int xout, int yout); + const struct ImBuf *in, struct ImBuf *out, float u, float v, int xout, int yout); void nearest_interpolation( - struct ImBuf *in, struct ImBuf *out, float u, float v, int xout, int yout); + const struct ImBuf *in, struct ImBuf *out, float u, float v, int xout, int yout); void bilinear_interpolation( - struct ImBuf *in, struct ImBuf *out, float u, float v, int xout, int yout); + const struct ImBuf *in, struct ImBuf *out, float u, float v, int xout, int yout); void bicubic_interpolation_color( - struct ImBuf *in, unsigned char outI[4], float outF[4], float u, float v); + const struct ImBuf *in, unsigned char outI[4], float outF[4], float u, float v); void nearest_interpolation_color( - struct ImBuf *in, unsigned char outI[4], float outF[4], float u, float v); + const struct ImBuf *in, unsigned char outI[4], float outF[4], float u, float v); void nearest_interpolation_color_wrap( - struct ImBuf *in, unsigned char outI[4], float outF[4], float u, float v); + const struct ImBuf *in, unsigned char outI[4], float outF[4], float u, float v); void bilinear_interpolation_color( - struct ImBuf *in, unsigned char outI[4], float outF[4], float u, float v); + const struct ImBuf *in, unsigned char outI[4], float outF[4], float u, float v); void bilinear_interpolation_color_wrap( - struct ImBuf *in, unsigned char outI[4], float outF[4], float u, float v); + const struct ImBuf *in, unsigned char outI[4], float outF[4], float u, float v); void IMB_alpha_under_color_float(float *rect_float, int x, int y, float backcol[3]); void IMB_alpha_under_color_byte(unsigned char *rect, int x, int y, const float backcol[3]); @@ -756,11 +756,26 @@ void IMB_processor_apply_threaded_scanlines(int total_scanlines, ScanlineThreadFunc do_thread, void *custom_data); -void IMB_transform(struct ImBuf *src, +/** + * \brief Transform modes to use for IMB_transform function. + * + * These are not flags as the combination of cropping and repeat can lead to different expectation. + */ +typedef enum eIMBTransformMode { + /** \brief Do not crop or repeat. */ + IMB_TRANSFORM_MODE_REGULAR = 0, + /** \brief Crop the source buffer. */ + IMB_TRANSFORM_MODE_CROP_SRC = 1, + /** \brief Wrap repeat the source buffer. Only supported in with nearest filtering. */ + IMB_TRANSFORM_MODE_WRAP_REPEAT = 2, +} eIMBTransformMode; + +void IMB_transform(const struct ImBuf *src, struct ImBuf *dst, - float transform_matrix[4][4], - struct rctf *src_crop, - const eIMBInterpolationFilterMode filter); + const eIMBTransformMode mode, + const eIMBInterpolationFilterMode filter, + const float transform_matrix[4][4], + const struct rctf *src_crop); /* ffmpeg */ void IMB_ffmpeg_init(void); diff --git a/source/blender/imbuf/intern/imageprocess.c b/source/blender/imbuf/intern/imageprocess.c index 0ec1e4c19d8..1a59ec75ed0 100644 --- a/source/blender/imbuf/intern/imageprocess.c +++ b/source/blender/imbuf/intern/imageprocess.c @@ -76,7 +76,8 @@ void IMB_convert_rgba_to_abgr(struct ImBuf *ibuf) } } -static void pixel_from_buffer(struct ImBuf *ibuf, unsigned char **outI, float **outF, int x, int y) +static void pixel_from_buffer( + const struct ImBuf *ibuf, unsigned char **outI, float **outF, int x, int y) { size_t offset = ((size_t)ibuf->x) * y * 4 + 4 * x; @@ -95,7 +96,7 @@ static void pixel_from_buffer(struct ImBuf *ibuf, unsigned char **outI, float ** * \{ */ void bicubic_interpolation_color( - struct ImBuf *in, unsigned char outI[4], float outF[4], float u, float v) + const struct ImBuf *in, unsigned char outI[4], float outF[4], float u, float v) { if (outF) { BLI_bicubic_interpolation_fl(in->rect_float, outF, in->x, in->y, 4, u, v); @@ -105,7 +106,7 @@ void bicubic_interpolation_color( } } -void bicubic_interpolation(ImBuf *in, ImBuf *out, float u, float v, int xout, int yout) +void bicubic_interpolation(const ImBuf *in, ImBuf *out, float u, float v, int xout, int yout) { unsigned char *outI = NULL; float *outF = NULL; @@ -127,7 +128,7 @@ void bicubic_interpolation(ImBuf *in, ImBuf *out, float u, float v, int xout, in * \{ */ BLI_INLINE void bilinear_interpolation_color_fl( - struct ImBuf *in, unsigned char UNUSED(outI[4]), float outF[4], float u, float v) + const struct ImBuf *in, unsigned char UNUSED(outI[4]), float outF[4], float u, float v) { BLI_assert(outF); BLI_assert(in->rect_float); @@ -135,7 +136,7 @@ BLI_INLINE void bilinear_interpolation_color_fl( } BLI_INLINE void bilinear_interpolation_color_char( - struct ImBuf *in, unsigned char outI[4], float UNUSED(outF[4]), float u, float v) + const struct ImBuf *in, unsigned char outI[4], float UNUSED(outF[4]), float u, float v) { BLI_assert(outI); BLI_assert(in->rect); @@ -143,7 +144,7 @@ BLI_INLINE void bilinear_interpolation_color_char( } void bilinear_interpolation_color( - struct ImBuf *in, unsigned char outI[4], float outF[4], float u, float v) + const struct ImBuf *in, unsigned char outI[4], float outF[4], float u, float v) { if (outF) { BLI_bilinear_interpolation_fl(in->rect_float, outF, in->x, in->y, 4, u, v); @@ -161,7 +162,7 @@ void bilinear_interpolation_color( * This the same as bilinear_interpolation_color except it wraps * rather than using empty and emptyI. */ void bilinear_interpolation_color_wrap( - struct ImBuf *in, unsigned char outI[4], float outF[4], float u, float v) + const struct ImBuf *in, unsigned char outI[4], float outF[4], float u, float v) { float *row1, *row2, *row3, *row4, a, b; unsigned char *row1I, *row2I, *row3I, *row4I; @@ -232,7 +233,7 @@ void bilinear_interpolation_color_wrap( } } -void bilinear_interpolation(ImBuf *in, ImBuf *out, float u, float v, int xout, int yout) +void bilinear_interpolation(const ImBuf *in, ImBuf *out, float u, float v, int xout, int yout) { unsigned char *outI = NULL; float *outF = NULL; @@ -255,7 +256,7 @@ void bilinear_interpolation(ImBuf *in, ImBuf *out, float u, float v, int xout, i /* functions assumes out to be zero'ed, only does RGBA */ BLI_INLINE void nearest_interpolation_color_char( - struct ImBuf *in, unsigned char outI[4], float UNUSED(outF[4]), float u, float v) + const struct ImBuf *in, unsigned char outI[4], float UNUSED(outF[4]), float u, float v) { BLI_assert(outI); BLI_assert(in->rect); @@ -278,7 +279,7 @@ BLI_INLINE void nearest_interpolation_color_char( } BLI_INLINE void nearest_interpolation_color_fl( - struct ImBuf *in, unsigned char UNUSED(outI[4]), float outF[4], float u, float v) + const struct ImBuf *in, unsigned char UNUSED(outI[4]), float outF[4], float u, float v) { BLI_assert(outF); BLI_assert(in->rect_float); @@ -298,7 +299,7 @@ BLI_INLINE void nearest_interpolation_color_fl( } void nearest_interpolation_color( - struct ImBuf *in, unsigned char outI[4], float outF[4], float u, float v) + const struct ImBuf *in, unsigned char outI[4], float outF[4], float u, float v) { if (outF) { nearest_interpolation_color_fl(in, outI, outF, u, v); @@ -309,7 +310,7 @@ void nearest_interpolation_color( } void nearest_interpolation_color_wrap( - struct ImBuf *in, unsigned char outI[4], float outF[4], float u, float v) + const struct ImBuf *in, unsigned char outI[4], float outF[4], float u, float v) { const float *dataF; unsigned char *dataI; @@ -347,7 +348,7 @@ void nearest_interpolation_color_wrap( } } -void nearest_interpolation(ImBuf *in, ImBuf *out, float u, float v, int xout, int yout) +void nearest_interpolation(const ImBuf *in, ImBuf *out, float u, float v, int xout, int yout) { unsigned char *outI = NULL; float *outF = NULL; @@ -366,7 +367,7 @@ void nearest_interpolation(ImBuf *in, ImBuf *out, float u, float v, int xout, in /** \name Image transform * \{ */ typedef struct TransformUserData { - ImBuf *src; + const ImBuf *src; ImBuf *dst; float start_uv[2]; float add_x[2]; @@ -416,7 +417,7 @@ static void imb_transform_calc_add_y(const float transform_matrix[4][4], } typedef void (*InterpolationColorFunction)( - struct ImBuf *in, unsigned char outI[4], float outF[4], float u, float v); + const struct ImBuf *in, unsigned char outI[4], float outF[4], float u, float v); BLI_INLINE void imb_transform_scanlines(const TransformUserData *user_data, int scanline, InterpolationColorFunction interpolation) @@ -431,6 +432,57 @@ BLI_INLINE void imb_transform_scanlines(const TransformUserData *user_data, pixel_from_buffer(user_data->dst, &outI, &outF, 0, scanline); for (int xi = 0; xi < width; xi++) { + interpolation(user_data->src, outI, outF, uv[0], uv[1]); + add_v2_v2(uv, user_data->add_x); + if (outI) { + outI += 4; + } + if (outF) { + outF += 4; + } + } +} + +static void imb_transform_nearest_scanlines(void *custom_data, int scanline) +{ + const TransformUserData *user_data = custom_data; + InterpolationColorFunction interpolation = NULL; + if (user_data->dst->rect_float) { + interpolation = nearest_interpolation_color_fl; + } + else { + interpolation = nearest_interpolation_color_char; + } + imb_transform_scanlines(user_data, scanline, interpolation); +} + +static void imb_transform_bilinear_scanlines(void *custom_data, int scanline) +{ + const TransformUserData *user_data = custom_data; + InterpolationColorFunction interpolation = NULL; + if (user_data->dst->rect_float) { + interpolation = bilinear_interpolation_color_fl; + } + else if (user_data->dst->rect) { + interpolation = bilinear_interpolation_color_char; + } + imb_transform_scanlines(user_data, scanline, interpolation); +} + +BLI_INLINE void imb_transform_scanlines_with_crop(const TransformUserData *user_data, + int scanline, + InterpolationColorFunction interpolation) +{ + const int width = user_data->dst->x; + + float uv[2]; + madd_v2_v2v2fl(uv, user_data->start_uv, user_data->add_y, scanline); + + unsigned char *outI = NULL; + float *outF = NULL; + pixel_from_buffer(user_data->dst, &outI, &outF, 0, scanline); + + for (int xi = 0; xi < width; xi++) { if (uv[0] >= user_data->src_crop.xmin && uv[0] < user_data->src_crop.xmax && uv[1] >= user_data->src_crop.ymin && uv[1] < user_data->src_crop.ymax) { interpolation(user_data->src, outI, outF, uv[0], uv[1]); @@ -445,7 +497,7 @@ BLI_INLINE void imb_transform_scanlines(const TransformUserData *user_data, } } -static void imb_transform_nearest_scanlines(void *custom_data, int scanline) +static void imb_transform_nearest_scanlines_with_crop(void *custom_data, int scanline) { const TransformUserData *user_data = custom_data; InterpolationColorFunction interpolation = NULL; @@ -455,10 +507,10 @@ static void imb_transform_nearest_scanlines(void *custom_data, int scanline) else { interpolation = nearest_interpolation_color_char; } - imb_transform_scanlines(user_data, scanline, interpolation); + imb_transform_scanlines_with_crop(user_data, scanline, interpolation); } -static void imb_transform_bilinear_scanlines(void *custom_data, int scanline) +static void imb_transform_bilinear_scanlines_with_crop(void *custom_data, int scanline) { const TransformUserData *user_data = custom_data; InterpolationColorFunction interpolation = NULL; @@ -468,37 +520,72 @@ static void imb_transform_bilinear_scanlines(void *custom_data, int scanline) else if (user_data->dst->rect) { interpolation = bilinear_interpolation_color_char; } + imb_transform_scanlines_with_crop(user_data, scanline, interpolation); +} + +static void imb_transform_nearest_scanlines_with_repeat(void *custom_data, int scanline) +{ + const TransformUserData *user_data = custom_data; + InterpolationColorFunction interpolation = nearest_interpolation_color_wrap; imb_transform_scanlines(user_data, scanline, interpolation); } -static ScanlineThreadFunc imb_transform_scanline_func(const eIMBInterpolationFilterMode filter) +static ScanlineThreadFunc imb_transform_scanline_func(const eIMBInterpolationFilterMode filter, + const eIMBTransformMode mode) { ScanlineThreadFunc scanline_func = NULL; - switch (filter) { - case IMB_FILTER_NEAREST: - scanline_func = imb_transform_nearest_scanlines; + switch (mode) { + case IMB_TRANSFORM_MODE_REGULAR: + switch (filter) { + case IMB_FILTER_NEAREST: + scanline_func = imb_transform_nearest_scanlines; + break; + case IMB_FILTER_BILINEAR: + scanline_func = imb_transform_bilinear_scanlines; + break; + } break; - case IMB_FILTER_BILINEAR: - scanline_func = imb_transform_bilinear_scanlines; + + case IMB_TRANSFORM_MODE_CROP_SRC: + switch (filter) { + case IMB_FILTER_NEAREST: + scanline_func = imb_transform_nearest_scanlines_with_crop; + break; + case IMB_FILTER_BILINEAR: + scanline_func = imb_transform_bilinear_scanlines_with_crop; + break; + } + break; + + case IMB_TRANSFORM_MODE_WRAP_REPEAT: + BLI_assert_msg(filter == IMB_FILTER_NEAREST, + "Repeat mode is only supported with nearest interpolation."); + scanline_func = imb_transform_nearest_scanlines_with_repeat; break; } return scanline_func; } -void IMB_transform(struct ImBuf *src, +void IMB_transform(const struct ImBuf *src, struct ImBuf *dst, - float transform_matrix[4][4], - struct rctf *src_crop, - const eIMBInterpolationFilterMode filter) + const eIMBTransformMode mode, + const eIMBInterpolationFilterMode filter, + const float transform_matrix[4][4], + const struct rctf *src_crop) { + BLI_assert_msg(mode != IMB_TRANSFORM_MODE_CROP_SRC || src_crop != NULL, + "No source crop rect given, but crop source is requested. Or source crop rect " + "was given, but crop source was not requested."); TransformUserData user_data; user_data.src = src; user_data.dst = dst; - user_data.src_crop = *src_crop; + if (mode == IMB_TRANSFORM_MODE_CROP_SRC) { + user_data.src_crop = *src_crop; + } imb_transform_calc_start_uv(transform_matrix, user_data.start_uv); imb_transform_calc_add_x(transform_matrix, user_data.start_uv, src->x, user_data.add_x); imb_transform_calc_add_y(transform_matrix, user_data.start_uv, src->y, user_data.add_y); - ScanlineThreadFunc scanline_func = imb_transform_scanline_func(filter); + ScanlineThreadFunc scanline_func = imb_transform_scanline_func(filter, mode); IMB_processor_apply_threaded_scanlines(dst->y, scanline_func, &user_data); } diff --git a/source/blender/sequencer/intern/render.c b/source/blender/sequencer/intern/render.c index 6030b49537c..27274626929 100644 --- a/source/blender/sequencer/intern/render.c +++ b/source/blender/sequencer/intern/render.c @@ -459,11 +459,7 @@ static void sequencer_thumbnail_transform(ImBuf *in, ImBuf *out) transform_pivot_set_m4(transform_matrix, pivot); invert_m4(transform_matrix); - /* No crop. */ - rctf source_crop; - BLI_rctf_init(&source_crop, 0, in->x, 0, in->y); - - IMB_transform(in, out, transform_matrix, &source_crop, IMB_FILTER_NEAREST); + IMB_transform(in, out, IMB_TRANSFORM_MODE_REGULAR, IMB_FILTER_NEAREST, transform_matrix, NULL); } /* Check whether transform introduces transparent ares in the result (happens when the transformed @@ -528,7 +524,7 @@ static void sequencer_preprocess_transform_crop( const eIMBInterpolationFilterMode filter = context->for_render ? IMB_FILTER_BILINEAR : IMB_FILTER_NEAREST; - IMB_transform(in, out, transform_matrix, &source_crop, filter); + IMB_transform(in, out, IMB_TRANSFORM_MODE_CROP_SRC, filter, transform_matrix, &source_crop); if (!seq_image_transform_transparency_gained(context, seq)) { out->planes = in->planes; |