<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html xmlns:fn="http://www.w3.org/2005/02/xpath-functions"> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <link rel="stylesheet" href="../../../../doc/otp_doc.css" type="text/css"> <title>Erlang -- egd</title> </head> <body bgcolor="white" text="#000000" link="#0000ff" vlink="#ff00ff" alink="#ff0000"><div id="container"> <script id="js" type="text/javascript" language="JavaScript" src="../../../../doc/js/flipmenu/flipmenu.js"></script><script id="js2" type="text/javascript" src="../../../../doc/js/erlresolvelinks.js"></script><script language="JavaScript" type="text/javascript"> <!-- function getWinHeight() { var myHeight = 0; if( typeof( window.innerHeight ) == 'number' ) { //Non-IE myHeight = window.innerHeight; } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) { //IE 6+ in 'standards compliant mode' myHeight = document.documentElement.clientHeight; } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) { //IE 4 compatible myHeight = document.body.clientHeight; } return myHeight; } function setscrollpos() { var objf=document.getElementById('loadscrollpos'); document.getElementById("leftnav").scrollTop = objf.offsetTop - getWinHeight()/2; } function addEvent(obj, evType, fn){ if (obj.addEventListener){ obj.addEventListener(evType, fn, true); return true; } else if (obj.attachEvent){ var r = obj.attachEvent("on"+evType, fn); return r; } else { return false; } } addEvent(window, 'load', setscrollpos); //--></script><div id="leftnav"><div class="innertube"> <img alt="Erlang logo" src="../../../../doc/erlang-logo.png"><br><small><a href="users_guide.html">User's Guide</a><br><a href="index.html">Reference Manual</a><br><a href="release_notes.html">Release Notes</a><br><a href="../pdf/percept-0.8.7.pdf">PDF</a><br><a href="../../../../doc/index.html">Top</a></small><p><strong>Percept</strong><br><strong>User's Guide</strong><br><small>Version 0.8.7</small></p> <br><a href="javascript:openAllFlips()">Expand All</a><br><a href="javascript:closeAllFlips()">Contract All</a><p><small><strong>Chapters</strong></small></p> <ul class="flipMenu" imagepath="../../../../doc/js/flipmenu"> <li id="no" title="Percept" expanded="false">Percept<ul> <li><a href="percept_ug.html"> Top of chapter </a></li> <li title="Introduction"><a href="percept_ug.html#id57231">Introduction</a></li> <li title="Getting started"><a href="percept_ug.html#id61101">Getting started</a></li> </ul> </li> <li id="loadscrollpos" title="egd" expanded="true">egd<ul> <li><a href="egd_ug.html"> Top of chapter </a></li> <li title="Introduction"><a href="egd_ug.html#id61190">Introduction</a></li> <li title="File example"><a href="egd_ug.html#id61212">File example</a></li> <li title="ESI example"><a href="egd_ug.html#id62010">ESI example</a></li> </ul> </li> </ul> </div></div> <div id="content"> <div class="innertube"> <h1>2 egd</h1> <h3><a name="id61190">2.1 Introduction</a></h3> <p> The egd module is an interface for 2d-image rendering and is used by Percept to generate dynamic graphs to its web pages. All code is pure erlang, no drivers needed. </p> <p> The library is intended for small to medium image sizes with low complexity for optimal performance. The library handles horizontal lines better then vertical lines. </p> <p> The foremost purpose for this module is to enable users to generate images from erlang code and/or datasets and to send these images to either files or web servers. </p> <h3><a name="id61212">2.2 File example</a></h3> <p>Drawing examples:</p> <div class="example"><pre> -module(img). -export([do/0]). do() -> Im = egd:create(200,200), Red = egd:color({255,0,0}), Green = egd:color({0,255,0}), Blue = egd:color({0,0,255}), Black = egd:color({0,0,0}), Yellow = egd:color({255,255,0}), % Line and fillRectangle egd:filledRectangle(Im, {20,20}, {180,180}, Red), egd:line(Im, {0,0}, {200,200}, Black), egd:save(egd:render(Im, png), "/home/egil/test1.png"), egd:filledEllipse(Im, {45, 60}, {55, 70}, Yellow), egd:filledEllipse(Im, {145, 60}, {155, 70}, Blue), egd:save(egd:render(Im, png), "/home/egil/test2.png"), R = 80, X0 = 99, Y0 = 99, Pts = [ { X0 + trunc(R*math:cos(A*math:pi()*2/360)), Y0 + trunc(R*math:sin(A*math:pi()*2/360)) } || A <- lists:seq(0,359,5)], lists:map( fun({X,Y}) -> egd:rectangle(Im, {X-5, Y-5}, {X+5,Y+5}, Green) end, Pts), egd:save(egd:render(Im, png), "/home/egil/test3.png"), % Text Filename = filename:join([code:priv_dir(percept), "fonts", "6x11_latin1.wingsfont"]), Font = egd_font:load(Filename), {W,H} = egd_font:size(Font), String = "egd says hello", Length = length(String), egd:text(Im, {round(100 - W*Length/2), 200 - H - 5}, Font, String, Black), egd:save(egd:render(Im, png), "/home/egil/test4.png"), egd:destroy(Im). </pre></div> <img alt="IMAGE MISSING" src="test1.gif"><br> First save. <em>Figure 2.1: test1.png</em> <img alt="IMAGE MISSING" src="test2.gif"><br> Second save. <em>Figure 2.2: test2.png</em> <img alt="IMAGE MISSING" src="test3.gif"><br> Third save. <em>Figure 2.3: test3.png</em> <img alt="IMAGE MISSING" src="test4.gif"><br> Fourth save. <em>Figure 2.4: test4.png</em> <h3><a name="id62010">2.3 ESI example</a></h3> <p>Using egd with inets ESI to generate images on the fly:</p> <div class="example"><pre> -module(img_esi). -export([image/3]). image(SessionID, _Env, _Input) -> mod_esi:deliver(SessionID, header()), Binary = my_image(), mod_esi:deliver(SessionID, binary_to_list(Binary)). my_image() -> Im = egd:create(300,20), Black = egd:color({0,0,0}), Red = egd:color({255,0,0}), egd:filledRectangle(Im, {30,14}, {270,19}, Red), egd:rectangle(Im, {30,14}, {270,19}, Black), Filename = filename:join([code:priv_dir(percept), "fonts", "6x11_latin1.wingsfont"]), Font = egd_font:load(Filename), egd:text(Im, {30, 0}, Font, "egd with esi callback", Black), Bin = egd:render(Im, png), egd:destroy(Im), Bin. header() -> "Content-Type: image/png\r\n\r\n". </pre></div> <img alt="IMAGE MISSING" src="img_esi_result.gif"><br> <em>Figure 2.5: Example of result.</em> <p> For more information regarding ESI, please see inets application <span class="bold_code"><a href="javascript:erlhref('../../../../doc/../','inets','mod_esi.html');">mod_esi</a></span>. </p> </div> <div class="footer"> <hr> <p>Copyright © 2007-2012 Ericsson AB, All Rights Reserved</p> </div> </div> </div></body> </html>