⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 imgsimple.cpp

📁 wince5.0解码gif jpg 那位兄弟需要 可以一起研究一下
💻 CPP
📖 第 1 页 / 共 2 页
字号:
		WS_VISIBLE,
		x,
		y,
		width,
		height,
		NULL,
		NULL,
		(HINSTANCE) g_hInst,
		NULL );
	if( g_hWnd == NULL )
		return FALSE;	
    ShowWindow( g_hWnd, SW_SHOWNORMAL );
	UpdateWindow( g_hWnd );
    //info(TEXT("Main window created, x = %d, y=%d, width = %d, height = %d"), 
    //    x, y, width, height);
	
    return TRUE;
}

LRESULT CALLBACK WndProc(HWND hWnd, UINT wMsg, WPARAM wParam, LPARAM lParam)
{
	static UINT uiFrame=0;
    HDC hdc;
    RECT rc;
	RECT rcTaskbar;
    PAINTSTRUCT ps;
	
	//myself taskbar's size
	rcTaskbar.left=0;
	rcTaskbar.right=240;
	rcTaskbar.top=0;
	rcTaskbar.bottom=24;

    switch(wMsg)
    {
    case WM_PAINT:
        GetClientRect(hWnd, &rc);
        //info (TEXT("client width: %d; height: %d"), rc.right - rc.left, rc.bottom - rc.top);
        //Show desktop
		hdc = BeginPaint(hWnd, &ps);
        g_pImage->Draw(hdc, &rc, NULL);
		//Draw the myself taskbar

        EndPaint(hWnd, &ps);
        return 0;
	case WM_TIMER:
		//info(TEXT("QueryInterface for Image from BitmapImage failed"));
		if(uiFrame==count)uiFrame=0;
		if (FAILED(hr = pImageDecoder->SelectActiveFrame(&FrameDimensionTime, uiFrame++)))
		{
		   info(TEXT("GetFrameCount failed, hr: 0x%08x"), hr);
		   return 0;
		}
	

        if (FAILED(hr = pImageDecoder->BeginDecode(pImageSink, NULL)))
        {
            info(TEXT("BeginDecode into Bitmap Image failed, hr = 0x%08d"), hr);
            goto finish;
        }
        
        for(;;)
        {
            //info(TEXT("Decoding . . ."));
            hr = pImageDecoder->Decode();
			//UINT my_error=GetLastError();

            if (E_PENDING == hr)
            {
                info(TEXT("E_PENDING returned, sleeping"));
                Sleep(500);
            }
            else if (FAILED(hr))
            {
				//MessageBox(NULL,TEXT("decode fail"),NULL,MB_OK);
                info(TEXT("Decode failed, hr = 0x%08x"), hr);
                pImageDecoder->EndDecode(hr);
                goto finish;
            }
            else
            {
				//wsprintf(msg_buf, TEXT("%s %d"), __FILE__, __LINE__);
				//MessageBox(NULL,msg_buf,NULL,MB_OK);

                //info(TEXT("Decode successful"));
                break;
            }
        }

        //while (E_PENDING == (hr = pImageDecoder->Decode()))
         //   Sleep(0);

		//info(TEXT("Decode successful"));

        hr = pImageDecoder->EndDecode(hr);

        if (FAILED(hr))
        {
            info(TEXT("Decoding failed, hr = 0x%08x"), hr);
            goto finish;
        }
		InvalidateRect(g_hWnd,NULL,TRUE);
		break;
    case WM_DESTROY:
        PostQuitMessage(0);
        return 0;
    }
    g_hWnd = hWnd;
    return DefWindowProc(hWnd, wMsg, wParam, lParam);
finish:
    if (pBitmapImage)
        pBitmapImage->Release();
        
    if (pStream)
        pStream->Release();
        
    if (pImageSink)
        pImageSink->Release();

    if (pImageDecoder)
        pImageDecoder->Release();

    if (pImageEncoder)
        pImageEncoder->Release();

    if (g_pImage)
        g_pImage->Release();
        
    if (pImagingFactory)
        pImagingFactory->Release();

    CoUninitialize();
}

void info(const TCHAR* tszFormat, ...)
{
    va_list va;

    TCHAR tszInfo[1024];

    va_start(va, tszFormat);
    _vstprintf(tszInfo, tszFormat, va);
    va_end(va);

	//MessageBox(NULL,tszInfo,NULL,MB_OK);
    //OutputDebugString(tszInfo);
}

HRESULT CreateStreamOnFile(const TCHAR * tszFilename, IStream ** ppStream)
{
    HRESULT hrRet = S_OK;
    HGLOBAL hg = NULL;
    HANDLE hFile = NULL;
    DWORD dwSize, dwRead;
    BYTE* pbLocked = NULL;

    // Open the file
    hFile = CreateFile(tszFilename, GENERIC_READ, 0, NULL, OPEN_EXISTING, 0, NULL);
    if (INVALID_HANDLE_VALUE == hFile)
    {
        info(TEXT("CreateFile failed with GLE = %d"), GetLastError());
        hrRet = 0x80000000 + GetLastError();
        goto error;
    }

    dwSize = GetFileSize(hFile, NULL);
    if (0xffffffff == dwSize)
    {
        info(TEXT("GetFileSize failed with GLE = %d"), GetLastError());
        hrRet = 0x80000000 + GetLastError();
        goto error;
    }

    // Open a memory object
    hg = GlobalAlloc(GMEM_MOVEABLE, dwSize);
    if (NULL == hg)
    {
        info(TEXT("GlobalAlloc failed with GLE = %d"), GetLastError());
        hrRet = 0x80000000 + GetLastError();
        goto error;
    }

    // Ge a pointer to the memory we just allocated
    pbLocked = (BYTE*) GlobalLock(hg);
    if (NULL == pbLocked)
    {
        info(TEXT("GlobalLock failed with GLE = %d"), GetLastError());
        hrRet = 0x80000000 + GetLastError();
        goto error;
    }

    // copy the file
    if (!ReadFile(hFile, pbLocked, dwSize, &dwRead, NULL))
    {
        info(TEXT("ReadFile failed with GLE = %d"), GetLastError());
        hrRet = 0x80000000 + GetLastError();
        goto error;
    }

    GlobalUnlock(hg);
    
    // Create the stream
    hrRet = CreateStreamOnHGlobal(hg, TRUE, ppStream);

    CloseHandle(hFile);
    return hrRet;
error:
    if (pbLocked)
        GlobalUnlock(hg);
    if (hg)
        GlobalFree(hg);
    if (hFile)
        CloseHandle(hFile);
    return hrRet;
}

BOOL GetCodecCLSID(IImagingFactory* pImagingFactory, CLSID * pclsid, WCHAR * wszMimeType, CodecType ctCodec)
{
    UINT uiCount = 0;
    ImageCodecInfo * codecs = NULL;//[28];
	//ImageCodecInfo * *p_code = codecs;
    HRESULT hr;
    BOOL fRet = FALSE;
    TCHAR * tszCodec;
	UINT i;

    if (eEncoder == ctCodec)
    {
		//MessageBox(NULL,TEXT("eEncoder == ctCodec"),NULL,MB_OK);
        hr = pImagingFactory->GetInstalledEncoders(&uiCount, &codecs);//&codecs);
        tszCodec = TEXT("Encoder");
    }
    else
    {
        //hr = pImagingFactory->GetInstalledDecoders(&uiCount, &codecs);
        tszCodec = TEXT("Decoder");
    }
    if (FAILED(hr))
    {
		//if(!codecs)
			//MessageBox(NULL,TEXT("FAILED(hr)"),NULL,MB_OK);
        info(TEXT("GetInstalled%ss returned 0x%x, %d"), tszCodec, hr, uiCount);
        return FALSE;
    }

    for (i = 0; i < uiCount; i++)
    {
        info(TEXT("%s %d of %d: MimeType = %s"), tszCodec, i + 1, uiCount, codecs[i].MimeType);
        if (wszMimeType && !wcscmp(wszMimeType, codecs[i].MimeType))
        {
            *pclsid = codecs[i].Clsid;
            fRet = TRUE;
            break;
        }
    }
	//for ()
    CoTaskMemFree(codecs);
    return fRet;
}

void DisplayEncoder(IImageEncoder * pImageEncoder)
{
    EncoderParameters * pParams = NULL;
    UINT uiParamSize, ui;
    HRESULT hr;

    if (!pImageEncoder)
    {
        return;
    }

    hr = pImageEncoder->GetEncoderParameterListSize(&uiParamSize);
    if (FAILED(hr))
    {
        info(TEXT("GetEncoderParameterListSize failed, hr = 0x%08x"), hr);
        goto Finish;
    }

    pParams = (EncoderParameters*)CoTaskMemAlloc(uiParamSize);
    if (!pParams)
    {
        info(TEXT("Could not CoTaskMemAlloc %d bytes"), uiParamSize);
        goto Finish;
    }

    hr = pImageEncoder->GetEncoderParameterList(uiParamSize, pParams);
    if (FAILED(hr))
    {
        info(TEXT("GetEncoderParameterList failed, hr = 0x%08x"), hr);
        goto Finish;
    }

    for (ui = 0; ui < pParams->Count; ui++)
    {
        info(TEXT("%s: %d entries of type %s (%d)\n"), 
            g_mpEncoder[pParams->Parameter[ui].Guid],
            pParams->Parameter[ui].NumberOfValues,
            g_mpEncoderValueTypes[pParams->Parameter[ui].Type],
            pParams->Parameter[ui].Type);
        for (UINT uiValue = 0; uiValue < pParams->Parameter[ui].NumberOfValues; uiValue++)
        {
            switch (pParams->Parameter[ui].Type)
            {
            case EncoderParameterValueTypeByte:         // fall through
            case EncoderParameterValueTypeUndefined:
                info(TEXT("        %d\n"), 
                    (LONG)((BYTE*)pParams->Parameter[ui].Value)[uiValue]);
                break;
            case EncoderParameterValueTypeASCII:
                info(TEXT("        %c\n"), 
                    (CHAR)((CHAR*)pParams->Parameter[ui].Value)[uiValue]);
                break;
            case EncoderParameterValueTypeShort:
                info(TEXT("        %d\n"), 
                    (LONG)((SHORT*)pParams->Parameter[ui].Value)[uiValue]);
                break;
            case EncoderParameterValueTypeLong:
                info(TEXT("        %d (%s)\n"), 
                    ((LONG*)pParams->Parameter[ui].Value)[uiValue],
                    g_mpEncoderValues[((LONG*)pParams->Parameter[ui].Value)[uiValue]]);
                break;
            case EncoderParameterValueTypeRational:
                info(TEXT("        %d / %d\n"), 
                    ((LONG*)pParams->Parameter[ui].Value)[uiValue*2],
                    ((LONG*)pParams->Parameter[ui].Value)[uiValue*2 + 1]);
                break;
            case EncoderParameterValueTypeLongRange:
                info(TEXT("        %d - %d\n"), 
                    ((LONG*)pParams->Parameter[ui].Value)[uiValue*2],
                    ((LONG*)pParams->Parameter[ui].Value)[uiValue*2 + 1]);
                break;
            case EncoderParameterValueTypeRationalRange:
                info(TEXT("        (%d / %d) - (%d / %d)\n"), 
                    ((LONG*)pParams->Parameter[ui].Value)[uiValue*4],
                    ((LONG*)pParams->Parameter[ui].Value)[uiValue*4 + 1],
                    ((LONG*)pParams->Parameter[ui].Value)[uiValue*4 + 2],
                    ((LONG*)pParams->Parameter[ui].Value)[uiValue*4 + 3]);
                break;
            }
        }
    }

Finish:
    if (pParams)
        CoTaskMemFree(pParams);
}

void UserCodecTest(IImagingFactory* pImagingFactory)
{
    ImageCodecInfo ici;
    BYTE SigPattern[] = {'l', 'h', 'c', 0, '1'};
    BYTE SigMask[] = {0xFF, 0xFF, 0xFF, 0x00, 0xFF};
    CLSID clsidDecoder = { 0xb4e2718f, 0xade1, 0x49bf, 
        { 0xa9, 0xbb, 0x83, 0xdf, 0x7c, 0x45, 0xcc, 0xac } };

    memset(&ici, 0x00, sizeof(ici));

    ici.Clsid = clsidDecoder;

    ici.CodecName = L"Sample User Decoder";
    ici.DllName = L"imgcodec";
    ici.FormatDescription = L"Simplified Metafile";
    ici.FilenameExtension = L"*.LHC";
    ici.MimeType = L"image/x-lhc";
    ici.Flags = ImageCodecFlagsDecoder | ImageCodecFlagsBlockingDecode | ImageCodecFlagsSystem;
    ici.Version = 1;
    ici.SigCount = 1;
    ici.SigSize = 5;
    ici.SigPattern = SigPattern;
    ici.SigMask = SigMask;

    HRESULT hr = pImagingFactory->InstallImageCodec(&ici);
    if (FAILED(hr))
    {
        //info(TEXT("Error in UserCodecTest: hr = 0x%08x"), hr);
    }
    GetCodecCLSID(pImagingFactory, NULL, NULL, eDecoder);
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -