diff options
author | Geoffrey Irving <geoffreyi@google.com> | 2016-02-09 11:20:41 -0800 |
---|---|---|
committer | Vijay Vasudevan <vrv@google.com> | 2016-02-09 11:23:03 -0800 |
commit | 49e337b4f163aa0a570333da7933303096ba3d29 (patch) | |
tree | 02a1ad33b74cb905e119a0a9bda3447c2e1776b3 /tensorflow/core/lib/jpeg/jpeg_mem.cc | |
parent | 1bcebcc665e7e9c280be65899002fd3d5a7456a5 (diff) |
Make decode_jpeg automatically turn CMYK to RGB
Our core/lib/jpeg now refuses to output 4 channel jpegs. It still accepts them
as input, but unconditionally converts them to grayscale or RGB.
Change: 114239759
Diffstat (limited to 'tensorflow/core/lib/jpeg/jpeg_mem.cc')
-rw-r--r-- | tensorflow/core/lib/jpeg/jpeg_mem.cc | 16 |
1 files changed, 10 insertions, 6 deletions
diff --git a/tensorflow/core/lib/jpeg/jpeg_mem.cc b/tensorflow/core/lib/jpeg/jpeg_mem.cc index 0dddae2459..8727f1367b 100644 --- a/tensorflow/core/lib/jpeg/jpeg_mem.cc +++ b/tensorflow/core/lib/jpeg/jpeg_mem.cc @@ -77,11 +77,16 @@ uint8* UncompressLow(const void* srcdata, FewerArgsForCompiler* argball) { int stride = flags.stride; // may be 0 int* const nwarn = argball->pnwarn_; // may be NULL - // can't decode if the ratio is not recognized by libjpeg + // Can't decode if the ratio is not recognized by libjpeg if ((ratio != 1) && (ratio != 2) && (ratio != 4) && (ratio != 8)) { return nullptr; } + // Channels must be autodetect, grayscale, or rgb. + if (!(components == 0 || components == 1 || components == 3)) { + return nullptr; + } + // if empty image, return if (datasize == 0 || srcdata == NULL) return nullptr; @@ -105,8 +110,8 @@ uint8* UncompressLow(const void* srcdata, FewerArgsForCompiler* argball) { SetSrc(&cinfo, srcdata, datasize, flags.try_recover_truncated_jpeg); jpeg_read_header(&cinfo, TRUE); - // Set components automatically if desired - if (components == 0) components = cinfo.num_components; + // Set components automatically if desired, autoconverting cmyk to rgb. + if (components == 0) components = std::min(cinfo.num_components, 3); // set grayscale and ratio parameters switch (components) { @@ -114,11 +119,10 @@ uint8* UncompressLow(const void* srcdata, FewerArgsForCompiler* argball) { cinfo.out_color_space = JCS_GRAYSCALE; break; case 3: - case 4: if (cinfo.jpeg_color_space == JCS_CMYK || cinfo.jpeg_color_space == JCS_YCCK) { - // always use cmyk for output in a 4 channel jpeg. libjpeg has a builtin - // decoder. + // Always use cmyk for output in a 4 channel jpeg. libjpeg has a builtin + // decoder. We will further convert to rgb below. cinfo.out_color_space = JCS_CMYK; } else { cinfo.out_color_space = JCS_RGB; |