[poppler] test: ChangeLog, 1.15, 1.16 buffer-diff.c, 1.2, 1.3 buffer-diff.h, 1.1.1.1, 1.2 test-poppler.c, 1.4, 1.5

Jeff Muizelaar jrmuizel at kemper.freedesktop.org
Tue Apr 11 22:29:13 PDT 2006


Update of /cvs/poppler/test
In directory kemper:/tmp/cvs-serv8504

Modified Files:
	ChangeLog buffer-diff.c buffer-diff.h test-poppler.c 
Log Message:
2006-04-12  Jeff Muizelaar  <jeff at infidigm.net>

	* buffer-diff.c: (buffer_diff), (image_buf_diff):
	* buffer-diff.h:
	* test-poppler.c: (gdk_pixbuf_compare), (poppler_test_page),
	(poppler_test), (main): large performance improvements. I really
	should have split this commit up, but I don't know if this code is
	used by anyone other than me anyways. It should be correct.


Index: ChangeLog
===================================================================
RCS file: /cvs/poppler/test/ChangeLog,v
retrieving revision 1.15
retrieving revision 1.16
diff -u -d -r1.15 -r1.16
--- ChangeLog	31 Mar 2006 06:06:21 -0000	1.15
+++ ChangeLog	12 Apr 2006 05:29:11 -0000	1.16
@@ -1,3 +1,12 @@
+2006-04-12  Jeff Muizelaar  <jeff at infidigm.net>
+
+	* buffer-diff.c: (buffer_diff), (image_buf_diff):
+	* buffer-diff.h:
+	* test-poppler.c: (gdk_pixbuf_compare), (poppler_test_page),
+	(poppler_test), (main): large performance improvements. I really
+	should have split this commit up, but I don't know if this code is
+	used by anyone other than me anyways. It should be correct.
+
 2006-03-31  Jeff Muizelaar  <jeff at infidigm.net>
 
 	* read-png.c: (read_png_argb32): improve performance by only

Index: buffer-diff.c
===================================================================
RCS file: /cvs/poppler/test/buffer-diff.c,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -d -r1.2 -r1.3
--- buffer-diff.c	10 Dec 2005 04:15:49 -0000	1.2
+++ buffer-diff.c	12 Apr 2006 05:29:11 -0000	1.3
@@ -27,6 +27,7 @@
 #include <unistd.h>
 #include <errno.h>
 #include <string.h>
+#include <stdint.h>
 
 #include "buffer-diff.h"
 #include "read-png.h"
@@ -63,32 +64,100 @@
 	for (x = 0; x < width; x++)
 	{
 	    int channel;
-	    unsigned char value_a, value_b;
+	    uint32_t value_a, value_b;
 	    int pixel_differs = 0;
-	    for (channel = 0; channel < 4; channel++)
-	    {
-		double diff;
-		value_a = row_a[x * 4 + channel];
-		value_b = row_b[x * 4 + channel];
-		if (value_a != value_b)
-		    pixel_differs = 1;
-		diff = value_a - value_b;
-		row[x * 4 + channel] = 128 + diff / 3.0;
+	    value_a = *(uint32_t*)(&(row_a[x*4]));
+	    value_b = *(uint32_t*)(&(row_b[x*4]));
+	    if (value_a != value_b) {
+	        for (channel = 0; channel < 4; channel++)
+		{
+		    double diff;
+		    unsigned char channel_value_a, channel_value_b;
+		    channel_value_a = row_a[x * 4 + channel];
+		    channel_value_b = row_b[x * 4 + channel];
+		    if (channel_value_a != channel_value_b) {
+			pixel_differs = 1;
+			diff = channel_value_a - channel_value_b;
+			row[x * 4 + channel] = 128 + diff / 3.0;
+		    }
+		}
 	    }
 	    if (pixel_differs) {
 		pixels_changed++;
+		*(uint32_t*)(&(row[x*4])) |= 0xff000000; /* Set ALPHA to 100% (opaque) */
 	    } else {
-		row[x*4+0] = 0;
-		row[x*4+1] = 0;
-		row[x*4+2] = 0;
+		*(uint32_t*)(&(row[x*4])) = 0xff000000; /* Set ALPHA to 100% (opaque) */
 	    }
-	    row[x * 4 + 3] = 0xff; /* Set ALPHA to 100% (opaque) */
 	}
     }
-
     return pixels_changed;
 }
 
+int
+image_buf_diff (char *buf_a, int width_a, int height_a, int stride_a,
+	    const char *filename_a,
+	    const char *filename_b,
+	    const char *filename_diff)
+{
+    int pixels_changed;
+    unsigned int width_b, height_b, stride_b;
+    unsigned char *buf_b, *buf_diff;
+    read_png_status_t status;
+
+    status = read_png_argb32 (filename_b, &buf_b, &width_b, &height_b, &stride_b);
+    if (status) {
+	// write out the buffer on failure
+	FILE *png_file = fopen (filename_a, "wb");
+	write_png_argb32 (buf_a, png_file, width_a, height_a, stride_a);
+	fclose (png_file);
+	return -1;
+    }
+
+    if (width_a  != width_b  ||
+	height_a != height_b ||
+	stride_a != stride_b)
+    {
+	fprintf (stderr,
+		 "Error: Image size mismatch: (%dx%d@%d) vs. (%dx%d@%d)\n"
+		 "       for %s vs. %s\n",
+		 width_a, height_a, stride_a,
+		 width_b, height_b, stride_b,
+		 filename_a, filename_b);
+	free (buf_b);
+	return -1;
+    }
+
+    buf_diff = xcalloc (stride_a * height_a, 1);
+
+    pixels_changed = buffer_diff (buf_a, buf_b, buf_diff,
+				  width_a, height_a, stride_a);
+
+    if (pixels_changed) {
+	FILE *png_file = fopen (filename_diff, "wb");
+	write_png_argb32 (buf_diff, png_file, width_a, height_a, stride_a);
+	fclose (png_file);
+	png_file = fopen (filename_a, "wb");
+	write_png_argb32 (buf_a, png_file, width_a, height_a, stride_a);
+	fclose (png_file);
+    } else {
+	char buf[4096];
+	FILE *ref_file = fopen(filename_b, "r");	
+	FILE *png_file = fopen (filename_a, "wb");
+	int count = sizeof(buf);
+	while (count == sizeof(buf)) {
+	  count = fread(buf, 1, sizeof(buf), ref_file);
+	  fwrite(buf, 1, count, png_file);
+	}
+	fclose(ref_file);
+	fclose(png_file);
+	xunlink (filename_diff);
+    }
+
+    free (buf_b);
+    free (buf_diff);
+
+    return pixels_changed;
+}
 /* Image comparison code courtesy of Richard Worth <richard at theworths.org>
  * Returns number of pixels changed, (or -1 on error).
  * Also saves a "diff" image intended to visually show where the

Index: buffer-diff.h
===================================================================
RCS file: /cvs/poppler/test/buffer-diff.h,v
retrieving revision 1.1.1.1
retrieving revision 1.2
diff -u -d -r1.1.1.1 -r1.2
--- buffer-diff.h	21 May 2005 02:09:40 -0000	1.1.1.1
+++ buffer-diff.h	12 Apr 2006 05:29:11 -0000	1.2
@@ -38,6 +38,11 @@
 	     int	    height,
 	     int	    stride);
 
+int
+image_buf_diff (char *buf_a, int width_a, int height_a, int stride_a,
+	    const char *filename_a,
+	    const char *filename_b,
+	    const char *filename_diff);
 /* Returns number of pixels changed, (or -1 on error).
  * Also saves a "diff" image intended to visually show where the
  * images differ.

Index: test-poppler.c
===================================================================
RCS file: /cvs/poppler/test/test-poppler.c,v
retrieving revision 1.4
retrieving revision 1.5
diff -u -d -r1.4 -r1.5
--- test-poppler.c	27 Dec 2005 19:17:06 -0000	1.4
+++ test-poppler.c	12 Apr 2006 05:29:11 -0000	1.5
@@ -18,51 +18,86 @@
       POPPLER_TEST_FAILURE
 } poppler_test_status_t;
 
-poppler_test_status_t poppler_test_page(char *pdf_file, PopplerDocument *document, int page_index) {
-  char *log_name, *png_name, *ref_name, *diff_name;
+poppler_test_status_t gdk_pixbuf_compare(GdkPixbuf *pixbuf, char *pdf_file, int page_index)
+{
+  char *png_name, *ref_name, *diff_name;
   char *srcdir;
-  GdkPixbuf *pixbuf, *thumb;
-  double width, height;
-  PopplerPage *page;
-  GError *error;
-  const char *backend = poppler_get_backend() == POPPLER_BACKEND_SPLASH ? "splash" : "cairo";
-  poppler_test_status_t ret;
+  GError *error = NULL;
   int pixels_changed;
-  /* Get the strings ready that we'll need. */
+  const char *backend = poppler_get_backend() == POPPLER_BACKEND_SPLASH ? "splash" : "cairo";
+   /* Get the strings ready that we'll need. */
   srcdir = getenv ("srcdir");
   if (!srcdir)
     srcdir = ".";
-
-  error = NULL;
-  asprintf (&log_name, "%s-%d-%s%s", pdf_file, page_index, backend, POPPLER_TEST_LOG_SUFFIX);
+ 
   asprintf (&png_name, "%s-%d-%s%s", pdf_file, page_index, backend, POPPLER_TEST_PNG_SUFFIX);
   asprintf (&ref_name, "%s/%s-%d-%s%s", srcdir, pdf_file, page_index, backend, POPPLER_TEST_REF_SUFFIX);
   asprintf (&diff_name, "%s-%d-%s%s", pdf_file, page_index, backend, POPPLER_TEST_DIFF_SUFFIX);
+#ifdef PNG_SAVE
+  gdk_pixbuf_save (pixbuf, png_name, "png", &error, NULL);
+  pixels_changed = image_diff (png_name, ref_name, diff_name);
+#else
+  int n_channels = gdk_pixbuf_get_n_channels(pixbuf);
+
+  int width = gdk_pixbuf_get_width (pixbuf);
+  int height = gdk_pixbuf_get_height (pixbuf);
+
+  int rowstride = gdk_pixbuf_get_rowstride (pixbuf);
+  unsigned int *cairo_pixels = malloc(height * width * 4);
+  unsigned char *pixels = gdk_pixbuf_get_pixels(pixbuf);
+  unsigned char *pixels_p = pixels;
+
+  int j,i;
+  for (j=0; j<height; j++) {
+    pixels = pixels_p + rowstride * j;
+    for (i=0; i<width; i++) {
+      cairo_pixels[i+width*j] = (0xff000000) | (pixels[0] << 16) | (pixels[1] << 8) | pixels[2] << 0;
+      pixels += 3;
+    }
+  }
 
+  pixels_changed = image_buf_diff (cairo_pixels, width, height, width*4, png_name, ref_name, diff_name);
+
+  free (cairo_pixels);
+#endif
+  free (png_name);
+  free (ref_name);
+  free (diff_name);
+
+  return pixels_changed ? POPPLER_TEST_FAILURE : POPPLER_TEST_SUCCESS;
+}
+
+poppler_test_status_t poppler_test_page(char *pdf_file, PopplerDocument *document, int page_index) {
+  GdkPixbuf *pixbuf, *thumb;
+  double width, height;
+  PopplerPage *page;
+  poppler_test_status_t ret;
+  
+  char thumb_file[strlen(pdf_file) + strlen("-thumb") + 1];
+  strcpy(thumb_file, pdf_file);
+  strcat(thumb_file, "-thumb");
+  
   page = poppler_document_get_page (document, page_index);
-  if (page == NULL)
+  if (!page)
     FAIL ("page not found");
 
   poppler_page_get_size (page, &width, &height);
 
   pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB, FALSE, 8, width, height);
   gdk_pixbuf_fill (pixbuf, 0x00106000);
-  poppler_page_render_to_pixbuf (page, 0, 0, width, height, 1, 0, pixbuf);
-
-  gdk_pixbuf_save (pixbuf, png_name, "png", &error, NULL);
+  poppler_page_render_to_pixbuf (page, 0, 0, width, height, 1.0, 0, pixbuf);
 
-  if (error != NULL)
-    FAIL (error->message);
+  ret = gdk_pixbuf_compare(pixbuf, pdf_file, page_index);
+  
+  thumb = poppler_page_get_thumbnail(page);
+  if (thumb)
+    ret |= gdk_pixbuf_compare(thumb, thumb_file, page_index);
+ 
+  if (thumb)
+    g_object_unref (G_OBJECT (thumb));
   g_object_unref (G_OBJECT (page));
   g_object_unref (G_OBJECT (pixbuf));
 
-
-  pixels_changed = image_diff (png_name, ref_name, diff_name);
-
-  ret = pixels_changed ? POPPLER_TEST_FAILURE : POPPLER_TEST_SUCCESS;
-  free (png_name);
-  free (ref_name);
-  free (diff_name);
   return ret;
 }
 
@@ -95,6 +130,7 @@
     else
       printf("PASS\n");
   }
+  g_object_unref (G_OBJECT (document));
   gfree (uri);
 }
 
@@ -112,6 +148,7 @@
 	  for (i=0; i<globbuf.gl_pathc; i++) {
 		  poppler_test(globbuf.gl_pathv[i]);
 	  }
+	  globfree(&globbuf);
   }
 
   return 0;



More information about the poppler mailing list