source: libpipi/trunk/pipi/analysis/measure.c @ 3517

Last change on this file since 3517 was 3517, checked in by Sam Hocevar, 11 years ago

pipi_measure_msd(): fix this function so that it works with colour images, too.

File size: 2.5 KB
Line 
1/*
2 *  libpipi       Pathetic image processing interface library
3 *  Copyright (c) 2004-2008 Sam Hocevar <sam@zoy.org>
4 *                All Rights Reserved
5 *
6 *  $Id$
7 *
8 *  This library is free software. It comes without any warranty, to
9 *  the extent permitted by applicable law. You can redistribute it
10 *  and/or modify it under the terms of the Do What The Fuck You Want
11 *  To Public License, Version 2, as published by Sam Hocevar. See
12 *  http://sam.zoy.org/wtfpl/COPYING for more details.
13 */
14
15/*
16 * measure.c: distance functions
17 */
18
19#include "config.h"
20
21#include <math.h>
22
23#include "pipi.h"
24#include "pipi_internals.h"
25
26double pipi_measure_rmsd(pipi_image_t *i1, pipi_image_t *i2)
27{
28    return sqrt(pipi_measure_msd(i1, i2));
29}
30
31double pipi_measure_msd(pipi_image_t *i1, pipi_image_t *i2)
32{
33    pipi_format_t f1, f2;
34    double ret = 0.0;
35    float *p1, *p2;
36    int x, y, w, h, gray;
37
38    w = i1->w < i2->w ? i1->w : i2->w;
39    h = i1->h < i2->h ? i1->h : i2->h;
40
41    f1 = i1->last_modified;
42    f2 = i2->last_modified;
43
44    gray = f1 == PIPI_PIXELS_Y_F32 && f2 == PIPI_PIXELS_Y_F32;
45
46    /* FIXME: this is not right */
47    if(gray)
48    {
49        p1 = (float *)i1->p[PIPI_PIXELS_Y_F32].pixels;
50        p2 = (float *)i2->p[PIPI_PIXELS_Y_F32].pixels;
51    }
52    else
53    {
54        pipi_get_pixels(i1, PIPI_PIXELS_RGBA_F32);
55        pipi_get_pixels(i2, PIPI_PIXELS_RGBA_F32);
56        p1 = (float *)i1->p[PIPI_PIXELS_RGBA_F32].pixels;
57        p2 = (float *)i2->p[PIPI_PIXELS_RGBA_F32].pixels;
58    }
59
60    if(gray)
61    {
62        for(y = 0; y < h; y++)
63            for(x = 0; x < w; x++)
64            {
65                float a = p1[y * i1->w + x];
66                float b = p2[y * i2->w + x];
67                ret += (a - b) * (a - b);
68            }
69    }
70    else
71    {
72        for(y = 0; y < h; y++)
73            for(x = 0; x < w; x++)
74            {
75                float a, b, sum = 0.0;
76
77                a = p1[(y * i1->w + x) * 4];
78                b = p2[(y * i2->w + x) * 4];
79                sum += (a - b) * (a - b);
80
81                a = p1[(y * i1->w + x) * 4 + 1];
82                b = p2[(y * i2->w + x) * 4 + 1];
83                sum += (a - b) * (a - b);
84
85                a = p1[(y * i1->w + x) * 4 + 2];
86                b = p2[(y * i2->w + x) * 4 + 2];
87                sum += (a - b) * (a - b);
88
89                ret += sum / 3;
90            }
91    }
92
93    /* TODO: free pixels if they were allocated */
94
95    /* Restore original image formats */
96    i1->last_modified = f1;
97    i2->last_modified = f2;
98
99    return ret / (w * h);
100}
101
Note: See TracBrowser for help on using the repository browser.