1 #ifndef IMAGE_IO_BASE_BYTE_BUFFER_H_ // NOLINT 2 #define IMAGE_IO_BASE_BYTE_BUFFER_H_ // NOLINT 3 4 #include <memory> 5 #include <vector> 6 7 #include "image_io/base/byte_data.h" 8 9 namespace photos_editing_formats { 10 namespace image_io { 11 12 /// This class provides a means to allocate and fill a Byte buffer with the 13 /// data specified in a vector of ByteData objects, and then to release that 14 /// buffer to be used in a DataSegment. This is used for testing purposes 15 /// initially, but has applicability for use in the image_io itself. 16 class ByteBuffer { 17 public: 18 /// Constructs a ByteBuffer using a previously allocated buffer. 19 /// @param size The size of the buffer. 20 /// @param buffer The previously allocated buffer 21 ByteBuffer(size_t size, std::unique_ptr<Byte[]> buffer); 22 23 /// Constructs a ByteBuffer using the vector of byte data. 24 /// @param byte_data_vector The data to used to define the length and value of 25 /// the buffer. If any ByteData in the vector is of kHex type, and it 26 /// contains invalid hex digits, the size value will be set to 0, 27 /// resulting in a ByteBuffer the IsValid() function of which will return 28 /// false. 29 explicit ByteBuffer(const std::vector<ByteData>& byte_data_vector); 30 31 /// @return Whether the byte buffer is valid. IsValid()32 bool IsValid() const { return size_ > 0; } 33 34 /// @return The size of the byte buffer. GetSize()35 size_t GetSize() const { return size_; } 36 37 /// @param location The location in the byte buffer to set. 38 /// @param value The two-byte value. 39 /// @return Whether the value was set successfully. 40 bool SetBigEndianValue(size_t location, std::uint16_t value); 41 42 /// Releases the buffer to the caller and sets this ByteBuffer object to an 43 /// invalid state. That is, after this call IsValid() will return false, and 44 /// GetSize() will return 0. 45 /// @return The buffer pointer or nullptr if the ByteBuffer was invalid. The 46 /// caller is responsible for deleting the buffer when done. 47 Byte* Release(); 48 49 private: 50 std::unique_ptr<Byte[]> buffer_; 51 size_t size_; 52 }; 53 54 } // namespace image_io 55 } // namespace photos_editing_formats 56 57 #endif // IMAGE_IO_BASE_BYTE_BUFFER_H_ // NOLINT 58