File: ReadImg.cpp

package info (click to toggle)
gmsh 2.4.2.dfsg-5
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 15,620 kB
  • ctags: 24,598
  • sloc: cpp: 198,691; ansic: 42,228; yacc: 3,727; perl: 672; fortran: 531; python: 391; lex: 341; sh: 140; makefile: 59; modula3: 32
file content (146 lines) | stat: -rw-r--r-- 3,975 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
// Gmsh - Copyright (C) 1997-2009 C. Geuzaine, J.-F. Remacle
//
// See the LICENSE.txt file for license information. Please report all
// bugs and problems to <gmsh@geuz.org>.

#include <string.h>
#include "ReadImg.h"
#include "GmshMessage.h"
#include "PView.h"
#include "PViewDataList.h"
#include "GmshConfig.h"

#if defined(HAVE_FLTK)

#include <FL/Fl_JPEG_Image.H>
#include <FL/Fl_PNM_Image.H>
#include <FL/Fl_PNG_Image.H>
#include <FL/Fl_BMP_Image.H>

// from an image, we create a post-procession view

static PViewDataList *Img2Data(Fl_RGB_Image &img_init, int quads=1,
                               int resizex=0, int resizey=0) 
{
  img_init.desaturate(); // convert to grayscale

  // resize if necessary
  Fl_RGB_Image *img;
  if(!resizex || !resizey)
    img = (Fl_RGB_Image*)img_init.copy();
  else
    img = (Fl_RGB_Image*)img_init.copy(resizex, resizey);

  const unsigned char *data = img->array;
  int height = img->h();
  int width = img->w();
  int dim = img->d();

  if(dim != 1) {
    Msg::Error("Unable to obtain one-channel image");
    return 0;
  }

  PViewDataList *d = new PViewDataList();

  double z = 0.;
  for(int i = 0; i < height - 1; i++) {
    const unsigned char *a = data + i * width * dim;
    const unsigned char *a1 = data + (i + 1) * width * dim;
    double y = height - i - 1;
    double y1 = height - i - 2;
    for(int j = 0; j < width - 1; j++) {
      double x = j;
      double x1 = j + 1;
      double val1 = (double)a[j]/255.;
      double val2 = (double)a1[j]/255.;
      double val3 = (double)a1[j + 1]/255.;
      double val4 = (double)a[j + 1]/255.;
      if(quads){ // generate quads
        d->SQ.push_back(x); d->SQ.push_back(x); 
        d->SQ.push_back(x1); d->SQ.push_back(x1);
        d->SQ.push_back(y); d->SQ.push_back(y1);
        d->SQ.push_back(y1); d->SQ.push_back(y);
        d->SQ.push_back(z); d->SQ.push_back(z);
        d->SQ.push_back(z); d->SQ.push_back(z);
        d->SQ.push_back(val1); d->SQ.push_back(val2);
        d->SQ.push_back(val3); d->SQ.push_back(val4);
        d->NbSQ++;
      }
      else{ // generate triangles
        d->ST.push_back(x); d->ST.push_back(x); d->ST.push_back(x1);
        d->ST.push_back(y); d->ST.push_back(y1); d->ST.push_back(y1);
        d->ST.push_back(z); d->ST.push_back(z); d->ST.push_back(z);
        d->ST.push_back(val1); d->ST.push_back(val2); d->ST.push_back(val3);
        d->NbST++;
        d->ST.push_back(x); d->ST.push_back(x1); d->ST.push_back(x1);
        d->ST.push_back(y); d->ST.push_back(y1); d->ST.push_back(y);
        d->ST.push_back(z); d->ST.push_back(z); d->ST.push_back(z);
        d->ST.push_back(val1); d->ST.push_back(val3); d->ST.push_back(val4);
        d->NbST++;
      }
    }
  }
  delete img;
  return d;
}

static int EndPos(const char *name, PViewData *d)
{
  if(!d) return 0;
  char name_pos[256], title[256];
  strcpy(name_pos, name);
  strcat(name_pos, ".pos");
  int i;
  for(i = strlen(name) - 1; i >= 0; i--){
    if(name[i] == '/' || name[i] == '\\')
      break;
  }
  if(i <= 0)
    strcpy(title, name);
  else
    strcpy(title, &name[i + 1]);
  d->setName(title);
  d->setFileName(name_pos);
  if(d->finalize()){
    new PView(d);
    return 1;
  }
  else{
    delete d;
    return 0;
  }
}

int read_pnm(std::string fileName) 
{
  Fl_PNM_Image img(fileName.c_str());
  return EndPos(fileName.c_str(), Img2Data(img));
}

int read_jpeg(std::string fileName) 
{
  Fl_JPEG_Image img(fileName.c_str());
  return EndPos(fileName.c_str(), Img2Data(img));
}

int read_png(std::string fileName) 
{
  Fl_PNG_Image img(fileName.c_str());
  return EndPos(fileName.c_str(), Img2Data(img));
}

int read_bmp(std::string fileName) 
{
  Fl_BMP_Image img(fileName.c_str());
  return EndPos(fileName.c_str(), Img2Data(img));
}

#else

int read_pnm(std::string fileName){ return 0; }
int read_jpeg(std::string fileName){ return 0; }
int read_png(std::string fileName){ return 0; }
int read_bmp(std::string fileName){ return 0; }

#endif