Fix failed JPX image rendering

BUG=pdfium:338
R=thestig@chromium.org

Review URL: https://codereview.chromium.org/1549043003 .
diff --git a/core/include/fxcodec/fx_codec.h b/core/include/fxcodec/fx_codec.h
index c068a08..1d4d333 100644
--- a/core/include/fxcodec/fx_codec.h
+++ b/core/include/fxcodec/fx_codec.h
@@ -15,6 +15,7 @@
 
 class CFX_DIBSource;
 class CJPX_Decoder;
+class CPDF_ColorSpace;
 class CPDF_PrivateData;
 class CPDF_StreamAcc;
 class ICodec_ScanlineDecoder;
@@ -207,7 +208,7 @@
 
   virtual CJPX_Decoder* CreateDecoder(const uint8_t* src_buf,
                                       FX_DWORD src_size,
-                                      bool use_colorspace) = 0;
+                                      CPDF_ColorSpace* cs) = 0;
 
   virtual void GetImageInfo(CJPX_Decoder* pDecoder,
                             FX_DWORD* width,
diff --git a/core/src/fpdfapi/fpdf_render/fpdf_render_loadimage.cpp b/core/src/fpdfapi/fpdf_render/fpdf_render_loadimage.cpp
index 0306ff0..6bb8033 100644
--- a/core/src/fpdfapi/fpdf_render/fpdf_render_loadimage.cpp
+++ b/core/src/fpdfapi/fpdf_render/fpdf_render_loadimage.cpp
@@ -684,9 +684,8 @@
 
   nonstd::unique_ptr<JpxBitMapContext> context(
       new JpxBitMapContext(pJpxModule));
-  context->set_decoder(pJpxModule->CreateDecoder(m_pStreamAcc->GetData(),
-                                                 m_pStreamAcc->GetSize(),
-                                                 m_pColorSpace == nullptr));
+  context->set_decoder(pJpxModule->CreateDecoder(
+      m_pStreamAcc->GetData(), m_pStreamAcc->GetSize(), m_pColorSpace));
   if (!context->decoder())
     return;
 
diff --git a/core/src/fxcodec/codec/codec_int.h b/core/src/fxcodec/codec/codec_int.h
index 1495f9e..0f2d565 100644
--- a/core/src/fxcodec/codec/codec_int.h
+++ b/core/src/fxcodec/codec/codec_int.h
@@ -18,6 +18,7 @@
 
 class CFX_IccProfileCache;
 class CFX_IccTransformCache;
+class CPDF_ColorSpace;
 
 class CCodec_BasicModule : public ICodec_BasicModule {
  public:
@@ -258,7 +259,7 @@
   // ICodec_JpxModule:
   CJPX_Decoder* CreateDecoder(const uint8_t* src_buf,
                               FX_DWORD src_size,
-                              bool use_colorspace) override;
+                              CPDF_ColorSpace* cs) override;
   void GetImageInfo(CJPX_Decoder* pDecoder,
                     FX_DWORD* width,
                     FX_DWORD* height,
diff --git a/core/src/fxcodec/codec/fx_codec_jpx_opj.cpp b/core/src/fxcodec/codec/fx_codec_jpx_opj.cpp
index 21c1e44..d327645 100644
--- a/core/src/fxcodec/codec/fx_codec_jpx_opj.cpp
+++ b/core/src/fxcodec/codec/fx_codec_jpx_opj.cpp
@@ -9,6 +9,7 @@
 #include <vector>
 
 #include "codec_int.h"
+#include "core/include/fpdfapi/fpdf_resource.h"
 #include "core/include/fxcodec/fx_codec.h"
 #include "core/include/fxcrt/fx_safe_types.h"
 #include "third_party/lcms2-2.6/include/lcms2.h"
@@ -660,7 +661,7 @@
 }
 class CJPX_Decoder {
  public:
-  explicit CJPX_Decoder(bool use_colorspace);
+  explicit CJPX_Decoder(CPDF_ColorSpace* cs);
   ~CJPX_Decoder();
   FX_BOOL Init(const unsigned char* src_data, FX_DWORD src_size);
   void GetInfo(FX_DWORD* width, FX_DWORD* height, FX_DWORD* components);
@@ -674,15 +675,11 @@
   opj_image_t* image;
   opj_codec_t* l_codec;
   opj_stream_t* l_stream;
-  const bool m_UseColorSpace;
+  const CPDF_ColorSpace* const m_ColorSpace;
 };
 
-CJPX_Decoder::CJPX_Decoder(bool use_colorspace)
-    : image(nullptr),
-      l_codec(nullptr),
-      l_stream(nullptr),
-      m_UseColorSpace(use_colorspace) {
-}
+CJPX_Decoder::CJPX_Decoder(CPDF_ColorSpace* cs)
+    : image(nullptr), l_codec(nullptr), l_stream(nullptr), m_ColorSpace(cs) {}
 
 CJPX_Decoder::~CJPX_Decoder() {
   if (l_codec) {
@@ -724,6 +721,8 @@
   if (!l_codec) {
     return FALSE;
   }
+  if (m_ColorSpace && m_ColorSpace->GetFamily() == PDFCS_INDEXED)
+    parameters.flags |= OPJ_DPARAMETERS_IGNORE_PCLR_CMAP_CDEF_FLAG;
   opj_set_info_handler(l_codec, fx_info_callback, 00);
   opj_set_warning_handler(l_codec, fx_warning_callback, 00);
   opj_set_error_handler(l_codec, fx_error_callback, 00);
@@ -734,7 +733,7 @@
     image = NULL;
     return FALSE;
   }
-  image->pdfium_use_colorspace = m_UseColorSpace;
+  image->pdfium_use_colorspace = !!m_ColorSpace;
 
   if (!parameters.nb_tile_to_decode) {
     if (!opj_set_decode_area(l_codec, image, parameters.DA_x0, parameters.DA_y0,
@@ -865,8 +864,8 @@
 
 CJPX_Decoder* CCodec_JpxModule::CreateDecoder(const uint8_t* src_buf,
                                               FX_DWORD src_size,
-                                              bool use_colorspace) {
-  nonstd::unique_ptr<CJPX_Decoder> decoder(new CJPX_Decoder(use_colorspace));
+                                              CPDF_ColorSpace* cs) {
+  nonstd::unique_ptr<CJPX_Decoder> decoder(new CJPX_Decoder(cs));
   return decoder->Init(src_buf, src_size) ? decoder.release() : nullptr;
 }