function ImageCallback(event)
% ImageCallback - Callback called by a MatlabServer object for generating
% an image of a Matlab figure as response
%
% CALL:
%     ImageCallback(event)
%
% INPUT:
%     event: <modelit.webserver.HttpExchange>
%       with the request data and methods to generate a response.
%
% OUTPUT:
%     No output
%
% EXAMPLE:
%  server = modelit.web.server.Server(8081, @ImageCallback).start()
%
%  % Open a webbrowser and type: http://localhost:8081 in the address bar
%  % Now an image with the Matlab logo appears in the browser.

%   Copyright 2006-2015 Modelit, www.modelit.nl

HWIN = figure;
membrane;
shading interp;

% Generate the response
[~, response] = fig2base64(HWIN, 'png');

delete(HWIN);

% Set the MIME type
% See https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/MIME_types
% And allow Cross Origin Resource Sharing
event.addResponseHeader('Content-Type', 'image/png',...
                        'Access-Control-Allow-Origin', '*');

% Send the response back to the client, with statuscode 200 (OK)
% See https://developer.mozilla.org/en-US/docs/Web/HTTP/Status
event.send(200, response);