cCairoSurface: How can I load an image that throws an out of memory error?
Hello!
I am using Olaf's great PDFium Viewer.
I am trying to extract image objects, and it seems that one simply is too big for Cairo.
The line in question is
Code:
Set GetCairoSurfaceFromImgObject = Cairo.CreateSurface(dx, DY)
I have tested it using this code:
Code:
Dim n As cCairoSurface
Set n = Cairo.CreateSurface(2104, 3208)
How could I deal with the situation below?
Thank you!
Code:
Public Function GetCairoSurfaceFromImgObject() As cCairoSurface
If ObjType <> FPDF_PAGEOBJ_IMAGE Then
Exit Function
End If
Const FPDFBitmap_Gray = 1, FPDFBitmap_BGR = 2, FPDFBitmap_BGRx = 3, FPDFBitmap_BGRA = 4
Dim hBM As Long, Fmt&, x&, y&, dx&, DY&, Stride&, i&, pData As Long, bl() As Byte, b() As Byte
hBM = FPDFImageObj_GetBitmap(mPageObject)
If hBM = 0 Then
Debug.Print "couldn't create FPDF-Bitmap from PageObject"
Exit Function
End If
dx = FPDFBitmap_GetWidth(hBM)
DY = FPDFBitmap_GetHeight(hBM)
Stride = FPDFBitmap_GetStride(hBM)
pData = FPDFBitmap_GetBuffer(hBM)
Fmt = FPDFBitmap_GetFormat(hBM)
If pData = 0 Or dx <= 0 Or DY <= 0 Or Stride <= 0 Or Fmt = 0 Then
FPDFBitmap_Destroy hBM
Err.Raise vbObjectError, , "Unable to retrieve FPDF-Bitmap-Data"
End If
'
' Debug.Print "pdata: " & pData
' Debug.Print "stride: " & Stride
' Debug.Print "dx: " & dx
' Debug.Print "dy: " & DY
' Debug.Print "fmt: " & Fmt
'
' Dim n As cCairoSurface
' Set n = Cairo.CreateSurface(2104, 3208)
Set GetCairoSurfaceFromImgObject = Cairo.CreateSurface(dx, DY)
ReDim bl(0 To Stride - 1) 'allocate a line-buffer
GetCairoSurfaceFromImgObject.BindToArray b
For y = 0 To DY - 1
i = 0: New_c.MemCopy VarPtr(bl(0)), pData, Stride
Select Case Fmt
Case FPDFBitmap_Gray
For x = 0 To dx - 1
b(i, y) = bl(x): b(i + 1, y) = bl(x): b(i + 2, y) = bl(x): b(i + 3, y) = 255
i = i + 4
Next
Case FPDFBitmap_BGR
For x = 0 To dx * 3 - 1 Step 3
b(i, y) = bl(x): b(i + 1, y) = bl(x + 1): b(i + 2, y) = bl(x + 2): b(i + 3, y) = 255
i = i + 4
Next
Case FPDFBitmap_BGRx: New_c.MemCopy VarPtr(b(0, y)), pData, dx * 4
For x = 3 To dx * 4 Step 4: b(x, y) = 255: Next
Case FPDFBitmap_BGRA: New_c.MemCopy VarPtr(b(0, y)), pData, dx * 4
End Select
pData = pData + Stride
Next
GetCairoSurfaceFromImgObject.ReleaseArray b
Cairo.PreMultiplyAlpha GetCairoSurfaceFromImgObject.DataPtr, dx * DY * 4
FPDFBitmap_Destroy hBM
End Function
Re: cCairoSurface: How can I load an image that throws an out of memory error?
Edit: Sorry, I believe that I had some things in memory / not cleaned up. The project crashed, and now I set the cPdfiumViewer to nothing when I am done.
Since then, the error has not appeared anymore.