Victor Image Processing Library How-to Tips

Enlarge or Reduce an Image

To enlarge or reduce an image first allocate memory for the new image then call the resizeex function to create the new image. After successful creation of the new image discard the original and replace it with the new one.

Original Reduced to 83% of original


In the examples below, the variable pct represents the relative size of the new image as a percent of the original, in this case, 83%. The dimensions of the new image are calculated and used to allocate a new image of the correct size.

The resizeex function takes the source image and resizes it into the result image. The new pixel values can be calculated by pixel replication or interpolation. Replication is faster, but interpolation creates better quality images without blockiness.

   rcode = resizeex(srcimg, resimg, mode)

   srcimg is the source image descriptor
   resimg is the result image descriptor
   mode is the pixel calculation mode (0=pixel replication, 1=interpolation)

Also, the resizeex function gives you the ability to change the horizontal and vertical dimensions independently -- they don't have to be equal. The result image is sized to fit the dimensions of the result image area.


VB Source Code | C/C++ Source Code | Java Source Code


Enlarge or Reduce - the Visual Basic Source Code

Requires Victor Image Processing Library for 32-bit Windows v 5.3 or higher.
' Data Type Definitions ........................................... 
' Image descriptor
Type imgdes
   ibuff As Long
   stx As Long
   sty As Long
   endx As Long
   endy As Long
   buffwidth As Long
   palette As Long
   colors As Long
   imgtype As Long
   bmh As Long
   hBitmap As Long
End Type

Type BITMAPINFOHEADER
    biSize As Long
    biWidth As Long
    biHeight As Long
    biPlanes As Integer
    biBitCount As Integer
    biCompression As Long
    biSizeImage As Long
    biXPelsPerMeter As Long
    biYPelsPerMeter As Long
    biClrUsed As Long
    biClrImportant As Long
End Type

' Function Declarations ........................................... 
Declare Function allocimage Lib "VIC32.DLL" (image As imgdes, ByVal wid As Long, ByVal leng As Long, ByVal BPPixel As Long) As Long
Declare Sub copyimgdes Lib "VIC32.DLL" (srcimg As imgdes, desimg As imgdes)
Declare Sub freeimage Lib "VIC32.DLL" (image As imgdes)
Declare Function resizeex Lib "VIC32.DLL" (srcimg As imgdes, resimg As imgdes, ByVal mode As Long) As Long

' The Function ........................................... 
Public Function enlarge_or_reduce(ByRef image1 As imgdes) As Long
Dim timage As imgdes
Dim dx As Integer
Dim dy As Integer
Dim rcode As Integer
Dim pct As Integer
Dim bmh1 As BITMAPINFOHEADER

    pct = 83    '83% percent of original size 

   ' Calculate the width and length of the new image 
   dx = (image1.endx - image1.stx + 1) * pct / 100
   dy = (image1.endy - image1.sty + 1) * pct / 100
   
   ' Get the bitmapinfoheader data for the image, contains the pixel depth as biBitCount
   getbmhfromimage bmh1, image1     ' gethmhfromimage is below
   
   ' Allocate space for the new image
   rcode = allocimage(timage, dx, dy, bmh1.biBitCount)
   
   If (rcode = NO_ERROR) Then
        rcode = resizeex(image1, timage, 1)
        If (rcode = NO_ERROR) Then
            freeimage image1
            copyimgdes timage, image1
        Else
            freeimage timage
        End If
   
   End If
    enlarge_or_reduce = rcode
End Function

' Function Declarations for Helper Function ........................................... 

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)


' Helper Function ........................................... 
' Get BITMAPINFOHEADER from an image descriptor 
Public Sub getbmhfromimage(newBmh As BITMAPINFOHEADER, image As imgdes)
    CopyMemory newBmh, image.bmh, 40   ' 40=size of BITMAPINFOHEADER
End Sub


Enlarge or Reduce - the C Source Code

Requires Victor Image Processing Library for 32-bit Windows v 5.3 or higher.
int enlarge_or_reduce(imgdes *image1)
{
   imgdes timage;
   int dx, dy, rcode, pct = 83; // 83% percent of original size

   // Allocate space for the new image
   dx = (int)(((long)(image1->endx - image1->stx + 1)) * pct / 100);
   dy = (int)(((long)(image1->endy - image1->sty + 1)) * pct / 100);
   if((rcode = allocimage(&timage, dx, dy,
      image1->bmh->biBitCount)) == NO_ERROR) {
      // Resize Image into timage
      if((rcode = resizeex(image1, &timage, 1)) == 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);
}

This example resizes an image area and replaces the original image with the new image.


Copyright © 1997-2002 Catenary Systems Inc. All rights reserved. Victor Image Processing Library is a trademark of Catenary Systems.


Victor Image Processing Library homepage | Victor Sample Code