Clean up image_diff_png.cpp.
- Merge anonymous namespaces, and put more code into the anonymous
namespace.
- Initialize member variables where defined.
- Use constexpr in more places.
- s/NULL/nullptr/
Change-Id: I94ae18b46239b41cce8cda542b0f264d0d6f4601
Reviewed-on: https://pdfium-review.googlesource.com/c/pdfium/+/56730
Commit-Queue: Lei Zhang <thestig@chromium.org>
Reviewed-by: Tom Sepez <tsepez@chromium.org>
diff --git a/testing/image_diff/image_diff_png.cpp b/testing/image_diff/image_diff_png.cpp
index 9825e28..ef9d4aa 100644
--- a/testing/image_diff/image_diff_png.cpp
+++ b/testing/image_diff/image_diff_png.cpp
@@ -101,56 +101,47 @@
}
}
-} // namespace
-
// Decoder
//
// This code is based on WebKit libpng interface (PNGImageDecoder), which is
// in turn based on the Mozilla png decoder.
-namespace {
-
// Gamma constants: We assume we're on Windows which uses a gamma of 2.2.
-const double kMaxGamma = 21474.83; // Maximum gamma accepted by png library.
-const double kDefaultGamma = 2.2;
-const double kInverseGamma = 1.0 / kDefaultGamma;
+constexpr double kDefaultGamma = 2.2;
+
+// Maximum gamma accepted by PNG library.
+constexpr double kMaxGamma = 21474.83;
+
+constexpr double kInverseGamma = 1.0 / kDefaultGamma;
class PngDecoderState {
public:
- // Output is a vector<unsigned char>.
- PngDecoderState(ColorFormat ofmt, std::vector<unsigned char>* o)
- : output_format(ofmt),
- output_channels(0),
- is_opaque(true),
- output(o),
- row_converter(nullptr),
- width(0),
- height(0),
- done(false) {}
+ PngDecoderState(ColorFormat ofmt, std::vector<unsigned char>* out)
+ : output_format(ofmt), output(out) {}
- ColorFormat output_format;
- int output_channels;
+ const ColorFormat output_format;
+ int output_channels = 0;
// Used during the reading of an SkBitmap. Defaults to true until we see a
// pixel with anything other than an alpha of 255.
- bool is_opaque;
+ bool is_opaque = true;
// An intermediary buffer for decode output.
- std::vector<unsigned char>* output;
+ std::vector<unsigned char>* const output;
// Called to convert a row from the library to the correct output format.
- // When NULL, no conversion is necessary.
+ // When null, no conversion is necessary.
void (*row_converter)(const unsigned char* in,
int w,
unsigned char* out,
- bool* is_opaque);
+ bool* is_opaque) = nullptr;
// Size of the image, set in the info callback.
- int width;
- int height;
+ int width = 0;
+ int height = 0;
// Set to true when we've found the end of the data.
- bool done;
+ bool done = false;
};
void ConvertRGBtoRGBA(const unsigned char* rgb,
@@ -304,7 +295,7 @@
return;
}
- unsigned char* base = NULL;
+ unsigned char* base = nullptr;
base = &state->output->front();
unsigned char* dest = &base[state->width * state->output_channels * row_num];
@@ -328,7 +319,7 @@
class PngReadStructDestroyer {
public:
PngReadStructDestroyer(png_struct** ps, png_info** pi) : ps_(ps), pi_(pi) {}
- ~PngReadStructDestroyer() { png_destroy_read_struct(ps_, pi_, NULL); }
+ ~PngReadStructDestroyer() { png_destroy_read_struct(ps_, pi_, nullptr); }
private:
png_struct** ps_;
@@ -346,30 +337,28 @@
if (png_sig_cmp(const_cast<unsigned char*>(input), 0, 8) != 0)
return false;
- *png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
+ *png_ptr =
+ png_create_read_struct(PNG_LIBPNG_VER_STRING, nullptr, nullptr, nullptr);
if (!*png_ptr)
return false;
*info_ptr = png_create_info_struct(*png_ptr);
if (!*info_ptr) {
- png_destroy_read_struct(png_ptr, NULL, NULL);
+ png_destroy_read_struct(png_ptr, nullptr, nullptr);
return false;
}
return true;
}
-} // namespace
-
-// static
std::vector<unsigned char> Decode(const unsigned char* input,
size_t input_size,
ColorFormat format,
int* w,
int* h) {
std::vector<unsigned char> output;
- png_struct* png_ptr = NULL;
- png_info* info_ptr = NULL;
+ png_struct* png_ptr = nullptr;
+ png_info* info_ptr = nullptr;
if (!BuildPNGStruct(input, input_size, &png_ptr, &info_ptr))
return output;
@@ -405,8 +394,6 @@
// This section of the code is based on nsPNGEncoder.cpp in Mozilla
// (Copyright 2005 Google Inc.)
-namespace {
-
// Passed around as the io_ptr in the png structs so our callbacks know where
// to write data.
struct PngEncoderState {
@@ -518,7 +505,7 @@
#ifdef PNG_TEXT_SUPPORTED
CommentWriter comment_writer(comments);
#endif
- unsigned char* row_buffer = NULL;
+ unsigned char* row_buffer = nullptr;
// Make sure to not declare any locals here -- locals in the presence
// of setjmp() in C++ code makes gcc complain.
@@ -556,7 +543,7 @@
// Needs conversion using a separate buffer.
row_buffer = new unsigned char[width * output_color_components];
for (int y = 0; y < height; y++) {
- converter(&input[y * row_byte_width], width, row_buffer, NULL);
+ converter(&input[y * row_byte_width], width, row_buffer, nullptr);
png_write_row(png_ptr, row_buffer);
}
delete[] row_buffer;
@@ -566,9 +553,6 @@
return true;
}
-} // namespace
-
-// static
std::vector<unsigned char> EncodeWithCompressionLevel(
const unsigned char* input,
ColorFormat format,
@@ -580,7 +564,7 @@
int compression_level) {
std::vector<unsigned char> output;
- // Run to convert an input row into the output row format, NULL means no
+ // Run to convert an input row into the output row format, nullptr means no
// conversion is necessary.
FormatConverter converter = nullptr;
@@ -640,12 +624,12 @@
return output;
png_struct* png_ptr =
- png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
+ png_create_write_struct(PNG_LIBPNG_VER_STRING, nullptr, nullptr, nullptr);
if (!png_ptr)
return output;
png_info* info_ptr = png_create_info_struct(png_ptr);
if (!info_ptr) {
- png_destroy_write_struct(&png_ptr, NULL);
+ png_destroy_write_struct(&png_ptr, nullptr);
return output;
}
@@ -661,7 +645,6 @@
return output;
}
-// static
std::vector<unsigned char> Encode(const unsigned char* input,
ColorFormat format,
const int width,
@@ -674,6 +657,8 @@
comments, Z_DEFAULT_COMPRESSION);
}
+} // namespace
+
std::vector<unsigned char> DecodePNG(const unsigned char* input,
size_t input_size,
int* width,