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