<?php

/*******************************************************************************

 WINBINDER - The native Windows binding for PHP for PHP

 Copyright © Hypervisual - see LICENSE.TXT for details
 Author: Rubem Pechansky (http://winbinder.org/contact.php)

 Using DIBs with WinBinder

*******************************************************************************/

//------------------------------------------------------------ SYSTEM PARAMETERS

define("PATH_SCRIPT",	dirname(__FILE__) . "/");
define("PATH_INC",		PATH_SCRIPT . "../../include/");
define("PATH_RES",		PATH_SCRIPT . "../../resources/");

//----------------------------------------------------------------- DEPENDENCIES

include PATH_INC . "winbinder.php";
include PATH_INC . "fi/freeimage.inc.php";

//-------------------------------------------------------------------- CONSTANTS

define("ID", 0);
define("APPNAME", "Using DIBs in WinBinder");

//-------------------------------------------------------------- EXECUTABLE CODE

// NOTES:

// This kind of application does not need a window handler function.
// This is a rare case where part of the code is inserted AFTER the main loop.
// This software uses the FreeImage open source image library. See http://freeimage.sourceforge.net for details. FreeImage is used under the FIPL version 1.0.

// Create window

$mainwin = wb_create_window(NULL, PopupWindow, APPNAME, WBC_CENTER, WBC_CENTER, 240, 220, 0, 0);

// Use FreeImage to load several images

create_frame($mainwin,  10, 20, PATH_RES . "cassini.jpg");
create_frame($mainwin, 150, 20, PATH_RES . "icon24.png");
create_frame($mainwin, 150, 60, PATH_RES . "icon8t.png");
create_frame($mainwin, 150,100, PATH_RES . "icon4.png");
create_frame($mainwin, 150,140, PATH_RES . "icon2.png");
create_frame($mainwin, 190, 20, PATH_RES . "icon8t.gif");
create_frame($mainwin,  10,140, PATH_RES . "menu_print.bmp");

wb_main_loop();

//--------- The following code is executed after the application is closed

// Create a bitmap and draw something on it

$bmp = wb_create_image(120, 20);
wb_draw_rect($bmp, 0, 0, 120, 20, 0xA0F0E0);
$font = wb_create_font("Arial", 8);
wb_draw_text($bmp, "This is a test image.", 0, 0, 120, 20, $font, WBC_CENTER);

// Copy the bitmap color data to a DIB and save it as a PNG file

$pixeldata = wb_get_image_data($bmp, true);
$dib = FreeImage_Allocate(120, 20, 24);
wb_poke(FreeImage_GetBits($dib), $pixeldata);
if(FreeImage_Save(FIF_PNG, $dib, "test.png"))
	wb_message_box(null, "Test image generated successfully.", APPNAME, WBC_INFO);

// Cleanup

wb_destroy_image($bmp);
FreeImage_Unload($dib);

//-------------------------------------------------------------------- FUNCTIONS

/* Use FreeImage to load an image (BMP, JPEG or PNG), then create
  a frame to hold it */

function create_frame($window, $xpos, $ypos, $filename)
{
	$path_parts = pathinfo($filename);

	switch(strtolower($path_parts["extension"])) {
		case "bmp":
			$fmt = FIF_BMP;
			break;

		case "jpg":
		case "jpeg":
			$fmt = FIF_JPEG;
			break;

		case "png":
			$fmt = FIF_PNG;
			break;

		case "gif":
			if(FreeImage_GetVersion() < "3.4.0") {
				wb_message_box($window, "You must update your version of FreeImage to support GIFs.", APPNAME, WBC_WARNING);
				return;
			}
			$fmt = FIF_GIF;
			break;
	}

	// Load image file

	$dib = FreeImage_Load($fmt, $filename, 0);
	$width = FreeImage_GetWidth($dib);
	$height = FreeImage_GetHeight($dib);

	// Create a WinBinder (Windows) bitmap

	$bmp = wb_create_image($width, $height,
		FreeImage_GetInfoHeader($dib),
		FreeImage_GetBits($dib));
	FreeImage_Unload($dib);

	// Create control

	$frame = wb_create_control($window, Frame, "",
		$xpos, $ypos, $width, $height, ID, WBC_IMAGE);
	wb_set_image($frame, $bmp);
	wb_destroy_image($bmp);
}

//-------------------------------------------------------------------------- END

?>