decode_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 decode_cxx.h
  9. /// @brief C++ header-only helper for @ref decode.h.
  10. ///
  11. /// There's no binary library associated with the header since this is a header
  12. /// only library.
  13. #ifndef JXL_DECODE_CXX_H_
  14. #define JXL_DECODE_CXX_H_
  15. #include <jxl/decode.h>
  16. #include <jxl/memory_manager.h>
  17. #include <memory>
  18. #ifndef __cplusplus
  19. #error "This a C++ only header. Use jxl/decode.h from C sources."
  20. #endif
  21. /// Struct to call JxlDecoderDestroy from the JxlDecoderPtr unique_ptr.
  22. struct JxlDecoderDestroyStruct {
  23. /// Calls @ref JxlDecoderDestroy() on the passed decoder.
  24. void operator()(JxlDecoder* decoder) { JxlDecoderDestroy(decoder); }
  25. };
  26. /// std::unique_ptr<> type that calls JxlDecoderDestroy() when releasing the
  27. /// decoder.
  28. ///
  29. /// Use this helper type from C++ sources to ensure the decoder is destroyed and
  30. /// their internal resources released.
  31. typedef std::unique_ptr<JxlDecoder, JxlDecoderDestroyStruct> JxlDecoderPtr;
  32. /// Creates an instance of JxlDecoder into a JxlDecoderPtr and initializes it.
  33. ///
  34. /// This function returns a unique_ptr that will call JxlDecoderDestroy() when
  35. /// releasing the pointer. See @ref JxlDecoderCreate 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 JxlDecoderPtr if the instance can not be allocated or
  41. /// initialized
  42. /// @return initialized JxlDecoderPtr instance otherwise.
  43. static inline JxlDecoderPtr JxlDecoderMake(
  44. const JxlMemoryManager* memory_manager) {
  45. return JxlDecoderPtr(JxlDecoderCreate(memory_manager));
  46. }
  47. #endif // JXL_DECODE_CXX_H_
  48. /// @}