forked from pytorch/vision
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add decode_image op * Fix lint * More lint * Add C10_EXPORT
- Loading branch information
1 parent
59af739
commit 18ec9f6
Showing
8 changed files
with
112 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,9 @@ | ||
|
||
#pragma once | ||
|
||
// Comment | ||
#include <torch/script.h> | ||
#include <torch/torch.h> | ||
#include "read_image_cpu.h" | ||
#include "readjpeg_cpu.h" | ||
#include "readpng_cpu.h" | ||
#include "writejpeg_cpu.h" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
#include "read_image_cpu.h" | ||
#include <string.h> | ||
|
||
torch::Tensor decode_image(const torch::Tensor& data) { | ||
// 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 | ||
TORCH_CHECK( | ||
data.dim() == 1 && data.numel() > 0, | ||
"Expected a non empty 1-dimensional tensor"); | ||
|
||
auto datap = data.data_ptr<uint8_t>(); | ||
|
||
const uint8_t jpeg_signature[3] = {255, 216, 255}; // == "\xFF\xD8\xFF" | ||
const uint8_t png_signature[4] = {137, 80, 78, 71}; // == "\211PNG" | ||
|
||
if (memcmp(jpeg_signature, datap, 3) == 0) { | ||
return decodeJPEG(data); | ||
} else if (memcmp(png_signature, datap, 4) == 0) { | ||
return decodePNG(data); | ||
} else { | ||
TORCH_CHECK( | ||
false, | ||
"Unsupported image file. Only jpeg and png ", | ||
"are currently supported."); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
#pragma once | ||
|
||
#include "readjpeg_cpu.h" | ||
#include "readpng_cpu.h" | ||
|
||
C10_EXPORT torch::Tensor decode_image(const torch::Tensor& data); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters