changeset 17120:c43fe083a34b

Change image format for extracting data from PPM to PNG. Allowed fontsize, fontname and color text properties for Latex. * latex.cc, latex.h: GhostScript is using PNG as output format. Added color propertie, font size and name properties. * configure.ac: Chceck if there is libpng on system. And linking to this library. * link-deps.mk: Added flags for libpng library.
author AndrejLojdl <andrej.lojdl@gmail.com>
date Fri, 16 Aug 2013 20:29:35 +0200
parents 0f6cfae86e85
children db53d3b6ac2f
files configure.ac libinterp/Makefile.am libinterp/corefcn/txt-latex.cc libinterp/corefcn/txt-latex.h libinterp/link-deps.mk
diffstat 5 files changed, 164 insertions(+), 42 deletions(-) [+]
line wrap: on
line diff
--- a/configure.ac
+++ b/configure.ac
@@ -712,6 +712,17 @@
   AC_DEFINE(HAVE_ZLIB, 1, [Define to 1 if ZLIB is available.])
 fi
 
+### Check for LIBPNG library.
+
+OCTAVE_CHECK_LIB(png, PNG,
+  [LIBPNG library not found.  Octave will not be able to save or load PNG image format files.],
+  [libpng/png.h], [png_init_io])
+
+### Also define HAVE_PNG if libpng is found.
+if test $octave_cv_lib_png = yes; then
+  AC_DEFINE(HAVE_PNG, 1, [Define to 1 if LIBPNG is available.])
+fi
+
 ### Check for the LLVM library
 
 build_jit=no
--- a/libinterp/Makefile.am
+++ b/libinterp/Makefile.am
@@ -40,6 +40,8 @@
 
 AM_CXXFLAGS += $(WARN_CXXFLAGS)
 
+
+
 octlib_LTLIBRARIES = liboctinterp.la
 
 ## Order matters here.  Leave builtins.cc last, because it depends on
@@ -105,7 +107,7 @@
   parse-tree/oct-gperf.h \
   builtins.h \
   builtin-defun-decls.h \
-  octave.h \
+  octave.h \ 
   $(OCTAVE_VALUE_INC) \
   $(PARSE_TREE_INC) \
   $(PARSER_INC) \
--- a/libinterp/corefcn/txt-latex.cc
+++ b/libinterp/corefcn/txt-latex.cc
@@ -24,16 +24,23 @@
 #include <config.h>
 #endif
 
-#include"oct.h"
-#include"octave.h"
-#include"parse.h"
-#include"dim-vector.h"
-
 #include <iostream>
 #include <fstream>
 #include <string>
 #include <cstdlib>
+#include <cstring>
+
+#ifdef HAVE_ZLIB
+#include <zlib.h>
+#endif
+
+#ifdef HAVE_PNG
+#include <png.h>
+#endif
+
 #include "txt-latex.h"
+#include "oct.h"
+#include "dim-vector.h"
 
 
 latex_render::latex_render (void)
@@ -52,7 +59,8 @@
 
   file.open ("default.tex");
   /* Adding LaTeX configuration lines to this file */
-  file << "\\documentclass[fleqn]{article} \n\\usepackage{amsmath} \n\\usepackage{color} \n\\pagestyle{empty}	\n\\begin{document} \n\t\\normalsize \n\t\t";
+  file << "\\documentclass[fleqn]{article} \n\\usepackage{amsmath} \n\\usepackage{color} \n\\pagestyle{empty}	\n\\begin{document} \n\t\\fontsize{12pt}{12pt}\\selectfont \n\t";
+  file << "\\usefont{T1}{" << fname << "}{m}{n} \n\t\t";
   file << txt;
   file << "\n\\end{document}";
   file.close();	
@@ -61,19 +69,19 @@
 uint8NDArray 
 latex_render::render (void)
 {
-  int cmd,width,height,max_color;
+  int cmd;
   std::string comment;
   uint8NDArray data;
   std::ifstream file;
-  char magic_number[2],skip;
+  char skip,resolution[100],command[200];
 
-  // Checking if command processor is available //
+  /* Check if command processor is available */
   if(system( NULL ))
     {
-      // .tex -> .dvi
+      /* .tex -> .dvi */
       cmd = system ("latex default.tex > latex.log");
 
-      // checking if there is error inside latex.log file
+      /* checking if there is sign of error inside latex.log file */
       file.open("latex.log");
 
       for(int i=0; i<22; i++)
@@ -88,48 +96,132 @@
 
           file.close();
 
-          // .dvi -> .eps
+          /* .dvi -> .eps */
           cmd = system ("dvips default.dvi -E -o default.eps -q");
 
-          // .eps -> .ppm
-          cmd = system ("gs -dEPSCrop -dSAFER -dBATCH -dNOPAUSE -r300 -sDEVICE=ppm -sOutputFile=default.ppm \"default.eps\" > ghostscript.log");
+          /* .eps -> .png */
+          fsize = fsize*(72/12);
+          sprintf(resolution, "%d", fsize);
+          sprintf(command, "gs -q -dEPSCrop -dSAFER -dBATCH -dNOPAUSE -r%s -dTextAlphaBits=4 -sDEVICE=pngalpha -sOutputFile=default.png \"default.eps\"", resolution) ;
 
-          // removing temporary files 
+          cmd = system (command);
+
+          /* removing temporary files */
           cmd = remove ("default.tex");
           cmd = remove ("default.dvi");
           cmd = remove ("default.log");
           cmd = remove ("default.aux");
           cmd = remove ("default.eps");
 
-          // read data from PPM file to Array for OpenGL rendering
-          file.open("default.ppm");
-          file>>magic_number>>skip;
-          std::getline(file, comment);
-          file>>width>>height;
+          /* read image from PNG file */
+          int x;
+          int y;
+
+          int width, height;
+          png_byte color_type;
+          png_byte bit_depth;
+
+          png_structp png_ptr;
+          png_infop info_ptr;
+          int number_of_passes;
+          png_bytep * row_pointers;
+
+          char header [8], name [12] = "default.png";
+
+          /* open file and test for it being a png */
+          FILE *fp = fopen(name, "rb");
+          if (!fp)
+            {
+              fprintf(stderr,"File default.png could not be opened for reading");
+              abort();
+            }
+          fread(header, 1, 8, fp);
+
+
+          /* initialize stuff */
+          png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
+
+          if (!png_ptr)
+            {
+              fprintf(stderr,"png_create_read_struct failed");
+              abort();
+            }
+
+          info_ptr = png_create_info_struct(png_ptr);
+          if (!info_ptr)
+            {
+              fprintf(stderr,"png_create_info_struct failed");
+              abort();
+            }
+
+          if (setjmp(png_jmpbuf(png_ptr)))
+            {
+              fprintf(stderr,"Error during init_io");
+              abort();
+            }
+
+          png_init_io(png_ptr, fp);
+          png_set_sig_bytes(png_ptr, 8);
+
+          png_read_info(png_ptr, info_ptr);
+
+          width = png_get_image_width(png_ptr, info_ptr);
+          height = png_get_image_height(png_ptr, info_ptr);
+          color_type = png_get_color_type(png_ptr, info_ptr);
+          bit_depth = png_get_bit_depth(png_ptr, info_ptr);
+
+          number_of_passes = png_set_interlace_handling(png_ptr);
+          png_read_update_info(png_ptr, info_ptr);
+
+          /* read file */
+          if (setjmp(png_jmpbuf(png_ptr)))
+            {
+              fprintf(stderr,"Error during read_image");
+              abort();
+            }
+
+          row_pointers = (png_bytep*) malloc(sizeof(png_bytep) * height);
+          for (y=0; y<height; y++)
+            row_pointers[y] = (png_byte*) malloc(png_get_rowbytes(png_ptr,info_ptr));
+
+          png_read_image(png_ptr, row_pointers);
+
+          fclose(fp);
+
+          /* instantise data array */
           data = uint8NDArray (dim_vector (4, width, height), static_cast<uint8_t> (0));
 
-          // reading the data, row by row 
-          for(int i=height-1; i>=0; i--)
+          for (int i=0; i<height; i++)
             {
-              for(int j=0; j<width; j++)
+              png_byte* row = row_pointers[height-1-i];
+              for (int j=0; j<width; j++)
                 {
-                  int r,g,b;
+                  png_byte* ptr = &(row[j*4]);
 
-                  file >> r >> g >> b;
-                  data(0,j,i) = r; //red
-                  data(1,j,i) = g; //green
-                  data(2,j,i) = b; //blue
-                  data(3,j,i) = 255;   //alpha -> transparent
+                  if(ptr[0] == 0 && ptr[1] == 0 && ptr[2] == 0 && ptr[3] == 0)
+                    {
+                      data(0,j,i) = red;
+                      data(1,j,i) = green;
+                      data(2,j,i) = blue;
+                      data(3,j,i) = 255;
+                    }
+                  else
+                    {
+                      data(0,j,i) = ptr[0];
+                      data(1,j,i) = ptr[1];
+                      data(2,j,i) = ptr[2];
+                      data(3,j,i) = ptr[3];                  	
+                    }
+
+
                 }
             }
-          file.close();
-          cmd = remove ("default.ppm");
-          cmd = remove ("latex.log");
-          cmd = remove ("ghostscript.log");
+          cmd = remove("default.png");
+
         }
       else
         {
-          std::cout<<"There is some problem with your string. For more information take a look at latex.log file in your working directory. \n";
+          std::cout<<"There is some problem with your string. For more information take a look at latex.log file in your working directory."<<std::endl;
           file.close();
         }
     }
@@ -145,10 +237,24 @@
 void
 latex_render::set_font (const std::string& name, const std::string& weight,
                         const std::string& angle, double size)
-{}
+{
+  fsize = size;
+  fname = name;
+}
 
 void
-latex_render::set_color (Matrix c) {}
+latex_render::set_color (Matrix c)
+{
+  if (c.numel () == 3)
+    {
+      red = static_cast<uint8_t> (c(0)*255);
+      green = static_cast<uint8_t> (c(1)*255);
+      blue = static_cast<uint8_t> (c(2)*255);
+    }
+  else
+    ::warning ("latex_render::set_color: invalid color");
+}
+
 
 void
 latex_render::text_to_pixels (const std::string& txt,
@@ -159,7 +265,7 @@
   pixels = render();
 
   if(pixels.ndims () < 3)
-    std::cout<<"Pixels variable not properly set. \n";
+    std::cout<<"Pixels variable not properly set."<<std::endl;
   else
     {
 
--- a/libinterp/corefcn/txt-latex.h
+++ b/libinterp/corefcn/txt-latex.h
@@ -23,6 +23,7 @@
 #if ! defined (txt_latex_h)
 #define txt_latex_h 1
 
+#include <string>
 #include <dMatrix.h>
 #include <uint8NDArray.h>
 #include "txt-render.h"
@@ -48,15 +49,15 @@
   Matrix get_extent (text_element *elt, double rotation = 0.0);
   Matrix get_extent (const std::string& txt, double rotation = 0.0);
 
-  /* remaining methods of latex_text_renderer class */
-
-  // method making TEX file from text (input string) 
+  /* method making TEX file from text (input string) */ 
   void adapter (const std::string& txt);
 
-  // method rendering text TEX -> DVI -> EPS -> PPM 
+  /* method rendering text TEX -> DVI -> EPS -> PNG -> RAW */ 
   uint8NDArray render (void);
 
 private:
+  int fsize;
+  std::string fname;
   Matrix bbox;
   uint8NDArray pixels;
   uint8_t red, green, blue;
--- a/libinterp/link-deps.mk
+++ b/libinterp/link-deps.mk
@@ -10,6 +10,7 @@
   $(FT2_LIBS) \
   $(HDF5_LIBS) \
   $(Z_LIBS) \
+  $(PNG_LIBS) \
   $(FFTW_XLIBS) \
   $(REGEX_LIBS) \
   $(OPENGL_LIBS) \
@@ -23,6 +24,7 @@
   $(FT2_LDFLAGS) \
   $(HDF5_LDFLAGS) \
   $(Z_LDFLAGS) \
+  $(PNG_LDFLAGS) \
   $(REGEX_LDFLAGS) \
   $(FFTW_XLDFLAGS) \
   $(LLVM_LDFLAGS)