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# Source Code | C/C++ Source Code | Java Source Code

Enlarge or Reduce - the C# Source Code

Requires Victor Image Processing Library for 32-bit Windows v 6 or higher, using the viclib namespace.
 public int resizeex_enlarge_or_reduce(ref vicwin.imgdes image1)
 {
 vicwin.imgdes timage;
 int dx;
 int dy;
 int rcode;
 int pct;
 int bpp;         

 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;
 bpp = getbpp(ref image1);         
 
// Allocate space for the new DIB
 timage = new vicwin.imgdes();      
 rcode = vicwin.allocimage(ref timage, dx, dy, bpp);
 
 if (rcode == vicwin.NO_ERROR)
 {
      // Resize Image into timage
     rcode = vicwin.resizeex(ref image1, ref timage, 1);
     if (rcode == vicwin.NO_ERROR)
        {
         // Success, free source image
         vicwin.freeimage(ref image1);
         // Assign timage to image1
         vicwin.copyimgdes(ref timage, ref image1);
        }
     else   // Error in resizing image, release timage memory
        vicwin.freeimage(ref timage);
 }
return (rcode);
}

    //................. helper functions .............
  [DllImport("kernel32.dll",EntryPoint="RtlMoveMemory")] public static extern void RtlMoveMemory(int des, int src, int count);
  [DllImport("kernel32.dll",EntryPoint="RtlMoveMemory")] public static extern void Copybmh(ref vicwin.BITMAPINFOHEADER des, int src, int count);

  public static int getbpp(ref vicwin.imgdes timage )
  {
     vicwin.BITMAPINFOHEADER bmh = new vicwin.BITMAPINFOHEADER();

     Copybmh(ref bmh, timage.bmh, 40);
     return(bmh.biBitCount);
  }


Enlarge or Reduce - the Visual Basic Source Code

Requires Victor Image Processing Library for 32-bit Windows v 5.3 or higher.
' Add VICDEF32.BAS to your project 
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.


Victor Image Processing Library

Victor Image Processing Library homepage | Victor Product Summary | more source code


Copyright  2006 - 2008 Catenary Systems Inc. All rights reserved. Victor Image Processing Library is a trademark of Catenary Systems.