My internship at Microsoft

I felt relieved after finalizing my last formal step on the way to my internship at Microsoft in Redmond - getting the visa to the United States.  I will be working in the Active Directory team building the so-called NGAD (Next-Generation Active Directory). More info about this project is on this presentation from PDC 2009.

 

On this blog I will try to regularly post updates about the 3 months that I'll spend in Redmond, in Seattle, and generally in the US. Promises to be an awesome adventure!

Stay tuned!

Immediate data binding update

While writing a WPF application that made use of incremental search basing on the value of a TextBox I've faced a problem that the ViewModel (the DataContext) was notified about the changed value of the property only when it lost focus.

So let's say I had on the ViewModel side:

 

public string QueryString
{
    get
    {
        return _queryString;
    }
    set
    {
        _queryString = value;
        OnPropertyChanged("Patients");
    }
}

and in my WPF:

 

<TextBox Height="36" Margin="12,23,12,0" VerticalAlignment="Top" FontSize="18" Text="{Binding QueryString, Mode=TwoWay}" />

A change on the QueryString property on the ViewModel should be whenever the value of the TextBox is changed - no matter whether it lost focus or not.

The solution to this is to set the UpdateSourceTrigger property of the binding to "PropertyChanged". Now a PropertyChanged is triggered immediately after a new character is typed (or pasted):

 

<TextBox Height="36" Margin="12,23,12,0" VerticalAlignment="Top" FontSize="18" Text="{Binding QueryString, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />

 

Yet, everytime a new value is entered a new search is performed. Although the searching algorithm is quite efficient (as it's built on suffix trees) but the user could experience some delays between his keystroke and the new value appearing on his TextBox, thus why I recommend setting it's IsAsync value to "true":

 

<TextBox Height="36" Margin="12,23,12,0" VerticalAlignment="Top" FontSize="18" Text="{Binding QueryString, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged, IsAsync=True}" />

DirectShow custom transform filter skeleton

After my last struggles with building a DirectShow video filter, I've decided to publish a skeleton application for a video transform filter.

This skeleton application is using the CTransformFilter approach, it means that it performs per frame transformations on a copy of the frame.

 

While you might think that transforming a copy of the video frame is not effective as you are using double the size of the memory for each frame, but in fact - this approach is quite more performance-effective for the project that I was building as accessing and modifying the data in the video card memory consumes more time than working on your local memory in some cases (when your video cards is not fast i.e  an embedded devide) and my project was the case.

 

Of course, you should consult the MSDN documentation upon writing your filters and I would suggest you to write it from scratch if it's a sophisticated one, if you might need more than one input and output pin, or in general you need to customize it to meet your needs. This code is by no means a silver bullet.

I would like you to excuse me for leaving the classes and methods unchanged in the source code. As this skeleton is a crippled-down version of my original project that was really a pain in the ass.

Using the attached source code to create a new filter is limited to implementing one method - the Transform method in the OculusVideoFilter class:

 

HRESULT OculusVideoFilter::Transform(IMediaSample *pMedia)
{
    BYTE *pData;                
    long lDataLen;              
    int iPixel;                 
    int temp,x,y;               
    RGBTRIPLE *pRGB;            

    AM_MEDIA_TYPE* pType = &m_pInput->CurrentMediaType();
    VIDEOINFOHEADER *pvi = (VIDEOINFOHEADER *) pType->pbFormat;
    ASSERT(pvi);

    CheckPointer(pMedia,E_POINTER);
    pMedia->GetPointer(&pData);
    lDataLen = pMedia->GetSize();

    int cxImage    = pvi->bmiHeader.biWidth;
    int cyImage    = pvi->bmiHeader.biHeight;
    int numPixels  = cxImage * cyImage;

    pRGB = (RGBTRIPLE*) pData;
    for (iPixel=0; iPixel < numPixels; iPixel++, pRGB++) {
        /*
            here you implement your per-pixel operation on the currently processed frame.
        */
    }
    return NOERROR;
}
 
This skeleton app is COM-ready (It's required for DirectShow filters to be COM ready), so all you need after implementing your filtering logic is to compile the library, it will generate a .DLL file for you and use regsvr32.exe to register it.
One more note: Please remember to change the CLSID of the class, as it will conflict with other registered interfaces. If you would leave the CLSID:
 
// {5F1E81B9-9669-444D-A007-63EBB5CE92E3}
static const GUID CLSID_OculusFilter = 
{ 0x5f1e81b9, 0x9669, 0x444d, { 0xa0, 0x7, 0x63, 0xeb, 0xb5, 0xce, 0x92, 0xe3 } };
 

Unchanged and one day it might happen that you'll use my piece of software (or two people using this skeleton will not change this CLSID) you will end up with conflicts in your registry.

 

Download package.

 

Now as an example I'll show you how to create a video filter that will grayscale your output video.

The grayscale calculation is: grey = (30 * red + 59 * green + 11 * blue) / 100

A faster (but less precise) method would be: grey = (red + green) / 2

 

Now, with this navigate to the filter class to line number 137 and in your loop should look similar to this:

 

for (iPixel=0; iPixel < numPixels; iPixel++, pRGB++) {
      grey = (prgb->rgbtRed + prgb->rgbtGreen) >> 1;
      pRGB->rgbtRed = prgb->rgbtGreen = pRGB->rgbtBlue = (BYTE) grey;
   }

 

Have fun with your filters!

Microsoft SQL Server Enums

You know the enumeration data type, for sure. Either from programming languages (most often it's the enum keyword) or from database engines like MySQL.

What I was missing lately in my SQL Server database was the enum. To have a column that could accept only a defined set of values. As an example of MySQL enum column, is defining clothing sizes:

 

CREATE TABLE cloth (
    .
size ENUM('small', 'medium', 'large')
    .
);

 

Unfortunately SQL Server doesn't provide this functionality out of the box and I needed to create such data type in my project when trying to define if a financial operation is an income or an outcome. Of course I could bypass this by creating an IsIncome column that stores a boolean value and later on check, if the value is false, then it means that it money where withdrawn. But what if I need to add another financial operation type? Like freezing an amount of money? I could add IsFreeze column of course, but it won't be the smartest way of designing databases.

 

The ideal solution for this would be creating something like MySQL provides, allowing only three values to be set in that column. We could of course create a char column and set it's value to the desired type programmatically, but again, there would be no control over the consistency of our data. Nothing prevents us from adding a random value there that is not in the set of allowed values. There is a solution to that by creating additional table, who's primary key would be of char type. We could use int type, but the problem with integers that later on the system becomes less maintainable and the data unreadable. That's why I recommend you to use a meaningful char as a key.

 

I'll try to reproduce the above MySQL example in SQL Server.

 

First of all, we should create a table that will hold our allowed values. In this case the values are the sizes:

 

CREATE TABLE cloth_size (
   size  char(6) NOT NULL,
   CONSTRAINT PK_Size primary key (size)
)
 

Next, we add three rows to our table:

 

INSERT INTO cloth_size ('small')
INSERT INTO cloth_size ('medium')
INSERT INTO cloth_size ('large')

 

Next we create the cloth table with a column named size and we reference it to the cloth_size table:

 

CREATE TABLE cloth (
    ...
   size CHAR(6)
    ...
   FOREIGN KEY (size) REFERENCES cloth_size(size)
);
 

And now we are allowed only to insert the three values that we've inserted into cloth_size table.

License plate injection

Police in Wroclaw installed on-road monitoring cameras..

 

91779

 

. that recognize your car's license plate and warn you about your speed:

 

91781

 

("Zwolnij" in Polish means "slow down")

but some drivers decided to fuck the system:

 

dropdatabase

 

:)

©2008 Karim Agha. All rights reserved.