Click here to Skip to main content
12,350,653 members (25,044 online)
Click here to Skip to main content

Stats

165.7K views
7.7K downloads
135 bookmarked
Posted

Webcamera, Multithreading and VFW

, 15 Feb 2008 CPOL
An article on webcamera frame-grabbing in a multi-thread environment
using System;
using System.Drawing;
using System.Drawing.Imaging;


namespace SevenZ.ImageProcessing
{
   public class Filters
   {
      //Gilles Khouzams colour corrected grayscale shear
      static ColorMatrix cm = new ColorMatrix(new float[][]{   
                                   new float[]{0.3f,  0.3f, 0.3f,  0,  0},
                                   new float[]{0.59f,0.59f,0.59f,  0,  0},
                                   new float[]{0.11f,0.11f,0.11f,  0,  0},
                                   new float[]{0,  0,    0,    1,  0,  0},
                                   new float[]{0,  0,    0,    0,  1,  0},
                                   new float[]{0,  0,    0,    0,  0,  1}});

      public static Image ToGrayScale(Image img)
      {
         //BobPowell.Net
         //http://www.bobpowell.net/grayscale.htm
         Graphics g = Graphics.FromImage(img);
         ImageAttributes ia = new ImageAttributes();
         ia.SetColorMatrix(cm);
         g.DrawImage(img, new Rectangle(0, 0, img.Width, img.Height), 0, 0, img.Width, img.Height, GraphicsUnit.Pixel, ia);
         g.Dispose();
         return img;
      }


      public static bool Flip(Bitmap b, bool bHorz, bool bVert)
      {
         //http://www.codeproject.com/script/profile/whos_who.asp?vt=arts&id=6556
         Point[,] ptFlip = new Point[b.Width, b.Height];

         int nWidth = b.Width;
         int nHeight = b.Height;

         for (int x = 0; x < nWidth; ++x)
            for (int y = 0; y < nHeight; ++y)
            {
               ptFlip[x, y].X = (bHorz) ? nWidth - (x + 1) : x;
               ptFlip[x, y].Y = (bVert) ? nHeight - (y + 1) : y;
            }

         OffsetFilterAbs(b, ptFlip);

         return true;
      }

      public static bool OffsetFilterAbs(Bitmap b, Point[,] offset)
      {
         Bitmap bSrc = (Bitmap)b.Clone();

         // GDI+ still lies to us - the return format is BGR, NOT RGB.
         BitmapData bmData = b.LockBits(new Rectangle(0, 0, b.Width, b.Height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
         BitmapData bmSrc = bSrc.LockBits(new Rectangle(0, 0, bSrc.Width, bSrc.Height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);

         int scanline = bmData.Stride;

         System.IntPtr Scan0 = bmData.Scan0;
         System.IntPtr SrcScan0 = bmSrc.Scan0;

         unsafe
         {
            byte* p = (byte*)(void*)Scan0;
            byte* pSrc = (byte*)(void*)SrcScan0;

            int nOffset = bmData.Stride - b.Width * 3;
            int nWidth = b.Width;
            int nHeight = b.Height;

            int xOffset, yOffset;

            for (int y = 0; y < nHeight; ++y)
            {
               for (int x = 0; x < nWidth; ++x)
               {
                  xOffset = offset[x, y].X;
                  yOffset = offset[x, y].Y;

                  if (yOffset >= 0 && yOffset < nHeight && xOffset >= 0 && xOffset < nWidth)
                  {
                     p[0] = pSrc[(yOffset * scanline) + (xOffset * 3)];
                     p[1] = pSrc[(yOffset * scanline) + (xOffset * 3) + 1];
                     p[2] = pSrc[(yOffset * scanline) + (xOffset * 3) + 2];
                  }

                  p += 3;
               }
               p += nOffset;
            }
         }

         b.UnlockBits(bmData);
         bSrc.UnlockBits(bmSrc);

         return true;
      }
   }

   
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Zaur Nasibov
Software Developer
Finland Finland
I'm a Master degree student, studying at the University of Joensuu, Finland.

You may also be interested in...

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.160621.1 | Last Updated 16 Feb 2008
Article Copyright 2007 by Zaur Nasibov
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid