mirror of
https://gitlab.freedesktop.org/cairo/cairo.git
synced 2026-05-05 05:18:01 +02:00
pdiff: Rewrite Laplacian pyramid code from C++ to C
This commit is contained in:
parent
29456d3865
commit
c7379fcea4
6 changed files with 135 additions and 115 deletions
|
|
@ -1,87 +0,0 @@
|
|||
/*
|
||||
Laplacian Pyramid
|
||||
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 "LPyramid.h"
|
||||
|
||||
|
||||
/*
|
||||
* Construction/Destruction
|
||||
*/
|
||||
|
||||
LPyramid::LPyramid(float *image, int width, int height) :
|
||||
Width(width),
|
||||
Height(height)
|
||||
{
|
||||
/* Make the Laplacian pyramid by successively
|
||||
* copying the earlier levels and blurring them */
|
||||
for (int i=0; i<MAX_PYR_LEVELS; i++) {
|
||||
if (i == 0) {
|
||||
Levels[i] = Copy(image);
|
||||
} else {
|
||||
Levels[i] = new float[Width * Height];
|
||||
Convolve(Levels[i], Levels[i - 1]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
LPyramid::~LPyramid()
|
||||
{
|
||||
for (int i=0; i<MAX_PYR_LEVELS; i++) {
|
||||
if (Levels[i]) delete Levels[i];
|
||||
}
|
||||
}
|
||||
|
||||
float *LPyramid::Copy(float *img)
|
||||
{
|
||||
int max = Width * Height;
|
||||
float *out = new float[max];
|
||||
for (int i = 0; i < max; i++) out[i] = img[i];
|
||||
|
||||
return out;
|
||||
}
|
||||
|
||||
void LPyramid::Convolve(float *a, float *b)
|
||||
/* convolves image b with the filter kernel and stores it in a */
|
||||
{
|
||||
int y,x,i,j,nx,ny;
|
||||
const float Kernel[] = {0.05f, 0.25f, 0.4f, 0.25f, 0.05f};
|
||||
|
||||
for (y=0; y<Height; y++) {
|
||||
for (x=0; x<Width; x++) {
|
||||
int index = y * Width + x;
|
||||
a[index] = 0.0f;
|
||||
for (i=-2; i<=2; i++) {
|
||||
for (j=-2; j<=2; j++) {
|
||||
nx=x+i;
|
||||
ny=y+j;
|
||||
if (nx<0) nx=-nx;
|
||||
if (ny<0) ny=-ny;
|
||||
if (nx>=Width) nx=2*(Width-1)-nx;
|
||||
if (ny>=Height) ny=2*(Height-1)-ny;
|
||||
a[index] += Kernel[i+2] * Kernel[j+2] * b[ny * Width + nx];
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
float LPyramid::Get_Value(int x, int y, int level)
|
||||
{
|
||||
int index = x + y * Width;
|
||||
int l = level;
|
||||
if (l > MAX_PYR_LEVELS) l = MAX_PYR_LEVELS;
|
||||
return Levels[level][index];
|
||||
}
|
||||
|
|
@ -3,8 +3,8 @@ EXTRA_PROGRAMS = perceptualdiff
|
|||
noinst_LTLIBRARIES = libpdiff.la
|
||||
libpdiff_la_SOURCES = \
|
||||
pdiff.h \
|
||||
LPyramid.cpp \
|
||||
LPyramid.h \
|
||||
lpyramid.c \
|
||||
lpyramid.h \
|
||||
Metric.cpp \
|
||||
Metric.h \
|
||||
RGBAImage.cpp \
|
||||
|
|
|
|||
|
|
@ -17,7 +17,7 @@
|
|||
#include "Metric.h"
|
||||
#include "CompareArgs.h"
|
||||
#include "RGBAImage.h"
|
||||
#include "LPyramid.h"
|
||||
#include "lpyramid.h"
|
||||
#include <math.h>
|
||||
#include "pdiff.h"
|
||||
|
||||
|
|
@ -256,8 +256,8 @@ int Yee_Compare_Images(RGBAImage *image_a,
|
|||
|
||||
if (verbose) printf("Constructing Laplacian Pyramids\n");
|
||||
|
||||
LPyramid *la = new LPyramid(aLum, w, h);
|
||||
LPyramid *lb = new LPyramid(bLum, w, h);
|
||||
lpyramid_t *la = lpyramid_create (aLum, w, h);
|
||||
lpyramid_t *lb = lpyramid_create (bLum, w, h);
|
||||
|
||||
float num_one_degree_pixels = (float) (2 * tan(field_of_view * 0.5 * M_PI / 180) * 180 / M_PI);
|
||||
float pixels_per_degree = w / num_one_degree_pixels;
|
||||
|
|
@ -287,11 +287,11 @@ int Yee_Compare_Images(RGBAImage *image_a,
|
|||
float contrast[MAX_PYR_LEVELS - 2];
|
||||
float sum_contrast = 0;
|
||||
for (i = 0; i < MAX_PYR_LEVELS - 2; i++) {
|
||||
float n1 = fabsf(la->Get_Value(x,y,i) - la->Get_Value(x,y,i + 1));
|
||||
float n2 = fabsf(lb->Get_Value(x,y,i) - lb->Get_Value(x,y,i + 1));
|
||||
float n1 = fabsf(lpyramid_get_value (la,x,y,i) - lpyramid_get_value (la,x,y,i + 1));
|
||||
float n2 = fabsf(lpyramid_get_value (lb,x,y,i) - lpyramid_get_value (lb,x,y,i + 1));
|
||||
float numerator = (n1 > n2) ? n1 : n2;
|
||||
float d1 = fabsf(la->Get_Value(x,y,i+2));
|
||||
float d2 = fabsf(lb->Get_Value(x,y,i+2));
|
||||
float d1 = fabsf(lpyramid_get_value(la,x,y,i+2));
|
||||
float d2 = fabsf(lpyramid_get_value(lb,x,y,i+2));
|
||||
float denominator = (d1 > d2) ? d1 : d2;
|
||||
if (denominator < 1e-5f) denominator = 1e-5f;
|
||||
contrast[i] = numerator / denominator;
|
||||
|
|
@ -299,7 +299,7 @@ int Yee_Compare_Images(RGBAImage *image_a,
|
|||
}
|
||||
if (sum_contrast < 1e-5) sum_contrast = 1e-5f;
|
||||
float F_mask[MAX_PYR_LEVELS - 2];
|
||||
float adapt = la->Get_Value(x,y,adaptation_level) + lb->Get_Value(x,y,adaptation_level);
|
||||
float adapt = lpyramid_get_value(la,x,y,adaptation_level) + lpyramid_get_value(lb,x,y,adaptation_level);
|
||||
adapt *= 0.5f;
|
||||
if (adapt < 1e-5) adapt = 1e-5f;
|
||||
for (i = 0; i < MAX_PYR_LEVELS - 2; i++) {
|
||||
|
|
@ -311,7 +311,7 @@ int Yee_Compare_Images(RGBAImage *image_a,
|
|||
}
|
||||
if (factor < 1) factor = 1;
|
||||
if (factor > 10) factor = 10;
|
||||
float delta = fabsf(la->Get_Value(x,y,0) - lb->Get_Value(x,y,0));
|
||||
float delta = fabsf(lpyramid_get_value(la,x,y,0) - lpyramid_get_value(lb,x,y,0));
|
||||
bool pass = true;
|
||||
/* pure luminance test */
|
||||
if (delta > factor * tvi(adapt)) {
|
||||
|
|
@ -357,8 +357,8 @@ int Yee_Compare_Images(RGBAImage *image_a,
|
|||
if (bZ) delete[] bZ;
|
||||
if (aLum) delete[] aLum;
|
||||
if (bLum) delete[] bLum;
|
||||
if (la) delete la;
|
||||
if (lb) delete lb;
|
||||
lpyramid_destroy (la);
|
||||
lpyramid_destroy (lb);
|
||||
if (aA) delete aA;
|
||||
if (bA) delete bA;
|
||||
if (aB) delete aB;
|
||||
|
|
|
|||
|
|
@ -20,7 +20,7 @@
|
|||
#include <string.h>
|
||||
#include <math.h>
|
||||
#include <string>
|
||||
#include "LPyramid.h"
|
||||
#include "lpyramid.h"
|
||||
#include "RGBAImage.h"
|
||||
#include "CompareArgs.h"
|
||||
#include "Metric.h"
|
||||
|
|
|
|||
113
test/pdiff/lpyramid.cpp
Normal file
113
test/pdiff/lpyramid.cpp
Normal file
|
|
@ -0,0 +1,113 @@
|
|||
/*
|
||||
Laplacian Pyramid
|
||||
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 "lpyramid.h"
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
struct _lpyramid {
|
||||
/* Succesively blurred versions of the original image */
|
||||
float *levels[MAX_PYR_LEVELS];
|
||||
|
||||
int width;
|
||||
int height;
|
||||
};
|
||||
|
||||
static void
|
||||
convolve (lpyramid_t *pyramid, float *a, float *b)
|
||||
/* convolves image b with the filter kernel and stores it in a */
|
||||
{
|
||||
int y,x,i,j,nx,ny;
|
||||
const float Kernel[] = {0.05f, 0.25f, 0.4f, 0.25f, 0.05f};
|
||||
int width = pyramid->width;
|
||||
int height = pyramid->height;
|
||||
|
||||
for (y=0; y<height; y++) {
|
||||
for (x=0; x<width; x++) {
|
||||
int index = y * width + x;
|
||||
a[index] = 0.0f;
|
||||
for (i=-2; i<=2; i++) {
|
||||
for (j=-2; j<=2; j++) {
|
||||
nx=x+i;
|
||||
ny=y+j;
|
||||
if (nx<0) nx=-nx;
|
||||
if (ny<0) ny=-ny;
|
||||
if (nx>=width) nx=2*(width-1)-nx;
|
||||
if (ny>=height) ny=2*(height-1)-ny;
|
||||
a[index] += Kernel[i+2] * Kernel[j+2] * b[ny * width + nx];
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* Construction/Destruction
|
||||
*/
|
||||
|
||||
lpyramid_t *
|
||||
lpyramid_create (float *image, int width, int height)
|
||||
{
|
||||
lpyramid_t *pyramid;
|
||||
int i;
|
||||
|
||||
/* XXX: Remove stupid cast after finishing port to C */
|
||||
pyramid = (lpyramid_t *) malloc (sizeof (lpyramid_t));
|
||||
if (pyramid == NULL) {
|
||||
fprintf (stderr, "Out of memory.\n");
|
||||
exit (1);
|
||||
}
|
||||
pyramid->width = width;
|
||||
pyramid->height = height;
|
||||
|
||||
/* Make the Laplacian pyramid by successively
|
||||
* copying the earlier levels and blurring them */
|
||||
for (i=0; i<MAX_PYR_LEVELS; i++) {
|
||||
/* XXX: Remove stupid cast after finishing port to C */
|
||||
pyramid->levels[i] = (float *) malloc (width * height * sizeof (float));
|
||||
if (pyramid->levels[i] == NULL) {
|
||||
fprintf (stderr, "Out of memory.\n");
|
||||
exit (1);
|
||||
}
|
||||
if (i == 0) {
|
||||
memcpy (pyramid->levels[i], image, width * height * sizeof (float));
|
||||
} else {
|
||||
convolve(pyramid, pyramid->levels[i], pyramid->levels[i - 1]);
|
||||
}
|
||||
}
|
||||
|
||||
return pyramid;
|
||||
}
|
||||
|
||||
void
|
||||
lpyramid_destroy (lpyramid_t *pyramid)
|
||||
{
|
||||
int i;
|
||||
|
||||
for (i=0; i<MAX_PYR_LEVELS; i++)
|
||||
free (pyramid->levels[i]);
|
||||
}
|
||||
|
||||
float
|
||||
lpyramid_get_value (lpyramid_t *pyramid, int x, int y, int level)
|
||||
{
|
||||
int index = x + y * pyramid->width;
|
||||
int l = level;
|
||||
if (l > MAX_PYR_LEVELS)
|
||||
l = MAX_PYR_LEVELS;
|
||||
return pyramid->levels[level][index];
|
||||
}
|
||||
|
|
@ -18,21 +18,15 @@
|
|||
|
||||
#define MAX_PYR_LEVELS 8
|
||||
|
||||
class LPyramid
|
||||
{
|
||||
public:
|
||||
LPyramid(float *image, int width, int height);
|
||||
virtual ~LPyramid();
|
||||
float Get_Value(int x, int y, int level);
|
||||
protected:
|
||||
float *Copy(float *img);
|
||||
void Convolve(float *a, float *b);
|
||||
typedef struct _lpyramid lpyramid_t;
|
||||
|
||||
/* Succesively blurred versions of the original image */
|
||||
float *Levels[MAX_PYR_LEVELS];
|
||||
lpyramid_t *
|
||||
lpyramid_create (float *image, int width, int height);
|
||||
|
||||
int Width;
|
||||
int Height;
|
||||
};
|
||||
void
|
||||
lpyramid_destroy (lpyramid_t *pyramid);
|
||||
|
||||
float
|
||||
lpyramid_get_value (lpyramid_t *pyramid, int x, int y, int level);
|
||||
|
||||
#endif /* _LPYRAMID_H */
|
||||
Loading…
Add table
Reference in a new issue