浏览代码

obs-filters: Change 'Crop' filter to 'Crop/Pad' filter

Allows the ability to pad in addition to cropping.  Changes the name to
Crop/Pad filter.

(Additional edits by Jim: Greatly refactored/simplified filter code)

Closes jp9000/obs-studio#532
Olle Kelderman 9 年之前
父节点
当前提交
c7e3dfc347

+ 17 - 28
plugins/obs-filters/crop-filter.c

@@ -12,10 +12,10 @@ struct crop_filter_data {
 	int                            right;
 	int                            right;
 	int                            top;
 	int                            top;
 	int                            bottom;
 	int                            bottom;
-	uint32_t                       abs_cx;
-	uint32_t                       abs_cy;
-	uint32_t                       width;
-	uint32_t                       height;
+	int                            abs_cx;
+	int                            abs_cy;
+	int                            width;
+	int                            height;
 	bool                           absolute;
 	bool                           absolute;
 
 
 	struct vec2                    mul_val;
 	struct vec2                    mul_val;
@@ -108,13 +108,13 @@ static obs_properties_t *crop_filter_properties(void *data)
 	obs_property_set_modified_callback(p, relative_clicked);
 	obs_property_set_modified_callback(p, relative_clicked);
 
 
 	obs_properties_add_int(props, "left", obs_module_text("Crop.Left"),
 	obs_properties_add_int(props, "left", obs_module_text("Crop.Left"),
-			0, 8192, 1);
+			-8192, 8192, 1);
 	obs_properties_add_int(props, "top", obs_module_text("Crop.Top"),
 	obs_properties_add_int(props, "top", obs_module_text("Crop.Top"),
-			0, 8192, 1);
+			-8192, 8192, 1);
 	obs_properties_add_int(props, "right", obs_module_text("Crop.Right"),
 	obs_properties_add_int(props, "right", obs_module_text("Crop.Right"),
-			0, 8192, 1);
+			-8192, 8192, 1);
 	obs_properties_add_int(props, "bottom", obs_module_text("Crop.Bottom"),
 	obs_properties_add_int(props, "bottom", obs_module_text("Crop.Bottom"),
-			0, 8192, 1);
+			-8192, 8192, 1);
 	obs_properties_add_int(props, "cx", obs_module_text("Crop.Width"),
 	obs_properties_add_int(props, "cx", obs_module_text("Crop.Width"),
 			0, 8192, 1);
 			0, 8192, 1);
 	obs_properties_add_int(props, "cy", obs_module_text("Crop.Height"),
 	obs_properties_add_int(props, "cy", obs_module_text("Crop.Height"),
@@ -135,37 +135,26 @@ static void calc_crop_dimensions(struct crop_filter_data *filter,
 	obs_source_t *target = obs_filter_get_target(filter->context);
 	obs_source_t *target = obs_filter_get_target(filter->context);
 	uint32_t width;
 	uint32_t width;
 	uint32_t height;
 	uint32_t height;
-	uint32_t total;
 
 
 	if (!target) {
 	if (!target) {
 		width = 0;
 		width = 0;
 		height = 0;
 		height = 0;
+		return;
 	} else {
 	} else {
 		width = obs_source_get_base_width(target);
 		width = obs_source_get_base_width(target);
 		height = obs_source_get_base_height(target);
 		height = obs_source_get_base_height(target);
 	}
 	}
 
 
 	if (filter->absolute) {
 	if (filter->absolute) {
-		uint32_t max_abs_cx = (filter->left + filter->abs_cx);
-		if (max_abs_cx > width) max_abs_cx = width;
-		max_abs_cx -= filter->left;
-
-		total = max_abs_cx < width ? (width - max_abs_cx) : 0;
+		filter->width  = filter->abs_cx;
+		filter->height = filter->abs_cy;
 	} else {
 	} else {
-		total = filter->left + filter->right;
+		filter->width  = (int)width - filter->left - filter->right;
+		filter->height = (int)height - filter->top - filter->bottom;
 	}
 	}
-	filter->width = total > width ? 0 : (width - total);
-
-	if (filter->absolute) {
-		uint32_t max_abs_cy = (filter->top + filter->abs_cy);
-		if (max_abs_cy > height) max_abs_cy = height;
-		max_abs_cy -= filter->top;
 
 
-		total = max_abs_cy < height ? (height - max_abs_cy) : 0;
-	} else {
-		total = filter->top + filter->bottom;
-	}
-	filter->height = total > height ? 0 : (height - total);
+	if (filter->width  < 1) filter->width  = 1;
+	if (filter->height < 1) filter->height = 1;
 
 
 	if (width && filter->width) {
 	if (width && filter->width) {
 		mul_val->x = (float)filter->width / (float)width;
 		mul_val->x = (float)filter->width / (float)width;
@@ -209,13 +198,13 @@ static void crop_filter_render(void *data, gs_effect_t *effect)
 static uint32_t crop_filter_width(void *data)
 static uint32_t crop_filter_width(void *data)
 {
 {
 	struct crop_filter_data *crop = data;
 	struct crop_filter_data *crop = data;
-	return crop->width;
+	return (uint32_t)crop->width;
 }
 }
 
 
 static uint32_t crop_filter_height(void *data)
 static uint32_t crop_filter_height(void *data)
 {
 {
 	struct crop_filter_data *crop = data;
 	struct crop_filter_data *crop = data;
-	return crop->height;
+	return (uint32_t)crop->height;
 }
 }
 
 
 struct obs_source_info crop_filter = {
 struct obs_source_info crop_filter = {

+ 3 - 2
plugins/obs-filters/data/crop_filter.effect

@@ -6,8 +6,9 @@ uniform float2 add_val;
 
 
 sampler_state textureSampler {
 sampler_state textureSampler {
 	Filter    = Linear;
 	Filter    = Linear;
-	AddressU  = Wrap;
-	AddressV  = Wrap;
+	AddressU  = Border;
+	AddressV  = Border;
+	BorderColor = 00000000;
 };
 };
 
 
 struct VertData {
 struct VertData {

+ 1 - 1
plugins/obs-filters/data/locale/en-US.ini

@@ -1,7 +1,7 @@
 ColorFilter="Color Correction"
 ColorFilter="Color Correction"
 MaskFilter="Image Mask/Blend"
 MaskFilter="Image Mask/Blend"
 AsyncDelayFilter="Video Delay (Async)"
 AsyncDelayFilter="Video Delay (Async)"
-CropFilter="Crop"
+CropFilter="Crop/Pad"
 ScrollFilter="Scroll"
 ScrollFilter="Scroll"
 ChromaKeyFilter="Chroma Key"
 ChromaKeyFilter="Chroma Key"
 ColorKeyFilter="Color Key"
 ColorKeyFilter="Color Key"