Welcome to mirror list, hosted at ThFree Co, Russian Federation.

simpleimage.cpp « util « helper « mantaflow « extern - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 5f062bf0150a2e26a21e6ae9e6b753116c4a3175 (plain)
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
/******************************************************************************
 *
 * MantaFlow fluid solver framework
 * Copyright 2014 Tobias Pfaff, Nils Thuerey
 *
 * This program is free software, distributed under the terms of the
 * Apache License, Version 2.0
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Simple image IO
 *
 ******************************************************************************/

#include "vectorbase.h"
#include "simpleimage.h"

namespace Manta {

// write rectangle to ppm
bool SimpleImage::writePpm(
    std::string filename, int minx, int miny, int maxx, int maxy, bool invertXY)
{
  int w = maxx - minx;
  int h = maxy - miny;

  if (w <= 0 || h <= 0 || w > mSize[0] || h > mSize[1]) {
    errMsg("SimpleImage::WritePPM Invalid rect: w="
           << w << ", h=" << h << ", size=" << mSize[0] << "," << mSize[1] << " min/max: " << minx
           << "," << miny << " to " << maxx << "," << maxy << ", resetting... ");
    minx = miny = 0;
    maxx = mSize[0] - 1;
    maxy = mSize[1] - 1;
    w = mSize[0] - 1;
    h = mSize[1] - 1;
  }

  FILE *fp = fopen(filename.c_str(), "wb");
  if (fp == nullptr) {
    errMsg("SimpleImage::WritePPM Unable to open '" << filename << "' for writing");
    return false;
  }
  fprintf(fp, "P6\n%d %d\n255\n", w, h);

  int pixCnt = 0;
  for (int j = maxy - 1; j >= miny; j--)
    for (int i = minx; i < maxx; i++) {
      unsigned char col[3];
      for (int l = 0; l < 3; l++) {
        float val;
        if (invertXY)
          val = (float)get(j, i)[l];
        else
          val = (float)get(i, j)[l];

        val = clamp(val, (float)0., (float)1.);
        col[l] = (unsigned char)(255. * val);
      }
      // col[1] = col[2] = col[0];
      // if (fwrite(col,1,3, fp) != 3) errMsg("SimpleImage::writePpm fwrite failed");
      fwrite(col, 1, 3, fp);
      pixCnt++;
      // fprintf(stderr,"%d %d %d \n",col[0],i,j);
    }

  fclose(fp);
  // debMsg("WritePPM Wrote '"<<filename<<"', region="<<minx<<","<<miny<<" to
  // "<<maxx<<","<<maxy<<"; "<<pixCnt, 1);

  return true;
}

bool SimpleImage::writePpm(std::string filename)
{
  return writePpm(filename, 0, 0, getSize()[0], getSize()[1]);
}

// read in a ppm file, and init the image accordingly
bool SimpleImage::initFromPpm(std::string filename)
{
  // maximum length of a line of text
  const int MAXLINE = 1024;

  int filetype = 0;
  enum { PGM, PPM };  // possible file types

  FILE *fp;
  char line[MAXLINE];
  int size, rowsize;

  // Read in file type
  fp = fopen(filename.c_str(), "rb");
  if (!fp) {
    if (mAbortOnError)
      debMsg("SimpleImage Error - unable to open file '" << filename << "' for reading", 1);
    return 0;
  }

  // 1st line: PPM or PGM
  if (fgets(line, MAXLINE, fp) == nullptr) {
    if (mAbortOnError)
      debMsg("SimpleImage::initFromPpm fgets failed", 1);
    return 0;
  }

  if (line[1] == '5')
    filetype = PGM;
  else if (line[1] == '6')
    filetype = PPM;
  else {
    if (mAbortOnError)
      debMsg("SimpleImage Error: need PPM or PGM file as input!", 1);
    return 0;
  }

  // Read in width and height, & allocate space
  // 2nd line: width height
  if (fgets(line, MAXLINE, fp) == nullptr) {
    if (mAbortOnError)
      errMsg("SimpleImage::initFromPpm fgets failed");
    return 0;
  }
  int windW = 0, windH = 0;  // size of the window on the screen
  int intsFound = sscanf(line, "%d %d", &windW, &windH);
  if (intsFound == 1) {
    // only X found, search on next line as well for Y...
    if (sscanf(line, "%d", &windH) != 1) {
      if (mAbortOnError)
        errMsg("initFromPpm Ppm dimensions not found!" << windW << "," << windH);
      return 0;
    }
    else {
      // ok, found 2 lines
      // debMsg("initFromPpm Ppm dimensions found!"<<windW<<","<<windH, 1);
    }
  }
  else if (intsFound == 2) {
    // ok!
  }
  else {
    if (mAbortOnError)
      errMsg("initFromPpm Ppm dimensions not found at all!" << windW << "," << windH);
    return 0;
  }

  if (filetype == PGM) {
    size = windH * windW;  // greymap: 1 byte per pixel
    rowsize = windW;
  }
  else {
    // filetype == PPM
    size = windH * windW * 3;  // pixmap: 3 bytes per pixel
    rowsize = windW * 3;
  }

  unsigned char *pic = new unsigned char[size];  // (GLubyte *)malloc (size);

  // Read in maximum value (ignore) , could be scanned with sscanf as well, but this should be
  // 255... 3rd line
  if (fgets(line, MAXLINE, fp) == nullptr) {
    if (mAbortOnError)
      errMsg("SimpleImage::initFromPpm fgets failed");
    return 0;
  }

  // Read in the pixel array row-by-row: 1st row = top scanline */
  unsigned char *ptr = nullptr;
  ptr = &pic[(windH - 1) * rowsize];
  for (int i = windH; i > 0; i--) {
    assertMsg(fread((void *)ptr, 1, rowsize, fp) == rowsize,
              "SimpleImage::initFromPpm couldn't read data");
    ptr -= rowsize;
  }

  // init image
  this->init(windW, windH);
  if (filetype == PGM) {
    // grayscale
    for (int i = 0; i < windW; i++) {
      for (int j = 0; j < windH; j++) {
        double r = (double)pic[(j * windW + i) * 1 + 0] / 255.;
        (*this)(i, j) = Vec3(r, r, r);
      }
    }
  }
  else {
    // convert grid to RGB vec's
    for (int i = 0; i < windW; i++) {
      for (int j = 0; j < windH; j++) {
        // return mpData[y*mSize[0]+x];
        double r = (double)pic[(j * windW + i) * 3 + 0] / 255.;
        double g = (double)pic[(j * windW + i) * 3 + 1] / 255.;
        double b = (double)pic[(j * windW + i) * 3 + 2] / 255.;

        //(*this)(i,j) = Vec3(r,g,b);

        // RGB values have to be rotated to get the right colors!?
        // this might also be an artifact of photoshop export...?
        (*this)(i, j) = Vec3(g, b, r);
      }
    }
  }

  delete[] pic;
  fclose(fp);
  return 1;
}

// check index is valid
bool SimpleImage::indexIsValid(int i, int j)
{
  if (i < 0)
    return false;
  if (j < 0)
    return false;
  if (i >= mSize[0])
    return false;
  if (j >= mSize[1])
    return false;
  return true;
}

};  // namespace Manta

//*****************************************************************************

#include "grid.h"
namespace Manta {

// simple shaded output , note requires grid functionality!
static void gridPrecompLight(const Grid<Real> &density, Grid<Real> &L, Vec3 light = Vec3(1, 1, 1))
{
  FOR_IJK(density)
  {
    Vec3 n = getGradient(density, i, j, k) * -1.;
    normalize(n);

    Real d = dot(light, n);
    L(i, j, k) = d;
  }
}

// simple shading with pre-computed gradient
static inline void shadeCell(
    Vec3 &dst, int shadeMode, Real src, Real light, int depthPos, Real depthInv)
{
  switch (shadeMode) {

    case 1: {
      // surfaces
      Vec3 ambient = Vec3(0.1, 0.1, 0.1);
      Vec3 diffuse = Vec3(0.9, 0.9, 0.9);
      Real alpha = src;

      // different color for depth?
      diffuse[0] *= ((Real)depthPos * depthInv) * 0.7 + 0.3;
      diffuse[1] *= ((Real)depthPos * depthInv) * 0.7 + 0.3;

      Vec3 col = ambient + diffuse * light;

      // img( 0+i, j ) = (1.-alpha) * img( 0+i, j ) + alpha * col;
      dst = (1. - alpha) * dst + alpha * col;
    } break;

    default: {
      // volumetrics / smoke
      dst += depthInv * Vec3(src, src, src);
    } break;
  }
}

//! helper to project a grid intro an image (used for ppm export and GUI displauy)
void projectImg(SimpleImage &img, const Grid<Real> &val, int shadeMode = 0, Real scale = 1.)
{
  Vec3i s = val.getSize();
  Vec3 si = Vec3(1. / (Real)s[0], 1. / (Real)s[1], 1. / (Real)s[2]);

  // init image size
  int imgSx = s[0];
  if (val.is3D())
    imgSx += s[2] + s[0];  // mult views in 3D
  img.init(imgSx, std::max(s[0], std::max(s[1], s[2])));

  // precompute lighting
  Grid<Real> L(val);
  gridPrecompLight(val, L, Vec3(1, 1, 1));

  FOR_IJK(val)
  {
    Vec3i idx(i, j, k);
    shadeCell(img(0 + i, j), shadeMode, val(idx), L(idx), k, si[2]);
  }

  if (val.is3D()) {

    FOR_IJK(val)
    {
      Vec3i idx(i, j, k);
      shadeCell(img(s[0] + k, j), shadeMode, val(idx), L(idx), i, si[0]);
    }

    FOR_IJK(val)
    {
      Vec3i idx(i, j, k);
      shadeCell(img(s[0] + s[2] + i, k), shadeMode, val(idx), L(idx), j, si[1]);
    }

  }  // 3d

  img.mapRange(1. / scale);
}

};  // namespace Manta