Page 1 of 1

RAM is NOT released with BT_FILEFORMAT_JPG (2)

Posted: Wed Jan 25, 2017 10:45 am
by Rockart10
Hi all.

I re-write here an issue I posted into the General Help section, maybe here it is a better topic.

While opening an image (with GETFILE) and saving it on the disk (with BT_BitmapSaveFile),
and if the BT_BitmapSaveFile (hBitmap, cFileName, BT_FILEFORMAT_BMP) option is used, the RAM is correctly released.

On the contrary, if the BT_BitmapSaveFile (hBitmap, cFileName, BT_FILEFORMAT_JPG) option is used the RAM is NOT released and heavily charged. When doing repeated savings of images, specially with large dimensions, this causes a crash of the executable (RAM is completely filled).

If the nTypePicture (BT_FILEFORMAT_JPG or BT_FILEFORMAT_BMP) is omitted, the image could be saved with the .jpg extension, but it maintains a BMP format, and Photoshop won't open it.

So I can't find the way to solve this issue: is it due to a lack of mine knowledge or is it a bug (JPG uncorrectly managed regarding RAM reease)?

I attach here a sample .prg (see lines 143-151) to better explain the issue.
You can easily check the RAM behavior compiling the executable and trying to save an image as a JPG or as a BMP while conrtemporarily looking at the Task Manager.

Reading the topic at viewtopic.php?f=20&t=4882&start=10

I changed the original bt_SaveGDIPlusPicture function in C:\hmg.3.4.3\SOURCE\c_BosTaurus.c
(from line 684) adding: GlobalFree (hGlobalAlloc);

but it still doesn't work...

it works adding RELEASE MEMORY after each BT_BitmapSaveFile (hBitmap, cFileName, BT_FILEFORMAT_JPG) or at the end of the procedure, but I'm not sure it's the right solution (what is released? also variables?)...

Is there any better solution?

Many thanks for any help!

Code: Select all

#include "hmg.ch"
#include "SET_COMPILE_HMG_UNICODE.CH"
#include "BosTaurus.CH"

*******************************************************************************
 * HMG - Harbour Win32 GUI library Demo
 * Copyright 2002 Roberto Lopez <mail.box.hmg@gmail.com>
 * http://sites.google.com/site/hmgweb/
*******************************************************************************
* Bos Taurus Graphic Library for HMG
* AUTOR:    Dr. CLAUDIO SOTO
* PAIS:     URUGUAY
* E-MAIL:   srvet@adinet.com.uy
* BLOG:     http://srvet.blogspot.com
*******************************************************************************
* test_pix by aarca   2017 
* fatteio@yahoo.it
******************************************************************************* 
 
*-----------------------------------------------------------------------------*
* PROCEDURE test_pix
*-----------------------------------------------------------------------------*
hBitmap 		:= 0
pix_choose		:= 0
hBitmap_copy 	:= 0
hBitmap_clone 	:= 0 
savebmp 		:= .F.
savejpg 		:= .F.
m_filename 		:= ""


 m_appdir = GetStartUpFolder()
 SetCurrentFolder(m_appdir)
 
 DEFINE WINDOW manage_pix_test ;
 AT 0, 0 ;
 WIDTH 770 ;
 HEIGHT 680 ;
 SIZABLE .T. ;
 BACKCOLOR { 241, 255, 236} ;
 TITLE "Test managing pictures" ;
 NOMAXIMIZE	;
 ON INIT pix_test_ini()  ;
 ON RELEASE BT_bitmaprelease(hBitmap) ; 
 ON PAINT pix_test_paint() ;
 MAIN
 
  * ON MOUSECLICK manage_pix_test.release ;
 
	DEFINE STATUSBAR
		STATUSITEM "Test managing pictures"
	END STATUSBAR
  
 END WINDOW 
 
 If !IsControlDefined (openimage , manage_pix_test )
	 @ 20,30 BUTTON openimage ;
	 PARENT manage_pix_test ;
	 CAPTION "OPEN an image" ;
	 WIDTH 140 ;
	 HEIGHT 28 ;
	 ACTION pix_test_draw_image() ;
	 TOOLTIP 'Select an image (.JPG only) to be used as a background'	 
 ENDIF	 
 
 If !IsControlDefined (savebmp , manage_pix_test )
	 @ 20,180 BUTTON savebmp ;
	 PARENT manage_pix_test ;
	 CAPTION "save BMP image" ;
	 WIDTH 145 ;
	 HEIGHT 28 ;
	 ACTION ( savebmp := .T. , pix_test_save_image() ) ;
	 TOOLTIP "Save the selected image as the default name 'test.bmp' - RAM is released"	  
 ENDIF	 
 
  If !IsControlDefined (savejpg , manage_pix_test )
	 @ 20,335 BUTTON savejpg ;
	 PARENT manage_pix_test ;
	 CAPTION "save JPG image" ;
	 WIDTH 145 ;
	 HEIGHT 28 ;
	 ACTION ( savejpg := .T. , pix_test_save_image() ) ;
	 TOOLTIP "Save the selected image as the default name 'test.jpg' - RAM is NOT released"	  
 ENDIF	 
 
 CENTER WINDOW manage_pix_test 
 ACTIVATE WINDOW manage_pix_test

RETURN

*-----------------------------------------------------------------------------*
PROCEDURE pix_test_ini()
*-----------------------------------------------------------------------------*
pix_choose :=  GetFile( { {'.JPG only','*.jpg'} } , 'Open .JPG image file',  m_appdir , .F. , .T. )
hBitmap := BT_BitmapLoadFile (pix_choose) 
BT_ClientAreaInvalidateAll ("manage_pix_test")
SetCurrentFolder(m_appdir)

RETURN 

*-----------------------------------------------------------------------------*
PROCEDURE pix_test_paint()
*-----------------------------------------------------------------------------*
 LOCAL  hDC, BTstruct

	hDC := BT_CreateDC ("manage_pix_test",  BT_HDC_INVALIDCLIENTAREA, @BTstruct)
	BT_DrawBitmap (hDC, 70, 30, 770, 520, BT_SCALE, hBitmap)
	BT_DeleteDC (BTstruct)
	* BT_bitmaprelease(hBitmap)

	RETURN 

*-----------------------------------------------------------------------------*
PROCEDURE pix_test_draw_image()
*-----------------------------------------------------------------------------*
BT_bitmaprelease(hBitmap)
manage_pix_test.savebmp.show
manage_pix_test.savejpg.show

If IsControlDefined (background1 , manage_pix_test )
	manage_pix_test.background1.release 
ENDIF

pix_choose :=  GetFile( { {'.JPG only','*.jpg'} } , 'Open .JPG image file',  m_appdir , .F. , .T. )
hBitmap := BT_BitmapLoadFile (pix_choose) 
ERASE WINDOW manage_pix_test

* BT_bitmaprelease(hBitmap)

RETURN


*-----------------------------------------------------------------------------*
PROCEDURE pix_test_save_image()
*-----------------------------------------------------------------------------*
 SetCurrentFolder(m_appdir)

	hBitmap_copy := BT_BitmapCopyAndResize (hBitmap, BT_BitmapWidth (hBitmap),  BT_BitmapHeight (hBitmap), BT_SCALE, BT_RESIZE_HALFTONE)
	
	hBitmap_clone := BT_BitmapClone (hBitmap, 0, 0, BT_BitmapWidth (hBitmap), BT_BitmapHeight (hBitmap), BT_SCALE)	

	IF savebmp
		BT_BitmapSaveFile (hBitmap_copy, "test.bmp", BT_FILEFORMAT_BMP)
		** with BT_FILEFORMAT_BMP RAM is NOT charged
		manage_pix_test.savebmp.hide
		manage_pix_test.savejpg.hide
	ENDIF
	
	IF savejpg
		BT_BitmapSaveFile (hBitmap_copy, "test.jpg", BT_FILEFORMAT_JPG)
		** with BT_FILEFORMAT_JPG  RAM is HEAVILY charged
		manage_pix_test.savebmp.hide
		manage_pix_test.savejpg.hide		
	ENDIF

	BT_bitmaprelease(hBitmap)
	BT_bitmaprelease(hBitmap_copy)		
	BT_bitmaprelease(hBitmap_clone)	

	If IsControlDefined (background1 , manage_pix_test )
	manage_pix_test.background1.release 
	ENDIF
	
	ERASE  WINDOW manage_pix_test
	
	If IsControlDefined (background1 , manage_pix_test )
		manage_pix_test.background1.release 
	ELSE
		IF savebmp
			m_filename := "test.bmp"
		ELSE
			m_filename := "test.jpg"
		ENDIF

		@ 70,30 LABEL background1 ;
		PARENT manage_pix_test ;
		VALUE m_filename+" SAVED. Click to 'OPEN an image' to repeat" ;
		WIDTH 500 ;
		HEIGHT 40 ;
		FONT 'Verdana' SIZE 14
		manage_pix_test.background1.BackColor := { 241, 255, 236 }	 
		manage_pix_test.background1.FontColor := { 0, 0, 255 }	
	ENDIF

	savebmp	:= .F.
	savejpg	:= .F.

RETURN

Re: RAM is NOT released with BT_FILEFORMAT_JPG (2)

Posted: Wed Jan 25, 2017 10:47 am
by serge_girard
Rockart, Claudio can surely help!
Serge

Re: RAM is NOT released with BT_FILEFORMAT_JPG (2)

Posted: Wed Jan 25, 2017 11:53 am
by Pablo César
serge_girard wrote:Rockart, Claudio can surely help!
+1

Re: RAM is NOT released with BT_FILEFORMAT_JPG (2)

Posted: Wed Jan 25, 2017 5:29 pm
by srvet_claudio
Thanks for reporting, but that bug has already been corrected in the topic that you posted.

Re: RAM is NOT released with BT_FILEFORMAT_JPG (2)

Posted: Thu Jan 26, 2017 3:06 am
by Rockart10
Muchas gracias Claudio.

I changed the original bt_SaveGDIPlusPicture function in C:\hmg.3.4.3\SOURCE\c_BosTaurus.c adding: "GlobalFree (hGlobalAlloc);" at line 739.

But after doing this I don't know what to do: nothing is changed. If I delete or rename the C:\hmg.3.4.3\SOURCE folder (which contains the c_BosTaurus.c file) HMG works as before (I use C:\hmg.3.4.3\IDE1IDE.exe) and doesn't seem to need the C:\hmg.3.4.3\SOURCE folder during the compilation process of my executable.

You understand I'm not very expert: do I need to recompile HMG or whatever else?

Muchas gracias por tu trabajo

vale

Re: RAM is NOT released with BT_FILEFORMAT_JPG (2)

Posted: Thu Jan 26, 2017 8:46 am
by mol
You need to rebuild library by BuildAllib32.bat or BuildAllib64.bat script

Re: RAM is NOT released with BT_FILEFORMAT_JPG (2)

Posted: Thu Jan 26, 2017 8:38 pm
by KDJ
Hi Dr. Claudio Soto

By the way, I think you can remove some unnecessary code from c_BosTaurus.c. This function may look like the picture on the right.

BT_SCR_GETINFO3.png
BT_SCR_GETINFO3.png (84.81 KiB) Viewed 5654 times

Re: RAM is NOT released with BT_FILEFORMAT_JPG (2)

Posted: Fri Jan 27, 2017 12:32 am
by Rockart10
Thanx MOL.

I executed BuildAllLib32.bat (option UNICODE), opened IDE.exe, recompiled the test executable (to save an image as a JPG file), but nothing is changed. Maybe I need to do something else?

best

Re: RAM is NOT released with BT_FILEFORMAT_JPG (2)

Posted: Fri Jan 27, 2017 1:20 am
by Rockart10
I add this behaviour: after inserting a RELEASE MEMORY after a
BT_BitmapSaveFile (m_load_ph, "htm/temp.jpg", BT_FILEFORMAT_JPG)
(which is the only way I found not to charge a HUGE ram if large jpg files are managed)
a random error occurs:

Error BASE/1004 No exported method: EVAL
called fron (b)EVAL(0)

It seems to be produced in the occasion of "ACTIVATE WINDOW"

After removing RELEASE MEMORY (and changing all BT_FILEFORMAT_JPG into BT_FILEFORMAT_BMP, which anyway is not OK for my scopes) the error doesn't occur;

Also for this reason a solution to the BT_FILEFORMAT_JPG will be very useful: I can't manage to recompile HMG (3.4.3) with the update of C:\hmg.3.4.3\SOURCE\c_BosTaurus.c

Many thanks again for any help

Best of all

SOLVED

Posted: Mon Jan 30, 2017 4:56 pm
by Rockart10
After applying the correction to C:\hmg.3.4.3\SOURCE\c_BosTaurus.c (adding: "GlobalFree (hGlobalAlloc);" after "bt_Release_GDIplus ();" at line 737 ), I managed to recompile ALL libraries of HMG (including libhmg.a which is the one containing the BosTaurus related functions) reinstalling HMG by scratch and executing as Admin BuilAllLib32.bat

The RAM memory surcharge no more appeared (memory is correctly released after saving files with JPG extention), and also
the "Error BASE/1004 No exported method: EVAL" seems defeated.

So a long series (also in batch I suppose) of jobs involving jpg pictures can be finally performed!

many thanks to all.

Best.

AA