/***************************************************************************************/ /* */ /* SCS editor Version 2 */ /* File : Bitmaps.pkg */ /* Version : 22 Mai 2000 */ /* Bitmap struct and functions */ /* */ /***************************************************************************************/ struct BitmapElt = [ BEfilename : S , /* bitmap filename */ BEreference : I , /* reference count */ BEbitmap : AlphaBitmap , /* bitmap */ BEsign : S /* bitmap signature */ ] mkBitmapElt ;; /* bitmap collection */ typeof bitmapEltList = [BitmapElt r1];; /*************************************************************************************** Bitmap comparison function Return value: 1 (success) or 0 (failure) ***************************************************************************************/ fun BITMAP_EltByFilename (elt, filename) = !strcmpi elt.BEfilename filename ;; /*************************************************************************************** Search for a bitmap in the collection by its filename Return value: BitmapElt (success) or nil (failure) ***************************************************************************************/ fun BITMAP_SearchElt (filename) = search_in_list bitmapEltList @BITMAP_EltByFilename filename ;; /*************************************************************************************** Remove a bitmap reference in the collection by its filename ***************************************************************************************/ fun BITMAP_RemoveElt (filename) = removef_from_list bitmapEltList @BITMAP_EltByFilename filename ;; /*************************************************************************************** Add a bitmap to the collection ***************************************************************************************/ fun BITMAP_AddElt (bitmapElt) = set bitmapEltList = bitmapElt::bitmapEltList; 1 ;; /*************************************************************************************** Remove a bitmap to the collection ***************************************************************************************/ fun BITMAP_DelElt (bitmapElt) = set bitmapEltList = removef_from_list bitmapEltList @BITMAP_EltByFilename bitmapElt.BEfilename; 1 ;; /*************************************************************************************** Load a jpeg bitmap from its filename Return value: Objbitmap (success) or nil (failure) ***************************************************************************************/ fun BITMAP_LoadJpeg (Pfilename, Channel) = let _LDjpeg Channel Pfilename -> jpeg in if jpeg == nil /* not a jpeg file */ then nil else let _CRalphaBitmap Channel jpeg nil nil nil -> alpha in ( _DSbitmap jpeg; alpha ) ;; /*************************************************************************************** Load a bmp bitmap from its filename Return value: Objbitmap (success) or nil (failure) ***************************************************************************************/ fun BITMAP_LoadBmp (Pfilename, Channel) = let _LDbitmap Channel Pfilename -> bitmap in if bitmap == nil /* not a bitmap file */ then nil else let _CRalphaBitmap Channel bitmap nil nil nil -> alpha in ( _DSbitmap bitmap; alpha ) ;; /*************************************************************************************** Load a bitmap from its filename Return value: Objbitmap (success) or nil (failure) ***************************************************************************************/ fun BITMAP_Load (filename, Channel) = if filename == nil then nil else let _checkpack filename -> Pfilename in if Pfilename == nil then ( ERRORS_AddError 1 6 (strcatn (_locSCS "errorlabel-6" nil)::": "::filename::nil) ; logScsError "BITMAP_Load" "filename="::filename::nil "file does not exist" nil ) else let nil -> alpha in ( if (compareEndString filename ".jpeg") || (compareEndString filename ".jpg") then set alpha = BITMAP_LoadJpeg Pfilename Channel else nil; if (compareEndString filename ".bmp") then set alpha = BITMAP_LoadBmp Pfilename Channel else nil; if (compareEndString filename ".png") then set alpha = _LDalphaBitmap Channel Pfilename else nil; if alpha != nil then alpha else let BITMAP_LoadJpeg Pfilename Channel -> alpha2 in if alpha2 != nil then alpha2 else let BITMAP_LoadBmp Pfilename Channel -> alpha2 in if alpha2 != nil then alpha2 else let _LDalphaBitmap Channel Pfilename -> alpha2 in if alpha2 == nil then ( ERRORS_AddError 1 6 (strcatn (_locSCS "errorlabel-6" nil)::": "::filename::nil) ; logScsError "BITMAP_Load" "filename="::filename::nil "file is not a valid bitmap format" nil ) else alpha2 ) ;; /*************************************************************************************** Load a bitmap from its filename Return value: Objbitmap (success) or nil (failure) ***************************************************************************************/ fun BITMAP_Unload (BE, misc) = _DSalphaBitmap BE.BEbitmap; 1 ;; /*************************************************************************************** Bitmap handling functions ***************************************************************************************/ /*************************************************************************************** Free a bitmap in the collection Return value: 1 (success) or 0 (failure) ***************************************************************************************/ fun BITMAP_Free (filename) = /* MAC ICI à revoir */ let BITMAP_SearchElt filename -> BE in if BE == nil then /* send error message */ ( ERRORS_AddError 1 5 (_locSCS "errorlabel-5" nil) ; logScsError "BITMAP_Free" "filename="::filename::nil "file already freed" 0 ) else ( /* decrease the reference count */ set BE.BEreference = BE.BEreference - 1; /* check if the bitmap is still used */ if BE.BEreference > 0 then 1 else ( /* unload the bitmap from memory */ BITMAP_Unload BE nil; /* remove the bitmap from the collection */ BITMAP_DelElt BE ) ) ;; /*************************************************************************************** Initialize the collection Return value: 1 ***************************************************************************************/ fun BITMAP_Init () = set bitmapEltList = nil; 1 ;; /*************************************************************************************** Empty the collection Return value: 1 ***************************************************************************************/ fun BITMAP_Clear () = /* unload all bitmaps from memory */ apply_on_list bitmapEltList @BITMAP_Unload nil; /* reset the collection */ BITMAP_Init ;; /*************************************************************************************** Return a bitmap by its filename Return value: Objbitmap (success) or nil (failure) ***************************************************************************************/ fun BITMAP_Get (filename, Channel) = if filename == nil then nil else let BITMAP_SearchElt filename -> BE in if BE == nil then let BITMAP_Load filename Channel -> bitmap in if bitmap == nil then ( ERRORS_AddError 1 6 (strcatn (_locSCS "errorlabel-6" nil)::": "::filename::nil) ; logScsError "BITMAP_Get" "filename="::filename::nil "unable to add bitmap to the collection" nil ) else ( /* add the bitmap to the collection */ BITMAP_AddElt (mkBitmapElt [filename 1 bitmap (_fileSign _checkpack filename)]); bitmap ) else BE.BEbitmap ;; /*************************************************************************************** Return a bitmap by its filename Real correspondance to disk image Return value: Objbitmap (success) or nil (failure) ***************************************************************************************/ fun BITMAP_GetReal (filename, Channel) = if filename == nil then nil else let BITMAP_SearchElt filename -> BE in ( if (BE != nil) && (strcmpi (_fileSign _checkpack (BE.BEfilename)) BE.BEsign) then ( _DSalphaBitmap BE.BEbitmap; BITMAP_RemoveElt filename; set BE = nil; ) else nil; if BE == nil then let BITMAP_Load filename Channel -> bitmap in if bitmap == nil then ( ERRORS_AddError 1 6 (strcatn (_locSCS "errorlabel-6" nil)::": "::filename::nil) ; logScsError "BITMAP_Get" "filename="::filename::nil "unable to add bitmap to the collection" nil ) else ( /* add the bitmap to the collection */ BITMAP_AddElt (mkBitmapElt [filename 1 bitmap (_fileSign _checkpack filename)]); bitmap ) else BE.BEbitmap ) ;;