' *****************************************************
' PROGRAM:	gtkoverlay.bac
' PURPOSE:	to overlay png images from the command line
' AUTHOR:		vovchik (Puppy Linux forum)
' MODDED:		PvE (thanks for the help)
' DEPENDS:	gcc, bacon, bash, gtk
' PLATFORM:	Puppy Linux (actually, any *nix)
' DATE:		09-11-2010
' NOTES:		
' *****************************************************
' 110720 BK pngoverlay.


' *********************
' DEF ERROR HANDER
' *********************

TRAP LOCAL
CATCH GOTO PRINT_ERROR

' *********************
' END DEF ERROR HANDER
' *********************


' *********************
' CONSTANTS
' *********************

CONST GDK_INTERP_HYPER = 3
CONST Gtk$ = "libgtk-x11-2.0.so.0"
CONST Gdk$ = "libgdk-x11-2.0.so.0"
CONST Gdk_pixbuf$ = "libgdk_pixbuf-2.0.so.0"
CONST Gobject$ = "libgobject-2.0.so.0"
CONST MY_VERSION$ = "v.0.1a by vovchik, Puppy Linux Forum"
CONST BACON_BLURB$ = "Check out BaCon at www.basic-converter.org"

' *********************
' END CONSTANTS
' *********************


' *********************
' DECLARATIONS
' *********************

DECLARE img1_size, img2_size, window, img TYPE int
DECLARE NUL, BackImage$, FrontImage$, NewImage$ TYPE STRING
DECLARE GTK_WINDOW_TOPLEVEL, GDK_INTERP_BILINEAR TYPE int
DECLARE OVERALL_ALPHA TYPE int
DECLARE dest_x, dest_y, dest_wdth, dest_height TYPE int
DECLARE offset_x, offset_y, scale_x, scale_y TYPE double

GLOBAL clarray$ TYPE STRING
GLOBAL clitems TYPE NUMBER
GLOBAL hei_img1, wid_img1, hei_img2, wid_img2 TYPE NUMBER
GLOBAL img_dst, img_scr TYPE long

' *********************
' END DECLARATIONS
' *********************


' *********************
' IMPORTS
' *********************

IMPORT "g_object_unref(long)" FROM Gobject$ TYPE void
REM IMPORT "g_signal_connect_data(long,char*,void*,long,long,int)" FROM Gobject$ TYPE void
REM IMPORT "gtk_container_add(long, long)" FROM Gtk$ TYPE void
IMPORT "gtk_exit(int)" FROM Gtk$ TYPE void
IMPORT "gtk_init(int*, void*)" FROM Gtk$ TYPE void
IMPORT "gtk_image_new_from_pixbuf(long)" FROM Gtk$ TYPE long
REM IMPORT "gtk_main" FROM Gtk$ TYPE void
IMPORT "gtk_main_quit(void)" FROM Gtk$ TYPE void
REM IMPORT "gtk_widget_show(long)" FROM Gtk$ TYPE void
IMPORT "gtk_widget_show_all(long)" FROM Gtk$ TYPE void
REM IMPORT "gtk_window_new(int)" FROM Gtk$ TYPE long
IMPORT "gdk_pixbuf_composite(int, int, int, int, int, int, double, double, double, double, int, int)" FROM Gdk_pixbuf$ TYPE void
REM IMPORT "gdk_pixbuf_new_from_file_at_size(char*, int, int, void*)" FROM Gdk_pixbuf$ TYPE int
IMPORT "gdk_pixbuf_save(long,char*,char*,void*,...)" FROM Gdk_pixbuf$ TYPE int

IMPORT "gdk_pixbuf_new_from_file(char*,void*)" FROM Gdk_pixbuf$ TYPE long
IMPORT "gdk_pixbuf_get_width(int)" FROM Gdk_pixbuf$ TYPE int
IMPORT "gdk_pixbuf_get_height(int)" FROM Gdk_pixbuf$ TYPE int

' *********************
' END IMPORTS
' *********************

' *********************
' FUNCTIONS & SUBS
' *********************

SUB get_cl_func()
 SPLIT ARGUMENT$ BY " " TO clarray$ SIZE clitems
 IF clitems = 1 THEN
  PRINT "Usage: front-png back-png output-png"
 ENDIF
 IF clitems < 4 THEN END 1
 IF NOT(FILEEXISTS(clarray$[1])) THEN END 2
 IF NOT(FILEEXISTS(clarray$[0])) THEN END 3
 BackImage$=clarray$[2]
 FrontImage$=clarray$[1]
 NewImage$=clarray$[3]
 IF FILEEXISTS(NewImage$) THEN DELETE FILE NewImage$
END SUB

'-----------------
SUB bacmain_func()
'-----------------
	GDK_INTERP_BILINEAR = 2
	'GDK_INTERP_HYPER = 3
	GTK_WINDOW_TOPLEVEL = 0
	OVERALL_ALPHA = 255
	window = 0
	img = 0
REM	img1_size = 128
REM	img2_size = 128
	scale_x = 1
	scale_y = 1
	dest_x = 0
	dest_y = 0
	offset_x = 0
	gtk_init(0, 0)
REM	img_dst = gdk_pixbuf_new_from_file_at_size(FrontImage$, img1_size, img1_size, 0)
	img_dst = gdk_pixbuf_new_from_file(FrontImage$, 0)
	hei_img1 = gdk_pixbuf_get_height(img_dst)
	wid_img1 = gdk_pixbuf_get_width(img_dst)
REM	img_scr = gdk_pixbuf_new_from_file_at_size(BackImage$, img2_size, img2_size, 0)
	img_scr = gdk_pixbuf_new_from_file(BackImage$, 0)
	hei_img2 = gdk_pixbuf_get_height(img_scr)
	wid_img2 = gdk_pixbuf_get_width(img_scr)
	img1_size=wid_img1
	img2_size=wid_img2
	IF NOT(img_dst) OR NOT(img_scr) THEN
		PRINT "Error loading one of the images!", NL$
		IF img_dst THEN
			g_object_unref(img_dst)
		ELSE 
			g_object_unref(img_scr)
		END IF 
		END
	END IF
	' gdk_composite args (12 nums - some ints some doubles):
	' src, dst, 
	' dest_x, dest_y, 
	' dest_wdth, dest_height, 
	' offset_x, offset_y, 
	' scale_x, scale_y, 
	' interp_type, overall_alpha 

REM	gdk_pixbuf_composite(img_scr, img_dst, \
REM					dest_x, img1_size - img2_size, \
REM					img2_size, img2_size, \
REM					offset_x, img1_size - img2_size, \
REM					scale_x, scale_y, \
REM					GDK_INTERP_HYPER, OVERALL_ALPHA)  

	IF (hei_img1 - hei_img2) >= 0 THEN
	 gdk_pixbuf_composite(img_scr, img_dst, dest_x, hei_img1 - hei_img2, wid_img2, hei_img2, offset_x, hei_img1 - hei_img2, scale_x, scale_y, GDK_INTERP_HYPER, OVERALL_ALPHA)
	ELSE
	 gdk_pixbuf_composite(img_scr, img_dst, dest_x, hei_img2 - hei_img1, wid_img2, hei_img2, offset_x, hei_img2 - hei_img1, scale_x, scale_y, GDK_INTERP_HYPER, OVERALL_ALPHA)
	ENDIF

	img = gtk_image_new_from_pixbuf(img_dst)
	g_object_unref(img_dst)
	g_object_unref(img_scr)
REM	window = gtk_window_new(GTK_WINDOW_TOPLEVEL)
REM	g_signal_connect_data(window, "destroy", gtk_main_quit, 0, 0, 0)   
REM	gtk_container_add(window, img)
REM	gtk_widget_show_all(window)
	gdk_pixbuf_save(img_dst, NewImage$, "png" ,NULL, NULL)
REM	gtk_main()
END SUB

' -----------
SUB REPORT_RESULTS()
' -----------
	IF FILEEXISTS(NewImage$) THEN
		PRINT "File ", NewImage$, " created."
	ELSE
		PRINT "OOPS.. something terrible happened and ", NewImage$, " was not created."
	END IF
	gtk_exit(0)
END SUB

' *********************
' END FUNCTIONS & SUBS
' *********************
        

' *********************
' MAIN
' *********************

get_cl_func
bacmain_func
REPORT_RESULTS
END

' *********************
' END MAIN
' *********************

' *********************
' ERROR HANDLER
' *********************

LABEL PRINT_ERROR
    PRINT "GTK library ", Gtk$, " is not available on this platform!"
    END

 
