Skip to content

Commit

Permalink
Rename io fodler to export folder, add import array fro file using Op…
Browse files Browse the repository at this point in the history
…enCV imread
  • Loading branch information
otto-link committed Aug 20, 2024
1 parent b1731e3 commit c7eb562
Show file tree
Hide file tree
Showing 13 changed files with 66 additions and 0 deletions.
15 changes: 15 additions & 0 deletions HighMap/include/highmap/export.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,21 @@ void export_vector_glyph_png_16bit(const std::string fname,
const float scale = 0.05f,
const uint seed = 0);

/**
* @brief Reads an image file and converts it to a 2D array.
*
* This function uses the OpenCV `imread` function to load an image from the
* specified file. The supported file formats are those recognized by OpenCV's
* `imread` function, such as JPEG, PNG, BMP, and others. If the image is in
* color, it is automatically converted to grayscale using the built-in OpenCV
* codec converter. This conversion process may introduce artifacts depending on
* the image's original format and content.
*
* @param fname The name of the image file to be read.
* @return Array A 2D array containing the pixel values of the grayscale image.
*/
Array read_to_array(std::string fname);

/**
* @brief Read an 8bit grayscale image to a png file.
*
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
28 changes: 28 additions & 0 deletions HighMap/src/export/read_to_array.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/* Copyright (c) 2023 Otto Link. Distributed under the terms of the GNU General
* Public License. The full license is in the file LICENSE, distributed with
* this software. */
#include "macrologger.h"

#include "highmap/array.hpp"

namespace hmap
{

Array read_to_array(std::string fname)
{
cv::Mat mat = cv::imread(fname, cv::IMREAD_GRAYSCALE);

if (mat.data == nullptr)
{
LOG_ERROR("error while reading the image file: %s", fname.c_str());
return Array();
}
else
{
cv::rotate(mat, mat, cv::ROTATE_90_CLOCKWISE);
bool remap = true;
return cv_mat_to_array(mat, remap);
}
}

} // namespace hmap
2 changes: 2 additions & 0 deletions examples/ex_read_to_array/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
add_executable(ex_read_to_array ex_read_to_array.cpp)
target_link_libraries(ex_read_to_array highmap)
21 changes: 21 additions & 0 deletions examples/ex_read_to_array/ex_read_to_array.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#include "highmap.hpp"

int main(void)
{
hmap::Vec2<int> shape = {256, 128};
hmap::Vec2<float> res = {4.f, 2.f};
int seed = 1;

hmap::Array z0 = hmap::noise_fbm(hmap::NoiseType::PERLIN, shape, res, seed);
hmap::remap(z0, 0.f, 1.f);

z0.to_png("out1.png", hmap::Cmap::GRAY); // grayscale
z0.to_png("out2.png", hmap::Cmap::JET); // RGB image

hmap::Array z1 = hmap::read_to_array("out1.png");
hmap::Array z2 = hmap::read_to_array("out2.png"); // converted to grayscale

hmap::export_banner_png("ex_read_to_array.png",
{z0, z1, z2},
hmap::Cmap::INFERNO);
}

0 comments on commit c7eb562

Please sign in to comment.