From caf5dbe2e4fa20a9f5b7f77f8feb2d90c15fa77b Mon Sep 17 00:00:00 2001
From: Thomas Freitag <Thomas.Freitag@kabelmail.de>
Date: Mon, 24 Jan 2011 11:14:18 +0100
Subject: [PATCH 3/3] Improve splash gradient rendering

TODO: explain the purpose of this change
---
 poppler/SplashOutputDev.cc |    8 ++--
 splash/Splash.cc           |   67 +++++++++++++++++++++++--------------------
 2 files changed, 40 insertions(+), 35 deletions(-)

diff --git a/poppler/SplashOutputDev.cc b/poppler/SplashOutputDev.cc
index 0669f98..b073b67 100644
--- a/poppler/SplashOutputDev.cc
+++ b/poppler/SplashOutputDev.cc
@@ -3144,14 +3144,14 @@ void SplashOutputDev::beginTransparencyGroup(GfxState *state, double *bbox,
   tx = (int)floor(xMin);
   if (tx < 0) {
     tx = 0;
-  } else if (tx > bitmap->getWidth()) {
-    tx = bitmap->getWidth();
+  } else if (tx > bitmap->getWidth() - 1) {
+    tx = bitmap->getWidth() - 1;
   }
   ty = (int)floor(yMin);
   if (ty < 0) {
     ty = 0;
-  } else if (ty > bitmap->getHeight()) {
-    ty = bitmap->getHeight();
+  } else if (ty > bitmap->getHeight() - 1) {
+    ty = bitmap->getHeight() - 1;
   }
   w = (int)ceil(xMax) - tx + 1;
   if (tx + w > bitmap->getWidth()) {
diff --git a/splash/Splash.cc b/splash/Splash.cc
index bc317a6..cc6c3f8 100644
--- a/splash/Splash.cc
+++ b/splash/Splash.cc
@@ -253,29 +253,7 @@ inline void Splash::pipeRun(SplashPipe *pipe) {
   // dynamic pattern
   if (pipe->pattern) {
     if (!pipe->pattern->getColor(pipe->x, pipe->y, pipe->cSrcVal)) {
-      switch (bitmap->mode) {
-        case splashModeMono1:
-          if (!(pipe->destColorMask >>= 1)) 
-            ++pipe->destColorPtr;
-        break;
-        case splashModeMono8:
-          ++pipe->destColorPtr;
-        break;
-        case splashModeBGR8:
-        case splashModeRGB8:
-          pipe->destColorPtr += 3;
-        break;
-        case splashModeXBGR8:
-#if SPLASH_CMYK
-        case splashModeCMYK8:
-#endif
-          pipe->destColorPtr += 4;
-        break;
-      }
-      if (pipe->destAlphaPtr) {
-        ++pipe->destAlphaPtr;
-      }
-      ++pipe->x;
+      pipeIncX(pipe);
       return;
     }
   }
@@ -3950,19 +3928,25 @@ SplashError Splash::shadedFill(SplashPath *path, GBool hasBBox,
   int xMinI, yMinI, xMaxI, yMaxI, x0, x1, y;
   SplashClipResult clipRes;
 
-  if (aaBuf == NULL) { // should not happen, but to be secure
+  if (vectorAntialias && aaBuf == NULL) { // should not happen, but to be secure
     return splashErrGeneric;
   }
   if (path->length == 0) {
     return splashErrEmptyPath;
   }
   xPath = new SplashXPath(path, state->matrix, state->flatness, gTrue);
-  xPath->aaScale();
+  if (vectorAntialias) {
+    xPath->aaScale();
+  }
   xPath->sort();
   scanner = new SplashXPathScanner(xPath, gFalse);
 
   // get the min and max x and y values
-  scanner->getBBoxAA(&xMinI, &yMinI, &xMaxI, &yMaxI);
+  if (vectorAntialias) {
+    scanner->getBBoxAA(&xMinI, &yMinI, &xMaxI, &yMaxI);
+  } else {
+    scanner->getBBox(&xMinI, &yMinI, &xMaxI, &yMaxI);
+  }
 
   // check clipping
   if ((clipRes = state->clip->testRect(xMinI, yMinI, xMaxI, yMaxI)) != splashClipAllOutside) {
@@ -3977,12 +3961,33 @@ SplashError Splash::shadedFill(SplashPath *path, GBool hasBBox,
     pipeInit(&pipe, 0, yMinI, pattern, NULL, state->fillAlpha, vectorAntialias && !hasBBox, gFalse);
 
     // draw the spans
-    for (y = yMinI; y <= yMaxI; ++y) {
-      scanner->renderAALine(aaBuf, &x0, &x1, y);
-      if (clipRes != splashClipAllInside) {
-        state->clip->clipAALine(aaBuf, &x0, &x1, y);
+    if (vectorAntialias) {
+      for (y = yMinI; y <= yMaxI; ++y) {
+	scanner->renderAALine(aaBuf, &x0, &x1, y);
+	if (clipRes != splashClipAllInside) {
+	  state->clip->clipAALine(aaBuf, &x0, &x1, y);
+	}
+	drawAALine(&pipe, x0, x1, y);
+      }
+    } else {
+      SplashClipResult clipRes2;
+      for (y = yMinI; y <= yMaxI; ++y) {
+	while (scanner->getNextSpan(y, &x0, &x1)) {
+	  if (clipRes == splashClipAllInside) {
+	    drawSpan(&pipe, x0, x1, y, gTrue);
+	  } else {
+	    // limit the x range
+	    if (x0 < state->clip->getXMinI()) {
+	      x0 = state->clip->getXMinI();
+	    }
+	    if (x1 > state->clip->getXMaxI()) {
+	      x1 = state->clip->getXMaxI();
+	    }
+	    clipRes2 = state->clip->testSpan(x0, x1, y);
+	    drawSpan(&pipe, x0, x1, y, clipRes2 == splashClipAllInside);
+	  }
+	}
       }
-      drawAALine(&pipe, x0, x1, y);
     }
   }
   opClipRes = clipRes;
-- 
1.7.0.4

