diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-03-09 13:19:48 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-03-09 13:20:02 +0000 |
commit | 58daab21cd043e1dc37024a7f99b396788372918 (patch) | |
tree | 96771e43bb69f7c1c2b0b4f7374cb74d7866d0cb /ml/dlib/dlib/image_saver/save_png.h | |
parent | Releasing debian version 1.43.2-1. (diff) | |
download | netdata-58daab21cd043e1dc37024a7f99b396788372918.tar.xz netdata-58daab21cd043e1dc37024a7f99b396788372918.zip |
Merging upstream version 1.44.3.
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'ml/dlib/dlib/image_saver/save_png.h')
-rw-r--r-- | ml/dlib/dlib/image_saver/save_png.h | 162 |
1 files changed, 162 insertions, 0 deletions
diff --git a/ml/dlib/dlib/image_saver/save_png.h b/ml/dlib/dlib/image_saver/save_png.h new file mode 100644 index 000000000..cddf03ff6 --- /dev/null +++ b/ml/dlib/dlib/image_saver/save_png.h @@ -0,0 +1,162 @@ +// Copyright (C) 2011 Davis E. King (davis@dlib.net) +// License: Boost Software License See LICENSE.txt for the full license. +#ifndef DLIB_SAVE_PnG_Hh_ +#define DLIB_SAVE_PnG_Hh_ + +#include "save_png_abstract.h" +#include "image_saver.h" +#include "../array2d.h" +#include <vector> +#include <string> +#include "../pixel.h" +#include "../matrix/matrix_exp.h" +#include "../image_transforms/assign_image.h" + +namespace dlib +{ + +// ---------------------------------------------------------------------------------------- + + namespace impl + { + enum png_type + { + png_type_rgb, + png_type_rgb_alpha, + png_type_gray, + }; + + void impl_save_png ( + const std::string& file_name, + std::vector<unsigned char*>& row_pointers, + const long width, + const png_type type, + const int bit_depth + ); + } + +// ---------------------------------------------------------------------------------------- + + template < + typename image_type + > + typename disable_if<is_matrix<image_type> >::type save_png( + const image_type& img_, + const std::string& file_name + ) + { + const_image_view<image_type> img(img_); + + // make sure requires clause is not broken + DLIB_CASSERT(img.size() != 0, + "\t save_png()" + << "\n\t You can't save an empty image as a PNG" + ); + + +#ifndef DLIB_PNG_SUPPORT + /* !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + You are getting this error because you are trying to use save_png() + but you haven't defined DLIB_PNG_SUPPORT. You must do so to use + this function. You must also make sure you set your build environment + to link against the libpng library. + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!*/ + COMPILE_TIME_ASSERT(sizeof(image_type) == 0); +#else + std::vector<unsigned char*> row_pointers(img.nr()); + typedef typename image_traits<image_type>::pixel_type pixel_type; + + if (is_same_type<rgb_pixel,pixel_type>::value) + { + for (unsigned long i = 0; i < row_pointers.size(); ++i) + row_pointers[i] = (unsigned char*)(&img[i][0]); + + impl::impl_save_png(file_name, row_pointers, img.nc(), impl::png_type_rgb, 8); + } + else if (is_same_type<rgb_alpha_pixel,pixel_type>::value) + { + for (unsigned long i = 0; i < row_pointers.size(); ++i) + row_pointers[i] = (unsigned char*)(&img[i][0]); + + impl::impl_save_png(file_name, row_pointers, img.nc(), impl::png_type_rgb_alpha, 8); + } + else if (pixel_traits<pixel_type>::lab || pixel_traits<pixel_type>::hsi || pixel_traits<pixel_type>::rgb) + { + // convert from Lab or HSI to RGB (Or potentially RGB pixels that aren't laid out as R G B) + array2d<rgb_pixel> temp_img; + assign_image(temp_img, img_); + for (unsigned long i = 0; i < row_pointers.size(); ++i) + row_pointers[i] = (unsigned char*)(&temp_img[i][0]); + + impl::impl_save_png(file_name, row_pointers, img.nc(), impl::png_type_rgb, 8); + } + else if (pixel_traits<pixel_type>::rgb_alpha) + { + // convert from RGBA pixels that aren't laid out as R G B A + array2d<rgb_alpha_pixel> temp_img; + assign_image(temp_img, img_); + for (unsigned long i = 0; i < row_pointers.size(); ++i) + row_pointers[i] = (unsigned char*)(&temp_img[i][0]); + + impl::impl_save_png(file_name, row_pointers, img.nc(), impl::png_type_rgb_alpha, 8); + } + else // this is supposed to be grayscale + { + DLIB_CASSERT(pixel_traits<pixel_type>::grayscale, "impossible condition detected"); + + if (pixel_traits<pixel_type>::is_unsigned && sizeof(pixel_type) == 1) + { + for (unsigned long i = 0; i < row_pointers.size(); ++i) + row_pointers[i] = (unsigned char*)(&img[i][0]); + + impl::impl_save_png(file_name, row_pointers, img.nc(), impl::png_type_gray, 8); + } + else if (pixel_traits<pixel_type>::is_unsigned && sizeof(pixel_type) == 2) + { + for (unsigned long i = 0; i < row_pointers.size(); ++i) + row_pointers[i] = (unsigned char*)(&img[i][0]); + + impl::impl_save_png(file_name, row_pointers, img.nc(), impl::png_type_gray, 16); + } + else + { + // convert from whatever this is to 16bit grayscale + array2d<dlib::uint16> temp_img; + assign_image(temp_img, img_); + for (unsigned long i = 0; i < row_pointers.size(); ++i) + row_pointers[i] = (unsigned char*)(&temp_img[i][0]); + + impl::impl_save_png(file_name, row_pointers, img.nc(), impl::png_type_gray, 16); + } + } + + +#endif + + } + +// ---------------------------------------------------------------------------------------- + + template < + typename EXP + > + void save_png( + const matrix_exp<EXP>& img, + const std::string& file_name + ) + { + array2d<typename EXP::type> temp; + assign_image(temp, img); + save_png(temp, file_name); + } + +// ---------------------------------------------------------------------------------------- + +} + +#ifdef NO_MAKEFILE +#include "save_png.cpp" +#endif + +#endif // DLIB_SAVE_PnG_Hh_ + |