Skip to content

Commit 6e5fe53

Browse files
committed
Core (LV::Video): Apply modern C++-isms to PNG writer.
1 parent 337a0fe commit 6e5fe53

File tree

1 file changed

+47
-46
lines changed

1 file changed

+47
-46
lines changed

libvisual/libvisual/private/lv_video_png.cpp

Lines changed: 47 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#include "lv_video_png.hpp"
2424
#include "lv_common.h"
2525
#include <iostream>
26+
#include <bit>
2627
#include <vector>
2728
#include <csetjmp>
2829
#include <png.h>
@@ -34,8 +35,8 @@ namespace LV {
3435

3536
void handle_png_read (png_structp png_ptr, png_bytep data, png_size_t length)
3637
{
37-
auto io_ptr = png_get_io_ptr (png_ptr);
38-
auto& input = *static_cast<std::istream*> (io_ptr);
38+
auto io_ptr {png_get_io_ptr (png_ptr)};
39+
auto& input {*static_cast<std::istream*> (io_ptr)};
3940

4041
if (!input.read (reinterpret_cast<char*> (data), length)) {
4142
std::longjmp (png_jmpbuf (png_ptr), -1);
@@ -44,8 +45,8 @@ namespace LV {
4445

4546
void handle_png_write (png_structp png_ptr, png_bytep data, png_size_t length)
4647
{
47-
auto io_ptr = png_get_io_ptr (png_ptr);
48-
auto& output = *static_cast<std::ostream*> (io_ptr);
48+
auto io_ptr {png_get_io_ptr (png_ptr)};
49+
auto& output {*static_cast<std::ostream*> (io_ptr)};
4950

5051
if (!output.write(reinterpret_cast<char*>(data), length)) {
5152
std::longjmp (png_jmpbuf (png_ptr), -1);
@@ -54,8 +55,8 @@ namespace LV {
5455

5556
void handle_png_flush (png_structp png_ptr)
5657
{
57-
auto io_ptr = png_get_io_ptr (png_ptr);
58-
auto& output = *static_cast<std::ostream*> (io_ptr);
58+
auto io_ptr {png_get_io_ptr (png_ptr)};
59+
auto& output {*static_cast<std::ostream*> (io_ptr)};
5960

6061
output.flush ();
6162
}
@@ -92,46 +93,46 @@ namespace LV {
9293

9394
VideoPtr bitmap_load_png (std::istream& input)
9495
{
95-
auto saved_stream_pos = input.tellg ();
96+
auto saved_stream_pos {input.tellg ()};
9697

9798
png_byte signature[8];
9899
input.read (reinterpret_cast<char*> (signature), sizeof (signature));
99100

100-
bool is_png = !png_sig_cmp (signature, 0, sizeof (signature));
101+
auto is_png {!png_sig_cmp (signature, 0, sizeof (signature))};
101102

102103
if (!is_png) {
103104
return nullptr;
104105
}
105106

106-
auto png_ptr = png_create_read_struct (PNG_LIBPNG_VER_STRING,
107-
nullptr,
108-
handle_png_load_error,
109-
handle_png_load_warning);
107+
auto png_ptr {png_create_read_struct (PNG_LIBPNG_VER_STRING,
108+
nullptr,
109+
handle_png_load_error,
110+
handle_png_load_warning)};
110111
if (!png_ptr) {
111112
return nullptr;
112113
}
113114

114-
auto info_ptr = png_create_info_struct (png_ptr);
115+
auto info_ptr {png_create_info_struct (png_ptr)};
115116
if (!info_ptr) {
116117
png_destroy_read_struct (&png_ptr, nullptr, nullptr);
117118
return nullptr;
118119
}
119120

120-
auto end_info = png_create_info_struct (png_ptr);
121+
auto end_info {png_create_info_struct (png_ptr)};
121122
if (!end_info) {
122123
png_destroy_read_struct (&png_ptr, &info_ptr, nullptr);
123124
return nullptr;
124125
}
125126

126-
uint8_t* pixels = nullptr;
127-
uint8_t** pixel_row_ptrs = nullptr;
127+
uint8_t* pixels {nullptr};
128+
uint8_t** pixel_row_ptrs {nullptr};
128129

129130
if (setjmp (png_jmpbuf (png_ptr))) {
130131
input.seekg (saved_stream_pos);
131132

132133
png_destroy_read_struct (&png_ptr, &info_ptr, &end_info);
133134

134-
delete []pixel_row_ptrs;
135+
delete[] pixel_row_ptrs;
135136
visual_mem_free (pixels);
136137

137138
return nullptr;
@@ -143,8 +144,8 @@ namespace LV {
143144

144145
png_read_info (png_ptr, info_ptr);
145146

146-
auto color_type = png_get_color_type (png_ptr, info_ptr);
147-
auto bit_depth = png_get_bit_depth (png_ptr, info_ptr);
147+
auto color_type {png_get_color_type (png_ptr, info_ptr)};
148+
auto bit_depth {png_get_bit_depth (png_ptr, info_ptr)};
148149

149150
if (color_type == PNG_COLOR_TYPE_PALETTE) {
150151
png_set_palette_to_rgb (png_ptr);
@@ -171,13 +172,13 @@ namespace LV {
171172
png_set_gray_to_rgb (png_ptr);
172173
}
173174

174-
#if VISUAL_LITTLE_ENDIAN
175-
png_set_bgr (png_ptr);
176-
#endif
175+
if constexpr (std::endian::native == std::endian::little) {
176+
png_set_bgr (png_ptr);
177+
}
177178

178179
png_read_update_info (png_ptr, info_ptr);
179180

180-
VisVideoDepth depth = VISUAL_VIDEO_DEPTH_NONE;
181+
auto depth {VISUAL_VIDEO_DEPTH_NONE};
181182

182183
switch (png_get_color_type (png_ptr, info_ptr)) {
183184
case PNG_COLOR_TYPE_RGB:
@@ -190,57 +191,57 @@ namespace LV {
190191
std::longjmp (png_jmpbuf (png_ptr), -1);
191192
}
192193

193-
auto width = png_get_image_width (png_ptr, info_ptr);
194-
auto height = png_get_image_height (png_ptr, info_ptr);
195-
auto row_stride = png_get_rowbytes (png_ptr, info_ptr);
194+
auto width {png_get_image_width (png_ptr, info_ptr)};
195+
auto height {png_get_image_height (png_ptr, info_ptr)};
196+
auto row_stride {png_get_rowbytes (png_ptr, info_ptr)};
196197

197198
// NOTE: We have to use visual_mem_malloc() here as LV::Video
198199
// will free the buffer with visual_mem_free()
199200
pixels = static_cast<uint8_t*> (visual_mem_malloc (row_stride * height));
200201

201202
pixel_row_ptrs = new uint8_t*[height];
202203

203-
for (unsigned int y = 0; y < height; y++) {
204+
for (unsigned int y {0}; y < height; y++) {
204205
pixel_row_ptrs[y] = pixels + y * row_stride;
205206
}
206207

207208
png_read_image (png_ptr, pixel_row_ptrs);
208209

209210
png_destroy_read_struct (&png_ptr, &info_ptr, &end_info);
210211

211-
delete []pixel_row_ptrs;
212+
delete[] pixel_row_ptrs;
212213

213214
return Video::wrap (pixels, true, width, height, depth);
214215
}
215216

216-
bool bitmap_save_png(Video const& bitmap, std::ostream &output)
217+
bool bitmap_save_png(Video const& bitmap, std::ostream& output)
217218
{
218-
auto saved_stream_pos = output.tellp ();
219+
auto saved_stream_pos {output.tellp ()};
219220

220-
auto png_ptr = png_create_write_struct (PNG_LIBPNG_VER_STRING,
221-
nullptr,
222-
handle_png_save_error,
223-
handle_png_save_warning);
221+
auto png_ptr {png_create_write_struct (PNG_LIBPNG_VER_STRING,
222+
nullptr,
223+
handle_png_save_error,
224+
handle_png_save_warning)};
224225
if (!png_ptr) {
225226
visual_log (VISUAL_LOG_ERROR, "Failed to create PNG write struct.");
226227
return false;
227228
}
228229

229-
auto info_ptr = png_create_info_struct (png_ptr);
230+
auto info_ptr {png_create_info_struct (png_ptr)};
230231
if (!info_ptr) {
231232
visual_log (VISUAL_LOG_ERROR, "Failed to create PNG info struct.");
232233
png_destroy_write_struct (&png_ptr, nullptr);
233234
return false;
234235
}
235236

236-
auto bitmap_width = bitmap.get_width ();
237-
auto bitmap_height = bitmap.get_height ();
237+
auto bitmap_width {bitmap.get_width ()};
238+
auto bitmap_height {bitmap.get_height ()};
238239

239240
std::vector<png_byte*> pixel_row_ptrs;
240241
pixel_row_ptrs.reserve (bitmap_height);
241242

242-
for (auto y = 0; y < bitmap_height; y++) {
243-
pixel_row_ptrs.push_back (static_cast<png_byte *> (bitmap.get_pixel_ptr (0, y)));
243+
for (auto y {0}; y < bitmap_height; y++) {
244+
pixel_row_ptrs.push_back (static_cast<png_byte*> (bitmap.get_pixel_ptr (0, y)));
244245
}
245246

246247
if (setjmp (png_jmpbuf (png_ptr))) {
@@ -249,8 +250,8 @@ namespace LV {
249250
png_destroy_write_struct (&png_ptr, &info_ptr);
250251
}
251252

252-
int bit_depth = 0;
253-
int color_type = 0;
253+
auto bit_depth {0};
254+
auto color_type {0};
254255

255256
switch (bitmap.get_depth ()) {
256257
case VISUAL_VIDEO_DEPTH_8BIT: {
@@ -274,9 +275,9 @@ namespace LV {
274275
}
275276
}
276277

277-
#if VISUAL_LITTLE_ENDIAN
278-
png_set_bgr (png_ptr);
279-
#endif
278+
if constexpr (std::endian::native == std::endian::little) {
279+
png_set_bgr (png_ptr);
280+
}
280281

281282
png_set_filter (png_ptr, 0, PNG_FILTER_NONE);
282283

@@ -287,13 +288,13 @@ namespace LV {
287288
PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);
288289

289290
if (color_type == PNG_COLOR_TYPE_PALETTE) {
290-
auto const& colors = bitmap.get_palette ().colors;
291+
auto const& colors {bitmap.get_palette ().colors};
291292

292293
std::vector<png_color> out_palette;
293294
out_palette.reserve (colors.size ());
294295

295296
for (auto const& color : colors) {
296-
out_palette.push_back ({color.r, color.g, color.b});
297+
out_palette.emplace_back (color.r, color.g, color.b);
297298
}
298299

299300
png_set_PLTE (png_ptr, info_ptr, out_palette.data (), out_palette.size ());

0 commit comments

Comments
 (0)