From 949ec83f26070bdccc0d08f88b9f423a17118a66 Mon Sep 17 00:00:00 2001 From: Alisha Date: Thu, 26 Oct 2023 20:43:17 -0700 Subject: [PATCH] new classes for resource management --- .../image/detail/jpeg_decompress_struct.cc | 26 ++++++++++++ .../image/detail/jpeg_decompress_struct.h | 27 ++++++++++++ .../data/image/detail/png_read_struct.cc | 40 ++++++++++++++++++ .../data/image/detail/png_read_struct.h | 28 +++++++++++++ .../src/fairseq2n/data/image/image_decoder.cc | 42 ++++++++----------- 5 files changed, 139 insertions(+), 24 deletions(-) create mode 100644 fairseq2n/src/fairseq2n/data/image/detail/jpeg_decompress_struct.cc create mode 100644 fairseq2n/src/fairseq2n/data/image/detail/jpeg_decompress_struct.h create mode 100644 fairseq2n/src/fairseq2n/data/image/detail/png_read_struct.cc create mode 100644 fairseq2n/src/fairseq2n/data/image/detail/png_read_struct.h diff --git a/fairseq2n/src/fairseq2n/data/image/detail/jpeg_decompress_struct.cc b/fairseq2n/src/fairseq2n/data/image/detail/jpeg_decompress_struct.cc new file mode 100644 index 000000000..7ca080a41 --- /dev/null +++ b/fairseq2n/src/fairseq2n/data/image/detail/jpeg_decompress_struct.cc @@ -0,0 +1,26 @@ +// Copyright (c) Meta Platforms, Inc. and affiliates.error_ptr +// All rights reserved. +// +// This source code is licensed under the BSD-style license found in the +// LICENSE file in the root directory of this source tree. + +#include "fairseq2n/data/image/detail/jpeg_decompress_struct.h" + +#include + +#include "fairseq2n/exception.h" +#include "fairseq2n/detail/exception.h" + +namespace fairseq2n::detail { + +jpeg_decompress::jpeg_decompress() {} + +jpeg_decompress::~jpeg_decompress() { + jpeg_destroy_decompress(&cinfo); +} + +jpeg_decompress_struct& jpeg_decompress::get() { + return cinfo; +} + +} // namespace fairseq2n::detail diff --git a/fairseq2n/src/fairseq2n/data/image/detail/jpeg_decompress_struct.h b/fairseq2n/src/fairseq2n/data/image/detail/jpeg_decompress_struct.h new file mode 100644 index 000000000..03686a7c2 --- /dev/null +++ b/fairseq2n/src/fairseq2n/data/image/detail/jpeg_decompress_struct.h @@ -0,0 +1,27 @@ +// Copyright (c) Meta Platforms, Inc. and affiliates. +// All rights reserved. +// +// This source code is licensed under the BSD-style license found in the +// LICENSE file in the root directory of this source tree. + +#pragma once + +#include +#include "fairseq2n/exception.h" + +namespace fairseq2n::detail { + +class jpeg_decompress { +public: + jpeg_decompress(); + ~jpeg_decompress(); + + jpeg_decompress_struct& get(); + +private: + jpeg_decompress_struct cinfo; + jpeg_decompress(const jpeg_decompress&); + jpeg_decompress& operator=(const jpeg_decompress&); +}; + +} // namespace fairseq2n::detail diff --git a/fairseq2n/src/fairseq2n/data/image/detail/png_read_struct.cc b/fairseq2n/src/fairseq2n/data/image/detail/png_read_struct.cc new file mode 100644 index 000000000..1f2727a14 --- /dev/null +++ b/fairseq2n/src/fairseq2n/data/image/detail/png_read_struct.cc @@ -0,0 +1,40 @@ +// Copyright (c) Meta Platforms, Inc. and affiliates. +// All rights reserved. +// +// This source code is licensed under the BSD-style license found in the +// LICENSE file in the root directory of this source tree. + +#include "fairseq2n/data/image/detail/png_read_struct.h" + +#include "fairseq2n/exception.h" +#include "fairseq2n/detail/exception.h" + +namespace fairseq2n::detail { + +png_read::png_read() : png_ptr(nullptr), info_ptr(nullptr) { + png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, nullptr, nullptr, nullptr); + if (png_ptr == nullptr) { + throw internal_error("Failed to create PNG read struct."); + } + info_ptr = png_create_info_struct(png_ptr); + if (info_ptr == nullptr) { + png_destroy_read_struct(&png_ptr, nullptr, nullptr); + throw internal_error("Failed to create PNG info struct."); + } +} + +png_read::~png_read() { + if (png_ptr) { + png_destroy_read_struct(&png_ptr, &info_ptr, nullptr); + } +} + +png_structp png_read::getPngPtr() const { + return png_ptr; +} + +png_infop png_read::getInfoPtr() const { + return info_ptr; +} + +} // namespace fairseq2n::detail diff --git a/fairseq2n/src/fairseq2n/data/image/detail/png_read_struct.h b/fairseq2n/src/fairseq2n/data/image/detail/png_read_struct.h new file mode 100644 index 000000000..5c7a5f64f --- /dev/null +++ b/fairseq2n/src/fairseq2n/data/image/detail/png_read_struct.h @@ -0,0 +1,28 @@ +// Copyright (c) Meta Platforms, Inc. and affiliates. +// All rights reserved. +// +// This source code is licensed under the BSD-style license found in the +// LICENSE file in the root directory of this source tree. + +#pragma once + +#include + +namespace fairseq2n::detail { + +class png_read{ +public: + png_read(); + ~png_read(); + + png_structp getPngPtr() const; + png_infop getInfoPtr() const; + +private: + png_structp png_ptr; + png_infop info_ptr; + png_read(const png_read&); + png_read& operator=(const png_read&); +}; + +} // namespace fairseq2n::detail diff --git a/fairseq2n/src/fairseq2n/data/image/image_decoder.cc b/fairseq2n/src/fairseq2n/data/image/image_decoder.cc index b945d33c8..8d1d6f232 100644 --- a/fairseq2n/src/fairseq2n/data/image/image_decoder.cc +++ b/fairseq2n/src/fairseq2n/data/image/image_decoder.cc @@ -9,17 +9,17 @@ #include #include #include -#include #include #include -#include +#include #include "fairseq2n/exception.h" #include "fairseq2n/float.h" #include "fairseq2n/fmt.h" #include "fairseq2n/memory.h" -#include "fairseq2n/span.h" +#include "fairseq2n/data/image/detail/png_read_struct.h" +#include "fairseq2n/data/image/detail/jpeg_decompress_struct.h" #include "fairseq2n/data/detail/tensor_helpers.h" #include "fairseq2n/detail/exception.h" @@ -67,15 +67,9 @@ image_decoder::operator()(data &&d) const data image_decoder::decode_png(const memory_block &block) const { - png_structp png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, nullptr, nullptr, nullptr); - if (png_ptr == nullptr) { - throw_("Failed to create PNG read struct."); - } - png_infop info_ptr = png_create_info_struct(png_ptr); - if (info_ptr == nullptr) { - png_destroy_read_struct(&png_ptr, nullptr, nullptr); - throw_("Failed to create PNG info struct."); - } + png_read pngReadStruct; + png_structp png_ptr = pngReadStruct.getPngPtr(); + png_infop info_ptr = pngReadStruct.getInfoPtr(); auto data_ptr = png_const_bytep(block.data()); auto data_len = block.size(); @@ -162,25 +156,25 @@ image_decoder::decode_png(const memory_block &block) const data image_decoder::decode_jpeg(const memory_block &block) const { + jpeg_decompress jpegDecompressStruct; + jpeg_decompress_struct cinfo = jpegDecompressStruct.get(); + + auto data_ptr = block.data(); + auto data_len = block.size(); + struct custom_error_mgr { struct jpeg_error_mgr pub; // Public fields jmp_buf setjmp_buffer; // Return to caller }; - typedef struct custom_error_mgr * error_ptr; - - auto data_ptr = block.data(); - auto data_len = block.size(); - - // Set up decompression process - struct jpeg_decompress_struct cinfo = {}; struct custom_error_mgr jerr = {}; + typedef struct custom_error_mgr * error_ptr; cinfo.err = jpeg_std_error(&jerr.pub); - // error_exit is called by libjpeg when fatal error occurs + // error_exit is called by libjpeg when a fatal error occurs jerr.pub.error_exit = [](j_common_ptr cinfo) { - // cinfo->err really points to a custom_error_mgr struct, so coerce pointer - error_ptr myerr = (error_ptr) cinfo->err; - (*cinfo->err->output_message) (cinfo); - // Return control to the setjmp point + // Coerce pointer to custom_error_mgr struct + auto myerr = reinterpret_cast(cinfo->err); + (*cinfo->err->output_message)(cinfo); + // Return control to the setjmp point longjmp(myerr->setjmp_buffer, 1); }; // If an error occurs, error_exit will longjmp back to setjmp