Reapply Foxit's libopenjpeg modifications.

They were lost in commit d53e6fd.

BUG=pdfium:168
R=tsepez@chromium.org

Review URL: https://codereview.chromium.org/1196523002 .
diff --git a/core/src/fpdfapi/fpdf_render/fpdf_render_loadimage.cpp b/core/src/fpdfapi/fpdf_render/fpdf_render_loadimage.cpp
index f754535..0362ff2 100644
--- a/core/src/fpdfapi/fpdf_render/fpdf_render_loadimage.cpp
+++ b/core/src/fpdfapi/fpdf_render/fpdf_render_loadimage.cpp
@@ -700,7 +700,7 @@
       new JpxBitMapContext(pJpxModule));
   context->set_decoder(pJpxModule->CreateDecoder(m_pStreamAcc->GetData(),
                                                  m_pStreamAcc->GetSize(),
-                                                 m_pColorSpace != nullptr));
+                                                 m_pColorSpace == nullptr));
   if (!context->decoder())
     return;
 
diff --git a/core/src/fxcodec/codec/fx_codec_jpx_opj.cpp b/core/src/fxcodec/codec/fx_codec_jpx_opj.cpp
index 81de30e..b7e032e 100644
--- a/core/src/fxcodec/codec/fx_codec_jpx_opj.cpp
+++ b/core/src/fxcodec/codec/fx_codec_jpx_opj.cpp
@@ -627,7 +627,7 @@
   opj_image_t* image;
   opj_codec_t* l_codec;
   opj_stream_t* l_stream;
-  bool m_UseColorSpace;
+  const bool m_UseColorSpace;
 };
 
 CJPX_Decoder::CJPX_Decoder(bool use_colorspace)
@@ -687,6 +687,8 @@
     image = NULL;
     return FALSE;
   }
+  image->pdfium_use_colorspace = m_UseColorSpace;
+
   if (!parameters.nb_tile_to_decode) {
     if (!opj_set_decode_area(l_codec, image, parameters.DA_x0, parameters.DA_y0,
                              parameters.DA_x1, parameters.DA_y1)) {
diff --git a/testing/resources/pixel/bug_71.in b/testing/resources/pixel/bug_71.in
new file mode 100644
index 0000000..6bb6f19
--- /dev/null
+++ b/testing/resources/pixel/bug_71.in
Binary files differ
diff --git a/testing/resources/pixel/bug_71_expected.pdf.0.png b/testing/resources/pixel/bug_71_expected.pdf.0.png
new file mode 100644
index 0000000..65d23c2
--- /dev/null
+++ b/testing/resources/pixel/bug_71_expected.pdf.0.png
Binary files differ
diff --git a/third_party/libopenjpeg20/0000-use-colorspace.patch b/third_party/libopenjpeg20/0000-use-colorspace.patch
new file mode 100644
index 0000000..92f83a2
--- /dev/null
+++ b/third_party/libopenjpeg20/0000-use-colorspace.patch
@@ -0,0 +1,25 @@
+diff -u a/jp2.c b/jp2.c
+--- a/jp2.c
++++ b/jp2.c
+@@ -1429,7 +1429,7 @@
+ 		    /* Part 1, I.5.3.4: Either both or none : */
+ 		    if( !jp2->color.jp2_pclr->cmap)
+ 			    opj_jp2_free_pclr(&(jp2->color));
+-		    else
++		    else if (p_image->pdfium_use_colorspace)
+ 			    opj_jp2_apply_pclr(p_image, &(jp2->color));
+ 	    }
+ 
+diff -u a/openjpeg.h b/openjpeg.h
+--- a/openjpeg.h
++++ b/openjpeg.h
+@@ -677,6 +677,9 @@
+ 	OPJ_BYTE *icc_profile_buf;
+ 	/** size of ICC profile */
+ 	OPJ_UINT32 icc_profile_len;
++
++        /** Whether to apply PCLR or not */
++        OPJ_BOOL pdfium_use_colorspace;
+ } opj_image_t;
+ 
+ 
diff --git a/third_party/libopenjpeg20/README.pdfium b/third_party/libopenjpeg20/README.pdfium
new file mode 100644
index 0000000..e1fad70
--- /dev/null
+++ b/third_party/libopenjpeg20/README.pdfium
@@ -0,0 +1,13 @@
+Name: OpenJPEG
+URL: http://www.openjpeg.org/
+Version: 0fa5a17c98c4b8f9ee2286f4f0a50cf52a5fccb0
+Security Critical: yes
+License: 2-clause BSD
+
+Description:
+JPEG 2000 library.
+
+Local Modifications:
+
+0000-use-colorspace.patch: Makes it possible to not call opj_jp2_apply_pclr().
+TODO(thestig): List all the other patches.
diff --git a/third_party/libopenjpeg20/jp2.c b/third_party/libopenjpeg20/jp2.c
index aafbe19..08b1953 100644
--- a/third_party/libopenjpeg20/jp2.c
+++ b/third_party/libopenjpeg20/jp2.c
@@ -1429,7 +1429,7 @@
 		    /* Part 1, I.5.3.4: Either both or none : */
 		    if( !jp2->color.jp2_pclr->cmap)
 			    opj_jp2_free_pclr(&(jp2->color));
-		    else
+		    else if (p_image->pdfium_use_colorspace)
 			    opj_jp2_apply_pclr(p_image, &(jp2->color));
 	    }
 
diff --git a/third_party/libopenjpeg20/openjpeg.h b/third_party/libopenjpeg20/openjpeg.h
index 4a8e75c..e988de4 100644
--- a/third_party/libopenjpeg20/openjpeg.h
+++ b/third_party/libopenjpeg20/openjpeg.h
@@ -677,6 +677,9 @@
 	OPJ_BYTE *icc_profile_buf;
 	/** size of ICC profile */
 	OPJ_UINT32 icc_profile_len;
+
+        /** Whether to apply PCLR or not */
+        OPJ_BOOL pdfium_use_colorspace;
 } opj_image_t;