diff options
Diffstat (limited to 'test/pdiff')
-rw-r--r-- | test/pdiff/CompareArgs.cpp | 1 | ||||
-rw-r--r-- | test/pdiff/CompareArgs.h | 3 | ||||
-rw-r--r-- | test/pdiff/Makefile.am | 4 | ||||
-rw-r--r-- | test/pdiff/PerceptualDiff.cpp | 1 | ||||
-rw-r--r-- | test/pdiff/RGBAImage.cpp | 144 | ||||
-rw-r--r-- | test/pdiff/RGBAImage.h | 105 | ||||
-rw-r--r-- | test/pdiff/pdiff.cpp | 80 | ||||
-rw-r--r-- | test/pdiff/pdiff.h | 2 |
8 files changed, 69 insertions, 271 deletions
diff --git a/test/pdiff/CompareArgs.cpp b/test/pdiff/CompareArgs.cpp index 38ff1f65..265cfed7 100644 --- a/test/pdiff/CompareArgs.cpp +++ b/test/pdiff/CompareArgs.cpp @@ -15,7 +15,6 @@ */ #include "CompareArgs.h" -#include "RGBAImage.h" #include <stdio.h> static const char* copyright = diff --git a/test/pdiff/CompareArgs.h b/test/pdiff/CompareArgs.h index ca51bf09..ac368d54 100644 --- a/test/pdiff/CompareArgs.h +++ b/test/pdiff/CompareArgs.h @@ -20,9 +20,6 @@ #include <string> #include <cairo.h> -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 67d13fb6..4d462389 100644 --- a/test/pdiff/Makefile.am +++ b/test/pdiff/Makefile.am @@ -5,9 +5,7 @@ libpdiff_la_SOURCES = \ pdiff.h \ lpyramid.c \ lpyramid.h \ - pdiff.cpp \ - RGBAImage.cpp \ - RGBAImage.h + pdiff.cpp perceptualdiff_SOURCES = \ CompareArgs.cpp \ diff --git a/test/pdiff/PerceptualDiff.cpp b/test/pdiff/PerceptualDiff.cpp index eb89fbf5..45ab7cb9 100644 --- a/test/pdiff/PerceptualDiff.cpp +++ b/test/pdiff/PerceptualDiff.cpp @@ -21,7 +21,6 @@ #include <math.h> #include <string> #include "lpyramid.h" -#include "RGBAImage.h" #include "CompareArgs.h" #include "pdiff.h" diff --git a/test/pdiff/RGBAImage.cpp b/test/pdiff/RGBAImage.cpp deleted file mode 100644 index 2c38f9c0..00000000 --- a/test/pdiff/RGBAImage.cpp +++ /dev/null @@ -1,144 +0,0 @@ -/* - RGBAImage.cpp - Copyright (C) 2006 Yangli Hector Yee - - This program is free software; you can redistribute it and/or modify it under the terms of the - GNU General Public License as published by the Free Software Foundation; either version 2 of the License, - or (at your option) any later version. - - This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; - without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. - See the GNU General Public License for more details. - - You should have received a copy of the GNU General Public License along with this program; - if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA -*/ - -#include "RGBAImage.h" -#include "png.h" - -#if HAVE_LIBTIFF -#include "tiff.h" -#include "tiffio.h" - -/* Reads Tiff Images */ -RGBAImage* RGBAImage::ReadTiff(char *filename) -{ - RGBAImage *fimg = 0; - - TIFF* tif = TIFFOpen(filename, "r"); - char emsg[1024]; - emsg[0] = 0; - if (tif) { - TIFFRGBAImage img; - - if (TIFFRGBAImageBegin(&img, tif, 0, emsg)) { - size_t npixels; - uint32* raster; - - npixels = img.width * img.height; - raster = (uint32*) _TIFFmalloc(npixels * sizeof (uint32)); - if (raster != NULL) { - if (TIFFRGBAImageGet(&img, raster, img.width, img.height)) { - /* result is in ABGR */ - fimg = new RGBAImage(img.width, img.height); - for (int y = img.height - 1; y >= 0; y--) { - for (int x = 0; x < (int) img.width; x++) { - fimg->Set(x,img.height - (y+1), raster[x + y * img.width]); - } - } - } - _TIFFfree(raster); - } - } - TIFFRGBAImageEnd(&img); - } - return fimg; -} -#endif /* HAVE_LIBTIFF */ - -/* This portion was written by Scott Corley */ -RGBAImage* RGBAImage::ReadPNG(char *filename) -{ - RGBAImage *fimg = 0; - FILE *fp=fopen(filename, "rb"); - if (!fp) - { - return NULL; - } - png_byte header[8]; - - fread(header, 1, 8, fp); - bool is_png = !png_sig_cmp(header, 0, 8); - if (!is_png) - { - return NULL; - } - - png_structp png_ptr = png_create_read_struct - (PNG_LIBPNG_VER_STRING, (png_voidp)NULL, - NULL, NULL); - if (!png_ptr) - return (NULL); - - png_infop info_ptr = png_create_info_struct(png_ptr); - if (!info_ptr) - { - png_destroy_read_struct(&png_ptr, - (png_infopp)NULL, (png_infopp)NULL); - return (NULL); - } - - png_infop end_info = png_create_info_struct(png_ptr); - if (!end_info) - { - png_destroy_read_struct(&png_ptr, &info_ptr, - (png_infopp)NULL); - return (NULL); - } - - png_init_io(png_ptr, fp); - png_set_sig_bytes(png_ptr, 8); - - png_read_png(png_ptr, info_ptr, 0, NULL); - - png_bytep *row_pointers; - row_pointers = png_get_rows(png_ptr, info_ptr); - - fimg = new RGBAImage(png_ptr->width, png_ptr->height); - for (int y = 0; y < (int) png_ptr->height; y++) { - for (int x = 0; x < (int) png_ptr->width; x++) { - uint32_t value = 0; - if (png_ptr->color_type == PNG_COLOR_TYPE_RGB_ALPHA) - value = ((uint32_t)row_pointers[y][x*4]) | (((uint32_t)row_pointers[y][x*4+1])<<8) | (((uint32_t)row_pointers[y][x*4+2])<<16) |(((uint32_t)row_pointers[y][x*4+3])<<24); - else if (png_ptr->color_type == PNG_COLOR_TYPE_RGB) - value = ((uint32_t)row_pointers[y][x*3] /*B*/) | (((uint32_t)row_pointers[y][x*3+1] /*G*/)<<8) | (((uint32_t)row_pointers[y][x*3+2]/*R*/)<<16) | (0xFFUL << 24); - fimg->Set(x,y, value); - } - } - - png_read_destroy(png_ptr, info_ptr, end_info); - return fimg; -} - -bool RGBAImage::WritePPM() -{ - if (Width <= 0) return false; - if (Height <=0 ) return false; - FILE *out = fopen(Name.c_str(), "wb"); - if (!out) return false; - fprintf(out, "P6\n%d %d 255\n", Width, Height); - for (int y = 0; y < Height; y++) { - for (int x = 0; x < Width; x++) { - int i = x + y * Width; - unsigned char r = Get_Red(i); - unsigned char g = Get_Green(i); - unsigned char b = Get_Blue(i); - fwrite(&r, 1, 1, out); - fwrite(&g, 1, 1, out); - fwrite(&b, 1, 1, out); - } - } - fclose(out); - return true; -} diff --git a/test/pdiff/RGBAImage.h b/test/pdiff/RGBAImage.h deleted file mode 100644 index 710cabeb..00000000 --- a/test/pdiff/RGBAImage.h +++ /dev/null @@ -1,105 +0,0 @@ -/* - RGBAImage.h - Copyright (C) 2006 Yangli Hector Yee - - This program is free software; you can redistribute it and/or modify it under the terms of the - GNU General Public License as published by the Free Software Foundation; either version 2 of the License, - or (at your option) any later version. - - This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; - without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. - See the GNU General Public License for more details. - - You should have received a copy of the GNU General Public License along with this program; - if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA -*/ - -#ifndef _RGBAIMAGE_H -#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; - Height = h; - if (name) Name = name; - Data = new unsigned int[w * h]; - }; - ~RGBAImage() { if (Data) delete[] Data; } - 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; } - 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(); - static RGBAImage* ReadTiff(char *filename); - static RGBAImage* ReadPNG(char *filename); -protected: - int Width; - int Height; - std::string Name; - 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_Unpremultiply(i)); } -}; - -#endif diff --git a/test/pdiff/pdiff.cpp b/test/pdiff/pdiff.cpp index ad8dd72c..1609cdad 100644 --- a/test/pdiff/pdiff.cpp +++ b/test/pdiff/pdiff.cpp @@ -14,10 +14,9 @@ if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ -#include "CompareArgs.h" -#include "RGBAImage.h" #include "lpyramid.h" #include <math.h> +#include <stdint.h> #include "pdiff.h" #ifndef M_PI @@ -128,6 +127,63 @@ XYZToLAB (float x, float y, float z, float *L, float *A, float *B) *B = 200.0f * (f[1] - f[2]); } +static uint32_t +_get_pixel (cairo_surface_t *surface, int i) +{ + uint32_t *data; + + data = (uint32_t *) cairo_image_surface_get_data (surface); + return data[i]; +} + +static unsigned char +_get_red (cairo_surface_t *surface, int i) +{ + uint32_t pixel; + uint8_t alpha; + + pixel = _get_pixel (surface, i); + + alpha = (pixel & 0xff000000) >> 24; + + if (alpha == 0) + return 0; + else + return (((pixel & 0x00ff0000) >> 16) * 255 + alpha / 2) / alpha; +} + +static unsigned char +_get_green (cairo_surface_t *surface, int i) +{ + uint32_t pixel; + uint8_t alpha; + + pixel = _get_pixel (surface, i); + + alpha = (pixel & 0xff000000) >> 24; + + if (alpha == 0) + return 0; + else + return (((pixel & 0x0000ff00) >> 8) * 255 + alpha / 2) / alpha; +} + +static unsigned char +_get_blue (cairo_surface_t *surface, int i) +{ + uint32_t pixel; + uint8_t alpha; + + pixel = _get_pixel (surface, i); + + alpha = (pixel & 0xff000000) >> 24; + + if (alpha == 0) + return 0; + else + return (((pixel & 0x000000ff) >> 0) * 255 + alpha / 2) / alpha; +} + int pdiff_compare (cairo_surface_t *surface_a, cairo_surface_t *surface_b, @@ -135,7 +191,6 @@ pdiff_compare (cairo_surface_t *surface_a, double luminance, double field_of_view) { - RGBAImage *image_a, *image_b; unsigned int dim = (cairo_image_surface_get_width (surface_a) * cairo_image_surface_get_height (surface_a)); unsigned int i; @@ -168,24 +223,21 @@ pdiff_compare (cairo_surface_t *surface_a, unsigned int pixels_failed; - image_a = new RGBACairoImage (surface_a); - image_b = new RGBACairoImage (surface_b); - - w = image_a->Get_Width(); - h = image_a->Get_Height(); + w = cairo_image_surface_get_width (surface_a); + h = cairo_image_surface_get_height (surface_a); for (y = 0; y < h; y++) { for (x = 0; x < w; x++) { float r, g, b, l; i = x + y * w; - r = powf(image_a->Get_Red(i) / 255.0f, gamma); - g = powf(image_a->Get_Green(i) / 255.0f, gamma); - b = powf(image_a->Get_Blue(i) / 255.0f, gamma); + r = powf(_get_red (surface_a, i) / 255.0f, gamma); + g = powf(_get_green (surface_a, i) / 255.0f, gamma); + b = powf(_get_blue (surface_a, i) / 255.0f, gamma); AdobeRGBToXYZ(r,g,b,&aX[i],&aY[i],&aZ[i]); XYZToLAB(aX[i], aY[i], aZ[i], &l, &aA[i], &aB[i]); - r = powf(image_b->Get_Red(i) / 255.0f, gamma); - g = powf(image_b->Get_Green(i) / 255.0f, gamma); - b = powf(image_b->Get_Blue(i) / 255.0f, gamma); + r = powf(_get_red (surface_b, i) / 255.0f, gamma); + g = powf(_get_green (surface_b, i) / 255.0f, gamma); + b = powf(_get_blue (surface_b, i) / 255.0f, gamma); AdobeRGBToXYZ(r,g,b,&bX[i],&bY[i],&bZ[i]); XYZToLAB(bX[i], bY[i], bZ[i], &l, &bA[i], &bB[i]); diff --git a/test/pdiff/pdiff.h b/test/pdiff/pdiff.h index 195a34c4..d20ae86d 100644 --- a/test/pdiff/pdiff.h +++ b/test/pdiff/pdiff.h @@ -21,6 +21,8 @@ extern "C" { #endif +#include <cairo.h> + /* Image comparison metric using Yee's method (and a cairo interface) * References: A Perceptual Metric for Production Testing, Hector Yee, Journal of Graphics Tools 2004 */ |