From 6e5fe53f467899afaa10300ba9c793a910e71623 Mon Sep 17 00:00:00 2001 From: Chong Kai Xiong Date: Fri, 17 Jan 2025 16:14:39 +0800 Subject: [PATCH] Core (LV::Video): Apply modern C++-isms to PNG writer. --- libvisual/libvisual/private/lv_video_png.cpp | 93 ++++++++++---------- 1 file changed, 47 insertions(+), 46 deletions(-) diff --git a/libvisual/libvisual/private/lv_video_png.cpp b/libvisual/libvisual/private/lv_video_png.cpp index 067da849f..e71f1e2e9 100644 --- a/libvisual/libvisual/private/lv_video_png.cpp +++ b/libvisual/libvisual/private/lv_video_png.cpp @@ -23,6 +23,7 @@ #include "lv_video_png.hpp" #include "lv_common.h" #include +#include #include #include #include @@ -34,8 +35,8 @@ namespace LV { void handle_png_read (png_structp png_ptr, png_bytep data, png_size_t length) { - auto io_ptr = png_get_io_ptr (png_ptr); - auto& input = *static_cast (io_ptr); + auto io_ptr {png_get_io_ptr (png_ptr)}; + auto& input {*static_cast (io_ptr)}; if (!input.read (reinterpret_cast (data), length)) { std::longjmp (png_jmpbuf (png_ptr), -1); @@ -44,8 +45,8 @@ namespace LV { void handle_png_write (png_structp png_ptr, png_bytep data, png_size_t length) { - auto io_ptr = png_get_io_ptr (png_ptr); - auto& output = *static_cast (io_ptr); + auto io_ptr {png_get_io_ptr (png_ptr)}; + auto& output {*static_cast (io_ptr)}; if (!output.write(reinterpret_cast(data), length)) { std::longjmp (png_jmpbuf (png_ptr), -1); @@ -54,8 +55,8 @@ namespace LV { void handle_png_flush (png_structp png_ptr) { - auto io_ptr = png_get_io_ptr (png_ptr); - auto& output = *static_cast (io_ptr); + auto io_ptr {png_get_io_ptr (png_ptr)}; + auto& output {*static_cast (io_ptr)}; output.flush (); } @@ -92,46 +93,46 @@ namespace LV { VideoPtr bitmap_load_png (std::istream& input) { - auto saved_stream_pos = input.tellg (); + auto saved_stream_pos {input.tellg ()}; png_byte signature[8]; input.read (reinterpret_cast (signature), sizeof (signature)); - bool is_png = !png_sig_cmp (signature, 0, sizeof (signature)); + auto is_png {!png_sig_cmp (signature, 0, sizeof (signature))}; if (!is_png) { return nullptr; } - auto png_ptr = png_create_read_struct (PNG_LIBPNG_VER_STRING, - nullptr, - handle_png_load_error, - handle_png_load_warning); + auto png_ptr {png_create_read_struct (PNG_LIBPNG_VER_STRING, + nullptr, + handle_png_load_error, + handle_png_load_warning)}; if (!png_ptr) { return nullptr; } - auto info_ptr = png_create_info_struct (png_ptr); + auto info_ptr {png_create_info_struct (png_ptr)}; if (!info_ptr) { png_destroy_read_struct (&png_ptr, nullptr, nullptr); return nullptr; } - auto end_info = png_create_info_struct (png_ptr); + auto end_info {png_create_info_struct (png_ptr)}; if (!end_info) { png_destroy_read_struct (&png_ptr, &info_ptr, nullptr); return nullptr; } - uint8_t* pixels = nullptr; - uint8_t** pixel_row_ptrs = nullptr; + uint8_t* pixels {nullptr}; + uint8_t** pixel_row_ptrs {nullptr}; if (setjmp (png_jmpbuf (png_ptr))) { input.seekg (saved_stream_pos); png_destroy_read_struct (&png_ptr, &info_ptr, &end_info); - delete []pixel_row_ptrs; + delete[] pixel_row_ptrs; visual_mem_free (pixels); return nullptr; @@ -143,8 +144,8 @@ namespace LV { png_read_info (png_ptr, info_ptr); - auto color_type = png_get_color_type (png_ptr, info_ptr); - auto bit_depth = png_get_bit_depth (png_ptr, info_ptr); + auto color_type {png_get_color_type (png_ptr, info_ptr)}; + auto bit_depth {png_get_bit_depth (png_ptr, info_ptr)}; if (color_type == PNG_COLOR_TYPE_PALETTE) { png_set_palette_to_rgb (png_ptr); @@ -171,13 +172,13 @@ namespace LV { png_set_gray_to_rgb (png_ptr); } -#if VISUAL_LITTLE_ENDIAN - png_set_bgr (png_ptr); -#endif + if constexpr (std::endian::native == std::endian::little) { + png_set_bgr (png_ptr); + } png_read_update_info (png_ptr, info_ptr); - VisVideoDepth depth = VISUAL_VIDEO_DEPTH_NONE; + auto depth {VISUAL_VIDEO_DEPTH_NONE}; switch (png_get_color_type (png_ptr, info_ptr)) { case PNG_COLOR_TYPE_RGB: @@ -190,9 +191,9 @@ namespace LV { std::longjmp (png_jmpbuf (png_ptr), -1); } - auto width = png_get_image_width (png_ptr, info_ptr); - auto height = png_get_image_height (png_ptr, info_ptr); - auto row_stride = png_get_rowbytes (png_ptr, info_ptr); + auto width {png_get_image_width (png_ptr, info_ptr)}; + auto height {png_get_image_height (png_ptr, info_ptr)}; + auto row_stride {png_get_rowbytes (png_ptr, info_ptr)}; // NOTE: We have to use visual_mem_malloc() here as LV::Video // will free the buffer with visual_mem_free() @@ -200,7 +201,7 @@ namespace LV { pixel_row_ptrs = new uint8_t*[height]; - for (unsigned int y = 0; y < height; y++) { + for (unsigned int y {0}; y < height; y++) { pixel_row_ptrs[y] = pixels + y * row_stride; } @@ -208,39 +209,39 @@ namespace LV { png_destroy_read_struct (&png_ptr, &info_ptr, &end_info); - delete []pixel_row_ptrs; + delete[] pixel_row_ptrs; return Video::wrap (pixels, true, width, height, depth); } - bool bitmap_save_png(Video const& bitmap, std::ostream &output) + bool bitmap_save_png(Video const& bitmap, std::ostream& output) { - auto saved_stream_pos = output.tellp (); + auto saved_stream_pos {output.tellp ()}; - auto png_ptr = png_create_write_struct (PNG_LIBPNG_VER_STRING, - nullptr, - handle_png_save_error, - handle_png_save_warning); + auto png_ptr {png_create_write_struct (PNG_LIBPNG_VER_STRING, + nullptr, + handle_png_save_error, + handle_png_save_warning)}; if (!png_ptr) { visual_log (VISUAL_LOG_ERROR, "Failed to create PNG write struct."); return false; } - auto info_ptr = png_create_info_struct (png_ptr); + auto info_ptr {png_create_info_struct (png_ptr)}; if (!info_ptr) { visual_log (VISUAL_LOG_ERROR, "Failed to create PNG info struct."); png_destroy_write_struct (&png_ptr, nullptr); return false; } - auto bitmap_width = bitmap.get_width (); - auto bitmap_height = bitmap.get_height (); + auto bitmap_width {bitmap.get_width ()}; + auto bitmap_height {bitmap.get_height ()}; std::vector pixel_row_ptrs; pixel_row_ptrs.reserve (bitmap_height); - for (auto y = 0; y < bitmap_height; y++) { - pixel_row_ptrs.push_back (static_cast (bitmap.get_pixel_ptr (0, y))); + for (auto y {0}; y < bitmap_height; y++) { + pixel_row_ptrs.push_back (static_cast (bitmap.get_pixel_ptr (0, y))); } if (setjmp (png_jmpbuf (png_ptr))) { @@ -249,8 +250,8 @@ namespace LV { png_destroy_write_struct (&png_ptr, &info_ptr); } - int bit_depth = 0; - int color_type = 0; + auto bit_depth {0}; + auto color_type {0}; switch (bitmap.get_depth ()) { case VISUAL_VIDEO_DEPTH_8BIT: { @@ -274,9 +275,9 @@ namespace LV { } } -#if VISUAL_LITTLE_ENDIAN - png_set_bgr (png_ptr); -#endif + if constexpr (std::endian::native == std::endian::little) { + png_set_bgr (png_ptr); + } png_set_filter (png_ptr, 0, PNG_FILTER_NONE); @@ -287,13 +288,13 @@ namespace LV { PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT); if (color_type == PNG_COLOR_TYPE_PALETTE) { - auto const& colors = bitmap.get_palette ().colors; + auto const& colors {bitmap.get_palette ().colors}; std::vector out_palette; out_palette.reserve (colors.size ()); for (auto const& color : colors) { - out_palette.push_back ({color.r, color.g, color.b}); + out_palette.emplace_back (color.r, color.g, color.b); } png_set_PLTE (png_ptr, info_ptr, out_palette.data (), out_palette.size ());