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

IMAGE.h « intern « smoke « intern - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: a57f164509e0de3ad56f636cd70c40723a8e4d16 (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
//////////////////////////////////////////////////////////////////////
// This file is part of Wavelet Turbulence.
//
// Wavelet Turbulence is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Wavelet Turbulence is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Wavelet Turbulence.  If not, see <http://www.gnu.org/licenses/>.
//
// Copyright 2008 Theodore Kim and Nils Thuerey
//
//////////////////////////////////////////////////////////////////////
//
#ifndef IMAGE_H
#define IMAGE_H

#include <stdlib.h>
#include <string>
#include <fstream>
#include <sstream>
#include <zlib.h>

//////////////////////////////////////////////////////////////////////
// NT helper functions
//////////////////////////////////////////////////////////////////////
template < class T > inline T ABS( T a ) {
	return (0 < a) ? a : -a ;
}

template < class T > inline void SWAP_POINTERS( T &a, T &b ) {
	T temp = a;
	a = b;
	b = temp;
}

template < class T > inline void CLAMP( T &a, T b=0., T c=1.) {
	if(a<b) { a=b; return; }
	if(a>c) { a=c; return; }
}

template < class T > inline T MIN( T a, T b) {
	return (a < b) ? a : b;
}

template < class T > inline T MAX( T a, T b) {
	return (a > b) ? a : b;
}

template < class T > inline T MAX3( T a, T b, T c) {
	T max = (a > b) ? a : b;
	max = (max > c) ? max : c;
	return max;
}

template < class T > inline float MAX3V( T vec) {
	float max = (vec[0] > vec[1]) ? vec[0] : vec[1];
	max = (max > vec[2]) ? max : vec[2];
	return max;
}

template < class T > inline float MIN3V( T vec) {
	float min = (vec[0] < vec[1]) ? vec[0] : vec[1];
	min = (min < vec[2]) ? min : vec[2];
	return min;
}

//////////////////////////////////////////////////////////////////////
// PNG, POV-Ray, and PBRT output functions
//////////////////////////////////////////////////////////////////////
#include <png.h>

namespace IMAGE {
	/*
  static int writePng(const char *fileName, unsigned char **rowsp, int w, int h)
  {
    // defaults
    const int colortype = PNG_COLOR_TYPE_RGBA;
    const int bitdepth = 8;
    png_structp png_ptr = NULL;
    png_infop info_ptr = NULL;
    png_bytep *rows = rowsp;

    FILE *fp = NULL;
    std::string doing = "open for writing";
    if (!(fp = fopen(fileName, "wb"))) goto fail;

    if(!png_ptr) {
      doing = "create png write struct";
      if (!(png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL))) goto fail;
    }
    if(!info_ptr) {
      doing = "create png info struct";
      if (!(info_ptr = png_create_info_struct(png_ptr))) goto fail;
    }

    if (setjmp(png_jmpbuf(png_ptr))) goto fail;
    doing = "init IO";
    png_init_io(png_ptr, fp);
    doing = "write header";
    png_set_IHDR(png_ptr, info_ptr, w, h, bitdepth, colortype, PNG_INTERLACE_NONE,
        PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE);
    doing = "write info";
    png_write_info(png_ptr, info_ptr);
    doing = "write image";
    png_write_image(png_ptr, rows);
    doing = "write end";
    png_write_end(png_ptr, NULL);
    doing = "write destroy structs";
    png_destroy_write_struct(&png_ptr, &info_ptr);

    fclose( fp );
    return 0;

  fail:
    std::cerr << "writePng: could not "<<doing<<" !\n";
    if(fp) fclose( fp );
    if(png_ptr || info_ptr) png_destroy_write_struct(&png_ptr, &info_ptr);
    return -1;
  }
  */

  /////////////////////////////////////////////////////////////////////////////////
  // write a numbered PNG file out, padded with zeros up to three zeros
  /////////////////////////////////////////////////////////////////////////////////
  /*
  static void dumpNumberedPNG(int counter, std::string prefix, float* field, int xRes, int yRes)
  {
	char buffer[256];
    sprintf(buffer,"%04i", counter);
    std::string number = std::string(buffer);

    unsigned char pngbuf[xRes*yRes*4];
    unsigned char *rows[yRes];
    float *pfield = field;
    for (int j=0; j<yRes; j++) {
      for (int i=0; i<xRes; i++) {
        float val = *pfield;
        if(val>1.) val=1.;
        if(val<0.) val=0.;
        pngbuf[(j*xRes+i)*4+0] = (unsigned char)(val*255.);
        pngbuf[(j*xRes+i)*4+1] = (unsigned char)(val*255.);
        pngbuf[(j*xRes+i)*4+2] = (unsigned char)(val*255.);
        pfield++;
        pngbuf[(j*xRes+i)*4+3] = 255;
      }
      rows[j] = &pngbuf[(yRes-j-1)*xRes*4];
    }
    std::string filenamePNG = prefix + number + std::string(".png");
    writePng(filenamePNG.c_str(), rows, xRes, yRes, false);
    printf("Writing %s\n", filenamePNG.c_str());
   
  }
*/
  /////////////////////////////////////////////////////////////////////////////////
  // export pbrt volumegrid geometry object
  /////////////////////////////////////////////////////////////////////////////////
	/*
  static void dumpPBRT(int counter, std::string prefix, float* fieldOrg, int xRes, int yRes, int zRes)
  {
    char buffer[256];
    sprintf(buffer,"%04i", counter);
    std::string number = std::string(buffer);

    std::string filenamePbrt = prefix + number + std::string(".pbrt.gz");
    printf("Writing PBRT %s\n", filenamePbrt.c_str());

    float *field = new float[xRes*yRes*zRes];
    // normalize values
    float maxDensVal = ABS(fieldOrg[0]);
    float targetNorm = 0.5;
    for (int i = 0; i < xRes * yRes * zRes; i++) {
      if(ABS(fieldOrg[i])>maxDensVal) maxDensVal = ABS(fieldOrg[i]);
      field[i] = 0.;
    }
    if(maxDensVal>0.) {
      for (int i = 0; i < xRes * yRes * zRes; i++) {
        field[i] = ABS(fieldOrg[i]) / maxDensVal * targetNorm;
      }
    }

    std::fstream fout;
    fout.open(filenamePbrt.c_str(), std::ios::out);

    int maxRes = (xRes > yRes) ? xRes : yRes;
    maxRes = (maxRes > zRes) ? maxRes : zRes;

    const float xSize = 1.0 / (float)maxRes * (float)xRes;
    const float ySize = 1.0 / (float)maxRes * (float)yRes;
    const float zSize = 1.0 / (float)maxRes * (float)zRes;

    gzFile file;
    file = gzopen(filenamePbrt.c_str(), "wb1");
    if (file == NULL) {
      std::cerr << " Couldn't write file " << filenamePbrt << "!!!" << std::endl;
      return;
    }

    // dimensions
    gzprintf(file, "Volume \"volumegrid\" \n");
    gzprintf(file, " \"integer nx\" %i\n", xRes);
    gzprintf(file, " \"integer ny\" %i\n", yRes);
    gzprintf(file, " \"integer nz\" %i\n", zRes);
    gzprintf(file, " \"point p0\" [ 0.0 0.0 0.0 ] \"point p1\" [%f %f %f ] \n", xSize, ySize, zSize);
    gzprintf(file, " \"float density\" [ \n");
    for (int i = 0; i < xRes * yRes * zRes; i++)
      gzprintf(file, "%f ", field[i]);
    gzprintf(file, "] \n \n");

    gzclose(file);
    delete[] field;
  }
  */

  /////////////////////////////////////////////////////////////////////////////////
  // 3D df3 export
  /////////////////////////////////////////////////////////////////////////////////
/*
  static void dumpDF3(int counter, std::string prefix, float* fieldOrg, int xRes, int yRes, int zRes)
  {
    char buffer[256];

    // do deferred copying to final directory, better for network directories
    sprintf(buffer,"%04i", counter);
    std::string number = std::string(buffer);
    std::string filenameDf3 = prefix + number + std::string(".df3.gz");
    printf("Writing DF3 %s\n", filenameDf3.c_str());

    gzFile file;
    file = gzopen(filenameDf3.c_str(), "wb1");
    if (file == NULL) {
      std::cerr << " Couldn't write file " << filenameDf3 << "!!!" << std::endl;
      return;
    }

    // dimensions
    const int byteSize = 2;
    const unsigned short int onx=xRes,ony=yRes,onz=zRes;
    unsigned short int nx,ny,nz;
    nx = onx >> 8;
    ny = ony >> 8;
    nz = onz >> 8;
    nx += (onx << 8);
    ny += (ony << 8);
    nz += (onz << 8);
    gzwrite(file, (void*)&nx, sizeof(short));
    gzwrite(file, (void*)&ny, sizeof(short));
    gzwrite(file, (void*)&nz, sizeof(short));
    const int nitems = onx*ony*onz;
    const float mul = (float)( (1<<(8*byteSize))-1);

    unsigned short int *buf = new unsigned short int[nitems];
    for (int k = 0; k < onz; k++)
      for (int j = 0; j < ony; j++)
        for (int i = 0; i < onx; i++) {
          float val = fieldOrg[k*(onx*ony)+j*onx+i] ;
          CLAMP(val);
          buf[k*(onx*ony)+j*onx+i] = (short int)(val*mul);
        }
    gzwrite(file, (void*)buf, sizeof(unsigned short int)* nitems);

    gzclose(file);
    delete[] buf;
  }
  */

};


#endif