Skip to content

Image enconding and deconding fixes #4051

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion torchvision/csrc/io/image/cpu/decode_jpeg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ torch::Tensor decode_jpeg(const torch::Tensor& data, ImageReadMode mode) {
"Expected a non empty 1-dimensional tensor");

struct jpeg_decompress_struct cinfo;
struct torch_jpeg_error_mgr jerr;
struct torch_jpeg_error_mgr jerr {};

auto datap = data.data_ptr<uint8_t>();
// Setup decompression structure
Expand Down
6 changes: 3 additions & 3 deletions torchvision/csrc/io/image/cpu/decode_png.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ torch::Tensor decode_png(const torch::Tensor& data, ImageReadMode mode) {
auto info_ptr = png_create_info_struct(png_ptr);
if (!info_ptr) {
png_destroy_read_struct(&png_ptr, nullptr, nullptr);
// Seems redundant with the if statement. done here to avoid leaking memory.
TORCH_CHECK(info_ptr, "libpng info structure allocation failed!")
// Seems redundant with the if statement. Done here to avoid leaking memory.
TORCH_CHECK(false, "libpng info structure allocation failed!")
}

auto datap = data.accessor<unsigned char, 1>().data();
Expand Down Expand Up @@ -68,7 +68,7 @@ torch::Tensor decode_png(const torch::Tensor& data, ImageReadMode mode) {

if (retval != 1) {
png_destroy_read_struct(&png_ptr, &info_ptr, nullptr);
TORCH_CHECK(retval == 1, "Could read image metadata from content.")
TORCH_CHECK(false, "Could read image metadata from content.")
}

int channels = png_get_channels(png_ptr, info_ptr);
Expand Down
4 changes: 2 additions & 2 deletions torchvision/csrc/io/image/cpu/encode_jpeg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ using namespace detail;

torch::Tensor encode_jpeg(const torch::Tensor& data, int64_t quality) {
// 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;
Expand Down
23 changes: 11 additions & 12 deletions torchvision/csrc/io/image/cpu/encode_png.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,13 @@ void torch_png_write_data(
size_t nsize = p->size + length;

/* allocate or grow buffer */
char* tmp = NULL;
if (p->buffer)
p->buffer = (char*)realloc(p->buffer, nsize);
tmp = (char*)realloc(p->buffer, nsize);
else
p->buffer = (char*)malloc(nsize);
tmp = (char*)malloc(nsize);

p->buffer = tmp;
if (!p->buffer)
png_error(png_ptr, "Write Error");

Expand All @@ -64,25 +66,22 @@ void torch_png_write_data(

torch::Tensor encode_png(const torch::Tensor& data, int64_t compression_level) {
// Define compression structures and error handling
png_structp png_write;
png_infop info_ptr;
struct torch_png_error_mgr err_ptr;
png_structp png_write{};
png_infop info_ptr{};
struct torch_png_error_mgr err_ptr {};

// Define output buffer
struct torch_mem_encode buf_info;
buf_info.buffer = NULL;
buf_info.size = 0;
struct torch_mem_encode buf_info {};

/* Establish the setjmp return context for my_error_exit to use. */
if (setjmp(err_ptr.setjmp_buffer)) {
/* 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) {
png_destroy_info_struct(png_write, &info_ptr);
}

if (png_write != NULL) {
if (info_ptr != NULL) {
png_destroy_info_struct(png_write, &info_ptr);
}
png_destroy_write_struct(&png_write, NULL);
}

Expand Down