Victor Image Processing Library How-to Tips

Increase the Canvas Size of an Image

To increase the canvas size is to increase the area surrounding the image. Copy the image into the center of a new larger image.

Gold metallic leaf Larger canvas
Original Canvas enlarged to 159% of original

In the example functions below, the variable pct represents the relative size of the new image canvas as a percent of the original, in this case, 159%. The dimensions are calculated and used to allocate a new image of the correct size. The starting x and y positions (stx and sty) in the new image are set and the copyimage function takes the central area and copies it into the new image. The Visual Basic example loads the original file and saves the new image into a new file. In the C example the original image is replaced by the new one.

VB Source Code | C Source Code

Enlarge the Canvas - the VB Source Code

Requires Victor Image Processing Library for 32-bit Windows v 5 or higher.
Private Sub mnuloadandenlargecanvas_Click()
' Load a JPEG image, enlarge the canvas 159% percent
   Dim srcimage As imgdes
   Dim desimage As imgdes
   Dim dx1, dy1, dx2, dy2, rcode, pct As Long
   Dim jdat As JpegData
   rcode = jpeginfo("goldcrop.jpg", jdat)
   If (rcode = NO_ERROR) Then
      rcode = allocimage(srcimage, jdat.width, jdat.length, jdat.vbitcount)
      rcode = loadjpg("goldcrop.jpg", srcimage)
   End If
   If (rcode = NO_ERROR) Then
      pct = 159  ' 159% percent of original size
            ' Allocate space for the new image
      dx1 = srcimage.endx - srcimage.stx + 1 ' Could also use jdat.width
      dy1 = srcimage.endy - srcimage.sty + 1 ' Could also use jdat.length
      dx2 = dx1 * pct / 100
      dy2 = dy1 * pct / 100
      rcode = allocimage(desimage, dx2, dy2, jdat.vbitcount)
      If (rcode = NO_ERROR) Then
         rcode = zeroimage(255, desimage)  ' Set background to white
            ' Copy image into center of desimage
         desimage.stx = (dx2 - dx1) / 2
         desimage.sty = (dy2 - dy1) / 2
         rcode = copyimage(srcimage, desimage)
         If (rcode = NO_ERROR) Then
            ' Reset starting x,y position in desimage
            desimage.stx = 0
            desimage.sty = 0
            ' Save the larger image to a new file
            rcode = savejpg("larger.jpg", desimage, 75)
         End If
         freeimage desimage
      End If
      freeimage srcimage
   End If
End Sub
........... Add these defines and declarations to your Global module ...........
' 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 JpegData
    ftype As Long
    width As Long
    length As Long
    comps As Long
    precision As Long
    sampfac0 As Long
    sampfac1 As Long
    sampfac2 As Long
    sampfac3 As Long
    vbitcount As Long
End Type
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 freeimage Lib "VIC32.DLL" (image As imgdes)
Declare Function jpeginfo Lib "VIC32.DLL" (ByVal Fname As String, jdat As JpegData) As Long
Declare Function loadjpg Lib "VIC32.DLL" (ByVal Fname As String, desimg As imgdes) As Long
Declare Function savejpg Lib "VIC32.DLL" (ByVal Fname As String, srcimg As imgdes, ByVal quality As Long) As Long
Declare Function zeroimage Lib "VIC32.DLL" (ByVal level As Long, image As imgdes) As Long

Enlarge the Canvas - the C Source Code

Requires Victor Image Processing Library for Windows v 4.1x or higher.
int enlarge_canvas(imgdes *image1)
   imgdes timage;
   int dx1, dy1, dx2, dy2, rcode, pct = 159; // 159% 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) {
      zeroimage(255, &timage); // Set background to white
      timage.stx = (dx2-dx1)/2;
      timage.sty = (dy2-dy1)/2;
            // Copy image area into timage
      if((rcode = copyimage(image1, &timage)) == NO_ERROR) {
            // Success, free source image
            // Reset starting x,y position
         timage.stx = 0; timage.sty = 0;
            // Assign timage to image1
         copyimgdes(&timage, image1);
      else // Error in resizing image, release timage memory
Victor Image Processing Library

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

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