Commit 1615912e authored by plgruener's avatar plgruener

move png output to own function

parent 9691d68b
......@@ -5,7 +5,7 @@ set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED TRUE)
find_package(OpenMP)
add_executable(dynamicsystems main.cpp compute.cpp)
add_executable(dynamicsystems main.cpp compute.cpp picture.cpp)
if(OPENMP_FOUND)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${OpenMP_CXX_FLAGS}")
endif()
......
......@@ -14,6 +14,7 @@
#include "colormaps.hpp"
#include "compute.hpp"
#include "picture.hpp"
int main(int argc, char* argv[]) {
// get arguments from CLI
......@@ -150,73 +151,6 @@ int main(int argc, char* argv[]) {
}
}
// transform floats into grayscale-colors:
for (int i = 0; i < alpha_num_params * beta_num_params; ++i) {
if (result[i] > threshold) {
colors[i] = 255;
colors_rgb[3 * i] = 255;
colors_rgb[3 * i + 1] = 255;
colors_rgb[3 * i + 2] = 255;
} else {
colors[i] = floor(254 * result[i] / threshold);
// RGB color gradient: viridis from matplotlib
int idx = floor(255 * result[i] / threshold);
unsigned char r = floor(255 * viridis[idx][0]);
unsigned char g = floor(255 * viridis[idx][1]);
unsigned char b = floor(255 * viridis[idx][2]);
colors_rgb[3 * i] = r; // red
colors_rgb[3 * i + 1] = g; // green
colors_rgb[3 * i + 2] = b; // blue
}
}
// Output pixel vector into PGM File
std::ofstream ostrm("picture.pgm", std::ofstream::binary);
ostrm << "P5" << '\n';
ostrm << alpha_num_params << ' ' << beta_num_params << '\n';
ostrm << 255 << '\n'; // max. gray value
// 1 byte per pixel
for (int i = 0; i < colors.size(); ++i) {
ostrm << static_cast<char>(colors[i]); // colors.size());
}
// Output pixel vector into PPM File
std::ofstream ostrm_rgb("picture_rgb.ppm", std::ofstream::binary);
ostrm_rgb << "P6" << '\n';
ostrm_rgb << alpha_num_params << ' ' << beta_num_params << '\n';
ostrm_rgb << 255 << '\n'; // max. gray value
// 3 byte per pixel
for (int i = 0; i < colors_rgb.size(); ++i) {
// das geht theoretisch auch mit dem ganzen Array, aber praktisch nicht?!
ostrm_rgb << static_cast<char>(colors_rgb[i]);
}
// write to libpng
png_image img;
memset(&img, 0, sizeof(img));
img.version = PNG_IMAGE_VERSION;
img.opaque = NULL;
img.width = alpha_num_params;
img.height = beta_num_params;
img.format = PNG_FORMAT_RGB;
img.flags = 0;
img.colormap_entries = 0;
// set to negative for bottom-up image
const int row_stride = alpha_num_params * 3;
png_bytep buffer = colors_rgb.data();
png_image_write_to_file(&img, "picture_rgb.png", false, buffer, row_stride, NULL);
if (PNG_IMAGE_FAILED(img)) {
std::cerr << img.message << std::endl;
return -1;
} else {
if (img.warning_or_error != 0) {
std::cerr << img.message << std::endl;
}
}
std::cout << "png written" << std::endl;
write_png("picture.png", result.data(), threshold, alpha_num_params, beta_num_params);
}
}
#include "picture.hpp"
// all functions for picture transformation and output:
// function: map result to color vector
bool write_png(const char * filename, const float *result, float threshold, int width, int height) {
std::vector<unsigned char> colors_gray(width * height);
std::vector<unsigned char> colors_rgb(3 * width * height);
for (int i = 0; i < width * height; ++i) {
if (result[i] > threshold) {
colors_gray[i] = 255;
colors_rgb[3 * i] = 255;
colors_rgb[3 * i + 1] = 255;
colors_rgb[3 * i + 2] = 255;
} else {
colors_gray[i] = floor(254 * result[i] / threshold);
// RGB color gradient: viridis from matplotlib
int idx = floor(255 * result[i] / threshold);
unsigned char r = floor(255 * viridis[idx][0]);
unsigned char g = floor(255 * viridis[idx][1]);
unsigned char b = floor(255 * viridis[idx][2]);
colors_rgb[3 * i] = r; // red
colors_rgb[3 * i + 1] = g; // green
colors_rgb[3 * i + 2] = b; // blue
}
}
png_bytep buffer = colors_rgb.data();
png_image img;
memset(&img, 0, sizeof(img));
img.version = PNG_IMAGE_VERSION;
img.opaque = NULL;
img.width = width;
img.height = height;
img.format = PNG_FORMAT_RGB;
img.flags = 0;
img.colormap_entries = 0;
// set to negative for bottom-up image
const int row_stride = width * 3;
png_image_write_to_file(&img, filename, false, buffer, row_stride, NULL);
if (PNG_IMAGE_FAILED(img)) {
std::cerr << img.message << std::endl;
return false;
} else {
if (img.warning_or_error != 0) {
std::cerr << img.message << std::endl;
}
return true;
}
}
#ifndef PICTURE_H
#define PICTURE_H
#include <boost/align/aligned_allocator.hpp>
#include <vector>
#include <cmath>
#include <iostream>
#include <cstring>
#include <png.h>
#include "colormaps.hpp"
bool write_png(const char * filename, const float *result, float threshold, int width, int height);
#endif
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment