gmsh-TingyuanDoc  0.1
An Open-Source Timing-driven Analytical Mixed-size FPGA Placer
ReadImg.cpp
Go to the documentation of this file.
1 // Gmsh - Copyright (C) 1997-2022 C. Geuzaine, J.-F. Remacle
2 //
3 // See the LICENSE.txt file in the Gmsh root directory for license information.
4 // Please report all issues on https://gitlab.onelab.info/gmsh/gmsh/issues.
5 
6 #include <string.h>
7 #include "ReadImg.h"
8 #include "GmshMessage.h"
9 #include "PView.h"
10 #include "PViewDataList.h"
11 #include "GmshConfig.h"
12 
13 #if defined(HAVE_FLTK)
14 
15 #include <FL/Fl_JPEG_Image.H>
16 #include <FL/Fl_PNM_Image.H>
17 #include <FL/Fl_PNG_Image.H>
18 #include <FL/Fl_BMP_Image.H>
19 
20 // from an image, we create a post-procession view
21 
22 static PViewDataList *Img2Data(Fl_RGB_Image &img_init, int quads = 1,
23  int resizex = 0, int resizey = 0)
24 {
25  img_init.desaturate(); // convert to grayscale
26 
27  // resize if necessary
28  Fl_RGB_Image *img;
29  if(!resizex || !resizey)
30  img = (Fl_RGB_Image *)img_init.copy();
31  else
32  img = (Fl_RGB_Image *)img_init.copy(resizex, resizey);
33 
34  const unsigned char *data = img->array;
35  int height = img->h();
36  int width = img->w();
37  int dim = img->d();
38 
39  if(dim != 1 && dim != 2) {
40  Msg::Error("Unable to obtain one-channel image");
41  return nullptr;
42  }
43 
44  PViewDataList *d = new PViewDataList();
45 
46  double z = 0.;
47  for(int i = 0; i < height - 1; i++) {
48  const unsigned char *a = data + i * width * dim;
49  const unsigned char *a1 = data + (i + 1) * width * dim;
50  double y = height - i - 1;
51  double y1 = height - i - 2;
52  for(int j = 0; j < width - 1; j++) {
53  double x = j;
54  double x1 = j + 1;
55  double val1 = (double)a[j * dim] / 255.;
56  double val2 = (double)a1[j * dim] / 255.;
57  double val3 = (double)a1[(j + 1) * dim] / 255.;
58  double val4 = (double)a[(j + 1) * dim] / 255.;
59  if(quads) { // generate quads
60  d->SQ.push_back(x);
61  d->SQ.push_back(x);
62  d->SQ.push_back(x1);
63  d->SQ.push_back(x1);
64  d->SQ.push_back(y);
65  d->SQ.push_back(y1);
66  d->SQ.push_back(y1);
67  d->SQ.push_back(y);
68  d->SQ.push_back(z);
69  d->SQ.push_back(z);
70  d->SQ.push_back(z);
71  d->SQ.push_back(z);
72  d->SQ.push_back(val1);
73  d->SQ.push_back(val2);
74  d->SQ.push_back(val3);
75  d->SQ.push_back(val4);
76  d->NbSQ++;
77  }
78  else { // generate triangles
79  d->ST.push_back(x);
80  d->ST.push_back(x);
81  d->ST.push_back(x1);
82  d->ST.push_back(y);
83  d->ST.push_back(y1);
84  d->ST.push_back(y1);
85  d->ST.push_back(z);
86  d->ST.push_back(z);
87  d->ST.push_back(z);
88  d->ST.push_back(val1);
89  d->ST.push_back(val2);
90  d->ST.push_back(val3);
91  d->NbST++;
92  d->ST.push_back(x);
93  d->ST.push_back(x1);
94  d->ST.push_back(x1);
95  d->ST.push_back(y);
96  d->ST.push_back(y1);
97  d->ST.push_back(y);
98  d->ST.push_back(z);
99  d->ST.push_back(z);
100  d->ST.push_back(z);
101  d->ST.push_back(val1);
102  d->ST.push_back(val3);
103  d->ST.push_back(val4);
104  d->NbST++;
105  }
106  }
107  }
108  delete img;
109  return d;
110 }
111 
112 static int EndPos(const char *name, PViewData *d)
113 {
114  if(!d) return 0;
115  char name_pos[256], title[256];
116  strcpy(name_pos, name);
117  strcat(name_pos, ".pos");
118  int i;
119  for(i = strlen(name) - 1; i >= 0; i--) {
120  if(name[i] == '/' || name[i] == '\\') break;
121  }
122  if(i <= 0)
123  strcpy(title, name);
124  else
125  strcpy(title, &name[i + 1]);
126  d->setName(title);
127  d->setFileName(name_pos);
128  if(d->finalize()) {
129  new PView(d);
130  return 1;
131  }
132  else {
133  delete d;
134  return 0;
135  }
136 }
137 
138 int read_pnm(const std::string &fileName)
139 {
140  Fl_PNM_Image img(fileName.c_str());
141  return EndPos(fileName.c_str(), Img2Data(img));
142 }
143 
144 int read_jpeg(const std::string &fileName)
145 {
146  Fl_JPEG_Image img(fileName.c_str());
147  return EndPos(fileName.c_str(), Img2Data(img));
148 }
149 
150 int read_png(const std::string &fileName)
151 {
152  Fl_PNG_Image img(fileName.c_str());
153  return EndPos(fileName.c_str(), Img2Data(img));
154 }
155 
156 int read_bmp(const std::string &fileName)
157 {
158  Fl_BMP_Image img(fileName.c_str());
159  return EndPos(fileName.c_str(), Img2Data(img));
160 }
161 
162 #else
163 
164 int read_pnm(std::string fileName) { return 0; }
165 int read_jpeg(std::string fileName) { return 0; }
166 int read_png(std::string fileName) { return 0; }
167 int read_bmp(std::string fileName) { return 0; }
168 
169 #endif
PView
Definition: PView.h:27
read_pnm
int read_pnm(std::string fileName)
Definition: ReadImg.cpp:164
PViewDataList
Definition: PViewDataList.h:17
Msg::Error
static void Error(const char *fmt,...)
Definition: GmshMessage.cpp:482
ReadImg.h
PViewDataList::NbSQ
int NbSQ
Definition: PViewDataList.h:32
PView.h
GmshMessage.h
PViewData::setFileName
virtual void setFileName(const std::string &val)
Definition: PViewData.h:75
read_bmp
int read_bmp(std::string fileName)
Definition: ReadImg.cpp:167
a1
const double a1
Definition: GaussQuadratureHex.cpp:10
PViewDataList.h
PViewData::setName
virtual void setName(const std::string &val)
Definition: PViewData.h:71
read_png
int read_png(std::string fileName)
Definition: ReadImg.cpp:166
read_jpeg
int read_jpeg(std::string fileName)
Definition: ReadImg.cpp:165
PViewDataList::ST
std::vector< double > ST
Definition: PViewDataList.h:31
PViewData
Definition: PViewData.h:29
z
const double z
Definition: GaussQuadratureQuad.cpp:56
PViewDataList::SQ
std::vector< double > SQ
Definition: PViewDataList.h:33
PViewData::finalize
virtual bool finalize(bool computeMinMax=true, const std::string &interpolationScheme="")
Definition: PViewData.cpp:30
PViewDataList::NbST
int NbST
Definition: PViewDataList.h:30