diff --git a/include/exiv2/jp2image.hpp b/include/exiv2/jp2image.hpp index 9fc41acb..e1c7bc16 100644 --- a/include/exiv2/jp2image.hpp +++ b/include/exiv2/jp2image.hpp @@ -82,6 +82,7 @@ class EXIV2API Jp2Image : public Image { void encodeJp2Header(const DataBuf& boxBuf, DataBuf& outBuf); //@} + static std::string toAscii(uint32_t n); }; // class Jp2Image // ***************************************************************************** diff --git a/src/bmffimage.cpp b/src/bmffimage.cpp index 6f6e39e0..3698d57e 100644 --- a/src/bmffimage.cpp +++ b/src/bmffimage.cpp @@ -84,17 +84,16 @@ BmffImage::BmffImage(BasicIo::UniquePtr io, bool /* create */) : std::string BmffImage::toAscii(uint32_t n) { const auto p = reinterpret_cast(&n); - std::string result; - for (int i = 0; i < 4; i++) { - char c = p[isBigEndianPlatform() ? i : (3 - i)]; - result += [c]() { - if (32 <= c && c < 127) - return c; // only allow 7-bit printable ascii - if (c == 0) - return '_'; // show 0 as _ - return '.'; // others . - }(); - } + std::string result(4, '.'); + std::transform(p, p + 4, result.begin(), [](char c) { + if (32 <= c && c < 127) + return c; // only allow 7-bit printable ascii + if (c == 0) + return '_'; // show 0 as _ + return '.'; // others . + }); + if (!isBigEndianPlatform()) + std::reverse(result.begin(), result.end()); return result; } diff --git a/src/jp2image.cpp b/src/jp2image.cpp index fb9fc786..525f1b50 100644 --- a/src/jp2image.cpp +++ b/src/jp2image.cpp @@ -72,26 +72,6 @@ void lf(std::ostream& out, bool& bLF) { } } -bool isBigEndian() { - union { - uint32_t i; - char c[4]; - } e = {0x01000000}; - - return e.c[0] != 0; -} - -// Obtains the ascii version from the box.type -std::string toAscii(long n) { - const auto p = reinterpret_cast(&n); - std::string result; - bool bBigEndian = isBigEndian(); - for (int i = 0; i < 4; i++) { - result += p[bBigEndian ? i : (3 - i)]; - } - return result; -} - void boxes_check(size_t b, size_t m) { if (b > m) { #ifdef EXIV2_DEBUG_MESSAGES @@ -117,6 +97,15 @@ Jp2Image::Jp2Image(BasicIo::UniquePtr io, bool create) : Image(ImageType::jp2, m } } +// Obtains the ascii version from the box.type +std::string Jp2Image::toAscii(uint32_t n) { + const auto p = reinterpret_cast(&n); + std::string result(p, p + 4); + if (!isBigEndianPlatform()) + std::reverse(result.begin(), result.end()); + return result; +} + std::string Jp2Image::mimeType() const { return "image/jp2"; }