Skip to content

Commit 87d5b8c

Browse files
authored
Merge branch 'master' into fix_rotate
2 parents 649fe85 + 7621a8e commit 87d5b8c

26 files changed

+390
-211
lines changed

torchvision/csrc/io/image/jpegcommon.cpp renamed to torchvision/csrc/io/image/cpu/common_jpeg.cpp

+10-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
1-
#if JPEG_FOUND
2-
#include "jpegcommon.h"
1+
#include "common_jpeg.h"
2+
3+
namespace vision {
4+
namespace image {
5+
namespace detail {
36

7+
#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
610
* pointer */
@@ -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
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
#pragma once
22

3-
// clang-format off
4-
#include <cstdio>
5-
#include <cstddef>
6-
// clang-format on
7-
83
#if JPEG_FOUND
4+
#include <stdio.h>
5+
96
#include <jpeglib.h>
107
#include <setjmp.h>
118

9+
namespace vision {
10+
namespace image {
11+
namespace detail {
12+
1213
static const JOCTET EOI_BUFFER[1] = {JPEG_EOI};
1314
struct torch_jpeg_error_mgr {
1415
struct jpeg_error_mgr pub; /* "public" fields */
@@ -19,4 +20,8 @@ struct torch_jpeg_error_mgr {
1920
using torch_jpeg_error_ptr = struct torch_jpeg_error_mgr*;
2021
void torch_jpeg_error_exit(j_common_ptr cinfo);
2122

23+
} // namespace detail
24+
} // namespace image
25+
} // namespace vision
26+
2227
#endif
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 renamed to torchvision/csrc/io/image/cpu/decode_image.cpp

+11-5
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
1-
#include "read_image_impl.h"
1+
#include "decode_image.h"
22

3-
#include "readjpeg_impl.h"
4-
#include "readpng_impl.h"
3+
#include "decode_jpeg.h"
4+
#include "decode_png.h"
5+
6+
namespace vision {
7+
namespace image {
58

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

1922
if (memcmp(jpeg_signature, datap, 3) == 0) {
20-
return decodeJPEG(data, mode);
23+
return decode_jpeg(data, mode);
2124
} else if (memcmp(png_signature, datap, 4) == 0) {
22-
return decodePNG(data, mode);
25+
return decode_png(data, mode);
2326
} else {
2427
TORCH_CHECK(
2528
false,
2629
"Unsupported image file. Only jpeg and png ",
2730
"are currently supported.");
2831
}
2932
}
33+
34+
} // namespace image
35+
} // namespace vision

torchvision/csrc/io/image/cpu/read_image_impl.h renamed to torchvision/csrc/io/image/cpu/decode_image.h

+6
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/readjpeg_impl.cpp renamed to torchvision/csrc/io/image/cpu/decode_jpeg.cpp

+18-6
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,19 @@
1-
#include "readjpeg_impl.h"
1+
#include "decode_jpeg.h"
2+
#include "common_jpeg.h"
3+
4+
namespace vision {
5+
namespace image {
26

37
#if !JPEG_FOUND
4-
torch::Tensor decodeJPEG(const torch::Tensor& data, ImageReadMode mode) {
8+
torch::Tensor decode_jpeg(const torch::Tensor& data, ImageReadMode mode) {
59
TORCH_CHECK(
6-
false, "decodeJPEG: torchvision not compiled with libjpeg support");
10+
false, "decode_jpeg: torchvision not compiled with libjpeg support");
711
}
812
#else
9-
#include "../jpegcommon.h"
13+
14+
using namespace detail;
15+
16+
namespace {
1017

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

67-
torch::Tensor decodeJPEG(const torch::Tensor& data, ImageReadMode mode) {
74+
} // namespace
75+
76+
torch::Tensor decode_jpeg(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");
7079
// Check that the input tensor is 1-dimensional
@@ -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/readpng_impl.h renamed to torchvision/csrc/io/image/cpu/decode_jpeg.h

+7-1
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-
C10_EXPORT torch::Tensor decodePNG(
6+
namespace vision {
7+
namespace image {
8+
9+
C10_EXPORT torch::Tensor decode_jpeg(
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 renamed to torchvision/csrc/io/image/cpu/decode_png.cpp

+13-7
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,17 @@
1-
#include "readpng_impl.h"
1+
#include "decode_png.h"
2+
#include "common_png.h"
3+
4+
namespace vision {
5+
namespace image {
26

37
#if !PNG_FOUND
4-
torch::Tensor decodePNG(const torch::Tensor& data, ImageReadMode mode) {
5-
TORCH_CHECK(false, "decodePNG: torchvision not compiled with libPNG support");
8+
torch::Tensor decode_png(const torch::Tensor& data, ImageReadMode mode) {
9+
TORCH_CHECK(
10+
false, "decode_png: torchvision not compiled with libPNG support");
611
}
712
#else
8-
#include <png.h>
9-
#include <setjmp.h>
1013

11-
torch::Tensor decodePNG(const torch::Tensor& data, ImageReadMode mode) {
14+
torch::Tensor decode_png(const torch::Tensor& data, ImageReadMode mode) {
1215
// Check that the input tensor dtype is uint8
1316
TORCH_CHECK(data.dtype() == torch::kU8, "Expected a torch.uint8 tensor");
1417
// Check that the input tensor is 1-dimensional
@@ -160,4 +163,7 @@ torch::Tensor decodePNG(const torch::Tensor& data, ImageReadMode mode) {
160163
png_destroy_read_struct(&png_ptr, &info_ptr, nullptr);
161164
return tensor.permute({2, 0, 1});
162165
}
163-
#endif // PNG_FOUND
166+
#endif
167+
168+
} // namespace image
169+
} // namespace vision

torchvision/csrc/io/image/cpu/readjpeg_impl.h renamed to torchvision/csrc/io/image/cpu/decode_png.h

+7-1
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-
C10_EXPORT torch::Tensor decodeJPEG(
6+
namespace vision {
7+
namespace image {
8+
9+
C10_EXPORT torch::Tensor decode_png(
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 renamed to torchvision/csrc/io/image/cpu/encode_jpeg.cpp

+14-5
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,22 @@
1-
#include "writejpeg_impl.h"
1+
#include "encode_jpeg.h"
2+
3+
#include "common_jpeg.h"
4+
5+
namespace vision {
6+
namespace image {
27

38
#if !JPEG_FOUND
49

5-
torch::Tensor encodeJPEG(const torch::Tensor& data, int64_t quality) {
10+
torch::Tensor encode_jpeg(const torch::Tensor& data, int64_t quality) {
611
TORCH_CHECK(
7-
false, "encodeJPEG: torchvision not compiled with libjpeg support");
12+
false, "encode_jpeg: torchvision not compiled with libjpeg support");
813
}
914

1015
#else
11-
#include "../jpegcommon.h"
1216

13-
torch::Tensor encodeJPEG(const torch::Tensor& data, int64_t quality) {
17+
using namespace detail;
18+
19+
torch::Tensor encode_jpeg(const torch::Tensor& data, int64_t quality) {
1420
// Define compression structures and error handling
1521
struct jpeg_compress_struct cinfo;
1622
struct torch_jpeg_error_mgr jerr;
@@ -98,3 +104,6 @@ torch::Tensor encodeJPEG(const torch::Tensor& data, int64_t quality) {
98104
return outTensor;
99105
}
100106
#endif
107+
108+
} // namespace image
109+
} // namespace vision
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#pragma once
2+
3+
#include <torch/types.h>
4+
5+
namespace vision {
6+
namespace image {
7+
8+
C10_EXPORT torch::Tensor encode_jpeg(
9+
const torch::Tensor& data,
10+
int64_t quality);
11+
12+
} // namespace image
13+
} // namespace vision

torchvision/csrc/io/image/cpu/writepng_impl.cpp renamed to torchvision/csrc/io/image/cpu/encode_png.cpp

+17-6
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,20 @@
1-
#include "writejpeg_impl.h"
1+
#include "encode_jpeg.h"
2+
3+
#include "common_png.h"
4+
5+
namespace vision {
6+
namespace image {
27

38
#if !PNG_FOUND
49

5-
torch::Tensor encodePNG(const torch::Tensor& data, int64_t compression_level) {
6-
TORCH_CHECK(false, "encodePNG: torchvision not compiled with libpng support");
10+
torch::Tensor encode_png(const torch::Tensor& data, int64_t compression_level) {
11+
TORCH_CHECK(
12+
false, "encode_png: torchvision not compiled with libpng support");
713
}
814

915
#else
10-
#include <png.h>
11-
#include <setjmp.h>
16+
17+
namespace {
1218

1319
struct torch_mem_encode {
1420
char* buffer;
@@ -59,7 +65,9 @@ void torch_png_write_data(
5965
p->size += length;
6066
}
6167

62-
torch::Tensor encodePNG(const torch::Tensor& data, int64_t compression_level) {
68+
} // namespace
69+
70+
torch::Tensor encode_png(const torch::Tensor& data, int64_t compression_level) {
6371
// Define compression structures and error handling
6472
png_structp png_write;
6573
png_infop info_ptr;
@@ -171,3 +179,6 @@ torch::Tensor encodePNG(const torch::Tensor& data, int64_t compression_level) {
171179
}
172180

173181
#endif
182+
183+
} // namespace image
184+
} // namespace vision
+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#pragma once
2+
3+
#include <torch/types.h>
4+
5+
namespace vision {
6+
namespace image {
7+
8+
C10_EXPORT torch::Tensor encode_png(
9+
const torch::Tensor& data,
10+
int64_t compression_level);
11+
12+
} // namespace image
13+
} // namespace vision

torchvision/csrc/io/image/cpu/read_write_file_impl.cpp renamed to torchvision/csrc/io/image/cpu/read_write_file.cpp

+13-1
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,17 @@
1-
#include "read_write_file_impl.h"
1+
#include "read_write_file.h"
2+
3+
#include <sys/stat.h>
24

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
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/writejpeg_impl.h

-5
This file was deleted.

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

-7
This file was deleted.

0 commit comments

Comments
 (0)