Fix rendering of embedded images in preview diff

For embedded images the XDocServlet should return the plain image
file, but for the preview of images it should use the ImageFormatter
and return the formatted image (e.g. with green background if added,
with red background if deleted). Add a URL parameter 'formatImage'
that decides if the ImageFormatter is used. If not given the plain
image file is returned.

Change-Id: I3a6ee6fb2738603aa45172d2ae7d5339945b7802
Signed-off-by: Edwin Kempin <edwin.kempin@sap.com>
diff --git a/src/main/java/com/googlesource/gerrit/plugins/xdocs/XDocServlet.java b/src/main/java/com/googlesource/gerrit/plugins/xdocs/XDocServlet.java
index 70f9598..0c2b368 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/xdocs/XDocServlet.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/xdocs/XDocServlet.java
@@ -127,8 +127,6 @@
       mimeType = new MimeType(FileContentUtil.resolveContentType(
           state, key.file, FileMode.FILE, mimeType.toString()));
       FormatterProvider formatter = getFormatter(req, key, mimeType);
-
-
       validateDiffMode(key, formatter, mimeType);
 
       ProjectControl projectControl = projectControlFactory.validateFor(key.project);
@@ -165,7 +163,7 @@
           rsc = docCache.get(formatter, key.project, key.file, revId,
               revIdB, key.diffMode);
         } else if (isImage(mimeType)) {
-          rsc = getImageResource(repo, revId, key.file);
+          rsc = getImageResource(repo, key.diffMode, revId, revIdB, key.file);
         } else {
           rsc = Resource.NOT_FOUND;
         }
@@ -189,10 +187,14 @@
     }
   }
 
-  private Resource getImageResource(Repository repo, ObjectId revId, String file) {
+  private Resource getImageResource(Repository repo, DiffMode diffMode,
+      ObjectId revId, ObjectId revIdB, String file) {
+    ObjectId id = diffMode == DiffMode.NO_DIFF || diffMode == DiffMode.SIDEBYSIDE_A
+        ? revId
+        : revIdB;
     RevWalk rw = new RevWalk(repo);
     try {
-      RevCommit commit = rw.parseCommit(revId);
+      RevCommit commit = rw.parseCommit(id);
       RevTree tree = commit.getTree();
       TreeWalk tw = new TreeWalk(repo);
       try {
@@ -234,8 +236,7 @@
   private static void validateDiffMode(ResourceKey key,
       FormatterProvider formatter, MimeType mimeType)
       throws ResourceNotFoundException {
-    if (key.diffMode != DiffMode.NO_DIFF
-        && (key.revisionB == null || (formatter == null && isImage(mimeType)))) {
+    if (key.diffMode != DiffMode.NO_DIFF && (key.revisionB == null)) {
       throw new ResourceNotFoundException();
     }
   }
@@ -258,9 +259,8 @@
       formatter = formatters.get(getProject(key), key.file);
     }
     if (isSafeImage(mimeType)) {
-      if (key.diffMode == DiffMode.NO_DIFF) {
-        // always return plain images when no diff is requested,
-        // use formatter on images only for diff mode
+      if (req.getParameter("formatImage") == null) {
+        // image formatting is not requested, return the plain image
         formatter = null;
       }
     } else {
diff --git a/src/main/java/com/googlesource/gerrit/plugins/xdocs/client/XDocApi.java b/src/main/java/com/googlesource/gerrit/plugins/xdocs/client/XDocApi.java
index e7cde1c..7fa7b01 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/xdocs/client/XDocApi.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/xdocs/client/XDocApi.java
@@ -37,6 +37,7 @@
     }
     url.append("/");
     url.append(path);
+    url.append("?formatImage");
     return url.toString();
   }
 
diff --git a/src/main/resources/Documentation/about.md b/src/main/resources/Documentation/about.md
index f59aa46..a15e124 100644
--- a/src/main/resources/Documentation/about.md
+++ b/src/main/resources/Documentation/about.md
@@ -78,7 +78,7 @@
   <tr>
     <td><tt>ImageFormatter</tt></td>
     <td><tt>IMAGE</tt></td>
-    <td>Formatter for images that were configured as safe.</td>
+    <td>Formatter for images that were configured as safe. Only used if `?formatImage` is appended to the URL.</td>
     <td><a href="../../../Documentation/licenses.html#Apache2_0">Apache2.0</a></td>
     <td><a href="http://commons.apache.org">http://commons.apache.org</a></td>
   </tr>