@xenonforlife
The example I refer to is located here;
%cvb%tutorial\image manager\csharp\CSIMG2Bitmap
or for sake of convenience;
private unsafe bool CopyIMGbitsToBitmap(Cvb.Image.IMG img, ref Bitmap bm)
{
// check image and bitmap
if ((!Cvb.Image.IsImage(img)) || (bm == null))
return false;
// get image info (function supports mono and color images, each plane @8bit per pixel)
int nWidth = Cvb.Image.ImageWidth(img);
int nHeight = Cvb.Image.ImageHeight(img);
int nDimension = Cvb.Image.ImageDimension(img);
// check image geometry
if ((bm.Width != nWidth) || (bm.Height != nHeight))
return false;
// prepare linear image access
IntPtr[] pBase = new IntPtr[3];
int[] xInc = new int[3];
int[] yInc = new int[3];
// prepare non-linear image access (through the VPAT)
IntPtr[] addrVPAT = new IntPtr[3];
Cvb.Image.VPAEntry*[] pVPAT = new Cvb.Image.VPAEntry*[3];
// flag to indicate wether the image data is linear or not
bool bIsLinear = true;
for (int i = 0; i < nDimension; i++)
{
// verify the datatype
if (Cvb.Image.ImageDatatype(img, i) != 8)
return false;
// try to get linear access to the image data
if (!Cvb.Utilities.GetLinearAccess(img, i, out pBase[i], out xInc[i], out yInc[i]))
bIsLinear = false;
}
switch (nDimension)
{
case 1:
{
// lock the bitmap
Rectangle rc = new Rectangle(0, 0, nWidth, nHeight);
BitmapData bmData = bm.LockBits(rc, ImageLockMode.ReadWrite, PixelFormat.Format8bppIndexed);
// the pointer to the bitmap bits
byte* pDst = (byte*)bmData.Scan0;
// the linear case
if (bIsLinear)
{
for (int y = 0; y < nHeight; y++)
{
// the pointer to the start of line y of the IMG
byte* pSrc = (byte*)pBase[0] + y * yInc[0];
for (int x = 0; x < nWidth; x++)
{
// inc. bitmap pointer
*(pDst++) = *pSrc;
// inc. IMG pointer
pSrc += xInc[0];
}
// jump to the stride
for (int k = 0; k < bmData.Stride - bmData.Width; k++)
pDst++;
}
}
// the confused VPAT case (e.g. unsorted multitap linescan)
else
{
// get VPAT access to the image data
Cvb.Image.GetImageVPA(img, 0, out pBase[0], out addrVPAT[0]);
// a pointer to the VPAT
pVPAT[0] = (Cvb.Image.VPAEntry*)addrVPAT[0].ToPointer();
for (int y = 0; y < nHeight; y++)
{
// a pointer to the start of the line
byte* pImageLine = (byte*)pBase[0] + pVPAT[0][y].YEntry.ToInt64();
for (int x = 0; x < nWidth; x++)
{
// copy the pixel
*(pDst++) = *(pImageLine + pVPAT[0][x].XEntry.ToInt64());
}
// jump to the stride
for (int k = 0; k < bmData.Stride - bmData.Width; k++)
pDst++;
}
}
// unlock the bitmap bits
bm.UnlockBits(bmData);
return true;
}
case 3:
{
// lock the bitmap
Rectangle rc = new Rectangle(0, 0, nWidth, nHeight);
BitmapData bmData = bm.LockBits(rc, ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
// the pointer to the bitmap bits
byte* pDst = (byte*)bmData.Scan0;
// the linear case
if (bIsLinear)
{
// the pointers to the start of the lines of the IMG
byte*[] pSrc = new byte*[3];
for (int y = 0; y < nHeight; y++)
{
// init the pointers to the start of line y of the IMG
for (int k = 0; k < 3; k++)
pSrc[k] = (byte*)pBase[k] + y * yInc[k];
for (int x = 0; x < nWidth; x++)
{
// copy the rgb (bgr) pixel
#if BGR
*(pDst++) = *pSrc[2];
*(pDst++) = *pSrc[1];
*(pDst++) = *pSrc[0];
#else
*(pDst++) = *pSrc[0];
*(pDst++) = *pSrc[1];
*(pDst++) = *pSrc[2];
#endif
// inc. IMG pointers
for (int k = 0; k < 3; k++)
pSrc[k] += xInc[k];
}
// jump to the stride
for (int k = 0; k < bmData.Stride - bmData.Width * 3; k++)
pDst++;
}
}
// the confused VPAT case (e.g. unsorted multitap linescan)
else
{
// get VPAT access to the image data
for (int k = 0; k < 3; k++)
{
Cvb.Image.GetImageVPA(img, k, out pBase[k], out addrVPAT[k]);
// a pointer to the VPAT
pVPAT[k] = (Cvb.Image.VPAEntry*)addrVPAT[k].ToPointer();
}
// the pointers to the start of the lines of the IMG
byte*[] pSrc = new byte*[3];
for (int y = 0; y < nHeight; y++)
{
// init the pointer to the start of line y of the IMG
for (int k = 0; k < 3; k++)
pSrc[k] = (byte*)pBase[k] + pVPAT[k][y].YEntry.ToInt64();
for (int x = 0; x < nWidth; x++)
{
// copy the rgb (bgr) pixel
#if BGR
*(pDst++) = *(pSrc[2] + pVPAT[2][x].XEntry.ToInt64());
*(pDst++) = *(pSrc[1] + pVPAT[1][x].XEntry.ToInt64());
*(pDst++) = *(pSrc[0] + pVPAT[0][x].XEntry.ToInt64());
#else
*(pDst++) = *(pSrc[0] + pVPAT[0][x].XEntry);
*(pDst++) = *(pSrc[1] + pVPAT[1][x].XEntry);
*(pDst++) = *(pSrc[2] + pVPAT[2][x].XEntry);
#endif
}
// jump to the stride
for (int k = 0; k < bmData.Stride - bmData.Width * 3; k++)
pDst++;
}
}
// unlock the bitmap bits
bm.UnlockBits(bmData);
return true;
}
default:
{
return false;
}
}
}
and the other function:
private unsafe Bitmap CvbImageToBitmap(Cvb.Image.IMG img, out bool RecquiresCopy)
{
// init flag indicating that the data has to be copied to the bitmap
RecquiresCopy = true;
// check image
if (!Cvb.Image.IsImage(img))
return null;
// get image info (supporting mono and color images)
int nWidth = Cvb.Image.ImageWidth(img);
int nHeight = Cvb.Image.ImageHeight(img);
int nDimension = Cvb.Image.ImageDimension(img);
// check the datatype (supporting so far only 8bit for each plane)
for (int i = 0; i < nDimension; i++)
{
if (Cvb.Image.ImageDatatype(img, i) != 8)
return null;
}
switch (nDimension)
{
case 1:
{
// the new bitmap
Bitmap bm = null;
IntPtr pBase = IntPtr.Zero;
long xInc = -1;
long yInc = -1;
// try to get linear access to plane 0
Cvb.Utilities.GetLinearAccess(img, 0, out pBase, out xInc, out yInc);
// we might use the image data without copying the data if the alignment matches
if (((int)yInc == nWidth) && ((int)xInc == 1))
{
// the y-pitch
int stride = nWidth * (int)xInc;
// create the bitmap directly from the pointer to the image data
bm = new Bitmap(nWidth, nHeight, stride, PixelFormat.Format8bppIndexed, pBase);
// this will not require data to be copied
RecquiresCopy = false;
}
else
{
// create a linear bitmap
bm = new Bitmap(nWidth, nHeight, PixelFormat.Format8bppIndexed);
// copy the data
CopyIMGbitsToBitmap(img, ref bm);
}
// the palette
ColorPalette pal = bm.Palette;
// rewrite the palette
for (int i = 0; i < bm.Palette.Entries.Length; i++)
pal.Entries[i] = Color.FromArgb(i, i, i);
// apply it
bm.Palette = pal;
return bm;
}
case 3:
{
// create a linear bitmap
Bitmap bm = new Bitmap(nWidth, nHeight, PixelFormat.Format24bppRgb);
// copy the data
CopyIMGbitsToBitmap(img, ref bm);
return bm;
}
default:
{
return null;
}
}
}