Skip to content

Commit

Permalink
Update decode_jpeg.cpp
Browse files Browse the repository at this point in the history
  • Loading branch information
vfdev-5 committed Aug 11, 2023
1 parent a6880ef commit bb46b5c
Showing 1 changed file with 5 additions and 5 deletions.
10 changes: 5 additions & 5 deletions torchvision/csrc/io/image/cpu/decode_jpeg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -157,15 +157,15 @@ torch::Tensor decode_jpeg(const torch::Tensor& data, ImageReadMode mode) {
jpeg_read_header(&cinfo, TRUE);

int channels = cinfo.num_components;
bool cmyk_to_rgb = false;
bool cmyk_to_rgb_or_gray = false;

if (mode != IMAGE_READ_MODE_UNCHANGED) {
switch (mode) {
case IMAGE_READ_MODE_GRAY:
if (cinfo.jpeg_color_space == JCS_CMYK ||
cinfo.jpeg_color_space == JCS_YCCK) {
cinfo.out_color_space = JCS_CMYK;
cmyk_to_rgb = true;
cmyk_to_rgb_or_gray = true;
} else {
cinfo.out_color_space = JCS_GRAYSCALE;
}
Expand All @@ -175,7 +175,7 @@ torch::Tensor decode_jpeg(const torch::Tensor& data, ImageReadMode mode) {
if (cinfo.jpeg_color_space == JCS_CMYK ||
cinfo.jpeg_color_space == JCS_YCCK) {
cinfo.out_color_space = JCS_CMYK;
cmyk_to_rgb = true;
cmyk_to_rgb_or_gray = true;
} else {
cinfo.out_color_space = JCS_RGB;
}
Expand Down Expand Up @@ -204,7 +204,7 @@ torch::Tensor decode_jpeg(const torch::Tensor& data, ImageReadMode mode) {
torch::empty({int64_t(height), int64_t(width), channels}, torch::kU8);
auto ptr = tensor.data_ptr<uint8_t>();
torch::Tensor temp_tensor;
if (cmyk_to_rgb) {
if (cmyk_to_rgb_or_gray) {
temp_tensor = torch::empty({int64_t(width), 4}, torch::kU8);
}

Expand All @@ -213,7 +213,7 @@ torch::Tensor decode_jpeg(const torch::Tensor& data, ImageReadMode mode) {
* Here the array is only one element long, but you could ask for
* more than one scanline at a time if that's more convenient.
*/
if (cmyk_to_rgb) {
if (cmyk_to_rgb_or_gray) {
auto temp_buffer = temp_tensor.data_ptr<uint8_t>();
jpeg_read_scanlines(&cinfo, &temp_buffer, 1);

Expand Down

0 comments on commit bb46b5c

Please sign in to comment.