Access the image buffer to send address outside

Hi,

I would like to ask a bit of your help to use the image to be sent to an external Api for Processing. this external API ( Barcode reader ) takes a pointer address, width and height.

I have Managed to get it working Ok with the following Code but I have to make a copy to an array to retrieve an address to the image buffer - what I would like is read direct from the image ( I am using cvb.net)

thanks for the help.

     public int DecodeByte(Device device, int ROITop, int ROILeft, int ROIWidth, int      ROIHeight)
     {
          int retCode;
          int offset = 0;
          var img = device.DeviceImage.Planes[0].Map().MapTo8Bit();

          var width = img.Width;
          var height = img.Height;
          var myNewImage = img.Planes[0].GetLinearAccess();

          var pBase = myNewImage.BasePtr;
          var XInc = myNewImage.XInc;
          var YInc = myNewImage.YInc;
          byte[] srcVal2 = new byte[width * height];

          for (int y = 0; y < height; y++)
          {
              byte* pSrcLine = (byte*)pBase + y * (int)YInc;

              for (int x = 0; x < width; x++)
              {
                  var srcVal = *(pSrcLine + x * (int)XInc);
                  srcVal2[offset++] = srcVal;
              }
          }
          
          fixed (byte* pix = &srcVal2[0])
          {
              // Set image properties
              CRD_Set(handle, P_IMAGE_ADDRESS, (void*)pix); // address of Image here
              CRD_Set(handle, P_IMAGE_WIDTH, (void*)width);
              CRD_Set(handle, P_IMAGE_HEIGHT, (void*)height);
              CRD_Set(handle, P_IMAGE_BUF_WIDTH, (void*)width);
              // Set Region of Interest for decoding
              CRD_Set(handle, P_BC_ROI_TOP, (void*)ROITop);
              CRD_Set(handle, P_BC_ROI_LEFT, (void*)ROILeft);
              CRD_Set(handle, P_BC_ROI_WIDTH, (void*)ROIWidth);
              CRD_Set(handle, P_BC_ROI_HEIGHT, (void*)ROIHeight);
              CRD_Set(handle, P_ENABLE_QR_2005, (void*)1);
              retCode = CRD_Decode(handle);
          }
          return retCode;
       }
2 Likes

Nevermind, found my bug-
I believe Pbase is already in a fixed statement hence putting inside another fixed statement didn’t make the compiler happy.

Moving everything outside the fixed statement did the trick to pass the Image Address to a third party Api.

Cheers

2 Likes

Hi @amsted34!

Sorry for the delayed reply. Your finding is correct: The base pointer returned for linear access is generally fixed (the Stemmer.Cvb.Image object is a proper CLR-object, but the memory in which the image is stored resides on the unmanaged heap, therefore the pixel data is not subject to being moved around during heap reorganization of the CLR, so unlike with e. g. the System.Drawing.Bitmap you don’t need to worry about pinning it before passing it to another unmanaged library.

2 Likes

Thanks @illusive for adding your input on this especially the extra explanation that goes with it :wink:

Cheers,
Amine

1 Like