Blitz FreeImage Library

BlitzPlus Forums/BlitzPlus Beginners Area/Blitz FreeImage Library

The forum is locked. These are the original blitzbasic.com forums, kept so they can still be read. No new topics, replies or edits can be made here. To join in, head over to the Blitz3D+ forums.

Btw, you might find my version of his lib to be easier to use. I made some changes if I remember correctly, I used some of his method and some of someone else's method so that I could do more of the stuff with Blitzplus commands instead of DLL calls.

My functions don't support all the functionality of the library, they just do what you probably want to, which is load and save images as simply as possible.

; -----------------------------------------------------------------------------------------------------------------------------------
; To use these functions you must create a freeimage.decls file in your decls directory with the following lines:
;
; 	.lib "FreeImage.dll"
;
; 	FIAllocate%( Width%,Height%,BPP%,RedMask%,GreenMask%,BlueMask% ) : "_FreeImage_Allocate@24"
; 	FILoad%( ImageType%,File$,Mode% ) : "_FreeImage_Load@12"
; 	FISave%( ImapeType%,FIBITMAP%,File$,Flags% ) : "_FreeImage_Save@16"
; 	FIUnload%( FIBITMAP% ) : "_FreeImage_Unload@4"
;	FIGetDescription$( FREE_IMAGE_FORMAT% ) : "_FreeImage_GetFIFDescription@4"
; 	FIGetType%( File$,Default% ) : "_FreeImage_GetFileType@8"
;	FIToBank( Bank*,FIBITMAP%,Pitch%,Depth%,Red_Mask%,Green_Mask%,Blue_Mask%,TopDown% ) : "_FreeImage_ConvertToRawBits@32"
;	FIGuessType%( File$ ) : "_FreeImage_GetFIFFromFilename@4"
;	FIGetClrUsed%( FIBITMAP% ) : "_FreeImage_GetColorsUsed@4"
;	FIGetBitStart%( FIBITMAP% ) : "_FreeImage_GetBits@4"
;	FIGetScanLine%( FIBITMAP%,ScanLine% ) : "_FreeImage_Get@8"
; 	FIGetDepth%( FIBITMAP% ) : "_FreeImage_GetBPP@4"
;	FIGetWidth%( FIBITMAP% ) : "_FreeImage_GetWidth@4"
;	FIGetBits%( FIBITMAP% ) : "_FreeImage_GetBits@4"
;	FIGetHeight%( FIBITMAP% ) : "_FreeImage_GetHeight@4"
;	FIGetLine%( FIBITMAP% ) : "_FreeImage_GetLine@4"
;	FIGetPitch%( FIBITMAP% ) : "_FreeImage_GetPitch@4"
;	FIGetSize%( FIBITMAP% ) : "_FreeImage_GetDIBSize@4"
;	FIGetPalette%( FIBITMAP% ) : "_FreeImage_GetPalette@4"
;	FIGetDPMX%( FIBITMAP% ) : "_FreeImage_GetDotsPerMeterX@4"
;	FIGetDPMY%( FIBITMAP% ) : "_FreeImage_GetDotsPerMeterY@4"
;	FIGetHeader%( FIBITMAP% ) : "_FreeImage_GetInfoHeader@4"
;	FIGetInfo%( FIBITMAP% ) : "_FreeImage_GetInfo@4"
;	FIGetColorType%( FIBITMAP% ) : "_FreeImage_GetColorType@4"
;	FIGetRedMask%( FIBITMAP% ) : "_FreeImage_GetRedMask@4"
;	FIGetGreenMask%( FIBITMAP% ) : "_FreeImage_GetGreenMask@4"
;	FIGetBlueMask%( FIBITMAP% ) : "_FreeImage_GetBlueMask@4"
;	FIGetTranCount%( FIBITMAP% ) : "_FreeImage_GetTransparencyCount@4"
;	FIGetTranTable%( FIBITMAP% ) : "_FreeImage_GetTransparencyTable@4"
;	FISetTran( FIBITMAP%,Flag% ) : "_FreeImage_SetTransparent@8"
;	FIIsTran%( FIBITMAP% ) : "_FreeImage_IsTransparent@4"
;	FIConvert8Bits%( FIBITMAP% ) : "_FreeImage_ConvertTo8Bits@4"
;	FIConvert555%( FIBITMAP% ) : "_FreeImage_ConvertTo16Bits555@4"
;	FIConvert565%( FIBITMAP% ) : "_FreeImage_ConvertTo16Bits565@4"
;	FIConvert24Bits%( FIBITMAP% ) : "_FreeImage_ConvertTo24Bits@4"
;	FIConvert32Bits%( FIBITMAP% ) : "_FreeImage_ConvertTo32Bits@4"
;	FIColorQuantize%( FIBITMAP%,Flag% ) : "_FreeImage_ColorQuantize@8"
;	FIThreshold%( FIBITMAP%,Flag% ) : "_FreeImage_Threshold@8"
;	FIDither%( FIBITMAP%,Algo% ) : "_FreeImage_Dither@8"
;	FIFromBank%( Bank*,Width%,Height%,Pitch%,Depth%,RedMask%,GreenMask%,BlueMask%,TopDown% ) : "_FreeImage_ConvertFromRawBits@36"
;	FIClone%(DIB%) : "_FreeImage_Clone@4"
;
; -----------------------------------------------------------------------------------------------------------------------------------
; Freeimage supports the following file types:
;
; 	BMP files       [reading, writing]
; 	TARGA files     [reading, writing]
; 	TIFF files      [reading, writing]
; 	JPEG/JIF files  [reading, writing]
; 	GIF files       [reading, writing]
; 	PNG files       [reading, writing]
; 	WBMP files      [reading, writing]
; 	ICO files       [reading, writing]
; 	PBM files       [reading, writing]
; 	PGM files       [reading, writing]
; 	PPM files       [reading, writing]
; 	XPM files       [reading, writing]
;
;	DDS files       [reading]
; 	PhotoShop files [reading]
; 	Kodak PhotoCD   [reading]
; 	PCX files       [reading]
; 	Dr. Halo files  [reading] 
; 	IFF files       [reading]
; 	JNG files       [reading]
; 	KOALA files     [reading]
; 	LBM files       [reading]
; 	MNG files       [reading]
; 	Sun RAS files   [reading]
; 	XBM files       [reading]
; -----------------------------------------------------------------------------------------------------------------------------------


; Image formats:

	Const FIF_UNKNOWN = -1
	Const FIF_BMP     =  0
	Const FIF_ICO     =  1
	Const FIF_JPEG    =  2
	Const FIF_JNG     =  3
	Const FIF_KOALA   =  4
	Const FIF_LBM     =  5
	Const FIF_IFF     =  5
	Const FIF_MNG     =  6
	Const FIF_PBM     =  7
	Const FIF_PBMRAW  =  8
	Const FIF_PCD     =  9
	Const FIF_PCX     = 10
	Const FIF_PGM     = 11
	Const FIF_PGMRAW  = 12
	Const FIF_PNG     = 13
	Const FIF_PPM     = 14
	Const FIF_PPMRAW  = 15
	Const FIF_RAS     = 16
	Const FIF_TARGA   = 17
	Const FIF_TIFF    = 18
	Const FIF_WBMP    = 19
	Const FIF_PSD     = 20
	Const FIF_CUT     = 21
	Const FIF_XBM     = 22
	Const FIF_XPM     = 23
	Const FIF_DDS     = 24
	Const FIF_GIF     = 25


; Compression settings:

	Const FIC_DEFAULT            = $0

	Const FIC_BMP_RLE            = $1

	Const FIC_JPEG_SUPERB        = $80	; 100% quality
	Const FIC_JPEG_GOOD          = $100	;  75% quality (default)
	Const FIC_JPEG_NORMAL        = $200	;  50% quality
	Const FIC_JPEG_AVERAGE       = $400	;  25% quality
	Const FIC_JPEG_BAD           = $800	;  10% quality
	Const FIC_TIFF_PACKBITS      = $100
	Const FIC_TIFF_DEFLATE       = $200
	Const FIC_TIFF_ADOBE_DEFLATE = $400	
	Const FIC_TIFF_NONE          = $800 


; Image loading flags:

	Const JPEG_ACCURATE = 2

; Image formats:

	Global FIB_BitDepth[4]
	Global FIB_RedMask[4]
	Global FIB_GreenMask[4]
	Global FIB_BlueMask[4]

	FIB_BitDepth[1]  = 16
	FIB_RedMask[1]   = $1F
	FIB_GreenMask[1] = $7E0
	FIB_BlueMask[1]  = $F800

	FIB_BitDepth[2]  = 16
	FIB_RedMask[2]   = $1F
	FIB_GreenMask[2] = $3E0
	FIB_BlueMask[2]  = $7C00

	FIB_BitDepth[3]  = 24
	FIB_RedMask[3]   = $FF0000
	FIB_GreenMask[3] = $FF00
	FIB_BlueMask[3]  = $FF

	FIB_BitDepth[4]  = 32
	FIB_RedMask[4]   = $FF0000
	FIB_GreenMask[4] = $FF00
	FIB_BlueMask[4]  = $FF


; -----------------------------------------------------------------------------------------------------------------------------------
; This function loads an image into Blitz using the Freeimage library.
; -----------------------------------------------------------------------------------------------------------------------------------
Function FILoadImage(Filename$)

	Local FI_FileType
	Local FI_Bitmap
	Local FI_Width, FI_Height
	Local DestImage, DestImageBuffer
	Local DestImageBank, DestImagePitch, DestImageFormat
	Local DestImageDepth, DestImageRedMask, DestImageGreenMask, DestImageBlueMask

	; Get the filetype.
		
		; First try to get the file type from the file header.
		; If that fails, try to guess the filetype from the file extension.
		; If that fails, return from function with failure.
		
		FI_FileType = FIGetType(Filename$, 0)
		
		If FI_FileType = -1
			FI_FileType = FIGuessType(Filename$)
			If FI_FileType = -1 Return
		EndIf
	
	; Attempt to load the image.  If the image fails to load, return from the function.
		
		If FI_FileType = FIF_JPEG
			;FI_Bitmap = FILoad(FI_FileType, Filename$, JPEG_ACCURATE)
			FI_Bitmap = FILoad(FI_FileType, Filename$, 0)
		Else
			FI_Bitmap = FILoad(FI_FileType, Filename$, 0)
		EndIf
		
		If Not FI_Bitmap Return 	
	
	; Get the width and height of the image.
		FI_Width  = FIGetWidth(FI_Bitmap)		
		FI_Height = FIGetHeight(FI_Bitmap)
		
	; Create an image in Blitz with the same width and height as the image loaded with freeimage.			
		DestImage = CreateImage(FI_Width, FI_Height, 1, 1)
		DestImageBuffer = ImageBuffer(DestImage)
	
	; Copy the FI_Image to the Blitz image.
	
		LockBuffer DestImageBuffer
			
			DestImageBank      = LockedPixels(DestImageBuffer)
			DestImagePitch     = LockedPitch(DestImageBuffer)
			DestImageFormat    = LockedFormat(DestImageBuffer)
			
			DestImageDepth     = FIB_BitDepth[DestImageFormat]
			DestimageRedMask   = FIB_RedMask[DestImageFormat]
			DestimageGreenMask = FIB_GreenMask[DestImageFormat]
			DestimageBlueMask  = FIB_BlueMask[DestImageFormat]
			
			FIToBank(DestImageBank, FI_Bitmap, DestImagePitch, DestImageDepth, DestimageRedMask, DestimageGreenMask, DestimageBlueMask, 0)                 

		UnlockBuffer DestImageBuffer

	; Free the loaded image.
		FIUnload FI_Bitmap
	
	; Return the pointer to the new Blitz image.
		Return DestImage
	
End Function


; -----------------------------------------------------------------------------------------------------------------------------------
; This function saves an image from Blitz using the Freeimage library.
; For JPEG, can specify integer 0..100 for compression to indicate desired quality.
; -----------------------------------------------------------------------------------------------------------------------------------
Function FISaveImage(SrcImage, Filename$, FI_FileType=FIF_UNKNOWN, FI_Compression=0)

	Local FI_Bitmap
	Local Success

	If SrcImage <> 0

		; If FI_FileType is unknown, attempt to determine the filetype the user is trying to save from the file extension.
		; Return from the function if fail.

			If FI_FileType = FIF_UNKNOWN
		
				FI_FileType = FIGuessType(Filename$)
				If FI_FileType = FIF_UNKNOWN Return
	
			EndIf
				
		; Is saving supported for this format?
				
		; Create a FI bitmap from the source image.
			FI_Bitmap = FIConvertImageToFIB(SrcImage)
		
		; Attempt to save the bitmap using the specified format.
			Success = FISave(FI_FileType, FI_Bitmap, Filename$, FI_Compression)
			If Not Success Then Notify "Unable to save image:" + Chr(10) + Filename$, True

		; Free the FI bitmap.		
			FIUnload(FI_Bitmap)
		
	EndIf

End Function


; -----------------------------------------------------------------------------------------------------------------------------------
; This function takes a Blitz image as input, and returns a FI bitmap.
; -----------------------------------------------------------------------------------------------------------------------------------
Function FIConvertImageToFIB(SrcImage)

	Local SrcBuffer
	Local SrcWidth, SrcHeight
	Local SrcBank, SrcPitch, SrcFormat
	Local SrcDepth
	Local SrcRedMask, SrcGreenMask, SrcBlueMask
	Local FI_Bitmap, FI_Bitmap24

	; Convert the Blitz image to an FI_Bitmap:

		SrcBuffer = ImageBuffer(SrcImage)
	
		LockBuffer SrcBuffer
		
			SrcWidth  = ImageWidth(SrcImage)
			SrcHeight = ImageHeight(SrcImage)
			
			SrcBank   = LockedPixels(SrcBuffer)
			SrcPitch  = LockedPitch(SrcBuffer)
			SrcFormat = LockedFormat(SrcBuffer)
		
			SrcDepth     = FIB_BitDepth[SrcFormat]
			SrcRedMask   = FIB_RedMask[SrcFormat]
			SrcGreenMask = FIB_GreenMask[SrcFormat]
			SrcBlueMask  = FIB_BlueMask[SrcFormat]
		
			FI_Bitmap = FIFromBank(SrcBank, SrcWidth, SrcHeight, SrcPitch, SrcDepth, SrcRedMask, SrcGreenMask, SrcBlueMask,0)

		UnlockBuffer SrcBuffer
	
	; Convert FI_Bitmap to 24bit if neccessary:	

		If FIGetDepth(FI_Bitmap) <> 24
	
			FI_Bitmap24 = FIConvert24Bits(FI_Bitmap)
		
			If FI_Bitmap24
				FIUnload(FI_Bitmap)
				FI_Bitmap = FI_Bitmap24
			EndIf
	
		EndIf
	
	; Return FI_Bitmap:
		Return FI_Bitmap

End Function