~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~ [ freetext search ] ~ [ file search ] ~

Linux Cross Reference
Tina6/tina-tools/tinatool/tlmedical/tlmedAnl_report.c

Version: ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

  1 /**********
  2  *
  3  * Copyright (c) 2003, Division of Imaging Science and Biomedical Engineering,
  4  * University of Manchester, UK.  All rights reserved.
  5  * 
  6  * Redistribution and use in source and binary forms, with or without modification, 
  7  * are permitted provided that the following conditions are met:
  8  * 
  9  *   . Redistributions of source code must retain the above copyright notice, 
 10  *     this list of conditions and the following disclaimer.
 11  *    
 12  *   . Redistributions in binary form must reproduce the above copyright notice,
 13  *     this list of conditions and the following disclaimer in the documentation 
 14  *     and/or other materials provided with the distribution.
 15  * 
 16  *   . Neither the name of the University of Manchester nor the names of its
 17  *     contributors may be used to endorse or promote products derived from this 
 18  *     software without specific prior written permission.
 19  * 
 20  * 
 21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 
 22  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 
 23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 
 24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 
 25  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 
 26  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 
 27  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 28  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 
 29  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 
 30  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
 31  * POSSIBILITY OF SUCH DAMAGE.
 32  *
 33  **********
 34  *
 35  * Program :    TINA
 36  * File    :  $Source: /home/tina/cvs/tina-tools/tinatool/tlmedical/tlmedAnl_report.c,v $
 37  * Date    :  $Date: 2007/02/15 01:55:50 $
 38  * Version :  $Revision: 1.5 $
 39  * CVS Id  :  $Id: tlmedAnl_report.c,v 1.5 2007/02/15 01:55:50 paul Exp $
 40  *
 41  * Notes :
 42  *
 43  *********
 44 */
 45 
 46 #include "tlmedAnl_report.h"
 47 
 48 #if HAVE_CONFIG_H
 49 #   include <config.h>
 50 #endif
 51 
 52 #include <stdio.h>
 53 #include <math.h>
 54 #include <tina/sys/sysDef.h>
 55 #include <tina/sys/sysPro.h>
 56 #include <tina/math/mathDef.h>
 57 #include <tina/math/mathPro.h>
 58 #include <tina/image/imgDef.h>
 59 #include <tina/image/imgPro.h>
 60 #include <tina/file/filePro.h>
 61 #include <tina/medical/medDef.h>
 62 #include <tina/medical/medPro.h>
 63 #include <tinatool/tlbase/tlbaseDef.h>
 64 #include <tinatool/tlbase/tlbasePro.h>
 65 #include <tinatool/draw/drawPlot_gnu.h>
 66 #include <tinatool/tlmedical/tlmedAnl_mouse.h>
 67 
 68 static char  c_convert[256] = {"convert"};
 69 static char  c_latex[256] = {"latex"};
 70 static char  c_dvips[256] = {"dvips"};
 71 static char  c_tidy[256] = {"rm -f"};
 72 
 73 static char     comm[512];
 74 static float    flow, area, pvol, tpvol;
 75 static float    pflow, tpflow, hrate;
 76 static int      pixs;
 77 
 78 
 79 static void  gen_flow_graph(Pl_flow *p, char *report)
 80 {
 81   FILE    *fp;
 82   int      i;
 83   char    *name;
 84 
 85   open_gnuplot_program_link("gnuplot");
 86   name = (char *)get_gnuplot_tname();
 87   send_gnuplot_comm("set nokey\n");
 88   send_gnuplot_comm("set term post\n");
 89   send_gnuplot_comm("set xlabel \"time (secs)\"\n");
 90   send_gnuplot_comm("set ylabel \"Flowrate (ml/sec)\"\n");
 91   sprintf(comm, "set output \"%s_graph.ps\"\n", report);
 92   send_gnuplot_comm(comm);
 93   sprintf(comm, "plot \"%s\" with linespoints\n", name);
 94   
 95   fp = (FILE *)open_gnuplot_data_link(comm);
 96   for (i = p->x_n1; i < p->x_n2; i++)
 97     {
 98       fprintf(fp, "%f %f\n", p->x[i], p->y1[i]);
 99       fflush(fp);
100     }
101 
102   close_gnuplot_link(NULL);
103 }
104 
105 
106 static Bool gen_flow_images(Pl_flow *p, char *report)
107 {
108   List  *store = (List *)get_current_seq_start_el();
109   Imrect  *im = NULL, *tim = NULL;
110 
111   if ((store == NULL) || (store->to == NULL))
112     return (false);
113   im = (Imrect *)(store->to);
114   
115   sprintf(comm, "%s_image.pgm", report);  
116   pgm_write_image(im, comm);
117   sprintf(comm, "%s %s_image.pgm %s_image.eps\n", c_convert, report, report);
118   system(comm);
119   sprintf(comm, "rm %s_image.pgm", report);
120   system(comm);
121 
122   if (p->mask != NULL)
123     {
124       sprintf(comm, "%s_mask.pgm", report);
125       tim = im_times(255.0, p->mask);  
126       pgm_write_image(tim, comm);
127       sprintf(comm, "%s %s_mask.pgm %s_mask.eps\n", c_convert, report, 
128               report);
129       system(comm);
130       sprintf(comm, "rm %s_mask.pgm", report);
131       system(comm);
132       im_free(tim);
133     }
134 
135   return (true);
136 }
137 
138 
139 static void  rep_sepline(FILE *fp)
140 {
141   fprintf(fp, "\\vspace{4mm}\n\n");
142 }
143 
144 
145 static void  rep_end_doc(FILE *fp)
146 {
147   fprintf(fp, "\\end{document}");
148 }
149 
150 static void  rep_latex_preamble(FILE *fp)
151 {
152   fprintf(fp, "%%\n%% TINA flow report\n%% Autogenerated - a.lacey@server1.smb.man.ac.uk\n%\n");
153   fprintf(fp, "\n\\documentclass{tina_rep}\n\\usepackage[dvips]{graphicx}\n");
154   fprintf(fp, "\\usepackage{subfigure}\n");
155   fprintf(fp, "\\begin{document}\n\n");
156   fprintf(fp, "\\centering{\\bf {\\Large Flow Report}}\n");
157 
158 }
159 
160 
161 static void rep_text_data(FILE *fp, char *patient, char *notes)
162 {
163   char    *date = NULL;
164   Sequence * seq = seq_get_current();
165 
166   if (seq != NULL)
167     {
168       date = (char *)prop_get(seq->props, PAT_DATA);
169     }
170   fprintf(fp, "%%\n%% Text data\n%\n\n");
171         fprintf(fp, "\\begin{center}\n");
172   fprintf(fp, "\\begin{tabular}{|ll|}\n");
173   fprintf(fp, "\\hline\n");
174   fprintf(fp, "{\\bf Patient:} & %s \\\\ \n", patient);
175         if (date != NULL)
176                 fprintf(fp, "{\\bf Study Date:} & %s \\\\ \n", date);
177   fprintf(fp, "{\\bf Notes:} & %s \\\\ \n", notes);
178   fprintf(fp, "\\hline\n");
179   fprintf(fp, "\\end{tabular}\n");
180   fprintf(fp, "\\end{center}\n\n");
181 }
182 
183 
184 static void rep_table_data(FILE *fp, Pl_flow *p)
185 {
186   /*  List  *store = (List *)get_seq_start_el(seq);*/
187   Vec3    *scale = NULL;
188   Sequence * seq = NULL;
189   /*if (store != NULL)
190     im = (Imrect *)(store->to);
191     if (im != NULL)*/
192   if ((seq = (Sequence *)seq_get_current()) != NULL)
193     scale = (Vec3 *)seq_dim_to_vec3();
194 
195   pixs = calc_mask_size(p->mask);
196   area = (scale->el[0])*(scale->el[1])*(double)pixs;
197   hrate = 60.0/(p->x[(p->x_n2-1)]-p->x[(p->x_n1)]);
198   flow = p->y1_area*hrate;
199   pflow = p->y1_diff_max;
200   pvol = p->y1_max;
201   tpvol = p->y1_max_t*(p->x[p->x_n1+1]-p->x[p->x_n1]);
202   tpflow = p->y1_diff_max_t*(p->x[p->x_n1+1]-p->x[p->x_n1]);
203  
204   fprintf(fp, "%%\n%% Table data\n%%\n\n");
205         fprintf(fp, "\\begin{center}\n");
206   fprintf(fp, "\\begin{tabular}{|l|l|l|}\n");
207   fprintf(fp, "\\hline\n");
208   fprintf(fp, "{\\bf C.S. Area} & %4.2f $mm^2$ & %d pixels\\\\ \n", area, pixs);  
209   fprintf(fp, "\\hline\n");
210   fprintf(fp, "{\\bf Total Flow } & %4.2f $ml$ & in 1 minute\\\\ \n", flow);  
211   fprintf(fp, "\\hline\n");
212   fprintf(fp, "{\\bf Peak Flowrate} & %4.2f $ml/sec$ & @%4.4f $secs$ \\\\ \n", 
213           pvol, tpvol);  
214   fprintf(fp, "\\hline\n");
215   fprintf(fp, "{\\bf Heart Rate} & %4.2f $bpm$ & - \\\\ \n", hrate);
216   fprintf(fp, "\\hline\n");
217   fprintf(fp, "\\end{tabular}\n");
218   fprintf(fp, "\\end{center}\n\n");
219 }
220 
221 
222 static void  rep_image_data(FILE *fp, Pl_flow *p, char *report, Bool pim)
223 {
224   fprintf(fp, "%%\n%% Table & image\n%%\n\n");
225   fprintf(fp, "\\begin{figure}[ht]\n");
226   if (pim && (p->mask != NULL))
227     {
228       fprintf(fp, "\\centering\n");
229       fprintf(fp, "\\begin{minipage}[c]{0.4\\textwidth}\n");
230       fprintf(fp, "\\centering\n");
231       fprintf(fp, "\\includegraphics[width=2in]{%s_image.eps}\n",report);
232       fprintf(fp, "\\caption{1st image in sequence}\n");  
233       fprintf(fp, "\\end{minipage}\n");
234 
235       fprintf(fp, "\\begin{minipage}[c]{0.4\\textwidth}\n");
236       fprintf(fp, "\\centering\n");
237       fprintf(fp, "\\includegraphics[width=2in]{%s_mask.eps}\n",report);
238       fprintf(fp, "\\caption{Image mask}\n");  
239       fprintf(fp, "\\end{minipage}\n");
240     }
241   else if (pim && (p->mask == NULL))
242     {
243       fprintf(fp, "\\centering{\\includegraphics[width=4in]{%s_image.eps}}\n", 
244               report);
245       fprintf(fp, "\\caption{Image}\n");
246     }
247   else   
248     fprintf(fp, "\\centering{No Image}\n");
249   fprintf(fp, "\\end{figure}\n\n");
250 }
251 
252 
253 
254 static void  rep_graph(FILE *fp, char *report)
255 {
256   fprintf(fp, "%%\n%%Graph\n%%\n\n");
257   fprintf(fp, "\\begin{figure}[ht]\n");
258   fprintf(fp, "\\centering{\\includegraphics[height=4.5in,angle=-90]{%s_graph.ps}}\n", report);
259   fprintf(fp, "\\caption{Flowrate graph}\n");
260   fprintf(fp, "\\end{figure}\n\n");
261 }
262 
263 
264 static void  build_postscript(char *report)
265 {
266   sprintf(comm, "%s %s_report > /dev/null", c_latex, report);
267   system(comm);
268   system(comm);
269   sprintf(comm, "%s -o%s.ps %s_report.dvi > /dev/null", c_dvips, report, report);
270   system(comm);
271 }
272 
273 
274 static void  tidy_files(char *report)
275 {
276   sprintf(comm, "%s %s_report.dvi > /dev/null", c_tidy, report);
277   system(comm);
278   sprintf(comm, "%s %s_report.tex > /dev/null", c_tidy, report);
279   system(comm);
280   sprintf(comm, "%s %s_report.aux > /dev/null", c_tidy, report);
281   system(comm);
282   sprintf(comm, "%s %s_report.log > /dev/null", c_tidy, report);
283   system(comm);
284   sprintf(comm, "%s %s_graph.ps > /dev/null", c_tidy, report);
285   system(comm);
286   sprintf(comm, "%s %s_image.eps > /dev/null", c_tidy, report);
287   system(comm);
288   sprintf(comm, "%s %s_mask.eps > /dev/null", c_tidy, report);
289   system(comm);
290 }
291 
292 
293 void   flow_report(char *report, char *patient, char *notes)
294 {
295   Pl_flow *p = get_pl_flow();
296   FILE    *fp;
297 
298   gen_flow_graph(p, report);
299 
300   sprintf(comm, "%s_report.tex", report);
301   if ((fp = fopen(comm, "w")) == NULL)
302     return;
303 
304   rep_latex_preamble(fp);
305   rep_sepline(fp);
306 
307   rep_text_data(fp, patient, notes);
308   rep_sepline(fp);
309 
310   rep_image_data(fp, p, report, gen_flow_images(p, report));
311   rep_sepline(fp);
312 
313   rep_table_data(fp, p);
314   rep_sepline(fp);
315 
316   rep_graph(fp, report);
317   
318   rep_end_doc(fp);
319 
320   fclose(fp);
321 
322   build_postscript(report);
323  
324   tidy_files(report);
325 }
326 

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~ [ freetext search ] ~ [ file search ] ~

This page was automatically generated by the LXR engine.
Visit the LXR main site for more information.