PolyBoRi
draw_matrix.h
Go to the documentation of this file.
1 // -*- c++ -*-
2 //*****************************************************************************
14 //*****************************************************************************
15 
16 #ifndef polybori_groebner_draw_matrix_h_
17 #define polybori_groebner_draw_matrix_h_
18 
19 // include basic definitions
20 #include "groebner_defs.h"
21 
22 #ifdef PBORI_HAVE_GD
23 #include <stdio.h>
24 #include <gd.h>
25 #endif
26 
27 #ifdef PBORI_HAVE_M4RI_PNG
28 #include <m4ri/io.h>
29 #include <stdexcept>
30 #endif
31 
33 
34 
35 inline void
36 draw_matrix(mzd_t* mat, const char* filename){
37 
38  if ((mat->ncols == 0) || (mat->nrows == 0)) {
39  std::cerr << "0-dimensional matrix cannot be drawed, skipping "<< filename<<"" <<std::endl;
40  return;
41  }
42 
43 #ifdef PBORI_HAVE_M4RI_PNG
44 
45  int err = mzd_to_png(mat, filename, 9, "Generated by PolyBoRi", 0);
46  assert(err == 0);
47 
48  if (err)
49  throw std::runtime_error("Error writing png");
50 
51 #elif defined(PBORI_HAVE_GD)
52  int i,r,c,j;
53  c=mat->ncols;
54  r=mat->nrows;
55  gdImagePtr im = gdImageCreate(c, r) ;
56  FILE * out = fopen(filename, "wb") ;
57  int black = gdImageColorAllocate(im, 0, 0, 0) ;
58  int white = gdImageColorAllocate(im, 255, 255, 255);
59  gdImageFilledRectangle(im, 0, 0, c-1, r-1, white) ;
60 
61  for(i=0;i<r;i++){
62  for(j=0;j<c;j++){
63  if (mzd_read_bit(mat, i, j))
64  gdImageSetPixel(im, j, i, black );
65  }
66  }
67 
68  gdImagePng(im, out);
69  gdImageDestroy(im);
70  fclose(out);
71 
72 #else
73  std::cerr<<"warning: for drawing matrices compile with png support";
74 #endif
75 
76 }
77 
79 
80 #endif /* polybori_groebner_draw_matrix_h_ */
#define END_NAMESPACE_PBORIGB
Definition: groebner_defs.h:16
void draw_matrix(mzd_t *mat, const char *filename)
Definition: draw_matrix.h:36
#define BEGIN_NAMESPACE_PBORIGB
Definition: groebner_defs.h:15