Skip to content

Add namespace on image C++ codebase #3312

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

Merged
merged 6 commits into from
Jan 28, 2021
Merged
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
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
#if JPEG_FOUND
#include "jpegcommon.h"
#include "common_jpeg.h"

namespace vision {
namespace image {
namespace detail {

#if JPEG_FOUND
void torch_jpeg_error_exit(j_common_ptr cinfo) {
/* cinfo->err really points to a torch_jpeg_error_mgr struct, so coerce
* pointer */
Expand All @@ -16,3 +20,7 @@ void torch_jpeg_error_exit(j_common_ptr cinfo) {
longjmp(myerr->setjmp_buffer, 1);
}
#endif

} // namespace detail
} // namespace image
} // namespace vision
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
#pragma once

// clang-format off
#include <cstdio>
#include <cstddef>
// clang-format on

#if JPEG_FOUND
#include <stdio.h>

#include <jpeglib.h>
#include <setjmp.h>

namespace vision {
namespace image {
namespace detail {

static const JOCTET EOI_BUFFER[1] = {JPEG_EOI};
struct torch_jpeg_error_mgr {
struct jpeg_error_mgr pub; /* "public" fields */
Expand All @@ -19,4 +20,8 @@ struct torch_jpeg_error_mgr {
using torch_jpeg_error_ptr = struct torch_jpeg_error_mgr*;
void torch_jpeg_error_exit(j_common_ptr cinfo);

} // namespace detail
} // namespace image
} // namespace vision

#endif
6 changes: 6 additions & 0 deletions torchvision/csrc/io/image/cpu/common_png.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#pragma once

#if PNG_FOUND
#include <png.h>
#include <setjmp.h>
#endif
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
#include "read_image_impl.h"
#include "decode_image.h"

#include "readjpeg_impl.h"
#include "readpng_impl.h"
#include "decode_jpeg.h"
#include "decode_png.h"

namespace vision {
namespace image {

torch::Tensor decode_image(const torch::Tensor& data, ImageReadMode mode) {
// Check that the input tensor dtype is uint8
Expand All @@ -17,13 +20,16 @@ torch::Tensor decode_image(const torch::Tensor& data, ImageReadMode mode) {
const uint8_t png_signature[4] = {137, 80, 78, 71}; // == "\211PNG"

if (memcmp(jpeg_signature, datap, 3) == 0) {
return decodeJPEG(data, mode);
return decode_jpeg(data, mode);
} else if (memcmp(png_signature, datap, 4) == 0) {
return decodePNG(data, mode);
return decode_png(data, mode);
} else {
TORCH_CHECK(
false,
"Unsupported image file. Only jpeg and png ",
"are currently supported.");
}
}

} // namespace image
} // namespace vision
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@
#include <torch/types.h>
#include "../image_read_mode.h"

namespace vision {
namespace image {

C10_EXPORT torch::Tensor decode_image(
const torch::Tensor& data,
ImageReadMode mode = IMAGE_READ_MODE_UNCHANGED);

} // namespace image
} // namespace vision
Original file line number Diff line number Diff line change
@@ -1,12 +1,19 @@
#include "readjpeg_impl.h"
#include "decode_jpeg.h"
#include "common_jpeg.h"

namespace vision {
namespace image {

#if !JPEG_FOUND
torch::Tensor decodeJPEG(const torch::Tensor& data, ImageReadMode mode) {
torch::Tensor decode_jpeg(const torch::Tensor& data, ImageReadMode mode) {
TORCH_CHECK(
false, "decodeJPEG: torchvision not compiled with libjpeg support");
false, "decode_jpeg: torchvision not compiled with libjpeg support");
}
#else
#include "../jpegcommon.h"

using namespace detail;

namespace {

struct torch_jpeg_mgr {
struct jpeg_source_mgr pub;
Expand Down Expand Up @@ -64,7 +71,9 @@ static void torch_jpeg_set_source_mgr(
src->pub.next_input_byte = src->data;
}

torch::Tensor decodeJPEG(const torch::Tensor& data, ImageReadMode mode) {
} // namespace

torch::Tensor decode_jpeg(const torch::Tensor& data, ImageReadMode mode) {
// Check that the input tensor dtype is uint8
TORCH_CHECK(data.dtype() == torch::kU8, "Expected a torch.uint8 tensor");
// Check that the input tensor is 1-dimensional
Expand Down Expand Up @@ -146,4 +155,7 @@ torch::Tensor decodeJPEG(const torch::Tensor& data, ImageReadMode mode) {
return tensor.permute({2, 0, 1});
}

#endif // JPEG_FOUND
#endif

} // namespace image
} // namespace vision
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@
#include <torch/types.h>
#include "../image_read_mode.h"

C10_EXPORT torch::Tensor decodePNG(
namespace vision {
namespace image {

C10_EXPORT torch::Tensor decode_jpeg(
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here Github fails to track the right file renames and thinks I moved png to jpeg and vice versa.

const torch::Tensor& data,
ImageReadMode mode = IMAGE_READ_MODE_UNCHANGED);

} // namespace image
} // namespace vision
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
#include "readpng_impl.h"
#include "decode_png.h"
#include "common_png.h"

namespace vision {
namespace image {

#if !PNG_FOUND
torch::Tensor decodePNG(const torch::Tensor& data, ImageReadMode mode) {
TORCH_CHECK(false, "decodePNG: torchvision not compiled with libPNG support");
torch::Tensor decode_png(const torch::Tensor& data, ImageReadMode mode) {
TORCH_CHECK(
false, "decode_png: torchvision not compiled with libPNG support");
}
#else
#include <png.h>
#include <setjmp.h>

torch::Tensor decodePNG(const torch::Tensor& data, ImageReadMode mode) {
torch::Tensor decode_png(const torch::Tensor& data, ImageReadMode mode) {
// Check that the input tensor dtype is uint8
TORCH_CHECK(data.dtype() == torch::kU8, "Expected a torch.uint8 tensor");
// Check that the input tensor is 1-dimensional
Expand Down Expand Up @@ -160,4 +163,7 @@ torch::Tensor decodePNG(const torch::Tensor& data, ImageReadMode mode) {
png_destroy_read_struct(&png_ptr, &info_ptr, nullptr);
return tensor.permute({2, 0, 1});
}
#endif // PNG_FOUND
#endif

} // namespace image
} // namespace vision
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@
#include <torch/types.h>
#include "../image_read_mode.h"

C10_EXPORT torch::Tensor decodeJPEG(
namespace vision {
namespace image {

C10_EXPORT torch::Tensor decode_png(
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Again Github fails to figure out which file was moved to what here.

const torch::Tensor& data,
ImageReadMode mode = IMAGE_READ_MODE_UNCHANGED);

} // namespace image
} // namespace vision
Original file line number Diff line number Diff line change
@@ -1,16 +1,22 @@
#include "writejpeg_impl.h"
#include "encode_jpeg.h"

#include "common_jpeg.h"

namespace vision {
namespace image {

#if !JPEG_FOUND

torch::Tensor encodeJPEG(const torch::Tensor& data, int64_t quality) {
torch::Tensor encode_jpeg(const torch::Tensor& data, int64_t quality) {
TORCH_CHECK(
false, "encodeJPEG: torchvision not compiled with libjpeg support");
false, "encode_jpeg: torchvision not compiled with libjpeg support");
}

#else
#include "../jpegcommon.h"

torch::Tensor encodeJPEG(const torch::Tensor& data, int64_t quality) {
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;
Expand Down Expand Up @@ -98,3 +104,6 @@ torch::Tensor encodeJPEG(const torch::Tensor& data, int64_t quality) {
return outTensor;
}
#endif

} // namespace image
} // namespace vision
13 changes: 13 additions & 0 deletions torchvision/csrc/io/image/cpu/encode_jpeg.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#pragma once

#include <torch/types.h>
Copy link
Member

@fmassa fmassa Jan 28, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I need to start using a smart editor. I would have just #include <torch/torch.h> and brought everything in :-)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unfortunately that's not a smart editor feature but I wish they had that functionality too.


namespace vision {
namespace image {

C10_EXPORT torch::Tensor encode_jpeg(
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Git fails to track the old file.

const torch::Tensor& data,
int64_t quality);

} // namespace image
} // namespace vision
Original file line number Diff line number Diff line change
@@ -1,14 +1,20 @@
#include "writejpeg_impl.h"
#include "encode_jpeg.h"

#include "common_png.h"

namespace vision {
namespace image {

#if !PNG_FOUND

torch::Tensor encodePNG(const torch::Tensor& data, int64_t compression_level) {
TORCH_CHECK(false, "encodePNG: torchvision not compiled with libpng support");
torch::Tensor encode_png(const torch::Tensor& data, int64_t compression_level) {
TORCH_CHECK(
false, "encode_png: torchvision not compiled with libpng support");
}

#else
#include <png.h>
#include <setjmp.h>

namespace {

struct torch_mem_encode {
char* buffer;
Expand Down Expand Up @@ -59,7 +65,9 @@ void torch_png_write_data(
p->size += length;
}

torch::Tensor encodePNG(const torch::Tensor& data, int64_t compression_level) {
} // namespace

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;
Expand Down Expand Up @@ -171,3 +179,6 @@ torch::Tensor encodePNG(const torch::Tensor& data, int64_t compression_level) {
}

#endif

} // namespace image
} // namespace vision
13 changes: 13 additions & 0 deletions torchvision/csrc/io/image/cpu/encode_png.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#pragma once

#include <torch/types.h>

namespace vision {
namespace image {

C10_EXPORT torch::Tensor encode_png(
const torch::Tensor& data,
int64_t compression_level);

} // namespace image
} // namespace vision
Original file line number Diff line number Diff line change
@@ -1,9 +1,17 @@
#include "read_write_file_impl.h"
#include "read_write_file.h"

#include <sys/stat.h>

#ifdef _WIN32
#define WIN32_LEAN_AND_MEAN
#include <Windows.h>
#endif

namespace vision {
namespace image {

#ifdef _WIN32
namespace {
std::wstring utf8_decode(const std::string& str) {
if (str.empty()) {
return std::wstring();
Expand All @@ -21,6 +29,7 @@ std::wstring utf8_decode(const std::string& str) {
size_needed);
return wstrTo;
}
} // namespace
#endif

torch::Tensor read_file(const std::string& filename) {
Expand Down Expand Up @@ -90,3 +99,6 @@ void write_file(const std::string& filename, torch::Tensor& data) {
fwrite(fileBytes, sizeof(uint8_t), data.numel(), outfile);
fclose(outfile);
}

} // namespace image
} // namespace vision
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
#pragma once

#include <sys/stat.h>
#include <torch/types.h>

namespace vision {
namespace image {

C10_EXPORT torch::Tensor read_file(const std::string& filename);

C10_EXPORT void write_file(const std::string& filename, torch::Tensor& data);

} // namespace image
} // namespace vision
5 changes: 0 additions & 5 deletions torchvision/csrc/io/image/cpu/writejpeg_impl.h

This file was deleted.

7 changes: 0 additions & 7 deletions torchvision/csrc/io/image/cpu/writepng_impl.h

This file was deleted.

Loading