Problem with gflResize

Discussions on GFL SDK, the graphic library for reading and writing graphic files

Moderators: helmut, XnTriq, xnview

Post Reply
jules

Problem with gflResize

Post by jules »

Hi,
DOes anybody know what's wrong in my code ???
->sometimes, the gflResize bug without returning the GflError, just exit the application.
->this app is used to resize pictures from a folder.

Here is the code:

Code: Select all

If Len(src.text) = 0 Or Len(dest.text) = 0 Then Exit Sub
    Dim GflBitmap As GFL_BITMAP
    Dim GflTempBitmap As GFL_BITMAP, whiteBitmap As GFL_BITMAP
    Dim GflLoadParams As GFL_LOAD_PARAMS
    Dim GflSaveParams As GFL_SAVE_PARAMS
    Dim GflInfo As GFL_FILE_INFORMATION
    Dim GflColor As GFL_COLOR
    Dim PtrBitmap As Long, ptrTempBitmap As Long, whitePtr As Long
    Dim gflError As GFL_ERROR
    Dim Error As Integer
    Dim XOffset As Double, YOffset As Double
    Dim File As String, FileDest As String
    Dim gflRect As GFL_RECT, gflTempRect As GFL_RECT
    
    Dim Color As GFL_COLOR
    Dim NewWidth As Long
    Dim NewHeight As Long
    
    Dim QualiteResize As Long
    Dim CompressionJPEG As Integer
    Dim ratio As Double
    
    gflLibraryInit 'Initialize the library
    gflGetDefaultLoadParams GflLoadParams 'Gets default parameters for loading
    gflEnableLZW True 'Activate the LZW compression
    
    With GflLoadParams
        .Flags = GFL_LOAD_SKIP_ALPHA Or GFL_LOAD_BY_EXTENSION_ONLY
        .Origin = GFL_BOTTOM_LEFT 'Origin is bottom-left
        .ColorModel = GFL_BGR 'Component order like DIB
        .LinePadding = 4 'Line padding on 4 bytes (32bits)
    End With
    
    gflGetDefaultSaveParams GflSaveParams
    With GflSaveParams
        .Flags = GFL_SAVE_REPLACE_EXTENSION
        ' prend les infos du format jpg
        .FormatIndex = gflGetFormatIndexByName("jpeg")
        .Quality = 100
        .Progressive = True
    End With
    
    gflRect.x = 0
    gflRect.y = 0
    gflRect.h = 150
    gflRect.w = 150
    gflTempRect.x = 0
    gflTempRect.y = 0
    ' Chargement de l'image
    GflColor.Red = 255
    GflColor.Blue = 255
    GflColor.Green = 255
    ptrTempBitmap = gflAllockBitmap(GFL_BGR, 150, 150, 4, GflColor)
    
    
    Error = gflLoadBitmap(App.Path & "\white.bmp", whitePtr, GflLoadParams, GflInfo)
    Error = Error + gflLoadBitmap(App.Path & "\white.bmp", ptrTempBitmap, GflLoadParams, GflInfo)
    If Error = GFL_NO_ERROR Then
        extGetGflBitmapFromPtr whitePtr, whiteBitmap
        extGetGflBitmapFromPtr ptrTempBitmap, GflTempBitmap
    Else
        Exit Sub
    End If
    File1.Path = Dir1.Path
    lbCount.Caption = "/" & File1.ListCount
    Timer1.Enabled = True
For i = 0 To File1.ListCount - 1
    lbCurrent.Caption = i + 1
    PGbar.Value = (i / (File1.ListCount - 1)) * 100
    redimImg.Refresh
    File = src & "\" & File1.List(i)
    FileDest = dest & "\" & File1.List(i)
    Error = gflLoadBitmap(File, PtrBitmap, GflLoadParams, GflInfo)
    If Error = GFL_NO_ERROR Then
        extGetGflBitmapFromPtr PtrBitmap, GflBitmap
        ratio = 1
        gflBitblt whiteBitmap, gflRect, GflTempBitmap, 0, 0
        ' Changement si image en mode portrait
        XOffset = 0
        YOffset = 0
        If GflBitmap.Height > GflBitmap.Width Then
            ratio = 150 / GflBitmap.Height
            NewHeight = GflBitmap.Height * ratio
            NewWidth = GflBitmap.Width * ratio
            XOffset = CDbl(150 - NewWidth) / 2#
        Else
            ratio = 150 / GflBitmap.Width
            NewHeight = GflBitmap.Height * ratio
            NewWidth = GflBitmap.Width * ratio
            YOffset = CDbl(150 - NewHeight) / 2#
        End If
        gflTempRect.w = NewWidth
        gflTempRect.h = NewHeight
        ' ---------- on resize l'image ----------------------------
        
        [b]Error = gflResize(GflBitmap, PtrBitmap, NewWidth, NewHeight, GFL_RESIZE_BILINEAR, 0)[/b]
        'Error = gflResizeCanvas(GflBitmap, PtrBitmap, NewWidth, NewHeight, 0, GflColor)
        If Error = GFL_NO_ERROR Then 'Si aucune erreur
            extGetGflBitmapFromPtr PtrBitmap, GflBitmap 'Get the picture data
            gflBitblt GflBitmap, gflTempRect, GflTempBitmap, CInt(XOffset), CInt(YOffset)
            Error = GFL_NO_ERROR
            If Error = GFL_NO_ERROR Then 'Si aucune erreur
                Error = gflSaveBitmap(FileDest, GflTempBitmap, GflSaveParams)
                gflFreeBitmap GflBitmap 'Libère la mémoire
            End If
        End If
    End If
Next i
Timer1.Enabled = False
MsgBox "terminé", vbOKOnly, "Redimensionner"
PGbar.Value = 0
Fin:
    If Error <> GFL_NO_ERROR Then MsgBox extGetStr(gflGetErrorString(Error)), vbOKOnly, "Erreur"
    gflLibraryExit
jules

I found why, sorry

Post by jules »

Just because I didn't need to free (gflFree...) the bitmap each time.
MaierMan
Posts: 78
Joined: Wed Aug 04, 2004 8:32 pm
Contact:

Post by MaierMan »

When keeping all those Images "alive" you might soon run out of memory ;)
An 1000x1000 24bit (RGB) image would require:
1000 x 1000 x 3 = 3000000 bytes ~ 2,9 MB (just the Data alone).
Even in "managed code" applications like C#, VB or java will produce memory leakage is fairly easily possible, especially when working with warpers to "native" stuff :p
Post Reply