www.pudn.com > QRAPPuie.rar > myimage.cpp
//#include "stdafx.h"
#define INITGUID
#include "myimage.h"
#undef INITGUID
HINSTANCE g_hInst;
HWND g_hWnd;
IImage* g_pImage;
//extern IImagingFactory * g_pImagingFactory;
//extern TCHAR * g_tszFilename;
int myimage(TCHAR *tszName, TCHAR* tszOutput,TCHAR* tszMime)
{
IImagingFactory *pImagingFactory = NULL;
IImageSink *pImageSink = NULL;
IImage *pImage = NULL;
IImageDecoder *pImageDecoder = NULL; // pImageDecoder A pointer to the resulting IImageDecoder interface pointer
IImageEncoder *pImageEncoder = NULL, *pIETemp = NULL; //A pointer to the resulting IImageEncoder interface pointer.
IBitmapImage *pBitmapImage = NULL;
IStream *pStream = NULL; //IStream:IUnknown interface for the stream of input data.
HRESULT hr;
HINSTANCE hiImaging = NULL;
ImageInfo ii;
// MSG msg;
TCHAR *tszError;
// TCHAR *tszName, *tszOutput, *tszMime;
TCHAR tszNotReg[] = TEXT("REGDB_E_CLASSNOTREG");
TCHAR tszNoAgg[] = TEXT("CLASS_E_NOAGGREGATION");
TCHAR tszUnknown[] = TEXT("unknown hr");
CLSID clsidEncoder;
// 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(" "));
/// tszOutput = _tcstok(NULL, _T(" "));
/// tszMime = _tcstok(NULL, _T(" "));
/* tszName = _T("\\storage card\\bmpopen\\test000.jpg\0");
tszOutput = _T("\\storage card\\bmpopen\\mymap.bmp");
tszMime = _T("image/bmp");
*/
GetCodecCLSID(pImagingFactory, NULL, NULL, eEncoder); ////////////??????
// info(TEXT("Opening %s"), tszName);
if (tszOutput)
{
if (FAILED(hr = CreateStreamOnFile(tszName, &pStream)))
{
// info(TEXT("CreateStreamOnFile failed, hr: 0x%08x"), hr);
goto finish;
}
// info(TEXT("Finding encoder for Mime Type %s"), tszMime);
GetCodecCLSID(pImagingFactory, NULL, NULL, eDecoder); ///??
if (!GetCodecCLSID(pImagingFactory, &clsidEncoder, tszMime, eEncoder))
{
// info(TEXT("GetCodecCLSID failed"));
goto finish;
}
// info(TEXT("Opening output file %s"), tszOutput);
if (FAILED(hr = pImagingFactory->CreateImageEncoderToFile(&clsidEncoder, tszOutput, &pImageEncoder)))//creates and initializes an IImageEncoder interface to output to a file in a specified image format.
{
// info(TEXT("CreateImageEncoderToFile failed, hr: 0x%08x"), hr);
goto finish;
}
if (FAILED(hr = pImagingFactory->CreateImageDecoder(pStream, DecoderInitFlagBuiltIn1st, &pImageDecoder)))// creates and initializes an IImageDecoder interface you can use to process the specified input data stream
{
// info(TEXT("CreateImageDecoder failed, hr: 0x%08x"), hr);
goto finish;
}
// DisplayEncoder(pImageEncoder);
if (FAILED(hr = pImageEncoder->GetEncodeSink(&pImageSink)))// return an IImageSink interface that can be used to encode the next image frame
{
// info(TEXT("GetEncodeSink failed, hr: 0x%08x"), hr);
goto finish;
}
// We now have an encoder and a decoder.
if (FAILED(hr = pImageDecoder->BeginDecode(pImageSink, NULL)))//start decoding the currently selected frame ,pImageSink=>A pointer to an IImageSink interface that receives the image data that results from decoding the frame.
{
// info(TEXT("BeginDecode failed, hr: 0x%08x"), hr);
goto finish;
}
for(;;)
{
// info(TEXT("Decoding . . ."));
hr = pImageDecoder->Decode(); //Decode() method tells the decoder object to continue decoding the current frame.
if (E_PENDING == hr)
{
// info(TEXT("E_PENDING returned, sleeping"));
Sleep(500);
}
else if (FAILED(hr))
{
// info(TEXT("Decode failed, hr = 0x%08x"), hr);
pImageDecoder->EndDecode(hr);
goto finish;
}
else
{
// info(TEXT("Decode successful"));
break;
}
}
pImageDecoder->EndDecode(hr);
pImageSink->Release();
pImageSink = NULL;
pImageEncoder->TerminateEncoder();
if (FAILED(hr = pImageDecoder->QueryInterface(IID_IImageEncoder, (void**)&pIETemp)))
{
// info(TEXT("Could not query encoder interface from the decoder"));
} else
{
// info(TEXT("Successfully queried encoder from the decoder"));
pIETemp->Release();
pIETemp = NULL;
}
pImageDecoder->GetImageInfo(&ii);
if (FAILED(hr = pImagingFactory->CreateNewBitmap(ii.Width, ii.Height,ii.PixelFormat, &pBitmapImage)))
{
// info(TEXT("CreateNewBitmap failed, hr = 0x%08x"), hr); ///eda PixelFormat1bppIndexed
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;
}
if (FAILED(hr = pImageDecoder->BeginDecode(pImageSink, NULL)))
{
// info(TEXT("BeginDecode into Bitmap Image failed, hr = 0x%08d"), hr);
goto finish;
}
while (E_PENDING == (hr = pImageDecoder->Decode()))
Sleep(0);
hr = pImageDecoder->EndDecode(hr);
if (FAILED(hr))
{
// info(TEXT("Decoding failed, hr = 0x%08x"), hr);
goto finish;
}
pImageSink->Release();
pImageSink = NULL;
}
else
{
UserCodecTest(pImagingFactory);
hr = pImagingFactory->CreateImageFromFile(tszName, &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); ////////////////
/* 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;
}
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;
ImageCodecInfo * codecs;
HRESULT hr;
BOOL fRet = FALSE;
TCHAR * tszCodec;
if (eEncoder == ctCodec)
{
hr = pImagingFactory->GetInstalledEncoders(&uiCount, &codecs);
tszCodec = TEXT("Encoder");
}
else
{
hr = pImagingFactory->GetInstalledDecoders(&uiCount, &codecs);
tszCodec = TEXT("Decoder");
}
if (FAILED(hr))
{
// info(TEXT("GetInstalled%ss returned 0x%08x"), tszCodec, hr);
return FALSE;
}
for (UINT 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;
}
}
CoTaskMemFree(codecs);
return fRet;
}
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);
}
/*
void info(const TCHAR* tszFormat, ...)
{
va_list va;
TCHAR tszInfo[1024];
va_start(va, tszFormat);
_vstprintf(tszInfo, tszFormat, va);
va_end(va);
OutputDebugString(tszInfo);
}*/