decode_jpeg.cpp 5.17 KB
Newer Older
1
2
3
4
5
#include "decode_jpeg.h"
#include "common_jpeg.h"

namespace vision {
namespace image {
6
7

#if !JPEG_FOUND
8
torch::Tensor decode_jpeg(const torch::Tensor& data, ImageReadMode mode) {
9
  TORCH_CHECK(
10
      false, "decode_jpeg: torchvision not compiled with libjpeg support");
11
12
}
#else
13
14
15
16

using namespace detail;

namespace {
17
18
19
20
21
22
23
24
25
26

struct torch_jpeg_mgr {
  struct jpeg_source_mgr pub;
  const JOCTET* data;
  size_t len;
};

static void torch_jpeg_init_source(j_decompress_ptr cinfo) {}

static boolean torch_jpeg_fill_input_buffer(j_decompress_ptr cinfo) {
27
28
  // No more data.  Probably an incomplete image;  Raise exception.
  torch_jpeg_error_ptr myerr = (torch_jpeg_error_ptr)cinfo->err;
29
  strcpy(myerr->jpegLastErrorMsg, "Image is incomplete or truncated");
30
  longjmp(myerr->setjmp_buffer, 1);
31
32
33
34
}

static void torch_jpeg_skip_input_data(j_decompress_ptr cinfo, long num_bytes) {
  torch_jpeg_mgr* src = (torch_jpeg_mgr*)cinfo->src;
35
  if (src->pub.bytes_in_buffer < (size_t)num_bytes) {
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
    // Skipping over all of remaining data;  output EOI.
    src->pub.next_input_byte = EOI_BUFFER;
    src->pub.bytes_in_buffer = 1;
  } else {
    // Skipping over only some of the remaining data.
    src->pub.next_input_byte += num_bytes;
    src->pub.bytes_in_buffer -= num_bytes;
  }
}

static void torch_jpeg_term_source(j_decompress_ptr cinfo) {}

static void torch_jpeg_set_source_mgr(
    j_decompress_ptr cinfo,
    const unsigned char* data,
    size_t len) {
  torch_jpeg_mgr* src;
  if (cinfo->src == 0) { // if this is first time;  allocate memory
    cinfo->src = (struct jpeg_source_mgr*)(*cinfo->mem->alloc_small)(
        (j_common_ptr)cinfo, JPOOL_PERMANENT, sizeof(torch_jpeg_mgr));
  }
  src = (torch_jpeg_mgr*)cinfo->src;
  src->pub.init_source = torch_jpeg_init_source;
  src->pub.fill_input_buffer = torch_jpeg_fill_input_buffer;
  src->pub.skip_input_data = torch_jpeg_skip_input_data;
  src->pub.resync_to_restart = jpeg_resync_to_restart; // default
  src->pub.term_source = torch_jpeg_term_source;
  // fill the buffers
  src->data = (const JOCTET*)data;
  src->len = len;
  src->pub.bytes_in_buffer = len;
  src->pub.next_input_byte = src->data;
}

70
71
72
} // namespace

torch::Tensor decode_jpeg(const torch::Tensor& data, ImageReadMode mode) {
Kai Zhang's avatar
Kai Zhang committed
73
74
  C10_LOG_API_USAGE_ONCE(
      "torchvision.csrc.io.image.cpu.decode_jpeg.decode_jpeg");
Francisco Massa's avatar
Francisco Massa committed
75
76
77
78
79
80
81
  // 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");

82
83
84
85
86
87
88
89
90
91
92
93
94
  struct jpeg_decompress_struct cinfo;
  struct torch_jpeg_error_mgr jerr;

  auto datap = data.data_ptr<uint8_t>();
  // Setup decompression structure
  cinfo.err = jpeg_std_error(&jerr.pub);
  jerr.pub.error_exit = torch_jpeg_error_exit;
  /* Establish the setjmp return context for my_error_exit to use. */
  if (setjmp(jerr.setjmp_buffer)) {
    /* If we get here, the JPEG code has signaled an error.
     * We need to clean up the JPEG object.
     */
    jpeg_destroy_decompress(&cinfo);
95
    TORCH_CHECK(false, jerr.jpegLastErrorMsg);
96
97
98
99
100
101
102
  }

  jpeg_create_decompress(&cinfo);
  torch_jpeg_set_source_mgr(&cinfo, datap, data.numel());

  // read info from header.
  jpeg_read_header(&cinfo, TRUE);
103

104
  int channels = cinfo.num_components;
105

106
107
108
109
110
111
112
  if (mode != IMAGE_READ_MODE_UNCHANGED) {
    switch (mode) {
      case IMAGE_READ_MODE_GRAY:
        if (cinfo.jpeg_color_space != JCS_GRAYSCALE) {
          cinfo.out_color_space = JCS_GRAYSCALE;
          channels = 1;
        }
113
        break;
114
115
116
117
118
      case IMAGE_READ_MODE_RGB:
        if (cinfo.jpeg_color_space != JCS_RGB) {
          cinfo.out_color_space = JCS_RGB;
          channels = 3;
        }
119
120
121
122
123
124
125
126
        break;
      /*
       * Libjpeg does not support converting from CMYK to grayscale etc. There
       * is a way to do this but it involves converting it manually to RGB:
       * https://github.com/tensorflow/tensorflow/blob/86871065265b04e0db8ca360c046421efb2bdeb4/tensorflow/core/lib/jpeg/jpeg_mem.cc#L284-L313
       */
      default:
        jpeg_destroy_decompress(&cinfo);
127
        TORCH_CHECK(false, "The provided mode is not supported for JPEG files");
128
129
130
131
132
    }

    jpeg_calc_output_dimensions(&cinfo);
  }

133
134
135
136
137
  jpeg_start_decompress(&cinfo);

  int height = cinfo.output_height;
  int width = cinfo.output_width;

138
139
140
  int stride = width * channels;
  auto tensor =
      torch::empty({int64_t(height), int64_t(width), channels}, torch::kU8);
141
142
143
144
145
146
147
148
149
150
151
152
  auto ptr = tensor.data_ptr<uint8_t>();
  while (cinfo.output_scanline < cinfo.output_height) {
    /* jpeg_read_scanlines expects an array of pointers to scanlines.
     * 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.
     */
    jpeg_read_scanlines(&cinfo, &ptr, 1);
    ptr += stride;
  }

  jpeg_finish_decompress(&cinfo);
  jpeg_destroy_decompress(&cinfo);
153
  return tensor.permute({2, 0, 1});
154
}
155
#endif // #if !JPEG_FOUND
156

157
158
159
160
161
162
163
164
165
166
167
168
169
int64_t _jpeg_version() {
#ifdef JPEG_FOUND
  return JPEG_LIB_VERSION;
#else
  return -1;
#endif
}

bool _is_compiled_against_turbo() {
#ifdef LIBJPEG_TURBO_VERSION
  return true;
#else
  return false;
170
#endif
171
}
172
173
174

} // namespace image
} // namespace vision