Pointers as generics C #

Does anyone have an idea whether it is possible to have primitive types as a general argument.

Pay attention before starting to talk about how I should not use pointers.

Images are raw data arrays allocated using IPP , for the most part I will use IPP, this part is a conversion from unmanaged to managed data in the preferred format, when individual processing of data is required in an unsupported IPP way.

I need the following functions in bytes, floats, short combinations, and instead of writing all the explicit combinations, I would rather use some common / template things in style

private static unsafe List<PixelF> GetPixels<Timg,Tmask>( VMImage img, int band, VMImage imgMask, int bandMask) 

where Timg and TMask can be one (byte, short or floating)

right now, one feature looks like this.

 private static unsafe List<PixelF> GetPixels_FloatImg_ByteMask( VMImage img, int band, VMImage imgMask, int bandMask) { List<PixelF> pixValues = new List<PixelF>(); int r, c; int imgSrcOffset = img.LineLength - img.Width; int imgMaskOffset = imgMask.LineLength - imgMask.Width; float* pf_imgSrc = (float*)img.GetRoiPointer(band); byte* pb_imgMask = (byte*)imgMask.GetRoiPointer(bandMask); for (r = 0; r < img.Height; r++) // Loop over rows { for (c = 0; c < img.Width; c++) // Loop over columns { if (*pb_imgMask > 0) { pixValues.Add(new PixelF(c, r, (float)*pf_imgSrc)); } ++pf_imgSrc; ++pb_imgMask; } pf_imgSrc += imgSrcOffset; pb_imgMask += imgMaskOffset; } return pixValues; } 

EDIT ** This is how I would like to do the following:

 private static unsafe List<PixelF> GetPixels<Timg, Tmask>( VMImage img, int band, VMImage imgMask, int bandMask) { List<PixelF> pixValues = new List<PixelF>(); int r, c; int imgSrcOffset = img.LineLength - img.Width; int imgMaskOffset = imgMask.LineLength - imgMask.Width; Timg* pf_imgSrc = (Timg*)img.GetRoiPointer(band); Tmask* pb_imgMask = (Tmask*)imgMask.GetRoiPointer(bandMask); for (r = 0; r < img.Height; r++) // Loop over rows { for (c = 0; c < img.Width; c++) // Loop over columns { if (*pb_imgMask > 0) { pixValues.Add(new PixelF(c, r, (float)*pf_imgSrc)); } ++pf_imgSrc; ++pb_imgMask; } pf_imgSrc += imgSrcOffset; pb_imgMask += imgMaskOffset; } return pixValues; } 

I expect I can do this in C ++ as a template, but is this possible in C #

Edit ***

As the shampoo pointed out, just trying to make

 private static unsafe GetPixels_ByteImg_FloatMask<TImg, TMask>( VMImage img, int band, VMImage imgMask, int bandMask) where TImg : struct where TMask : struct { TImg* pb_imgSrc = (TImg*)img.GetRoiPointer(band); } 

gives an error (Cannot get address, get size or declare a pointer to a managed type.)

Edit ** Basically, I need to know the size of the pointer (byte, short, floating) and, of course, it should be correctly applied to the float,

+4
source share
1 answer

My suggestion was to do this in a separate assembly of Managed C ++, for example C ++ / CLI, where the public .NET API provides your typed interface, and the "insecure" pointer-related stuff happens in native code.

+2
source

Source: https://habr.com/ru/post/1488669/


All Articles