76f8c5
--- poppler-0.26.5/poppler/CairoOutputDev.cc
76f8c5
+++ poppler-0.26.5/poppler/CairoOutputDev.cc
76f8c5
@@ -2571,7 +2571,11 @@ void CairoOutputDev::drawSoftMaskedImage
76f8c5
 
76f8c5
   cairo_surface_mark_dirty (image);
76f8c5
 
76f8c5
+  cairo_surface_set_user_data(image, (const cairo_user_data_key_t *) 0x1, state, NULL);
76f8c5
+  cairo_surface_set_user_data(image, (const cairo_user_data_key_t *) 0x2, colorMap, NULL);
76f8c5
   setMimeData(str, ref, image);
76f8c5
+  cairo_surface_set_user_data(image, (const cairo_user_data_key_t *) 0x2, NULL, NULL);
76f8c5
+  cairo_surface_set_user_data(image, (const cairo_user_data_key_t *) 0x1, NULL, NULL);
76f8c5
 
76f8c5
   pattern = cairo_pattern_create_for_surface (image);
76f8c5
   cairo_surface_destroy (image);
76f8c5
@@ -2674,24 +2678,72 @@ GBool CairoOutputDev::getStreamData (Str
76f8c5
   return gTrue;
76f8c5
 }
76f8c5
 
76f8c5
+static GBool colorMapHasIdentityDecodeMap(GfxImageColorMap *colorMap)
76f8c5
+{
76f8c5
+  for (int i = 0; i < colorMap->getNumPixelComps(); i++) {
76f8c5
+    if (colorMap->getDecodeLow(i) != 0.0 || colorMap->getDecodeHigh(i) != 1.0)
76f8c5
+      return gFalse;
76f8c5
+  }
76f8c5
+  return gTrue;
76f8c5
+}
76f8c5
+
76f8c5
 void CairoOutputDev::setMimeData(Stream *str, Object *ref, cairo_surface_t *image)
76f8c5
 {
76f8c5
   char *strBuffer;
76f8c5
   int len;
76f8c5
   Object obj;
76f8c5
+  GfxColorSpace *colorSpace = NULL;
76f8c5
+  GfxState *state = NULL;
76f8c5
+  GfxImageColorMap *colorMap = NULL;
76f8c5
+
76f8c5
+  if (image != NULL)
76f8c5
+    {
76f8c5
+      state = (GfxState *) cairo_surface_get_user_data(image, (const cairo_user_data_key_t *) 0x1);
76f8c5
+      colorMap = (GfxImageColorMap *) cairo_surface_get_user_data(image, (const cairo_user_data_key_t *) 0x2);
76f8c5
+    }
76f8c5
 
76f8c5
   if (!printing || !(str->getKind() == strDCT || str->getKind() == strJPX))
76f8c5
     return;
76f8c5
 
76f8c5
+  if (state != NULL) {
76f8c5
+    str->getDict()->lookup("ColorSpace", &obj);
76f8c5
+    colorSpace = GfxColorSpace::parse(&obj, this, state);
76f8c5
+    obj.free();
76f8c5
+  }
76f8c5
+
76f8c5
   // colorspace in stream dict may be different from colorspace in jpx
76f8c5
   // data
76f8c5
-  if (str->getKind() == strJPX) {
76f8c5
-    GBool hasColorSpace = !str->getDict()->lookup("ColorSpace", &obj)->isNull();
76f8c5
-    obj.free();
76f8c5
-    if (hasColorSpace)
76f8c5
+  if (str->getKind() == strJPX && colorSpace)
76f8c5
+    {
76f8c5
+      delete colorSpace;
76f8c5
       return;
76f8c5
+    }
76f8c5
+
76f8c5
+  // only embed mime data for gray, rgb, and cmyk colorspaces.
76f8c5
+  if (colorSpace) {
76f8c5
+    GfxColorSpaceMode mode = colorSpace->getMode();
76f8c5
+    delete colorSpace;
76f8c5
+    switch (mode) {
76f8c5
+      case csDeviceGray:
76f8c5
+      case csCalGray:
76f8c5
+      case csDeviceRGB:
76f8c5
+      case csCalRGB:
76f8c5
+      case csDeviceCMYK:
76f8c5
+      case csICCBased:
76f8c5
+	break;
76f8c5
+
76f8c5
+      case csLab:
76f8c5
+      case csIndexed:
76f8c5
+      case csSeparation:
76f8c5
+      case csDeviceN:
76f8c5
+      case csPattern:
76f8c5
+	return;
76f8c5
+    }
76f8c5
   }
76f8c5
 
76f8c5
+  if (colorMap && !colorMapHasIdentityDecodeMap(colorMap))
76f8c5
+    return;
76f8c5
+
76f8c5
   if (getStreamData (str->getNextStream(), &strBuffer, &len)) {
76f8c5
     cairo_status_t st;
76f8c5
 
76f8c5
@@ -2908,8 +2960,13 @@ void CairoOutputDev::drawImage(GfxState
76f8c5
   if (width == widthA && height == heightA)
76f8c5
     filter = getFilterForSurface (image, interpolate);
76f8c5
 
76f8c5
-  if (!inlineImg) /* don't read stream twice if it is an inline image */
76f8c5
+  if (!inlineImg) { /* don't read stream twice if it is an inline image */
76f8c5
+    cairo_surface_set_user_data(image, (const cairo_user_data_key_t *) 0x1, state, NULL);
76f8c5
+    cairo_surface_set_user_data(image, (const cairo_user_data_key_t *) 0x2, colorMap, NULL);
76f8c5
     setMimeData(str, ref, image);
76f8c5
+    cairo_surface_set_user_data(image, (const cairo_user_data_key_t *) 0x2, NULL, NULL);
76f8c5
+    cairo_surface_set_user_data(image, (const cairo_user_data_key_t *) 0x1, NULL, NULL);
76f8c5
+  }
76f8c5
 
76f8c5
   pattern = cairo_pattern_create_for_surface (image);
76f8c5
   cairo_surface_destroy (image);