encode_cxx.h 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. // Copyright (c) the JPEG XL Project Authors. All rights reserved.
  2. //
  3. // Use of this source code is governed by a BSD-style
  4. // license that can be found in the LICENSE file.
  5. /// @addtogroup libjxl_cpp
  6. ///@{
  7. ///
  8. /// @file encode_cxx.h
  9. /// @brief C++ header-only helper for @ref encode.h.
  10. ///
  11. /// There's no binary library associated with the header since this is a header
  12. /// only library.
  13. #ifndef JXL_ENCODE_CXX_H_
  14. #define JXL_ENCODE_CXX_H_
  15. #include <jxl/encode.h>
  16. #include <jxl/memory_manager.h>
  17. #include <memory>
  18. #ifndef __cplusplus
  19. #error "This a C++ only header. Use jxl/encode.h from C sources."
  20. #endif
  21. /// Struct to call JxlEncoderDestroy from the JxlEncoderPtr unique_ptr.
  22. struct JxlEncoderDestroyStruct {
  23. /// Calls @ref JxlEncoderDestroy() on the passed encoder.
  24. void operator()(JxlEncoder* encoder) { JxlEncoderDestroy(encoder); }
  25. };
  26. /// std::unique_ptr<> type that calls JxlEncoderDestroy() when releasing the
  27. /// encoder.
  28. ///
  29. /// Use this helper type from C++ sources to ensure the encoder is destroyed and
  30. /// their internal resources released.
  31. typedef std::unique_ptr<JxlEncoder, JxlEncoderDestroyStruct> JxlEncoderPtr;
  32. /// Creates an instance of JxlEncoder into a JxlEncoderPtr and initializes it.
  33. ///
  34. /// This function returns a unique_ptr that will call JxlEncoderDestroy() when
  35. /// releasing the pointer. See @ref JxlEncoderCreate for details on the
  36. /// instance creation.
  37. ///
  38. /// @param memory_manager custom allocator function. It may be NULL. The memory
  39. /// manager will be copied internally.
  40. /// @return a @c NULL JxlEncoderPtr if the instance can not be allocated or
  41. /// initialized
  42. /// @return initialized JxlEncoderPtr instance otherwise.
  43. static inline JxlEncoderPtr JxlEncoderMake(
  44. const JxlMemoryManager* memory_manager) {
  45. return JxlEncoderPtr(JxlEncoderCreate(memory_manager));
  46. }
  47. #endif // JXL_ENCODE_CXX_H_
  48. /// @}