Skip to content

Commit 7fdc7ba

Browse files
committed
Adding namespaces on image and moving private methods to anonymous.
1 parent 60b2894 commit 7fdc7ba

17 files changed

+135
-9
lines changed

torchvision/csrc/io/image/cpu/jpegcommon.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
#include "jpegcommon.h"
22

3+
namespace vision {
4+
namespace image {
5+
namespace detail {
6+
37
#if JPEG_FOUND
48
void torch_jpeg_error_exit(j_common_ptr cinfo) {
59
/* cinfo->err really points to a torch_jpeg_error_mgr struct, so coerce
@@ -16,3 +20,7 @@ void torch_jpeg_error_exit(j_common_ptr cinfo) {
1620
longjmp(myerr->setjmp_buffer, 1);
1721
}
1822
#endif
23+
24+
} // namespace detail
25+
} // namespace image
26+
} // namespace vision

torchvision/csrc/io/image/cpu/jpegcommon.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,13 @@
33
#if JPEG_FOUND
44
#include <jpeglib.h>
55
#include <setjmp.h>
6+
#endif
7+
8+
namespace vision {
9+
namespace image {
10+
namespace detail {
11+
12+
#if JPEG_FOUND
613

714
static const JOCTET EOI_BUFFER[1] = {JPEG_EOI};
815
struct torch_jpeg_error_mgr {
@@ -15,3 +22,7 @@ using torch_jpeg_error_ptr = struct torch_jpeg_error_mgr*;
1522
void torch_jpeg_error_exit(j_common_ptr cinfo);
1623

1724
#endif
25+
26+
} // namespace detail
27+
} // namespace image
28+
} // namespace vision
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#pragma once
2+
3+
#if PNG_FOUND
4+
#include <png.h>
5+
#include <setjmp.h>
6+
#endif

torchvision/csrc/io/image/cpu/read_image_impl.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
#include "readjpeg_impl.h"
44
#include "readpng_impl.h"
55

6+
namespace vision {
7+
namespace image {
8+
69
torch::Tensor decode_image(const torch::Tensor& data, ImageReadMode mode) {
710
// Check that the input tensor dtype is uint8
811
TORCH_CHECK(data.dtype() == torch::kU8, "Expected a torch.uint8 tensor");
@@ -27,3 +30,6 @@ torch::Tensor decode_image(const torch::Tensor& data, ImageReadMode mode) {
2730
"are currently supported.");
2831
}
2932
}
33+
34+
} // namespace image
35+
} // namespace vision

torchvision/csrc/io/image/cpu/read_image_impl.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,12 @@
33
#include <torch/types.h>
44
#include "../image_read_mode.h"
55

6+
namespace vision {
7+
namespace image {
8+
69
C10_EXPORT torch::Tensor decode_image(
710
const torch::Tensor& data,
811
ImageReadMode mode = IMAGE_READ_MODE_UNCHANGED);
12+
13+
} // namespace image
14+
} // namespace vision

torchvision/csrc/io/image/cpu/read_write_file_impl.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,17 @@
11
#include "read_write_file_impl.h"
22

3+
#include <sys/stat.h>
4+
35
#ifdef _WIN32
46
#define WIN32_LEAN_AND_MEAN
57
#include <Windows.h>
8+
#endif
9+
10+
namespace vision {
11+
namespace image {
612

13+
#ifdef _WIN32
14+
namespace {
715
std::wstring utf8_decode(const std::string& str) {
816
if (str.empty()) {
917
return std::wstring();
@@ -21,6 +29,7 @@ std::wstring utf8_decode(const std::string& str) {
2129
size_needed);
2230
return wstrTo;
2331
}
32+
} // namespace
2433
#endif
2534

2635
torch::Tensor read_file(const std::string& filename) {
@@ -90,3 +99,6 @@ void write_file(const std::string& filename, torch::Tensor& data) {
9099
fwrite(fileBytes, sizeof(uint8_t), data.numel(), outfile);
91100
fclose(outfile);
92101
}
102+
103+
} // namespace image
104+
} // namespace vision
Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
11
#pragma once
22

3-
#include <sys/stat.h>
43
#include <torch/types.h>
54

5+
namespace vision {
6+
namespace image {
7+
68
C10_EXPORT torch::Tensor read_file(const std::string& filename);
79

810
C10_EXPORT void write_file(const std::string& filename, torch::Tensor& data);
11+
12+
} // namespace image
13+
} // namespace vision

torchvision/csrc/io/image/cpu/readjpeg_impl.cpp

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,19 @@
11
#include "readjpeg_impl.h"
2+
#include "jpegcommon.h"
3+
4+
namespace vision {
5+
namespace image {
6+
7+
using namespace detail;
28

39
#if !JPEG_FOUND
410
torch::Tensor decodeJPEG(const torch::Tensor& data, ImageReadMode mode) {
511
TORCH_CHECK(
612
false, "decodeJPEG: torchvision not compiled with libjpeg support");
713
}
814
#else
9-
#include "jpegcommon.h"
15+
16+
namespace {
1017

1118
struct torch_jpeg_mgr {
1219
struct jpeg_source_mgr pub;
@@ -64,6 +71,8 @@ static void torch_jpeg_set_source_mgr(
6471
src->pub.next_input_byte = src->data;
6572
}
6673

74+
} // namespace
75+
6776
torch::Tensor decodeJPEG(const torch::Tensor& data, ImageReadMode mode) {
6877
// Check that the input tensor dtype is uint8
6978
TORCH_CHECK(data.dtype() == torch::kU8, "Expected a torch.uint8 tensor");
@@ -146,4 +155,7 @@ torch::Tensor decodeJPEG(const torch::Tensor& data, ImageReadMode mode) {
146155
return tensor.permute({2, 0, 1});
147156
}
148157

149-
#endif // JPEG_FOUND
158+
#endif
159+
160+
} // namespace image
161+
} // namespace vision

torchvision/csrc/io/image/cpu/readjpeg_impl.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,12 @@
33
#include <torch/types.h>
44
#include "../image_read_mode.h"
55

6+
namespace vision {
7+
namespace image {
8+
69
C10_EXPORT torch::Tensor decodeJPEG(
710
const torch::Tensor& data,
811
ImageReadMode mode = IMAGE_READ_MODE_UNCHANGED);
12+
13+
} // namespace image
14+
} // namespace vision

torchvision/csrc/io/image/cpu/readpng_impl.cpp

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
#include "readpng_impl.h"
2+
#include "pngcommon.h"
3+
4+
namespace vision {
5+
namespace image {
26

37
#if !PNG_FOUND
48
torch::Tensor decodePNG(const torch::Tensor& data, ImageReadMode mode) {
59
TORCH_CHECK(false, "decodePNG: torchvision not compiled with libPNG support");
610
}
711
#else
8-
#include <png.h>
9-
#include <setjmp.h>
1012

1113
torch::Tensor decodePNG(const torch::Tensor& data, ImageReadMode mode) {
1214
// Check that the input tensor dtype is uint8
@@ -160,4 +162,7 @@ torch::Tensor decodePNG(const torch::Tensor& data, ImageReadMode mode) {
160162
png_destroy_read_struct(&png_ptr, &info_ptr, nullptr);
161163
return tensor.permute({2, 0, 1});
162164
}
163-
#endif // PNG_FOUND
165+
#endif
166+
167+
} // namespace image
168+
} // namespace vision

torchvision/csrc/io/image/cpu/readpng_impl.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,12 @@
33
#include <torch/types.h>
44
#include "../image_read_mode.h"
55

6+
namespace vision {
7+
namespace image {
8+
69
C10_EXPORT torch::Tensor decodePNG(
710
const torch::Tensor& data,
811
ImageReadMode mode = IMAGE_READ_MODE_UNCHANGED);
12+
13+
} // namespace image
14+
} // namespace vision

torchvision/csrc/io/image/cpu/writejpeg_impl.cpp

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,10 @@
11
#include "writejpeg_impl.h"
2+
#include "jpegcommon.h"
3+
4+
namespace vision {
5+
namespace image {
6+
7+
using namespace detail;
28

39
#if !JPEG_FOUND
410

@@ -8,7 +14,6 @@ torch::Tensor encodeJPEG(const torch::Tensor& data, int64_t quality) {
814
}
915

1016
#else
11-
#include "jpegcommon.h"
1217

1318
torch::Tensor encodeJPEG(const torch::Tensor& data, int64_t quality) {
1419
// Define compression structures and error handling
@@ -98,3 +103,6 @@ torch::Tensor encodeJPEG(const torch::Tensor& data, int64_t quality) {
98103
return outTensor;
99104
}
100105
#endif
106+
107+
} // namespace image
108+
} // namespace vision

torchvision/csrc/io/image/cpu/writejpeg_impl.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,10 @@
22

33
#include <torch/types.h>
44

5+
namespace vision {
6+
namespace image {
7+
58
C10_EXPORT torch::Tensor encodeJPEG(const torch::Tensor& data, int64_t quality);
9+
10+
} // namespace image
11+
} // namespace vision

torchvision/csrc/io/image/cpu/writepng_impl.cpp

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
11
#include "writejpeg_impl.h"
2+
#include "pngcommon.h"
3+
4+
namespace vision {
5+
namespace image {
26

37
#if !PNG_FOUND
48

@@ -7,8 +11,8 @@ torch::Tensor encodePNG(const torch::Tensor& data, int64_t compression_level) {
711
}
812

913
#else
10-
#include <png.h>
11-
#include <setjmp.h>
14+
15+
namespace {
1216

1317
struct torch_mem_encode {
1418
char* buffer;
@@ -59,6 +63,8 @@ void torch_png_write_data(
5963
p->size += length;
6064
}
6165

66+
} // namespace
67+
6268
torch::Tensor encodePNG(const torch::Tensor& data, int64_t compression_level) {
6369
// Define compression structures and error handling
6470
png_structp png_write;
@@ -171,3 +177,6 @@ torch::Tensor encodePNG(const torch::Tensor& data, int64_t compression_level) {
171177
}
172178

173179
#endif
180+
181+
} // namespace image
182+
} // namespace vision

torchvision/csrc/io/image/cpu/writepng_impl.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,12 @@
22

33
#include <torch/types.h>
44

5+
namespace vision {
6+
namespace image {
7+
58
C10_EXPORT torch::Tensor encodePNG(
69
const torch::Tensor& data,
710
int64_t compression_level);
11+
12+
} // namespace image
13+
} // namespace vision

torchvision/csrc/io/image/image.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ PyMODINIT_FUNC PyInit_image(void) {
1111
}
1212
#endif
1313

14+
namespace vision {
15+
namespace image {
16+
1417
static auto registry = torch::RegisterOperators()
1518
.op("image::decode_png", &decodePNG)
1619
.op("image::encode_png", &encodePNG)
@@ -19,3 +22,6 @@ static auto registry = torch::RegisterOperators()
1922
.op("image::read_file", &read_file)
2023
.op("image::write_file", &write_file)
2124
.op("image::decode_image", &decode_image);
25+
26+
} // namespace image
27+
} // namespace vision
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,17 @@
11
#pragma once
22

3+
#include <stdint.h>
4+
5+
namespace vision {
6+
namespace image {
7+
38
/* Should be kept in-sync with Python ImageReadMode enum */
49
using ImageReadMode = int64_t;
510
const ImageReadMode IMAGE_READ_MODE_UNCHANGED = 0;
611
const ImageReadMode IMAGE_READ_MODE_GRAY = 1;
712
const ImageReadMode IMAGE_READ_MODE_GRAY_ALPHA = 2;
813
const ImageReadMode IMAGE_READ_MODE_RGB = 3;
914
const ImageReadMode IMAGE_READ_MODE_RGB_ALPHA = 4;
15+
16+
} // namespace image
17+
} // namespace vision

0 commit comments

Comments
 (0)