Skip to content

Commit

Permalink
Remove png reading functions
Browse files Browse the repository at this point in the history
  • Loading branch information
otto-link committed Aug 20, 2024
1 parent 333c62e commit 7067eb4
Show file tree
Hide file tree
Showing 5 changed files with 6 additions and 222 deletions.
2 changes: 1 addition & 1 deletion HighMap/include/highmap/array.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ class Array

Array(Vec2<int> shape, float value); ///< @overload

Array(std::string filename, bool resize_array = true); ///< @overload
Array(std::string filename); ///< @overload

//----------------------------------------
// accessors
Expand Down
31 changes: 0 additions & 31 deletions HighMap/include/highmap/export.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -258,37 +258,6 @@ void export_vector_glyph_png_16bit(const std::string fname,
*/
Array read_to_array(std::string fname);

/**
* @brief Read an 8bit grayscale image to a png file.
*
* @param fname File name.
* @param shape Image shape.
*/
std::vector<uint8_t> read_png_grayscale_8bit(std::string fname);

/**
* @brief Read an 16bit grayscale image to a png file.
*
* @param fname File name.
* @param shape Image shape.
*/
std::vector<uint16_t> read_png_grayscale_16bit(std::string fname);

/**
* @brief
*
* @param fname File name.
* @param width Image width.
* @param height Image width.
* @param color_type Color type.
* @param bit_depth Bit depth.
*/
void read_png_header(std::string fname,
int &width,
int &height,
png_byte &color_type,
png_byte &bit_depth);

/**
* @brief Export an 8bit grayscale image to a png file.
*
Expand Down
48 changes: 2 additions & 46 deletions HighMap/src/array/array_base.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,53 +27,9 @@ Array::Array(Vec2<int> shape, float value) : shape(shape)
std::fill(this->vector.begin(), this->vector.end(), value);
}

Array::Array(std::string filename, bool resize_array)
Array::Array(std::string filename)
{
int width;
int height;
png_byte color_type;
png_byte bit_depth;

read_png_header(filename, width, height, color_type, bit_depth);

if (resize_array)
this->set_shape(Vec2<int>(width, height));
else if (this->shape.x != width || this->shape.y != height)
{
// exit if image size does not match current array size
LOG_ERROR("image size (%d, %d) does not match current size (%d, %d)",
width,
height,
this->shape.x,
this->shape.y);
return;
}

if (bit_depth == 8)
{
LOG_DEBUG("8bit");
std::vector<uint8_t> img = read_png_grayscale_8bit(filename);

for (int i = 0; i < this->shape.x; i++)
for (int j = 0; j < this->shape.y; j++)
{
int k = (this->shape.y - 1 - j) * this->shape.y + i;
(*this)(i, j) = (float)img[k] / 255.f;
}
}

if (bit_depth == 16)
{
LOG_DEBUG("16bit");
std::vector<uint16_t> img = read_png_grayscale_16bit(filename);

for (int i = 0; i < this->shape.x; i++)
for (int j = 0; j < this->shape.y; j++)
{
int k = (this->shape.y - 1 - j) * this->shape.x + i;
(*this)(i, j) = (float)img[k] / 65535.f;
}
}
*this = read_to_array(filename);
}

Vec2<int> Array::get_shape()
Expand Down
143 changes: 0 additions & 143 deletions HighMap/src/export/format_png.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,149 +12,6 @@
namespace hmap
{

std::vector<uint8_t> read_png_grayscale_8bit(std::string fname)
{
FILE *fp = fopen(fname.c_str(), "rb");
if (!fp)
LOG_ERROR("Error opening file for writing: %s", fname.c_str());

png_structp png = png_create_read_struct(PNG_LIBPNG_VER_STRING,
nullptr,
nullptr,
nullptr);
if (!png)
{
LOG_ERROR("Error creating PNG write structure");
fclose(fp);
}

png_infop info = png_create_info_struct(png);
if (!info)
{
LOG_ERROR("Error creating PNG info structure");
png_destroy_write_struct(&png, nullptr);
fclose(fp);
}

png_init_io(png, fp);
png_read_info(png, info);

int width = png_get_image_width(png, info);
int height = png_get_image_height(png, info);

std::vector<png_bytep> row_pointers(height);
for (int j = 0; j < height; j++)
row_pointers[j] = (png_byte *)malloc(png_get_rowbytes(png, info));
png_read_image(png, row_pointers.data());

std::vector<uint8_t> img(width * height);

for (int j = 0; j < height; j++)
for (int i = 0; i < width; i++)
img[j * width + i] = (uint8_t)row_pointers[j][i];

png_destroy_read_struct(&png, &info, nullptr);
fclose(fp);

return img;
}

std::vector<uint16_t> read_png_grayscale_16bit(std::string fname)
{
FILE *fp = fopen(fname.c_str(), "rb");
if (!fp)
LOG_ERROR("Error opening file for writing: %s", fname.c_str());

png_structp png = png_create_read_struct(PNG_LIBPNG_VER_STRING,
nullptr,
nullptr,
nullptr);
if (!png)
{
LOG_ERROR("Error creating PNG write structure");
fclose(fp);
}

png_infop info = png_create_info_struct(png);
if (!info)
{
LOG_ERROR("Error creating PNG info structure");
png_destroy_write_struct(&png, nullptr);
fclose(fp);
}

png_init_io(png, fp);
png_read_info(png, info);

int width = png_get_image_width(png, info);
int height = png_get_image_height(png, info);

std::vector<png_bytep> row_pointers(height);
for (int j = 0; j < height; j++)
row_pointers[j] = (png_byte *)malloc(png_get_rowbytes(png, info));
png_read_image(png, row_pointers.data());

// convert data
std::vector<uint16_t> img(width * height);

for (int j = 0; j < height; j++)
{
for (int i = 0; i < 2 * width; i += 2)
img[j * width + i / 2] = (uint16_t)((row_pointers[j][i] << 8) |
row_pointers[j][i + 1]);
}

png_destroy_read_struct(&png, &info, nullptr);
fclose(fp);

return img;
}

void read_png_header(std::string fname,
int &width,
int &height,
png_byte &color_type,
png_byte &bit_depth)
{
FILE *fp = fopen(fname.c_str(), "rb");
if (!fp)
LOG_ERROR("Error opening file for writing: %s", fname.c_str());

png_structp png = png_create_read_struct(PNG_LIBPNG_VER_STRING,
nullptr,
nullptr,
nullptr);
if (!png)
{
LOG_ERROR("Error creating PNG write structure");
fclose(fp);
}

png_infop info = png_create_info_struct(png);
if (!info)
{
LOG_ERROR("Error creating PNG info structure");
png_destroy_write_struct(&png, nullptr);
fclose(fp);
}

png_init_io(png, fp);
png_read_info(png, info);

width = png_get_image_width(png, info);
height = png_get_image_height(png, info);
color_type = png_get_color_type(png, info);
bit_depth = png_get_bit_depth(png, info);

LOG_DEBUG("width: %d", width);
LOG_DEBUG("height: %d", height);
LOG_DEBUG("color_type: %d", color_type);
LOG_DEBUG("bit_depth: %d", bit_depth);

fclose(fp);
png_destroy_read_struct(&png, &info, nullptr);
}

void write_png_grayscale_8bit(std::string fname,
std::vector<uint8_t> &img,
Vec2<int> shape)
Expand Down
4 changes: 3 additions & 1 deletion examples/ex_read_to_array/ex_read_to_array.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ int main(void)
hmap::Array z1 = hmap::read_to_array("out1.png");
hmap::Array z2 = hmap::read_to_array("out2.png"); // converted to grayscale

hmap::Array z3 = hmap::Array("out1.png");

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

0 comments on commit 7067eb4

Please sign in to comment.