CropToEdges

#include "BIDocImg.H"

 

HANDLE CALLBACK CropToEdges( HANDLE      hDIB,
double            dThreshold,
LPDWORD  lpdwStartX,
int                  nBorder,
BOOL           bWhitePage,
BOOL           bMultiPage)

Description

This function will detect the edges of a scanned image, rotate the image to a precise position and crop the image to these edges. The input DIB must be monochrome, otherwise the function will return NULL. The most common case is that when a white scanned paper is surrounded by a black frame, thus the white page is on a black background. The rare case is that when a black scanned paper is surrounded by a white frame, thus the black page is on a white background. For images without a contrast frame the function does not work.

Parameters

HANDLE

hDIB

A monochrome, bottom-up DIB.

double

dThreshold

The ratio of black and white pixels in a row or column to detect edges. It must be between 0.0 and 1.0, otherwise the function will return NULL and set error code to PARAMETERERROR. For most of the images this value can be 0.7.

LPDWORD

lpdwStartX

The function will start the edge detection from that point from left to right. For multipage images you should set this value to 0, and the function will overwrite it with the new value when processing a page. See code example.

int

nBorder

The cropping border around the image. Positive value enlarges the image in all directions, negative value shrinks it in all directions.

BOOL

bWhitePage

TRUE, if a white page is on a black background. FALSE, if a black page is on a white background.

BOOL

bMultiPage

If TRUE, the function assumes that the input DIB is multipage. In this case the function will not rotate the image to precise position. Multiple images can be only in horizontal direction, thus in vertical direction only one image is allowed.

If FALSE, the function assumes that the input DIB contains a single page only.

Return values

The handle of the new, cropped DIB or NULL if an error occurred. All DIB parameters (except the size of the image) will be the same as in the input DIB (for example: biXPelsPerMeterwill not change).

Programming notes

The function will not free the input DIB.

Requirements

Header :     Declared in BIDocImg.h; include BIDocImg.h.

Library :    Use BIDocImg.lib.

DLLs :       BIDocImg.dll, BiDIB.dll.

Code example

DWORD dwStartX = 0;

HANDLE hDib = LoadTiffIntoDIB("d:\\Crop\\0001.tif", 0, FALSE);

HANDLE hCrp = CropToEdges(hDib, 0.25, &dwStartX, 0, TRUE, TRUE);

SaveDIBInTiffFile("d:\\crop.tif", hCrp, TCOMP_CCITTG4, FALSE);

GlobalFree(hCrp);

 

while(hCrp)

{

    hCrp = CropToEdges(hDib, 0.7, &dwStartX, 0, TRUE, TRUE);

    SaveDIBInTiffFile("d:\\crop.tif", hCrp, TCOMP_CCITTG4, FALSE);

    GlobalFree(hCrp);

}

 

GlobalFree(hDib);