| 1 |
/* |
| 2 |
Copyright 2015 Google Inc. All rights reserved. |
| 3 |
|
| 4 |
Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 |
you may not use this file except in compliance with the License. |
| 6 |
You may obtain a copy of the License at |
| 7 |
|
| 8 |
http://www.apache.org/licenses/LICENSE-2.0 |
| 9 |
|
| 10 |
Unless required by applicable law or agreed to in writing, software |
| 11 |
distributed under the License is distributed on an "AS IS" BASIS, |
| 12 |
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 |
See the License for the specific language governing permissions and |
| 14 |
limitations under the License. |
| 15 |
*/ |
| 16 |
|
| 17 |
|
| 18 |
#include <cstdlib> |
| 19 |
#include <cmath> |
| 20 |
#include <iostream> |
| 21 |
|
| 22 |
#include <string> |
| 23 |
#include <vector> |
| 24 |
|
| 25 |
#include <png.h> |
| 26 |
|
| 27 |
|
| 28 |
int main(int argc, const char **argv) |
| 29 |
{ |
| 30 |
|
| 31 |
if (argc != 8) { |
| 32 |
std::cerr <<"Usage: <width> <height> <iters> <left> <bottom> <right> <top>" << std::endl; |
| 33 |
std::cerr <<"PNG file is written to stdout." << std::endl; |
| 34 |
exit(EXIT_FAILURE); |
| 35 |
} |
| 36 |
|
| 37 |
unsigned long width = strtoul(argv[1], NULL, 10); |
| 38 |
unsigned long height = strtoul(argv[2], NULL, 10); |
| 39 |
unsigned long iterations = strtoul(argv[3], NULL, 10); |
| 40 |
double left = strtod(argv[4], NULL); |
| 41 |
double bottom = strtod(argv[5], NULL); |
| 42 |
double right = strtod(argv[6], NULL); |
| 43 |
double top = strtod(argv[7], NULL); |
| 44 |
|
| 45 |
struct Exception { |
| 46 |
const std::string &msg; |
| 47 |
Exception(const std::string &msg) : msg(msg) { } |
| 48 |
}; |
| 49 |
|
| 50 |
int code = EXIT_SUCCESS; |
| 51 |
png_structp png_write_struct = NULL; |
| 52 |
volatile png_infop info_ptr = NULL; |
| 53 |
|
| 54 |
try { |
| 55 |
png_write_struct = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL); |
| 56 |
if (png_write_struct == NULL) |
| 57 |
throw Exception("Could not allocate libpng write_struct."); |
| 58 |
|
| 59 |
info_ptr = png_create_info_struct(png_write_struct); |
| 60 |
if (info_ptr == NULL) |
| 61 |
throw Exception("Could not allocate libpng info_struct."); |
| 62 |
|
| 63 |
if (setjmp(png_jmpbuf(png_write_struct))) |
| 64 |
throw Exception("Exception from libpng"); |
| 65 |
|
| 66 |
png_init_io(png_write_struct, stdout); |
| 67 |
|
| 68 |
png_set_IHDR(png_write_struct, info_ptr, width, height, 8, PNG_COLOR_TYPE_RGB, |
| 69 |
PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE); |
| 70 |
|
| 71 |
png_write_info(png_write_struct, info_ptr); |
| 72 |
|
| 73 |
std::vector<png_byte> row(3 * width); |
| 74 |
for (unsigned long y=0 ; y<height ; y++) { |
| 75 |
const double scalar_y = double(y) / height; |
| 76 |
const double mb_y = scalar_y * bottom + (1-scalar_y) * top; // libpng y==0 is top |
| 77 |
for (unsigned long x=0 ; x<width ; x++) { |
| 78 |
const double scalar_x = double(x) / width; |
| 79 |
const double mb_x = scalar_x * right + (1-scalar_x) * left; |
| 80 |
row[3*x + 0] = 255; |
| 81 |
row[3*x + 1] = 255; |
| 82 |
row[3*x + 2] = 255; |
| 83 |
double c_x = mb_x; |
| 84 |
double c_y = mb_y; |
| 85 |
for (unsigned i=1 ; i<iterations ; ++i) { |
| 86 |
const double len2 = c_x*c_x + c_y*c_y; |
| 87 |
if (len2 > 4) { |
| 88 |
// escaped |
| 89 |
// smooth with fractional escape velocity |
| 90 |
float escape_vel = i + 1 - ::log2f(::logf(::sqrtf(len2))); |
| 91 |
float tonemapped = escape_vel / (20 + escape_vel); |
| 92 |
row[3*x + 0] = png_byte(powf(tonemapped, 10) * 255); |
| 93 |
row[3*x + 1] = png_byte(powf(tonemapped, 2.5) * 255); |
| 94 |
row[3*x + 2] = png_byte(tonemapped * 255); |
| 95 |
/* |
| 96 |
row[3*x + 0] = png_byte(escape_vel); |
| 97 |
row[3*x + 1] = png_byte(escape_vel); |
| 98 |
row[3*x + 2] = png_byte(escape_vel); |
| 99 |
*/ |
| 100 |
break; |
| 101 |
} |
| 102 |
const double o_x = c_x; |
| 103 |
c_x = c_x*c_x - c_y*c_y + mb_x; |
| 104 |
c_y = 2 * o_x * c_y + mb_y; |
| 105 |
} |
| 106 |
} |
| 107 |
png_write_row(png_write_struct, &row[0]); |
| 108 |
} |
| 109 |
|
| 110 |
png_write_end(png_write_struct, NULL); |
| 111 |
|
| 112 |
} catch (const Exception &e) { |
| 113 |
std::cerr << e.msg << std::endl; |
| 114 |
code = EXIT_FAILURE; |
| 115 |
} |
| 116 |
|
| 117 |
if (png_write_struct != NULL) png_destroy_write_struct(&png_write_struct, (png_infopp)NULL); |
| 118 |
if (info_ptr != NULL) png_free_data(png_write_struct, info_ptr, PNG_FREE_ALL, -1); |
| 119 |
|
| 120 |
return code; |
| 121 |
} |