11#include " decode_image.h"
22
3- #include " decode_gif.h "
4- #include " decode_jpeg.h "
5- # include " decode_png.h "
6- #include " decode_webp.h "
3+ #include < torch/csrc/stable/library.h >
4+ #include < torch/headeronly/util/Exception.h >
5+
6+ #include < cstring >
77
88namespace vision {
99namespace image {
1010
11- torch::Tensor decode_image (
12- const torch::Tensor& data,
11+ namespace {
12+
13+ // Shims over the legacy image::decode_jpeg, decode_png, decode_gif and
14+ // decode_webp ops not yet on the stable ABI.
15+ // TODO(stable-abi): remove each shim once its decoder is ported.
16+ torch::stable::Tensor decode_jpeg (
17+ const torch::stable::Tensor& data,
18+ ImageReadMode mode,
19+ bool apply_exif_orientation) {
20+ const auto num_args = 3 ;
21+ std::array<StableIValue, num_args> stack{
22+ torch::stable::detail::from (data),
23+ torch::stable::detail::from (mode),
24+ torch::stable::detail::from (apply_exif_orientation)};
25+ TORCH_ERROR_CODE_CHECK (torch_call_dispatcher (
26+ " image::decode_jpeg" , " " , stack.data (), TORCH_ABI_VERSION ));
27+ return torch::stable::detail::to<torch::stable::Tensor>(stack[0 ]);
28+ }
29+
30+ torch::stable::Tensor decode_png (
31+ const torch::stable::Tensor& data,
32+ ImageReadMode mode,
33+ bool apply_exif_orientation) {
34+ const auto num_args = 3 ;
35+ std::array<StableIValue, num_args> stack{
36+ torch::stable::detail::from (data),
37+ torch::stable::detail::from (mode),
38+ torch::stable::detail::from (apply_exif_orientation)};
39+ TORCH_ERROR_CODE_CHECK (torch_call_dispatcher (
40+ " image::decode_png" , " " , stack.data (), TORCH_ABI_VERSION ));
41+ return torch::stable::detail::to<torch::stable::Tensor>(stack[0 ]);
42+ }
43+
44+ torch::stable::Tensor decode_gif (const torch::stable::Tensor& data) {
45+ const auto num_args = 1 ;
46+ std::array<StableIValue, num_args> stack{torch::stable::detail::from (data)};
47+ TORCH_ERROR_CODE_CHECK (torch_call_dispatcher (
48+ " image::decode_gif" , " " , stack.data (), TORCH_ABI_VERSION ));
49+ return torch::stable::detail::to<torch::stable::Tensor>(stack[0 ]);
50+ }
51+
52+ torch::stable::Tensor decode_webp (
53+ const torch::stable::Tensor& data,
54+ ImageReadMode mode) {
55+ const auto num_args = 2 ;
56+ std::array<StableIValue, num_args> stack{
57+ torch::stable::detail::from (data), torch::stable::detail::from (mode)};
58+ TORCH_ERROR_CODE_CHECK (torch_call_dispatcher (
59+ " image::decode_webp" , " " , stack.data (), TORCH_ABI_VERSION ));
60+ return torch::stable::detail::to<torch::stable::Tensor>(stack[0 ]);
61+ }
62+
63+ } // namespace
64+
65+ torch::stable::Tensor decode_image (
66+ const torch::stable::Tensor& data,
1367 ImageReadMode mode,
1468 bool apply_exif_orientation) {
1569 // Check that tensor is a CPU tensor
16- STD_TORCH_CHECK (data.device () == torch:: kCPU , " Expected a CPU tensor" );
70+ STD_TORCH_CHECK (data.is_cpu () , " Expected a CPU tensor" );
1771 // Check that the input tensor dtype is uint8
18- STD_TORCH_CHECK (data.dtype () == torch::kU8 , " Expected a torch.uint8 tensor" );
72+ STD_TORCH_CHECK (
73+ data.scalar_type () == torch::headeronly::ScalarType::Byte,
74+ " Expected a torch.uint8 tensor" );
1975 // Check that the input tensor is 1-dimensional
2076 STD_TORCH_CHECK (
2177 data.dim () == 1 && data.numel () > 0 ,
@@ -24,7 +80,7 @@ torch::Tensor decode_image(
2480 auto err_msg =
2581 " Unsupported image file. Only jpeg, png, webp and gif are currently supported. For avif and heic format, please rely on `decode_avif` and `decode_heic` directly." ;
2682
27- auto datap = data.data_ptr <uint8_t >();
83+ auto datap = data.const_data_ptr <uint8_t >();
2884
2985 const uint8_t jpeg_signature[3 ] = {255 , 216 , 255 }; // == "\xFF\xD8\xFF"
3086 STD_TORCH_CHECK (data.numel () >= 3 , err_msg);
@@ -60,5 +116,14 @@ torch::Tensor decode_image(
60116 STD_TORCH_CHECK (false , err_msg);
61117}
62118
119+ STABLE_TORCH_LIBRARY_FRAGMENT (image, m) {
120+ m.def (
121+ " decode_image(Tensor data, int mode, bool apply_exif_orientation=False) -> Tensor" );
122+ }
123+
124+ STABLE_TORCH_LIBRARY_IMPL (image, CompositeExplicitAutograd, m) {
125+ m.impl (" decode_image" , TORCH_BOX (&decode_image));
126+ }
127+
63128} // namespace image
64129} // namespace vision
0 commit comments