|
|
| Original | Cropped to center 63% of original |
In the first example function below, the variable pct represents the relative size of the new cropped image as a percent of the original, in this case, 63%. The dimensions of the new image are calculated and used to allocate a new image of the correct size. The starting x and y positions (stx and sty) in the original image are set and the copyimage function takes the central area and copies it into the new image. The original image is replaced by the smaller cropped version.
In the second example function, the area of interest is saved to an image file without altering the original image or creating a new one.
Declare Function allocimage Lib "VIC32.DLL" (image As imgdes, ByVal wid As Long, ByVal leng As Long, ByVal bppixel As Long) As Long Declare Function copyimage Lib "VIC32.DLL" (srcimg As imgdes, desimg As imgdes) As Long Declare Sub copyimgdes Lib "VIC32.DLL" (srcimg As imgdes, desimg As imgdes) Declare Sub freeimage Lib "VIC32.DLL" (image As imgdes) Declare Sub RtlMoveMemory Lib "kernel32" (ByVal des As Long, ByVal src As Long, ByVal cnt As Long) Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (des As Any, ByVal src As Long, ByVal cnt As Long) ' Get BITMAPINFOHEADER from an image descriptor Private Sub getbmhdata(newBmh As BITMAPINFOHEADER, image As imgdes) CopyMemory newBmh, image.bmh, 40 ' 40=size of BITMAPINFOHEADER End Sub Public Function crop(image1 As imgdes) As Long Dim timage As imgdes Dim bmh As BITMAPINFOHEADER Dim dx1, dy1, dx2, dy2, rcode, pct As Long pct = 63 ' Crop to 63% percent of original size getbmhdata bmh, image1 ' Allocate space for the new image dx1 = image1.endx - image1.stx + 1 dy1 = image1.endy - image1.sty + 1 dx2 = Int(dx1 * pct / 100) dy2 = Int(dy1 * pct / 100) rcode = allocimage(timage, dx2, dy2, bmh.biBitCount) If (rcode = NO_ERROR) Then image1.stx = (dx1 - dx2) / 2 image1.sty = (dy1 - dy2) / 2 image1.endx = image1.stx + dx2 - 1 image1.endy = image1.sty + dy2 - 1 ' Copy image area into timage rcode = copyimage(image1, timage) If (rcode = NO_ERROR) Then ' Success, free source image freeimage image1 ' Assign timage to image1 copyimgdes timage, image1 Else ' Error in copying image, release timage memory freeimage timage End If End If crop = rcode End FunctionIf you merely want to save a small portion of an image then you don't need an entire function, just set the image area to the region of interest and call the desired savefile function. For example to save the image excluding a 10-pixel border around the edge:
image1.stx = image1.stx + 10 image1.endx = image1.endx - 10 image1.sty = image1.sty + 10 image1.endy = image1.endy - 10 savetif(filename, image1, 0)
int crop(imgdes *image1)
{
imgdes timage;
int dx1, dy1, dx2, dy2, rcode, pct = 63; // 63% percent of original size
// Allocate space for the new DIB
dx1 = image1->endx - image1->stx + 1;
dy1 = image1->endy - image1->sty + 1;
dx2 = (int)((long)(dx1) * pct / 100);
dy2 = (int)((long)(dy1) * pct / 100);
if((rcode = allocimage(&timage, dx2, dy2, image1->bmh->biBitCount)) == NO_ERROR) {
image1->stx = (dx1-dx2)/2;
image1->sty = (dy1-dy2)/2;
image1->endx = image1->stx + dx2 - 1;
image1->endy = image1->sty + dy2 - 1;
// Copy image area into timage
if((rcode = copyimage(image1, &timage)) == NO_ERROR) {
// Success, free source image
freeimage(image1);
// Assign timage to image1
copyimgdes(&timage, image1);
}
else // Error in resizing image, release timage memory
freeimage(&timage);
}
return(rcode);
}
int crop_and_save(imgdes *image1)
{
imgdes timage;
int dx1, dy1, dx2, dy2, rcode, pct = 63; // 63% percent of original size
int stx, sty, endy, endy;
// Calculate size of area to save
dx1 = image1->endx - image1->stx + 1;
dy1 = image1->endy - image1->sty + 1;
dx2 = (int)((long)(dx1) * pct / 100);
dy2 = (int)((long)(dy1) * pct / 100);
// Save coordinates of original area of interest
stx = image1->stx;
sty = image1->sty;
endx = image1->endx;
endy = image1->endy;
// Set area of interest to center 63% of image
image1->stx = (dx1-dx2)/2;
image1->sty = (dy1-dy2)/2;
image1->endx = image1->stx + dx2 - 1;
image1->endy = image1->sty + dy2 - 1;
// Save area of interest in TIFF image file
rcode = savetif("test.tif", image1, 0);
// Restore area of interest
image1->stx = stx;
image1->sty = sty;
image1->endx = endx;
image1->endy = endy;
return(rcode);
}
Victor Image Processing Library homepage |
Victor Sample Code