Useful for when dealing with images that you don't have direct access to the file, say like fetching them from a serial connection, and being able to load them into a graphic control without having to save the image to disk first.
Code:
' memory load and save wrappers for FreeImage FUNCTION FreeImage_LoadDIBFromData(dwData AS DWORD, dwLength AS DWORD) AS DWORD ' assumption: dwData points to beginning of block, dwLength is length of data LOCAL pDib, hStream AS DWORD LOCAL dibType AS LONG ' first we attempt to open the memory buffer provided by the caller hStream = FreeImage_OpenMemory(BYVAL dwData, dwLength) IF ISTRUE hStream THEN ' attempt to determine the filetype from the buffer dibType = FreeImage_GetFileTypeFromMemory(BYVAL hStream) IF dibType >= 0 THEN _ ' no other error checking or announcements if we fail pDib = FreeImage_LoadFromMemory(dibType, BYVAL hStream) FreeImage_CloseMemory hStream END IF ' we assume that the initialization of pDib will be ZERO FreeImage_LoadDIBFromData = pDib ' important note, do not call this without using the return value ' you WILL cause a memory leak END FUNCTION FUNCTION FreeImage_SaveDIBToData(hDib AS DWORD, FileExt AS STRING) AS STRING LOCAL Results AS STRING LOCAL rPointer, hPointer AS BYTE POINTER LOCAL iDib, hStream, hCount AS DWORD LOCAL dibType, Success AS LONG IF ISFALSE hDib THEN EXIT FUNCTION ' redundant but useful ' open a memory stream for output, managed by FreeImage hStream = FreeImage_OpenMemory() IF ISTRUE hStream THEN ' create a clone in case bit depth needs adjusting ' do not tamper with the caller's image iDib = FreeImage_Clone(hDib) SELECT CASE UCASE$(FileExt) ' add more types as desired ' these are just the types as defined by my fileloader CASE ".BMP" dibType = %FIF_BMP CASE ".JPG",".JPE",".JPEG" dibType = %FIF_JPEG IF FreeImage_GetBPP(iDib) <> 24 THEN FreeImage_UnLoad iDib iDib = FreeImage_ConvertTo24Bits(hDib) END IF CASE ".PNG" dibType = %FIF_PNG CASE ".TGA" dibType = %FIF_TARGA CASE ".GIF" dibType = %FIF_GIF IF FreeImage_GetBPP(iDib) <> 8 THEN FreeImage_UnLoad iDib iDib = FreeImage_ColorQuantize(hDib, %FIQ_WUQUANT) END IF CASE ELSE ' if for some strange reason, user entered type not supported FreeImage_UnLoad iDib FreeImage_CloseMemory hStream EXIT FUNCTION END SELECT ' attempt write to the stream, never assume success Success = FreeImage_SaveToMemory(dibType, iDib, hStream, %FISO_SAVE_DEFAULT) IF ISTRUE Success THEN ' if write was successful, gain access to the memory block Success = FreeImage_AcquireMemory(hStream, hPointer, hCount) IF ISTRUE Success THEN ' *cough* DIM the string and copy the memory block to string space Results = SPACE$(hCount) rPointer = STRPTR(Results) CopyMemory(BYVAL rPointer,BYVAL hPointer,hCount) END IF END IF ' clean up our FreeImage and close the memory stream FreeImage_UnLoad iDib FreeImage_CloseMemory hStream END IF ' unlike the load, the return value does not need to be used by the called ' powerbasic should not cause a memory leak if you don't use it FreeImage_SaveDIBToData = Results END FUNCTION
Comment