C#中继承该基类的子类?

3

有一个 Xamarin.ios 组件叫做 GPUImage,它最初是用 Objective-C 编写的,现在使用绑定以便在 C# 中运行。

我似乎无法得到其类 GPUImageThreeInputFilter 的可工作子类,该类应该接受一个字符串,然后将其处理为 GLSL。 以下是该类的样子:

using Foundation;
using System;
using System.ComponentModel;
using System.Runtime.CompilerServices;

namespace GPUImage.Filters
{
    [Register ("GPUImageThreeInputFilter", true)]
    public class GPUImageThreeInputFilter : GPUImageTwoInputFilter
    {
        //
        // Static Fields
        //
        [CompilerGenerated]
        private static readonly IntPtr class_ptr;

        [CompilerGenerated]
        private static NSString _ThreeInputTextureVertexShaderString;

        //
        // Static Properties
        //
        [Field ("kGPUImageThreeInputTextureVertexShaderString", "__Internal")]
        public static NSString ThreeInputTextureVertexShaderString {
            get;
        }

        //
        // Properties
        //
        public override IntPtr ClassHandle {
            get;
        }

        //
        // Constructors
        //
        [Export ("init"), EditorBrowsable (EditorBrowsableState.Advanced), CompilerGenerated]
        public GPUImageThreeInputFilter ();

        [EditorBrowsable (EditorBrowsableState.Advanced), CompilerGenerated]
        protected GPUImageThreeInputFilter (NSObjectFlag t);

        [EditorBrowsable (EditorBrowsableState.Advanced), CompilerGenerated]
        protected internal GPUImageThreeInputFilter (IntPtr handle);

        //
        // Methods
        //
        [Export ("disableThirdFrameCheck"), CompilerGenerated]
        public virtual void DisableThirdFrameCheck ();
    }
}

我已经创建了这个类的子类:

public class ImageProcess : GPUImageThreeInputFilter
    {
        public static new NSString ThreeInputTextureVertexShaderString =
               ((NSString)("    varying highp vec2 textureCoordinate;" +
                "    varying highp vec2 textureCoordinate2;" +
                "    varying highp vec2 textureCoordinate3;" +
                "" +
                "    uniform sampler2D inputImageTexture;" +
                "    uniform sampler2D inputImageTexture2;" +
                "    uniform sampler2D inputImageTexture3;" +
                "" +
                "    void main()" +
                "    {" +
                "          lowp vec4 one = texture2D(inputImageTexture, textureCoordinate);" +
                "          lowp vec4 two = texture2D(inputImageTexture2, textureCoordinate2);" +
                "          lowp vec4 three = texture2D(inputImageTexture3, textureCoordinate3);" +
                "          lowp vec4 out;" +
                "          float maxone = one.r + one.g + one.b;" +
                "          float maxtwo = two.r + two.g + two.b;" +
                "          float maxthree = three.r + three.g + three.b;" +
                "          if (maxone >= maxtwo && maxone >= maxthree)" +
                "          {" +
                "          out.r = one.r;" +
                "          out.b = one.b;" +
                "          out.g = one.g;" +
                "          };" +
                "          if (maxtwo >= maxone && maxtwo >= maxthree)" +
                "          {" +
                "          out.r = two.r;" +
                "          out.b = two.b;" +
                "          out.g = two.g;" +
                "          };" +
                "          if (maxthree >= maxtwo && maxthree >= maxone)" +
                "          {" +
                "          out.r = three.r;" +
                "          out.b = three.b;" +
                "          out.g = three.g;" +
                "          };" +
                "          out.a = 1.0;" +
                "          gl_FragColor = out;" +
                                      "     }"));
    }

子类的使用:

var firstFilter = new ImageProcess();

first.AddTarget(firstFilter);     // first, second, and third
first.ProcessImage();             // are unique GPUImagePicture
second.AddTarget(firstFilter);    // values (pictures) defined
second.ProcessImage();            // somewhere else.
third.AddTarget(firstFilter);
third.ProcessImage();
firstFilter.UseNextFrameForImageCapture();
firstFilter.AddTarget(output);    // outputs the end result to
                                  // the screen

子类应从三个图像中获取最亮的像素并返回一个图像。然而,它只返回了第一个图像,因为该字符串在子类中没有注册。所以,我的问题是,这样的子类应该长成什么样子,它接受的NSString应该放在哪里?我以前没有遇到过这样的类。谢谢。
更新:我暂时使用和重新编写了这里找到的方法,但是所有针对处理后颜色数组的调用都需要大约四到五分钟,而这个方法最多只需要几秒钟,所以我仍将尝试解决这个问题。
2个回答

0

您的自定义类“GPUImageThreeInputFilter”是从基类“GPUImageTwoInputFilter”派生而来。

这是您的意图吗? 也许您应该为自定义类命名并使其继承“GPUImageThreeInputFilter”类。


'GPUImageThreeInputFilter' 继承自 'GPUImageTwoInputFilter',但是这个自定义类又继承自 'GPUImageThreeInputFilter'。我已经更新了帖子以澄清这一点。 - Michael R

0

看起来它可以工作。我刚刚在一个示例项目中尝试了一下,这里是结果:

ImageProcess.cs:

public class ImageProcess : GPUImageThreeInputFilter
{
    public static new NSString ThreeInputTextureVertexShaderString
        => new NSString("My string");
}

输出:

var process = new ImageProcess();
Console.WriteLine(process);

enter image description here

你能告诉我你期望的结果是什么吗?还有你创建ImageProcess对象的代码部分可以给我看一下吗?


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接