📄 wiahelpers.cpp
字号:
if(SUCCEEDED(hr))
{
hr = wiasGetContextFromName(pWiasContext,0,bstrFullItemName,ppWiasContext);
if(FAILED(hr))
{
WIAS_ERROR((g_hInst, "Failed to get the parent's application item from the item name (%ws), hr = 0x%lx",bstrFullItemName,hr));
}
SysFreeString(bstrFullItemName);
bstrFullItemName = NULL;
}
else
{
WIAS_ERROR((g_hInst, "Failed to get full item name from parent IWiaDrvItem, hr = 0x%lx",hr));
}
}
else
{
WIAS_ERROR((g_hInst, "Failed to get the WIA driver item from the application item, hr = 0x%lx",hr));
}
}
else
{
WIAS_ERROR((g_hInst, "Invalid parameters were passed"));
}
return hr;
}
/**
* This function converts a unit (in pixels) to
* another unit (1/1000ths of an inch).
*
* @param lPixelLength
* Unit length in pixels
* @param lResolution
* Resolution of Pixel unit length (in Dots Per Inch "DPI")
* @return
*/
LONG ConvertTo1000thsOfAnInch(
LONG lPixelLength,
LONG lResolution)
{
LONG lConvertedValue = 0;
if((lPixelLength)&&(lResolution))
{
lConvertedValue = (LONG)((((lPixelLength * 1000) + lResolution - 1) / lResolution));
}
else
{
WIAS_ERROR((g_hInst, "Invalid parameters were passed"));
}
return lConvertedValue;
}
/**
* This function populates a BITMAPINFOHEADER structure
* using data contained in a Gdiplus::BitmapData object.
* This function only works with 24-bit data.
*
* @param pGDIPlusBitmapData
* Pointer to a GDI+ BitmapData object
* @param pBitmapInfoHeader
* Pointer to a BITMAPINFOHEADER structure
* @return
*/
HRESULT GetBitmapHeaderFromBitmapData(
__in Gdiplus::BitmapData *pGDIPlusBitmapData,
__out BITMAPINFOHEADER *pBitmapInfoHeader)
{
HRESULT hr = E_INVALIDARG;
if((pGDIPlusBitmapData) && (pBitmapInfoHeader) && (pGDIPlusBitmapData->PixelFormat == PixelFormat24bppRGB))
{
memset(pBitmapInfoHeader, 0, sizeof(BITMAPINFOHEADER));
pBitmapInfoHeader->biSize = sizeof(BITMAPINFOHEADER);
pBitmapInfoHeader->biPlanes = 1;
pBitmapInfoHeader->biWidth = pGDIPlusBitmapData->Width;
pBitmapInfoHeader->biHeight = pGDIPlusBitmapData->Height;
// We cannot use the stride to calculate the size, because if there is no
// format conversion, we might get the original bits...
// We need to calculate the size based on the width
pBitmapInfoHeader->biSizeImage = ((((pGDIPlusBitmapData->Width * 3) + 3) & ~3) * pGDIPlusBitmapData->Height);
pBitmapInfoHeader->biBitCount = 24;
hr = S_OK;
}
else
{
WIAS_ERROR((g_hInst, "Invalid parameters were passed"));
}
return hr;
}
/**
* This function returns a Gdiplus::Rect structure
* initialized with the current WIA extent setting
* values. This function assumes that the WIA item
* passed in supports WIA_IPS_XPOS, WIA_IPS_YPOS,
* WIA_IPS_XEXTENT and WIA_IPS_YEXTENT properties.
*
* @param pWiasContext
* Pointer to the WIA item context
* @param pRect Pointer to a Gdiplus::Rect object
* @return
*/
HRESULT GetSelectionAreaRect(
__in BYTE* pWiasContext,
__out Gdiplus::Rect *pRect)
{
HRESULT hr = E_INVALIDARG;
LONG lXPos = 0;
LONG lYPos = 0;
LONG lXExtent = 0;
LONG lYExtent = 0;
if((pWiasContext)&&(pRect))
{
hr = wiasReadPropLong(pWiasContext,WIA_IPS_XPOS,&lXPos,NULL,TRUE);
if(SUCCEEDED(hr))
{
hr = wiasReadPropLong(pWiasContext,WIA_IPS_YPOS,&lYPos,NULL,TRUE);
if(FAILED(hr))
{
WIAS_ERROR((g_hInst, "Failed to read the WIA_IPS_YPOS property, hr = 0x%lx",hr));
}
}
else
{
WIAS_ERROR((g_hInst, "Failed to read the WIA_IPS_XPOS property, hr = 0x%lx",hr));
}
if(SUCCEEDED(hr))
{
hr = wiasReadPropLong(pWiasContext,WIA_IPS_YEXTENT,&lYExtent,NULL,TRUE);
if(FAILED(hr))
{
WIAS_ERROR((g_hInst, "Failed to read the WIA_IPS_YEXTENT property, hr = 0x%lx",hr));
}
}
if(SUCCEEDED(hr))
{
hr = wiasReadPropLong(pWiasContext,WIA_IPS_XEXTENT,&lXExtent,NULL,TRUE);
if(FAILED(hr))
{
WIAS_ERROR((g_hInst, "Failed to read the WIA_IPS_XEXTENT property, hr = 0x%lx",hr));
}
}
if(SUCCEEDED(hr))
{
Gdiplus::Rect rFrame((INT)lXPos,(INT)lYPos,(INT)lXExtent,(INT)lYExtent);
*pRect = rFrame;
hr = S_OK;
}
}
else
{
WIAS_ERROR((g_hInst, "Invalid parameters were passed"));
}
return hr;
}
/**
* This function locks down a portion of a bitmap using
* the current WIA extent setting values. This function assumes that the WIA item
* passed in supports WIA_IPS_XPOS, WIA_IPS_YPOS,
* WIA_IPS_XEXTENT and WIA_IPS_YEXTENT properties.
*
* @param pWiasContext
* Pointer to the WIA item context
* @param pBitmap Pointer to a Gdiplus::Bitmap object containing the
* bitmap data.
* @param pBitmapData
* Pointer to a Gdiplus::BitmapData object
* @param pbmih Pointer to a BITMAPINFOHEADER structure that will
* receive the information about the locked area of the
* bitmap.
* @param ppBitmapBits
* Pointer to the first scan line of data of the locked
* portion of the bitmap
* @return
*/
HRESULT LockSelectionAreaOnBitmap(
__in BYTE *pWiasContext,
__in Gdiplus::Bitmap *pBitmap,
__in Gdiplus::BitmapData *pBitmapData,
__in BITMAPINFOHEADER *pbmih,
__out BYTE **ppBitmapBits)
{
HRESULT hr = E_INVALIDARG;
if((pBitmapData)&&(pbmih)&&(ppBitmapBits))
{
Gdiplus::Rect rFrame(0,0,0,0);
hr = GetSelectionAreaRect(pWiasContext,&rFrame);
if(SUCCEEDED(hr))
{
if(pBitmap->LockBits(&rFrame,
ImageLockModeRead,
PixelFormat24bppRGB,
pBitmapData) == Ok)
{
hr = GetBitmapHeaderFromBitmapData(pBitmapData,pbmih);
if(SUCCEEDED(hr))
{
*ppBitmapBits = (BYTE*)pBitmapData->Scan0;
}
else
{
WIAS_ERROR((g_hInst, "Failed to get the BITMAPINFOHEADER information from the GDI+ bitmap data object, hr = 0x%lx",hr));
}
}
else
{
hr = E_FAIL;
WIAS_ERROR((g_hInst, "Failed to LockBits on GDI+ bitmap object, hr = 0x%lx",hr));
}
}
else
{
WIAS_ERROR((g_hInst, "Failed to get selection area rect from WIA extent settings properties, hr = 0x%lx",hr));
}
}
else
{
WIAS_ERROR((g_hInst, "Invalid parameters were passed"));
}
return hr;
}
/**
* This function unlocks a portion of a bitmap previously locked
* by LockSelectionAreaOnBitmap function.
*
* @param pBitmap Pointer to a Gdiplus::Bitmap object containing the
* bitmap data.
* @param pBitmapData
* Pointer to a Gdiplus::BitmapData object
*/
void UnlockSelectionAreaOnBitmap(
__in Gdiplus::Bitmap *pBitmap,
__in Gdiplus::BitmapData *pBitmapData)
{
if((pBitmap)&&(pBitmapData))
{
if(pBitmap->UnlockBits(pBitmapData) != Ok)
{
WIAS_ERROR((g_hInst, "Failed to UnlockBits on GDI+ bitmap object"));
}
}
else
{
WIAS_ERROR((g_hInst, "Invalid parameters were passed"));
}
}
/**
* This helper function attempts to grab a IWiaMiniDrvTransferCallback interface
* from a PMINIDRV_TRANSFER_CONTEXT structure.
*
* If successful, caller must Release.
*
* @param pmdtc The PMINIDRV_TRANSFER_CONTEXT handed in during drvAcquireItemData.
* @param ppIWiaMiniDrvTransferCallback
* Address of a interface pointer which receives the callback.
* @return HRESULT return value.
*/
HRESULT GetTransferCallback(
__in PMINIDRV_TRANSFER_CONTEXT pmdtc,
__callback IWiaMiniDrvTransferCallback **ppIWiaMiniDrvTransferCallback)
{
HRESULT hr = E_INVALIDARG;
if (pmdtc && ppIWiaMiniDrvTransferCallback)
{
if (pmdtc->pIWiaMiniDrvCallBack)
{
hr = pmdtc->pIWiaMiniDrvCallBack->QueryInterface(IID_IWiaMiniDrvTransferCallback,
(void**) ppIWiaMiniDrvTransferCallback);
}
else
{
hr = E_UNEXPECTED;
WIAS_ERROR((g_hInst, "A NULL pIWiaMiniDrvCallBack was passed in the MINIDRV_TRANSFER_CONTEXT structure, hr = 0x%lx",hr));
}
}
else
{
WIAS_ERROR((g_hInst, "Invalid parameters were passed"));
}
return hr;
}
/**
* This function allocates a buffer to be used during
* data transfers. FreeTransferBuffer should be called
* to free the memory allocated by this function.
*
* @param pWiasContext
* Pointer to the WIA item context
* @param ppBuffer Pointer to the allocated buffer. The caller should call
* FreeTransferBuffer() when finished with this buffer.
* @param pulBufferSize
* Size of the buffer allocated.
* @return
*/
HRESULT AllocateTransferBuffer(
__in BYTE *pWiasContext,
__out BYTE **ppBuffer,
__in ULONG *pulBufferSize)
{
HRESULT hr = S_OK;
if (pWiasContext && ppBuffer && pulBufferSize)
{
//
// Set the buffer size to DEFAULT_BUFFER_SIZE
//
*pulBufferSize = DEFAULT_BUFFER_SIZE;
// Allocate the memory
*ppBuffer = (B
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -