Skip to content

Commit

Permalink
Enhance image rendering by adding aspect ratio scaling and updating c…
Browse files Browse the repository at this point in the history
…rop region limits in OCR filter properties
  • Loading branch information
royshil committed Nov 21, 2024
1 parent d1871de commit b8fb7b6
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 9 deletions.
25 changes: 24 additions & 1 deletion data/preview.effect
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
uniform float4x4 ViewProj;
uniform texture2d image;
uniform texture2d myimage;
uniform float2 source_size;
uniform float2 image_size;

sampler_state def_sampler {
Filter = Linear;
Expand All @@ -23,7 +25,28 @@ VertInOut VSDefault(VertInOut vert_in)

float4 PSDrawBare(VertInOut vert_in) : TARGET
{
return float4(myimage.Sample(def_sampler, vert_in.uv).bgr, 1);
float2 scaled_uv;

// Calculate aspect ratios
float target_aspect = source_size.x / source_size.y;
float image_aspect = image_size.x / image_size.y;

// Scale to fit while maintaining aspect ratio
if (image_aspect < target_aspect) {
scaled_uv.y = vert_in.uv.y;
scaled_uv.x = (vert_in.uv.x * source_size.x) / image_size.x;
} else {
scaled_uv.x = vert_in.uv.x;
scaled_uv.y = (vert_in.uv.y * source_size.y) / image_size.y;
}

// Sample texture if within bounds
if (scaled_uv.x >= 0.0 && scaled_uv.x <= 1.0 &&
scaled_uv.y >= 0.0 && scaled_uv.y <= 1.0) {
return float4(myimage.Sample(def_sampler, scaled_uv).bgr, 1.0);
}

return float4(0.0, 0.0, 0.0, 0.0);
}

technique MyDraw
Expand Down
16 changes: 8 additions & 8 deletions src/ocr-filter-properties.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -389,14 +389,14 @@ obs_properties_t *ocr_filter_properties(void *data)
OBS_GROUP_NORMAL, crop_group_props);

// add crop region settings
obs_properties_add_int_slider(crop_group_props, "crop_left", obs_module_text("CropLeft"), 0,
1000, 1);
obs_properties_add_int_slider(crop_group_props, "crop_right", obs_module_text("CropRight"),
0, 1000, 1);
obs_properties_add_int_slider(crop_group_props, "crop_top", obs_module_text("CropTop"), 0,
1000, 1);
obs_properties_add_int_slider(crop_group_props, "crop_bottom",
obs_module_text("CropBottom"), 0, 1000, 1);
obs_properties_add_int(crop_group_props, "crop_left", obs_module_text("CropLeft"), 0, 2000,
1);
obs_properties_add_int(crop_group_props, "crop_right", obs_module_text("CropRight"), 0,
2000, 1);
obs_properties_add_int(crop_group_props, "crop_top", obs_module_text("CropTop"), 0, 2000,
1);
obs_properties_add_int(crop_group_props, "crop_bottom", obs_module_text("CropBottom"), 0,
2000, 1);

// Add a informative text about the plugin
obs_properties_add_text(
Expand Down
9 changes: 9 additions & 0 deletions src/ocr-filter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,15 @@ void ocr_filter_video_render(void *data, gs_effect_t *_effect)

gs_eparam_t *imageParam = gs_effect_get_param_by_name(tf->effect, "myimage");
gs_effect_set_texture(imageParam, tex);
vec2 sourceSize = {static_cast<float>(width), static_cast<float>(height)};
gs_effect_set_vec2(gs_effect_get_param_by_name(tf->effect, "source_size"),
&sourceSize);
vec2 texSize = {static_cast<float>(tf->outputPreviewBGRA.cols),
static_cast<float>(tf->outputPreviewBGRA.rows)};
gs_effect_set_vec2(gs_effect_get_param_by_name(tf->effect, "image_size"), &texSize);

obs_log(LOG_INFO, "tex size %f %f", texSize.x, texSize.y);
obs_log(LOG_INFO, "source size %f %f", sourceSize.x, sourceSize.y);

gs_blend_state_push();
gs_blend_function(GS_BLEND_ONE, GS_BLEND_ZERO);
Expand Down

0 comments on commit b8fb7b6

Please sign in to comment.