1 /*
2 FLAM3 - cosmic recursive fractal flames
3 Copyright (C) 1992-2009 Spotworks LLC
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 3 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19 #ifdef WIN32
20 #define WINVER 0x0500
21 #include <windows.h>
22 #endif
23
24 #ifdef __APPLE__
25 #include <sys/sysctl.h>
26 #endif
27
28 #include <limits.h>
29
30
31 #include "private.h"
32 #include "img.h"
33 #include "isaacs.h"
34
35
calc_nstrips(flam3_frame * spec)36 int calc_nstrips(flam3_frame *spec) {
37 double mem_required;
38 double mem_available;
39 int nstrips,ninc;
40 char *testmalloc;
41 #ifdef WIN32
42 MEMORYSTATUS stat;
43 stat.dwLength = sizeof(stat);
44 GlobalMemoryStatus(&stat); // may want to go to GlobalMemoryStatusEx eventually
45 mem_available = (double)stat.dwTotalPhys;
46 // fprintf(stderr,"%lu bytes free memory...\n",(size_t)stat.dwAvailPhys);
47 // if (mem_available > 1e9) mem_available = 1e9;
48 #elif defined(_SC_PHYS_PAGES) && defined(_SC_PAGESIZE)
49 mem_available =
50 (double)sysconf(_SC_PHYS_PAGES) * sysconf(_SC_PAGESIZE);
51 #elif defined __APPLE__
52 #ifdef __LP64__
53 long physmem;
54 size_t len = sizeof(physmem);
55 static int mib[2] = { CTL_HW, HW_MEMSIZE };
56 #else
57 unsigned int physmem;
58 size_t len = sizeof(physmem);
59 static int mib[2] = { CTL_HW, HW_PHYSMEM };
60 #endif
61 if (sysctl(mib, 2, &physmem, &len, NULL, 0) == 0 && len == sizeof(physmem)) {
62 mem_available = (double )physmem;
63 } else {
64 fprintf(stderr, "warning: unable to determine physical memory.n");
65 mem_available = 2e9;
66 }
67 #else
68 fprintf(stderr, "warning: unable to determine physical memory.\n");
69 mem_available = 2e9;
70 #endif
71 #if 0
72 fprintf(stderr,"available phyical memory is %lu\n",
73 (unsigned long)mem_available);
74 #endif
75 mem_available *= 0.8;
76 if (getenv("use_mem")) {
77 mem_available = atof(getenv("use_mem"));
78 }
79 mem_required = flam3_render_memory_required(spec);
80 if (mem_available >= mem_required) return 1;
81 nstrips = (int) ceil(mem_required / mem_available);
82
83 if (0) {
84 /* Attempt to malloc a strip, and if it fails, try adding additional strips */
85 ninc=-1;
86 testmalloc = NULL;
87 while(NULL==testmalloc && ninc<3) {
88 ninc++;
89 testmalloc = (char *)malloc((int)ceil(mem_required / (nstrips+ninc)));
90 }
91 if (NULL==testmalloc) {
92 fprintf(stderr,"Unable to allocate memory for render. Please close some running programs and try to render again.\n");
93 exit(1);
94 } else {
95 free(testmalloc);
96 nstrips = nstrips + ninc;
97 }
98 }
99
100 return nstrips;
101 }
102
print_progress(void * foo,double fraction,int stage,double eta)103 int print_progress(void *foo, double fraction, int stage, double eta) {
104 fprintf(stderr, "stage=%s progress=%g eta=%g\n", stage?"filtering":"chaos", fraction, eta);
105 return 0;
106 }
107
main(int argc,char ** argv)108 int main(int argc, char **argv) {
109 flam3_frame f;
110 char *ai;
111 flam3_genome *cps;
112 int ncps;
113 int i;
114 void *image=NULL;
115 FILE *fp;
116 char fname[256];
117 size_t this_size, last_size = -1;
118 double imgmem;
119 unsigned int strip;
120 double center_y, center_base;
121 unsigned int nstrips = 1;
122 randctx savectx;
123 char *prefix = args("prefix", "");
124 char *out = args("out", NULL);
125 char *format = getenv("format");
126 int verbose = argi("verbose", 1);
127 int bits = argi("bits", 33);
128 int bpc = argi("bpc",8);
129 int transparency = argi("transparency", 0);
130 char *inf = getenv("in");
131 double qs = argf("qs", 1.0);
132 double ss = argf("ss", 1.0);
133 double pixel_aspect = argf("pixel_aspect", 1.0);
134 int sub_batch_size = argi("sub_batch_size",10000);
135 int name_enable = argi("name_enable",0);
136 int num_threads = argi("nthreads",0);
137 int earlyclip = argi("earlyclip",0);
138 FILE *in;
139 double zoom_scale;
140 unsigned int channels;
141 long start_time = (long)time(0);
142 flam3_img_comments fpc;
143 stat_struct stats;
144 char numiter_string[64];
145 char badval_string[64];
146 char rtime_string[64];
147
148 #ifdef WIN32
149
150 char *slashloc;
151 char exepath[256];
152 char palpath[256];
153 memset(exepath,0,256);
154 memset(palpath,0,256);
155
156 slashloc = strrchr(argv[0],'\\');
157 if (NULL==slashloc) {
158 sprintf(palpath,"flam3_palettes=flam3-palettes.xml");
159 } else {
160 strncpy(exepath,argv[0],slashloc-argv[0]+1);
161 sprintf(palpath,"flam3_palettes=%sflam3-palettes.xml",exepath);
162 }
163 putenv(palpath);
164
165 #endif
166
167
168 if (1 != argc) {
169 docstring();
170 exit(0);
171 }
172
173 /* Init random number generators */
174 flam3_init_frame(&f);
175 flam3_srandom();
176
177 /* Set the number of threads */
178 if (num_threads==0) {
179 num_threads = flam3_count_nthreads();
180 if (verbose > 1)
181 fprintf(stderr,"Automatically detected %d core(s)...\n",num_threads);
182 } else{
183 if (verbose)
184 fprintf(stderr,"Manually specified %d thread(s)...\n",num_threads);
185 }
186
187
188 if (NULL == format) format = "png";
189 if (strcmp(format, "jpg") &&
190 strcmp(format, "ppm") &&
191 strcmp(format, "png")) {
192 fprintf(stderr,
193 "format must be either jpg, ppm, or png, not %s.\n",
194 format);
195 exit(1);
196 }
197
198 channels = strcmp(format, "png") ? 3 : 4;
199
200 /* Check for 16-bit-per-channel processing */
201 if ( (16 == bpc) && (strcmp(format,"png") != 0)) {
202 fprintf(stderr,"Support for 16 bpc images is only present for the png format.\n");
203 exit(1);
204 } else if (bpc != 8 && bpc != 16) {
205 fprintf(stderr,"Unexpected bpc specified (%d)\n",bpc);
206 exit(1);
207 }
208
209 if (pixel_aspect <= 0.0) {
210 fprintf(stderr, "pixel aspect ratio must be positive, not %g.\n",
211 pixel_aspect);
212 exit(1);
213 }
214
215 if (inf)
216 in = fopen(inf, "rb");
217 else
218 in = stdin;
219 if (NULL == in) {
220 perror(inf);
221 exit(1);
222 }
223
224 cps = flam3_parse_from_file(in, inf, flam3_defaults_on, &ncps);
225 if (NULL == cps) {
226 fprintf(stderr,"error reading genomes from file\n");
227 exit(1);
228 }
229
230 if (inf)
231 fclose(in);
232
233 for (i = 0; i < ncps; i++) {
234 /* Force ntemporal_samples to 1 for -render */
235 cps[i].ntemporal_samples = 1;
236 cps[i].sample_density *= qs;
237 cps[i].height = (int)(cps[i].height * ss);
238 cps[i].width = (int)(cps[i].width * ss);
239 cps[i].pixels_per_unit *= ss;
240 if (cps[i].height<=0 || cps[i].width<=0) {
241 fprintf(stderr,"output image has dimension <=0, aborting.\n");
242 exit(1);
243 }
244 }
245
246 if (out && (ncps > 1)) {
247 fprintf(stderr, "hqi-flame: warning: writing multiple images "
248 "to one file. all but last will be lost.\n");
249 }
250
251 for (i = 0; i < ncps; i++) {
252 int real_height;
253
254 if (verbose && ncps > 1) {
255 fprintf(stderr, "flame = %d/%d ", i+1, ncps);
256 }
257
258 // f.temporal_filter_radius = 0.0;
259 f.genomes = &cps[i];
260 f.ngenomes = 1;
261 f.verbose = verbose;
262 f.bits = bits;
263 f.time = 0.0;
264 f.pixel_aspect_ratio = pixel_aspect;
265 f.progress = 0;//print_progress;
266 f.nthreads = num_threads;
267 f.earlyclip = earlyclip;
268 f.sub_batch_size = sub_batch_size;
269
270 if (16==bpc)
271 f.bytes_per_channel = 2;
272 else
273 f.bytes_per_channel = 1;
274
275
276 if (getenv("nstrips")) {
277 nstrips = atoi(getenv("nstrips"));
278 } else {
279 nstrips = calc_nstrips(&f);
280 }
281
282 if (nstrips > cps[i].height) {
283 fprintf(stderr, "cannot have more strips than rows but %d>%d.\n",
284 nstrips, cps[i].height);
285 exit(1);
286 }
287
288 imgmem = (double)channels * (double)cps[i].width
289 * (double)cps[i].height * f.bytes_per_channel;
290
291 if (imgmem > ULONG_MAX) {
292 fprintf(stderr,"Image size > ULONG_MAX. Aborting.\n");
293 exit(1);
294 }
295
296 this_size = (size_t)channels * (size_t)cps[i].width
297 * (size_t)cps[i].height * f.bytes_per_channel;
298 if (this_size != last_size) {
299 if (last_size != -1)
300 free(image);
301 last_size = this_size;
302 image = (void *) calloc(this_size, sizeof(char));
303 if (NULL==image) {
304 fprintf(stderr,"Error allocating memory for image. Aborting\n");
305 exit(1);
306 }
307 } else {
308 memset(image, 0, this_size);
309 }
310
311 cps[i].sample_density *= nstrips;
312 real_height = cps[i].height;
313 cps[i].height = (int) ceil(cps[i].height / (double) nstrips);
314 center_y = cps[i].center[1];
315 zoom_scale = pow(2.0, cps[i].zoom);
316 center_base = center_y - ((nstrips - 1) * cps[i].height) /
317 (2 * cps[i].pixels_per_unit * zoom_scale);
318
319 /* Copy off random context to use for each strip */
320 memcpy(&savectx, &f.rc, sizeof(randctx));
321
322 for (strip = 0; strip < nstrips; strip++) {
323 size_t ssoff = (size_t)cps[i].height * strip * cps[i].width * channels * f.bytes_per_channel;
324 void *strip_start = image + ssoff;
325 cps[i].center[1] = center_base + cps[i].height * (double) strip / (cps[i].pixels_per_unit * zoom_scale);
326
327 if ((cps[i].height * (strip + 1)) > real_height) {
328 int oh = cps[i].height;
329 cps[i].height = real_height - oh * strip;
330 cps[i].center[1] -=
331 (oh - cps[i].height) * 0.5 /
332 (cps[i].pixels_per_unit * zoom_scale);
333 }
334
335 /* Use the same random context for each strip */
336 memcpy(&f.rc, &savectx, sizeof(randctx));
337
338 if (verbose && nstrips > 1) {
339 fprintf(stderr, "strip = %d/%d\n", strip+1, nstrips);
340 }
341 if (verbose && (1 == nstrips) && (ncps > 1)) {
342 fprintf(stderr, "\n");
343 }
344 cps[i].ntemporal_samples = 1;
345 if (flam3_render(&f, strip_start, flam3_field_both, channels, transparency, &stats)) {
346 fprintf(stderr,"error rendering image: aborting.\n");
347 exit(1);
348 }
349
350 if (NULL != out) {
351 strcpy(fname,out);
352 } else if (name_enable && cps[i].flame_name[0]>0) {
353 sprintf(fname, "%s.%s",cps[i].flame_name,format);
354 } else {
355 sprintf(fname, "%s%05d.%s", prefix, i, format);
356 }
357 if (verbose) {
358 fprintf(stderr, "writing %s...", fname);
359 }
360 fp = fopen(fname, "wb");
361 if (NULL == fp) {
362 perror(fname);
363 exit(1);
364 }
365
366 /* Generate temp file with genome */
367 fpc.genome = flam3_print_to_string(f.genomes);
368
369 sprintf(badval_string,"%g",stats.badvals/(double)stats.num_iters);
370 fpc.badvals = badval_string;
371 sprintf(numiter_string,"%g",(double)stats.num_iters);
372 fpc.numiters = numiter_string;
373 sprintf(rtime_string,"%d",stats.render_seconds);
374 fpc.rtime = rtime_string;
375
376 if (!strcmp(format, "png")) {
377
378 write_png(fp, image, cps[i].width, real_height, &fpc, f.bytes_per_channel);
379
380 } else if (!strcmp(format, "jpg")) {
381
382 write_jpeg(fp, (unsigned char *)image, cps[i].width, real_height, &fpc);
383
384 } else {
385 fprintf(fp, "P6\n");
386 fprintf(fp, "%d %d\n255\n", cps[i].width, real_height);
387 if (this_size != fwrite((unsigned char *)image, 1, this_size, fp)) {
388 perror(fname);
389 }
390 }
391 /* Free string */
392 free(fpc.genome);
393
394 fclose(fp);
395 }
396
397 /* restore the cps values to their original values */
398 cps[i].sample_density /= nstrips;
399 cps[i].height = real_height;
400 cps[i].center[1] = center_y;
401
402 if (verbose) {
403 fprintf(stderr, "done.\n");
404 }
405 }
406 if (verbose && ((ncps > 1) || (nstrips > 1))) {
407 long total_time = (long)time(0) - start_time;
408
409 if (total_time > 100)
410 fprintf(stderr, "total time = %.1f minutes\n", total_time / 60.0);
411 else
412 fprintf(stderr, "total time = %ld seconds\n", total_time);
413 }
414
415 for (i=0;i<ncps;i++) {
416
417 xmlFreeDoc(cps[i].edits);
418 clear_cp(&cps[i],0);
419
420 }
421 free(cps);
422
423 free(image);
424 return 0;
425 }
426