inline HRESULT CreateRGBVideoType()

in TTDQueries/app-sample/dshowutil.h [2223:2318]


inline HRESULT CreateRGBVideoType(AM_MEDIA_TYPE &mt, WORD iBitDepth, long Width, long Height, 
                           double fps)
{
    DWORD color_mask_565[] = { 0x00F800, 0x0007E0, 0x00001F };


    if ((iBitDepth != 1) && (iBitDepth != 4) && (iBitDepth != 8) &&
        (iBitDepth != 16) && (iBitDepth != 24) && (iBitDepth != 32))
    {
        return E_INVALIDARG;
    }

    if (Width < 0)
    {
        return E_INVALIDARG;
    }

    _FreeMediaType(mt);

    mt.pbFormat = (BYTE*)CoTaskMemAlloc(sizeof(VIDEOINFO));
    if (!mt.pbFormat)
    {
        return E_OUTOFMEMORY;
    }
    mt.cbFormat = sizeof(VIDEOINFO);


    VIDEOINFO *pvi = (VIDEOINFO*)mt.pbFormat;
    ZeroMemory(pvi, sizeof(VIDEOINFO));

    pvi->AvgTimePerFrame = FramesPerSecToFrameLength(fps);

    BITMAPINFOHEADER *pBmi = &(pvi->bmiHeader);
    pBmi->biSize = sizeof(BITMAPINFOHEADER);
    pBmi->biWidth = Width;
    pBmi->biHeight = Height;
    pBmi->biPlanes = 1;
    pBmi->biBitCount = iBitDepth;

    if (iBitDepth == 16)
    {
        pBmi->biCompression = BI_BITFIELDS;
        memcpy(pvi->dwBitMasks, color_mask_565, sizeof(DWORD) * 3);
    }
    else
    {
        pBmi->biCompression = BI_RGB;
    }

    if (iBitDepth <= 8)
    {
        // Palettized format.
        pBmi->biClrUsed = PALETTE_ENTRIES(pvi);

        HDC hdc = GetDC(NULL);  // hdc for the current display.
        if (hdc == NULL)
        {
            _FreeMediaType(mt);
            return HRESULT_FROM_WIN32(GetLastError());
        }
        GetSystemPaletteEntries(hdc, 0, pBmi->biClrUsed, (PALETTEENTRY*)pvi->bmiColors);

        ReleaseDC(NULL, hdc);
    }

    pvi->bmiHeader.biSizeImage = DIBSIZE(pvi->bmiHeader);

    mt.majortype == MEDIATYPE_Video;
    mt.subtype = FORMAT_VideoInfo;

    switch (iBitDepth)
    {
    case 1:
        mt.subtype = MEDIASUBTYPE_RGB1;
        break;
    case 4:
        mt.subtype = MEDIASUBTYPE_RGB4;
        break;
    case 8:
        mt.subtype = MEDIASUBTYPE_RGB8;
        break;
    case 16:
        mt.subtype = MEDIASUBTYPE_RGB565;
        break;
    case 24:
        mt.subtype = MEDIASUBTYPE_RGB24;
        break;
    case 32:
        mt.subtype = MEDIASUBTYPE_RGB32;
    }

    mt.lSampleSize = pvi->bmiHeader.biSizeImage;
    mt.bTemporalCompression = FALSE;
    mt.bFixedSizeSamples = TRUE;
    return S_OK;
}