annotate cpack/dml/api/matlab.pl @ 0:718306e29690 tip

commiting public release
author Daniel Wolff
date Tue, 09 Feb 2016 21:05:06 +0100
parents
children
rev   line source
Daniel@0 1 /* Part of DML (Digital Music Laboratory)
Daniel@0 2 Copyright 2014-2015 Samer Abdallah, University of London
Daniel@0 3
Daniel@0 4 This program is free software; you can redistribute it and/or
Daniel@0 5 modify it under the terms of the GNU General Public License
Daniel@0 6 as published by the Free Software Foundation; either version 2
Daniel@0 7 of the License, or (at your option) any later version.
Daniel@0 8
Daniel@0 9 This program is distributed in the hope that it will be useful,
Daniel@0 10 but WITHOUT ANY WARRANTY; without even the implied warranty of
Daniel@0 11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Daniel@0 12 GNU General Public License for more details.
Daniel@0 13
Daniel@0 14 You should have received a copy of the GNU General Public
Daniel@0 15 License along with this library; if not, write to the Free Software
Daniel@0 16 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
Daniel@0 17 */
Daniel@0 18
Daniel@0 19 :- module(api_matlab, []).
Daniel@0 20
Daniel@0 21 :- use_module(library(http/http_dispatch)).
Daniel@0 22 :- use_module(library(http/http_parameters)).
Daniel@0 23 :- use_module(library(sandbox)).
Daniel@0 24 :- use_module(library(insist)).
Daniel@0 25 :- use_module(library(fileutils)).
Daniel@0 26 :- use_module(library(swipe)).
Daniel@0 27 :- use_module(library(httpfiles)).
Daniel@0 28 :- use_module(library(mlserver)).
Daniel@0 29
Daniel@0 30 :- set_prolog_flag(double_quotes,string).
Daniel@0 31 :- set_prolog_flag(back_quotes,symbol_char).
Daniel@0 32
Daniel@0 33 :- http_handler(api(matlab/render), figure_render, []).
Daniel@0 34
Daniel@0 35 :- setting(matlab:pixels_per_inch,number,150,"Pixels per inch for in browser figures").
Daniel@0 36 :- setting(matlab:default_figure_format,oneof([png,svg]),svg,"Default Matlab figure rendering method").
Daniel@0 37
Daniel@0 38 :- initialization catch(mutex_create(_,[alias(matlab_fig)]),Ex,print_message(warning,Ex)).
Daniel@0 39
Daniel@0 40 %% figure_render(+Request) is det.
Daniel@0 41 %
Daniel@0 42 % HTTP handler for rendering Matlab figures.
Daniel@0 43 figure_render(Request) :-
Daniel@0 44 setting(matlab:pixels_per_inch,DefPPI),
Daniel@0 45 http_parameters(Request,
Daniel@0 46 [ code(CodeA,[ atom, optional(false), description("Prolog rendering goal")])
Daniel@0 47 , format(F, [ oneof([eps,svg,pdf,png]), optional(false), description("Output format") ])
Daniel@0 48 , width(W, [ number, optional(true), default(10), description("Width in cm")])
Daniel@0 49 , height(H, [ number, optional(true), default(6), description("Height in cm")])
Daniel@0 50 , color_map(CMA, [ atom, optional(true), default(hot) ])
Daniel@0 51 , font_name(FN, [ atom, optional(true), default(helvetica) ])
Daniel@0 52 , font_size(FS, [ number, optional(true), default(8) ])
Daniel@0 53 , line_width(LW, [ number, optional(true), default(0.75) ])
Daniel@0 54 , marker_size(MS,[ number, optional(true), default(4) ])
Daniel@0 55 , ppi(PPI, [ number, optional(true), default(DefPPI), description("PNG resolution")])
Daniel@0 56 ]),
Daniel@0 57 debug(matlab,"Attempting to parse \"~s\".",[CodeA]),
Daniel@0 58 atom_to_term(CodeA,Code,[]),
Daniel@0 59 atom_to_term(CMA,CM,[]),
Daniel@0 60 ( user_db:logged_on(A)
Daniel@0 61 -> debug(matlab,"Logged on as ~w, no checking",[A])
Daniel@0 62 ; debug(matlab,"Checking ~q for safety...",[Code]),
Daniel@0 63 sandbox:safe_goal(Code),
Daniel@0 64 debug(matlab,"Goal is safe.",[])
Daniel@0 65 ),
Daniel@0 66 insist(with_temp_dir(Dir, (
Daniel@0 67 render(F,Code,Dir, [ size(W,H), ppi(PPI),color_map(CM),line_width(LW)
Daniel@0 68 , font_name(FN),font_size(FS),marker_size(MS) ]),
Daniel@0 69 atom_concat('tmp_.',F,Out),
Daniel@0 70 absolute_file_name(Dir/Out,File),
Daniel@0 71 reply_file(File,F)
Daniel@0 72 ))).
Daniel@0 73
Daniel@0 74
Daniel@0 75 render(eps,Code,D,Opts) :- with_mutex(matlab_fig,print_fig(Code,D,Opts,'-depsc2')).
Daniel@0 76 render(pdf,Code,D,Opts) :- render(eps,Code,D,Opts), run(in(D,sh(0>>0, "epstopdf tmp_.eps"))).
Daniel@0 77 render(svg,Code,D,Opts) :- render(pdf,Code,D,Opts), run(in(D,sh(0>>0, "pdf2svg tmp_.pdf tmp_.svg"))).
Daniel@0 78 render(png,Code,D,Opts) :-
Daniel@0 79 render(eps,Code,D,Opts),
Daniel@0 80 option(ppi(PPI),Opts),
Daniel@0 81 run(in(D,sh(0>>0, "gs -dBATCH -dNumRenderingThreads=2 -dEPSCrop -dNOPAUSE -sDEVICE=pngalpha -sOutputFile=tmp_.png -r~d -q tmp_.eps",[\PPI]))).
Daniel@0 82
Daniel@0 83 render_fig(Code,Opts) :-
Daniel@0 84 option(color_map(ColorMap),Opts),
Daniel@0 85 option(font_name(FontName),Opts),
Daniel@0 86 option(font_size(FontSize),Opts),
Daniel@0 87 option(line_width(LineWidth),Opts),
Daniel@0 88 option(marker_size(MarkerSize),Opts),
Daniel@0 89 option(colour(Colour),Opts,1),
Daniel@0 90 option(axes_line_width_ratio(LWR),Opts,0.5),
Daniel@0 91 option(figure(Fig),Opts,99),
Daniel@0 92 ml_async(exec( dml_paperfig(Fig,`FontName,FontSize,LineWidth,Colour,MarkerSize,LWR);
Daniel@0 93 colormap(ColorMap)),20),
Daniel@0 94 debug(matlab,'Calling: ~q',[Code]),
Daniel@0 95 with_output_to(string(_),call(Code)).
Daniel@0 96
Daniel@0 97 print_fig(Code,Dir,Opts,PrintOpt) :-
Daniel@0 98 render_fig(Code,Opts),
Daniel@0 99 debug(matlab,'Saving figure to ~w',[Dir]),
Daniel@0 100 absolute_file_name(Dir/'tmp_',Name),
Daniel@0 101 ( option(size(Width,Height),Opts)
Daniel@0 102 -> ml_async(exec(dml_fsetup(Width,Height,`centimeters)),20)
Daniel@0 103 ; true),
Daniel@0 104 ml_async(exec(print(`PrintOpt,`Name)),120).