📄 imgsimple.cpp
字号:
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
//
// Use of this source code is subject to the terms of the Microsoft end-user
// license agreement (EULA) under which you licensed this SOFTWARE PRODUCT.
// If you did not accept the terms of the EULA, you are not authorized to use
// this source code. For a copy of the EULA, please see the LICENSE.RTF on your
// install media.
//
#define INITGUID
#include "imgsimple.h"
#ifndef UNDER_CE
#include <tchar.h>
#endif
HINSTANCE g_hInst = NULL;
HWND g_hWnd = NULL;
IImage * g_pImage = NULL;
IImagingFactory * g_pImagingFactory = NULL;
TCHAR * g_tszFilename;
UINT uiTimer1;
IImagingFactory *pImagingFactory = NULL;
IImageSink *pImageSink = NULL;
IImage *pImage = NULL;
IImageDecoder *pImageDecoder = NULL;
IImageEncoder *pImageEncoder = NULL, *pIETemp = NULL;
IBitmapImage *pBitmapImage = NULL;
IStream *pStream = NULL;
HRESULT hr;
HINSTANCE hiImaging = NULL;
ImageInfo ii;
MSG msg;
TCHAR *tszError;
TCHAR *tszName, *tszMime;//*tszOutput,
TCHAR tszOutput[255];
UINT count;
TCHAR tszNotReg[] = TEXT("REGDB_E_CLASSNOTREG");
TCHAR tszNoAgg[] = TEXT("CLASS_E_NOAGGREGATION");
TCHAR tszUnknown[] = TEXT("unknown hr");
TCHAR *
cMapClass::operator[] (GUID index)
{
// initialize it to entry 0's string, which is "skip"
TCHAR * tcTmp = NULL;
for (int i = 0; NULL != m_funcEntryStruct[i].szName; i++)
if (*((const GUID*)m_funcEntryStruct[i].pValue) == index)
tcTmp = m_funcEntryStruct[i].szName;
return tcTmp;
}
TCHAR *
cMapClass::operator[] (LONG index)
{
// initialize it to entry 0's string, which is "skip"
TCHAR * tcTmp = NULL;
for (int i = 0; NULL != m_funcEntryStruct[i].szName; i++)
if (m_funcEntryStruct[i].pValue == index)
tcTmp = m_funcEntryStruct[i].szName;
return tcTmp;
}
// asdfg
NameValuePair g_nvpParamGuids[] = {
NAMEVALENTRY(&ENCODER_COMPRESSION),
NAMEVALENTRY(&ENCODER_COLORDEPTH),
NAMEVALENTRY(&ENCODER_SCANMETHOD),
NAMEVALENTRY(&ENCODER_VERSION),
NAMEVALENTRY(&ENCODER_RENDERMETHOD),
NAMEVALENTRY(&ENCODER_QUALITY),
NAMEVALENTRY(&ENCODER_ROTATION),
NAMEVALENTRY(&ENCODER_TRANSFORMATION),
NAMEVALENTRY(&ENCODER_LUMINANCE_TABLE),
NAMEVALENTRY(&ENCODER_CHROMINANCE_TABLE),
NAMEVALENTRY(&ENCODER_SAVE_FLAG),
{0,0}
};
NameValuePair g_nvpEncoderValues[] = {
NAMEVALENTRY(EncoderValueColorTypeCMYK),
NAMEVALENTRY(EncoderValueColorTypeYCCK),
NAMEVALENTRY(EncoderValueCompressionLZW),
NAMEVALENTRY(EncoderValueCompressionCCITT3),
NAMEVALENTRY(EncoderValueCompressionCCITT4),
NAMEVALENTRY(EncoderValueCompressionRle),
NAMEVALENTRY(EncoderValueCompressionNone),
NAMEVALENTRY(EncoderValueScanMethodInterlaced),
NAMEVALENTRY(EncoderValueScanMethodNonInterlaced),
NAMEVALENTRY(EncoderValueVersionGif87),
NAMEVALENTRY(EncoderValueVersionGif89),
NAMEVALENTRY(EncoderValueRenderProgressive),
NAMEVALENTRY(EncoderValueRenderNonProgressive),
NAMEVALENTRY(EncoderValueTransformRotate90),
NAMEVALENTRY(EncoderValueTransformRotate180),
NAMEVALENTRY(EncoderValueTransformRotate270),
NAMEVALENTRY(EncoderValueTransformFlipHorizontal),
NAMEVALENTRY(EncoderValueTransformFlipVertical),
NAMEVALENTRY(EncoderValueMultiFrame),
NAMEVALENTRY(EncoderValueLastFrame),
NAMEVALENTRY(EncoderValueFlush),
NAMEVALENTRY(EncoderValueFrameDimensionTime),
NAMEVALENTRY(EncoderValueFrameDimensionResolution),
NAMEVALENTRY(EncoderValueFrameDimensionPage),
{0,0}
};
NameValuePair g_nvpEncoderValueTypes[] = {
NAMEVALENTRY(EncoderParameterValueTypeByte),
NAMEVALENTRY(EncoderParameterValueTypeASCII),
NAMEVALENTRY(EncoderParameterValueTypeShort),
NAMEVALENTRY(EncoderParameterValueTypeLong),
NAMEVALENTRY(EncoderParameterValueTypeRational),
NAMEVALENTRY(EncoderParameterValueTypeLongRange),
NAMEVALENTRY(EncoderParameterValueTypeUndefined),
NAMEVALENTRY(EncoderParameterValueTypeRationalRange),
{0,0}
};
cMapClass g_mpEncoder(g_nvpParamGuids);
cMapClass g_mpEncoderValues(g_nvpEncoderValues);
cMapClass g_mpEncoderValueTypes(g_nvpEncoderValueTypes);
TCHAR msg_buf[255];
typedef HRESULT (*DLLGETCLASSOBJECT) (REFCLSID, REFIID, VOID**);
int WINAPI WinMain
(
HINSTANCE hInstance,
HINSTANCE hInstPrev,
#ifdef UNDER_CE
LPWSTR pszCmdLine,
#else
LPSTR pszCmdLine,
#endif
int nCmdShow
)
{
g_hInst = hInstance;
if (FAILED(hr = CoInitializeEx(NULL, COINIT_MULTITHREADED)))
{
//info(TEXT("CoInitializeEx failed, hr: 0x%08x"), hr);
return 1;
}
hr = CoCreateInstance(CLSID_ImagingFactory,
NULL,
CLSCTX_INPROC_SERVER,
IID_IImagingFactory,
(void**) &pImagingFactory);
if (FAILED(hr))
{
switch(hr)
{
case REGDB_E_CLASSNOTREG:
tszError = tszNotReg;
break;
case CLASS_E_NOAGGREGATION:
tszError = tszNoAgg;
break;
default:
tszError = tszUnknown;
break;
}
//info(TEXT("CoCreateInstance failed, hr: 0x%08x (%s)"), hr, tszError);
goto finish;
}
tszName = _tcstok(pszCmdLine, _T("test.bmp"));
//tszOutput = _tcstok(NULL, _T("out.gif"));
tszMime = _tcstok(NULL, _T(" "));
wsprintf(tszOutput, _T("out.txt"));
//GetCodecCLSID(pImagingFactory, NULL, NULL, eEncoder);
if(1)
{
if (FAILED(hr = CreateStreamOnFile(TEXT("\\pic\\MM_PB_ANI.GIF"), &pStream)))
{
//wsprintf(msg_buf, TEXT("%s %d"), __FILE__, __LINE__);
//MessageBox(NULL,msg_buf,NULL,MB_OK);
//info(TEXT("CreateStreamOnFile failed, hr: 0x%08x"), hr);
goto finish;
}
if (FAILED(hr = pImagingFactory->CreateImageDecoder(pStream, DecoderInitFlagBuiltIn1st, &pImageDecoder)))
{
//wsprintf(msg_buf, TEXT("%s %d"), __FILE__, __LINE__);
//MessageBox(NULL,msg_buf,NULL,MB_OK);
//info(TEXT("CreateImageDecoder failed, hr: 0x%08x"), hr);
goto finish;
}
if (FAILED(hr = pImageDecoder->GetFrameCount(&FrameDimensionTime, &count)))
{
//info(TEXT("GetFrameCount failed, hr: 0x%08x"), hr);
return 0;
}
//memset(msg_buf,0,255);
//wsprintf(msg_buf, TEXT("%s %d"), __FILE__, count);
//CString steCount;
//strCount.Format(L"%d",count);
//MessageBox(NULL,msg_buf,NULL,MB_OK);
pImageDecoder->GetImageInfo(&ii);
if (FAILED(hr = pImagingFactory->CreateNewBitmap(ii.Width, ii.Height, ii.PixelFormat, &pBitmapImage)))
{
info(TEXT("CreateNewBitmap failed, hr = 0x%08x"), hr);
goto finish;
}
if (FAILED(hr = pBitmapImage->QueryInterface(IID_IImageSink, (void**)&pImageSink)))
{
info(TEXT("QueryInterface for ImageSink from BitmapImage failed, hr: 0x%08x"), hr);
goto finish;
}
if (FAILED(hr = pBitmapImage->QueryInterface(IID_IImage, (void**)&g_pImage)))
{
info(TEXT("QueryInterface for Image from BitmapImage failed, hr: 0x%08x"), hr);
goto finish;
}
//////////////////////////////////////////////////////////////////////////////////////////
//MessageBox(NULL,TEXT("1"),NULL,MB_OK);
if (FAILED(hr = pImageDecoder->SelectActiveFrame(&FrameDimensionTime, count-1)))
{
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;
}
}
else
{
//UserCodecTest(pImagingFactory);
hr = pImagingFactory->CreateImageFromFile(TEXT("WALL02.gif"), &g_pImage);
if (FAILED(hr) || g_pImage == NULL)
{
//info(TEXT("CreateImageFromFile failed, hr = 0x%08x"), hr);
return 0;
}
g_pImage->GetImageInfo(&ii);
}
g_pImagingFactory = pImagingFactory;
g_tszFilename = tszName;
OpenWindow(ii.Width, ii.Height, tszName);
//info(TEXT("Create file 1"));
uiTimer1=SetTimer(g_hWnd,100,800,NULL);
while (GetMessage(&msg, NULL, 0, 0)) {
TranslateMessage(&msg);
DispatchMessage(&msg);
}
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();
return 0;
}
BOOL OpenWindow(DWORD dwWidth, DWORD dwHeight, const TCHAR * tszName)
{
WNDCLASS wc;
TCHAR tszClassName[] = TEXT("ImagingT");
int x, y, width, height;
wc.style = 0;
wc.lpfnWndProc = (WNDPROC) WndProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = (HINSTANCE) g_hInst;
wc.hIcon = 0;
wc.hCursor = 0;
wc.hbrBackground = (HBRUSH) GetStockObject(WHITE_BRUSH);
wc.lpszMenuName = 0;
wc.lpszClassName = tszClassName;
RegisterClass(&wc);
RECT rcWorkArea;
if (!SystemParametersInfo(SPI_GETWORKAREA, 0, &rcWorkArea, 0))
{
SetRect(&rcWorkArea, 0, 0, GetSystemMetrics(SM_CXVIRTUALSCREEN), GetSystemMetrics(SM_CYVIRTUALSCREEN));
}
x = 0;
y = 50;
width = dwWidth + 2 * GetSystemMetrics(SM_CXBORDER);
height = dwHeight + 2 * GetSystemMetrics(SM_CYBORDER) + GetSystemMetrics(SM_CYCAPTION);
g_hWnd = CreateWindowEx(
0,
tszClassName,
tszName,
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -