Commit bf491463 authored by limm's avatar limm
Browse files

add v0.19.1 release

parent e17f5ea2
......@@ -13,17 +13,27 @@ torch::Tensor encode_jpeg(const torch::Tensor& data, int64_t quality) {
}
#else
// For libjpeg version <= 9b, the out_size parameter in jpeg_mem_dest() is
// defined as unsigned long, whereas in later version, it is defined as size_t.
#if !defined(JPEG_LIB_VERSION_MAJOR) || JPEG_LIB_VERSION_MAJOR < 9 || \
(JPEG_LIB_VERSION_MAJOR == 9 && JPEG_LIB_VERSION_MINOR <= 2)
using JpegSizeType = unsigned long;
#else
using JpegSizeType = size_t;
#endif
using namespace detail;
torch::Tensor encode_jpeg(const torch::Tensor& data, int64_t quality) {
C10_LOG_API_USAGE_ONCE(
"torchvision.csrc.io.image.cpu.encode_jpeg.encode_jpeg");
// Define compression structures and error handling
struct jpeg_compress_struct cinfo;
struct torch_jpeg_error_mgr jerr;
struct jpeg_compress_struct cinfo {};
struct torch_jpeg_error_mgr jerr {};
// Define buffer to write JPEG information to and its size
unsigned long jpegSize = 0;
uint8_t* jpegBuf = NULL;
JpegSizeType jpegSize = 0;
uint8_t* jpegBuf = nullptr;
cinfo.err = jpeg_std_error(&jerr.pub);
jerr.pub.error_exit = torch_jpeg_error_exit;
......@@ -34,7 +44,7 @@ torch::Tensor encode_jpeg(const torch::Tensor& data, int64_t quality) {
* We need to clean up the JPEG object and the buffer.
*/
jpeg_destroy_compress(&cinfo);
if (jpegBuf != NULL) {
if (jpegBuf != nullptr) {
free(jpegBuf);
}
......@@ -92,16 +102,10 @@ torch::Tensor encode_jpeg(const torch::Tensor& data, int64_t quality) {
jpeg_destroy_compress(&cinfo);
torch::TensorOptions options = torch::TensorOptions{torch::kU8};
auto outTensor = torch::empty({(long)jpegSize}, options);
// Copy memory from jpeg buffer, since torch cannot get ownership of it via
// `from_blob`
auto outPtr = outTensor.data_ptr<uint8_t>();
std::memcpy(outPtr, jpegBuf, sizeof(uint8_t) * outTensor.numel());
free(jpegBuf);
return outTensor;
auto out_tensor =
torch::from_blob(jpegBuf, {(long)jpegSize}, ::free, options);
jpegBuf = nullptr;
return out_tensor;
}
#endif
......
......@@ -63,6 +63,7 @@ void torch_png_write_data(
} // namespace
torch::Tensor encode_png(const torch::Tensor& data, int64_t compression_level) {
C10_LOG_API_USAGE_ONCE("torchvision.csrc.io.image.cpu.encode_png.encode_png");
// Define compression structures and error handling
png_structp png_write;
png_infop info_ptr;
......@@ -70,7 +71,7 @@ torch::Tensor encode_png(const torch::Tensor& data, int64_t compression_level) {
// Define output buffer
struct torch_mem_encode buf_info;
buf_info.buffer = NULL;
buf_info.buffer = nullptr;
buf_info.size = 0;
/* Establish the setjmp return context for my_error_exit to use. */
......@@ -78,15 +79,15 @@ torch::Tensor encode_png(const torch::Tensor& data, int64_t compression_level) {
/* If we get here, the PNG code has signaled an error.
* We need to clean up the PNG object and the buffer.
*/
if (info_ptr != NULL) {
if (info_ptr != nullptr) {
png_destroy_info_struct(png_write, &info_ptr);
}
if (png_write != NULL) {
png_destroy_write_struct(&png_write, NULL);
if (png_write != nullptr) {
png_destroy_write_struct(&png_write, nullptr);
}
if (buf_info.buffer != NULL) {
if (buf_info.buffer != nullptr) {
free(buf_info.buffer);
}
......@@ -120,12 +121,12 @@ torch::Tensor encode_png(const torch::Tensor& data, int64_t compression_level) {
// Initialize PNG structures
png_write = png_create_write_struct(
PNG_LIBPNG_VER_STRING, &err_ptr, torch_png_error, NULL);
PNG_LIBPNG_VER_STRING, &err_ptr, torch_png_error, nullptr);
info_ptr = png_create_info_struct(png_write);
// Define custom buffer output
png_set_write_fn(png_write, &buf_info, torch_png_write_data, NULL);
png_set_write_fn(png_write, &buf_info, torch_png_write_data, nullptr);
// Set output image information
auto color_type = channels == 1 ? PNG_COLOR_TYPE_GRAY : PNG_COLOR_TYPE_RGB;
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment