diff --git a/reader/JFJochCBFReader.cpp b/reader/JFJochCBFReader.cpp index 3b35f4718..3b1edc7a4 100644 --- a/reader/JFJochCBFReader.cpp +++ b/reader/JFJochCBFReader.cpp @@ -301,7 +301,8 @@ void JFJochCBFReader::Close() { } template -CompressedImage JFJochCBFReader::DecodeInto(int64_t image_number, Buffer &buffer) const { +CompressedImage JFJochCBFReader::DecodeInto(int64_t image_number, Buffer &buffer, + std::vector &scratch) const { if (image_number < 0 || static_cast(image_number) >= files_.size()) throw JFJochException(JFJochExceptionCategory::InputParameterInvalid, "Image number out of range"); @@ -312,7 +313,7 @@ CompressedImage JFJochCBFReader::DecodeInto(int64_t image_number, Buffer &buffer // Decode straight into the caller's bytes: the pixels are plain int32 and nothing downstream has // to decompress them, so NO_COMPRESSION over that buffer is the whole image. const auto h = minicbf::ReadInto(files_[image_number], - reinterpret_cast(buffer.data()), npixel); + reinterpret_cast(buffer.data()), npixel, scratch); if (static_cast(h.nelem) != npixel) throw JFJochException(JFJochExceptionCategory::InputParameterInvalid, "CBF image size differs from the first image of the sweep"); @@ -333,13 +334,14 @@ bool JFJochCBFReader::LoadImage_i(std::shared_ptr &dataset, // The image must outlive this call, so it is decoded straight into the caller's buffer - the same // thing the argument is for on the HDF5 path - and message.image only points at it. - message.image = DecodeInto(image_number, buffer); + std::vector scratch; + message.image = DecodeInto(image_number, buffer, scratch); message.number = image_number; return true; } bool JFJochCBFReader::ReadRawImage(int64_t image_number, JFJochReaderRawImage &image) { - image.image = DecodeInto(image_number, image.image_buffer); + image.image = DecodeInto(image_number, image.image_buffer, image.read_buffer); return true; } diff --git a/reader/JFJochCBFReader.h b/reader/JFJochCBFReader.h index b57c73454..e13555d6a 100644 --- a/reader/JFJochCBFReader.h +++ b/reader/JFJochCBFReader.h @@ -32,9 +32,10 @@ class JFJochCBFReader : public JFJochReader { bool update_dataset) override; // Decodes one image into the caller's byte buffer (RawByteBuffer or std::vector) and - // returns the image that points at it. + // returns the image that points at it. The compressed file is read through scratch, which the + // caller keeps between frames. template - CompressedImage DecodeInto(int64_t image_number, Buffer &buffer) const; + CompressedImage DecodeInto(int64_t image_number, Buffer &buffer, std::vector &scratch) const; public: ~JFJochCBFReader() override = default; diff --git a/reader/JFJochReaderImage.h b/reader/JFJochReaderImage.h index b300bc9a8..e7283dcce 100644 --- a/reader/JFJochReaderImage.h +++ b/reader/JFJochReaderImage.h @@ -27,6 +27,10 @@ constexpr static int32_t SATURATED_PXL_VALUE = INT32_MAX; struct JFJochReaderRawImage { RawByteBuffer image_buffer; CompressedImage image; + // Scratch the reader needs to produce the image and that nothing else reads: the CBF reader + // reads the compressed file into it before decoding. It lives here so that a raw image reused + // from one frame to the next reuses this buffer too. + std::vector read_buffer; }; class JFJochReaderImage { diff --git a/reader/MiniCBF.cpp b/reader/MiniCBF.cpp index 33ad22213..d15ddd050 100644 --- a/reader/MiniCBF.cpp +++ b/reader/MiniCBF.cpp @@ -340,7 +340,7 @@ void DecodeByteOffset(const uint8_t *data, size_t size, int32_t *out, size_t n_p namespace { -std::vector Slurp(const std::string &path, size_t max_bytes) { +void Slurp(const std::string &path, size_t max_bytes, std::vector &buf) { std::ifstream f(path, std::ios::binary); if (!f) throw JFJochException(JFJochExceptionCategory::InputParameterInvalid, @@ -348,10 +348,9 @@ std::vector Slurp(const std::string &path, size_t max_bytes) { f.seekg(0, std::ios::end); const auto file_size = static_cast(f.tellg()); f.seekg(0, std::ios::beg); - std::vector buf(std::min(file_size, max_bytes)); + buf.resize(std::min(file_size, max_bytes)); f.read(reinterpret_cast(buf.data()), static_cast(buf.size())); buf.resize(static_cast(f.gcount())); - return buf; } // Enough to reach the separator on any header seen in the wild (the longest measured is ~6.3 kB). @@ -360,7 +359,8 @@ constexpr size_t HEADER_PROBE_BYTES = 256 * 1024; } // namespace Header ReadHeader(const std::string &path) { - const auto buf = Slurp(path, HEADER_PROBE_BYTES); + std::vector buf; + Slurp(path, HEADER_PROBE_BYTES, buf); const auto start = FindBinarySection(buf.data(), buf.size()); if (!start.has_value()) throw JFJochException(JFJochExceptionCategory::InputParameterInvalid, @@ -373,7 +373,7 @@ namespace { // One read of the file, header parsed, dimensions checked. The caller supplies where the pixels go. Header ReadCommon(const std::string &path, std::vector &buf, size_t &binary_start) { - buf = Slurp(path, std::numeric_limits::max()); + Slurp(path, std::numeric_limits::max(), buf); const auto start = FindBinarySection(buf.data(), buf.size()); if (!start.has_value()) throw JFJochException(JFJochExceptionCategory::InputParameterInvalid, @@ -413,14 +413,13 @@ Header Read(const std::string &path, std::vector &out) { return h; } -Header ReadInto(const std::string &path, int32_t *out, size_t capacity) { - std::vector buf; +Header ReadInto(const std::string &path, int32_t *out, size_t capacity, std::vector &scratch) { size_t start = 0; - const Header h = ReadCommon(path, buf, start); + const Header h = ReadCommon(path, scratch, start); if (static_cast(h.nelem) > capacity) throw JFJochException(JFJochExceptionCategory::InputParameterInvalid, "CBF image does not fit the supplied buffer: " + path); - DecodeByteOffset(buf.data() + start, buf.size() - start, out, static_cast(h.nelem)); + DecodeByteOffset(scratch.data() + start, scratch.size() - start, out, static_cast(h.nelem)); return h; } diff --git a/reader/MiniCBF.h b/reader/MiniCBF.h index 78a178955..dd8cb92a1 100644 --- a/reader/MiniCBF.h +++ b/reader/MiniCBF.h @@ -84,8 +84,10 @@ void DecodeByteOffset(const uint8_t *data, size_t size, int32_t *out, size_t n_p Header Read(const std::string &path, std::vector &out); // The same, decoding into memory the caller already has (one read of the file, no extra copy). +// The compressed file is read through scratch, which the caller keeps between frames: a fresh +// vector per image is megabytes of allocation and of zeroing nothing ever reads. // Throws if the image does not fit in capacity pixels. -Header ReadInto(const std::string &path, int32_t *out, size_t capacity); +Header ReadInto(const std::string &path, int32_t *out, size_t capacity, std::vector &scratch); // Header only - reads just enough of the file to reach the separator. Cheap enough to call per // frame for the rotation angles.