summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorCarl Worth <cworth@cworth.org>2006-12-12 16:15:08 -0800
committerCarl Worth <cworth@cworth.org>2006-12-14 07:57:59 -0800
commit0d7870b6bf13edfe513e2de25a5814a0a1b78c79 (patch)
tree548d26f02cd2b28b7f0e03e5ea7314511d881858
parenta87f494d4b91f3acc78d6d7dd737939633f28d71 (diff)
pdiff: Teach pdiff code to accept cairo image surfaces
This is a second small step in enabling cairo's test suite and the pdiff code to start working together.
-rw-r--r--test/pdiff/CompareArgs.cpp49
-rw-r--r--test/pdiff/CompareArgs.h1
-rw-r--r--test/pdiff/Makefile.am1
-rw-r--r--test/pdiff/RGBAImage.h64
4 files changed, 79 insertions, 36 deletions
diff --git a/test/pdiff/CompareArgs.cpp b/test/pdiff/CompareArgs.cpp
index aa69a7d1..60520b04 100644
--- a/test/pdiff/CompareArgs.cpp
+++ b/test/pdiff/CompareArgs.cpp
@@ -60,6 +60,7 @@ CompareArgs::~CompareArgs()
bool CompareArgs::Parse_Args(int argc, char **argv)
{
+ cairo_surface_t *surface;
if (argc < 3) {
ErrorStr = copyright;
ErrorStr += usage;
@@ -67,37 +68,29 @@ bool CompareArgs::Parse_Args(int argc, char **argv)
}
for (int i = 0; i < argc; i++) {
if (i == 1) {
-#if HAVE_LIBTIFF
- ImgA = RGBAImage::ReadTiff(argv[1]);
- if (!ImgA) {
-#endif /* HAVE_LIBTIFF */
- ImgA = RGBAImage::ReadPNG(argv[1]);
- if (!ImgA)
- {
- ErrorStr = "FAIL: Cannot open ";
- ErrorStr += argv[1];
- ErrorStr += "\n";
- return false;
- }
-#if HAVE_LIBTIFF
+ surface = cairo_image_surface_create_from_png (argv[1]);
+ if (cairo_surface_status (surface))
+ {
+ ErrorStr = "FAIL: Cannot open ";
+ ErrorStr += argv[1];
+ ErrorStr += " ";
+ ErrorStr += cairo_status_to_string (cairo_surface_status (surface));
+ ErrorStr += "\n";
+ return false;
}
-#endif /* HAVE_LIBTIFF */
+ ImgA = new RGBACairoImage (surface);
} else if (i == 2) {
-#if HAVE_LIBTIFF
- ImgB = RGBAImage::ReadTiff(argv[2]);
- if (!ImgB) {
-#endif /* HAVE_LIBTIFF */
- ImgB = RGBAImage::ReadPNG(argv[2]);
- if (!ImgB)
- {
- ErrorStr = "FAIL: Cannot open ";
- ErrorStr += argv[2];
- ErrorStr += "\n";
- return false;
- }
-#if HAVE_LIBTIFF
+ surface = cairo_image_surface_create_from_png (argv[2]);
+ if (cairo_surface_status (surface))
+ {
+ ErrorStr = "FAIL: Cannot open ";
+ ErrorStr += argv[2];
+ ErrorStr += " ";
+ ErrorStr += cairo_status_to_string (cairo_surface_status (surface));
+ ErrorStr += "\n";
+ return false;
}
-#endif /* HAVE_LIBTIFF */
+ ImgB = new RGBACairoImage (surface);
} else {
if (strstr(argv[i], "-fov")) {
if (i + 1 < argc) {
diff --git a/test/pdiff/CompareArgs.h b/test/pdiff/CompareArgs.h
index a4a1dddd..fc6993ff 100644
--- a/test/pdiff/CompareArgs.h
+++ b/test/pdiff/CompareArgs.h
@@ -20,6 +20,7 @@ if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
#include <string>
class RGBAImage;
+class RGBACairoImage;
// Args to pass into the comparison function
class CompareArgs
diff --git a/test/pdiff/Makefile.am b/test/pdiff/Makefile.am
index 36bebd00..9f441b92 100644
--- a/test/pdiff/Makefile.am
+++ b/test/pdiff/Makefile.am
@@ -11,4 +11,5 @@ perceptualdiff_SOURCES = \
RGBAImage.cpp \
RGBAImage.h
+INCLUDES = -I$(top_srcdir)/src
LDADD = $(top_builddir)/src/libcairo.la
diff --git a/test/pdiff/RGBAImage.h b/test/pdiff/RGBAImage.h
index 65802f92..6aa5ff96 100644
--- a/test/pdiff/RGBAImage.h
+++ b/test/pdiff/RGBAImage.h
@@ -18,11 +18,13 @@ if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
#define _RGBAIMAGE_H
#include<string>
+#include<cairo.h>
// assumes data is in the ABGR format
class RGBAImage
{
public:
+ RGBAImage() { Width = 0; Height = 0; Data = 0; }
RGBAImage(int w, int h, const char *name = 0)
{
Width = w;
@@ -31,17 +33,17 @@ public:
Data = new unsigned int[w * h];
};
~RGBAImage() { if (Data) delete[] Data; }
- unsigned char Get_Red(unsigned int i) { return (Data[i] & 0xFF); }
- unsigned char Get_Green(unsigned int i) { return ((Data[i]>>8) & 0xFF); }
- unsigned char Get_Blue(unsigned int i) { return ((Data[i]>>16) & 0xFF); }
- unsigned char Get_Alpha(unsigned int i) { return ((Data[i]>>24) & 0xFF); }
- void Set(unsigned char r, unsigned char g, unsigned char b, unsigned char a, unsigned int i)
+ virtual unsigned char Get_Red(unsigned int i) { return (Data[i] & 0xFF); }
+ virtual unsigned char Get_Green(unsigned int i) { return ((Data[i]>>8) & 0xFF); }
+ virtual unsigned char Get_Blue(unsigned int i) { return ((Data[i]>>16) & 0xFF); }
+ virtual unsigned char Get_Alpha(unsigned int i) { return ((Data[i]>>24) & 0xFF); }
+ virtual void Set(unsigned char r, unsigned char g, unsigned char b, unsigned char a, unsigned int i)
{ Data[i] = r | (g << 8) | (b << 16) | (a << 24); }
int Get_Width(void) const { return Width; }
int Get_Height(void) const { return Height; }
- void Set(int x, int y, unsigned int d) { Data[x + y * Width] = d; }
- unsigned int Get(int x, int y) const { return Data[x + y * Width]; }
- unsigned int Get(int i) const { return Data[i]; }
+ virtual void Set(int x, int y, unsigned int d) { Data[x + y * Width] = d; }
+ virtual unsigned int Get(int x, int y) const { return Data[x + y * Width]; }
+ virtual unsigned int Get(int i) const { return Data[i]; }
const std::string &Get_Name(void) const { return Name; }
bool WritePPM();
@@ -54,4 +56,50 @@ protected:
unsigned int *Data;
};
+class RGBACairoImage : public RGBAImage
+{
+public:
+ RGBACairoImage (cairo_surface_t *surface)
+ {
+ Width = cairo_image_surface_get_width (surface);
+ Height = cairo_image_surface_get_height (surface);
+ Data = (unsigned int *) cairo_image_surface_get_data (surface);
+ if (cairo_image_surface_get_stride (surface) != 4 * Width) {
+ fprintf (stderr, "Error: Currently only support images where stride == 4 * width\n");
+ exit (1);
+ }
+ }
+ ~RGBACairoImage() { }
+
+ unsigned int ARGB_to_ABGR(unsigned int pixel) const {
+ unsigned int new_pixel;
+ new_pixel = pixel & 0xff00ff00;
+ new_pixel |= (pixel & 0x00ff0000) >> 16;
+ new_pixel |= (pixel & 0x000000ff) << 16;
+ return new_pixel;
+ }
+ unsigned int Get_Unpremultiply(unsigned int i) const {
+ uint32_t pixel;
+ uint8_t alpha;
+
+ pixel = Data[i];
+
+ alpha = (pixel & 0xff000000) >> 24;
+
+ if (alpha == 0)
+ return 0;
+
+ return (alpha << 24) |
+ ((((pixel & 0xff0000) >> 16) * 255 + alpha / 2) / alpha) << 16 |
+ ((((pixel & 0x00ff00) >> 8) * 255 + alpha / 2) / alpha) << 8 |
+ ((((pixel & 0x0000ff) >> 0) * 255 + alpha / 2) / alpha) << 0;
+ }
+ unsigned char Get_Alpha(unsigned int i) { return (Get_Unpremultiply(i) & 0xff000000) >> 24;}
+ unsigned char Get_Red(unsigned int i) { return (Get_Unpremultiply(i) & 0x00ff0000) >> 16;}
+ unsigned char Get_Green(unsigned int i) { return (Get_Unpremultiply(i) & 0x0000ff00) >> 8;}
+ unsigned char Get_Blue(unsigned int i) { return (Get_Unpremultiply(i) & 0x000000ff) >> 0;}
+ unsigned int Get(int x, int y) const { return Get(Width * y + x); }
+ unsigned int Get(int i) const { return ARGB_to_ABGR(Get_Pixel_Unpremultiply(i)); }
+};
+
#endif