Do slightly fewer casts with FPDFBitmap_GetBuffer() result. Leave it as a void* and only cast it when needed. Change-Id: I9ffaaf992ccd963bc1ba541d249d4a56d626a44b Reviewed-on: https://pdfium-review.googlesource.com/c/pdfium/+/64072 Commit-Queue: Lei Zhang <thestig@chromium.org> Reviewed-by: Tom Sepez <tsepez@chromium.org>
diff --git a/samples/pdfium_test.cc b/samples/pdfium_test.cc index c65490e..d38227d 100644 --- a/samples/pdfium_test.cc +++ b/samples/pdfium_test.cc
@@ -218,10 +218,9 @@ return static_cast<FPDF_FORMFILLINFO_PDFiumTest*>(form_fill_info); } -void OutputMD5Hash(const char* file_name, const char* buffer, int len) { +void OutputMD5Hash(const char* file_name, const uint8_t* buffer, int len) { // Get the MD5 hash and write it to stdout. - std::string hash = - GenerateMD5Base16(reinterpret_cast<const uint8_t*>(buffer), len); + std::string hash = GenerateMD5Base16(buffer, len); printf("MD5:%s:%s\n", file_name, hash.c_str()); } @@ -748,8 +747,7 @@ FPDF_RenderPage_Close(page); int stride = FPDFBitmap_GetStride(bitmap.get()); - const char* buffer = - reinterpret_cast<const char*>(FPDFBitmap_GetBuffer(bitmap.get())); + void* buffer = FPDFBitmap_GetBuffer(bitmap.get()); std::string image_file_name; switch (options.output_format) { @@ -801,8 +799,10 @@ // Write the filename and the MD5 of the buffer to stdout if we wrote a // file. - if (options.md5 && !image_file_name.empty()) - OutputMD5Hash(image_file_name.c_str(), buffer, stride * height); + if (options.md5 && !image_file_name.empty()) { + OutputMD5Hash(image_file_name.c_str(), + static_cast<const uint8_t*>(buffer), stride * height); + } } else { fprintf(stderr, "Page was too large to be rendered.\n"); }
diff --git a/samples/pdfium_test_write_helper.cc b/samples/pdfium_test_write_helper.cc index 356bb08..b89b38a 100644 --- a/samples/pdfium_test_write_helper.cc +++ b/samples/pdfium_test_write_helper.cc
@@ -179,12 +179,10 @@ std::string WritePpm(const char* pdf_name, int num, - const void* buffer_void, + void* buffer_void, int stride, int width, int height) { - const auto* buffer = reinterpret_cast<const char*>(buffer_void); - if (!CheckDimensions(stride, width, height)) return ""; @@ -203,10 +201,11 @@ fprintf(fp, "P6\n# PDF test render\n%d %d\n255\n", width, height); // Source data is B, G, R, unused. // Dest data is R, G, B. - std::vector<char> result(out_len); + const uint8_t* buffer = reinterpret_cast<const uint8_t*>(buffer_void); + std::vector<uint8_t> result(out_len); for (int h = 0; h < height; ++h) { - const char* src_line = buffer + (stride * h); - char* dest_line = result.data() + (width * h * 3); + const uint8_t* src_line = buffer + (stride * h); + uint8_t* dest_line = result.data() + (width * h * 3); for (int w = 0; w < width; ++w) { // R dest_line[w * 3] = src_line[(w * 4) + 2]; @@ -377,17 +376,16 @@ std::string WritePng(const char* pdf_name, int num, - const void* buffer_void, + void* buffer, int stride, int width, int height) { if (!CheckDimensions(stride, width, height)) return ""; - const auto* buffer = static_cast<const unsigned char*>(buffer_void); - std::vector<unsigned char> png_encoding = - EncodePng(buffer, width, height, stride, FPDFBitmap_BGRA); + EncodePng(static_cast<const unsigned char*>(buffer), width, height, + stride, FPDFBitmap_BGRA); if (png_encoding.empty()) { fprintf(stderr, "Failed to convert bitmap to PNG\n"); return ""; @@ -420,7 +418,7 @@ #ifdef _WIN32 std::string WriteBmp(const char* pdf_name, int num, - const void* buffer, + void* buffer, int stride, int width, int height) {
diff --git a/samples/pdfium_test_write_helper.h b/samples/pdfium_test_write_helper.h index f44c2e7..b53d555 100644 --- a/samples/pdfium_test_write_helper.h +++ b/samples/pdfium_test_write_helper.h
@@ -16,7 +16,7 @@ std::string WritePpm(const char* pdf_name, int num, - const void* buffer_void, + void* buffer_void, int stride, int width, int height); @@ -24,7 +24,7 @@ void WriteAnnot(FPDF_PAGE page, const char* pdf_name, int num); std::string WritePng(const char* pdf_name, int num, - const void* buffer_void, + void* buffer, int stride, int width, int height); @@ -32,7 +32,7 @@ #ifdef _WIN32 std::string WriteBmp(const char* pdf_name, int num, - const void* buffer, + void* buffer, int stride, int width, int height);