Example: Simple Segmentation Filter
The following code example shows how a simple segmentation filter can be implemented. The segmentation filter in the example does not use the WIA_IPS_DESKEW_X and WIA_IPS_DESKEW_Y properties. For clarity, error checking code has been omitted.
typedef struct _SUB_RECT
{
LONG xpos;
LONG ypos;
LONG width;
LONG height;
} SUB_RECT;
STDMETHODIMP
SegFilter::DetectRegions(IN IStream *pInputStream,
IN IWiaItem2 *pWiaItem2)
{
HRESULT hr = S_OK;
SUB_RECT pSubRegions = NULL;
ULONG cRegionsFound = 0;
LONG parent_xpos, parent_ypos;
GUID formatGUID = {0};
LONG lItemFlags = WiaItemTypeGenerated |
WiaItemTypeTransfer |
WiaItemTypeImage |
WiaItemTypeFile |
WiaItemTypeProgrammableDataSource;
LONG lCreationFlags = COPY_PARENTS_PROPERTY_VALUES;
ReadPropertyGUID(pWiaItem2, WIA_IPA_FORMAT, &formatGUID);
//
// The algorithm that performs the actual region
// detection has been omitted for clarity.
//
FindSubRegions(pInputStream,
formatGUID,
&pSubRegions,
&cRegionsFound);
ReadPropertyLong(pWiaItem2, WIA_IPS_XPOS, &parent_xpos);
ReadPropertyLong(pWiaItem2, WIA_IPS_YPOS, &parent_ypos);
//
// For each subimage that was found, create
// a child item under pWiaItem2 into which
// the coordinates for the subimage are set.
//
for (int i = 0; i < cRegionsFound; i++)
{
BSTR bstrChildName = NULL;
BSTR bstrFullChildName = NULL;
IWiaItem2 *pChildIWiaItem = NULL;
GetNamesForChild(i,
&bstrChildName,
&bstrFullChildName);
pWiaItem2->CreateChildItem(lItemFlags,
lCreationFlags,
bstrChildName,
&pChildIWiaItem);
WritePropertyLong(pChildWiaItem,
WIA_IPS_XPOS,
parent_xpos + pSubRegions[i].xpos);
WritePropertyLong(pChildWiaItem,
WIA_IPS_YPOS,
parent_ypos + pSubRegions[i].ypos);
WritePropertyLong(pChildWiaItem,
WIA_IPS_XEXTENT,
pSubRegions[i].width);
WritePropertyLong(pChildWiaItem,
WIA_IPS_YEXTENT,
pSubRegions[i].height);
pChildIWiaItem->Release();
SysFreeString(bstrChildName);
SysFreeString(bstrFullChildName);
}
if (pSubRegions)
{
free(pSubRegions);
}
return hr;
}