You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

measure.c 2.5 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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. * measure.c: distance functions
  16. */
  17. #include "config.h"
  18. #include <math.h>
  19. #include "pipi.h"
  20. #include "pipi_internals.h"
  21. double pipi_measure_rmsd(pipi_image_t *i1, pipi_image_t *i2)
  22. {
  23. return sqrt(pipi_measure_msd(i1, i2));
  24. }
  25. double pipi_measure_msd(pipi_image_t *i1, pipi_image_t *i2)
  26. {
  27. pipi_format_t f1, f2;
  28. double ret = 0.0;
  29. float *p1, *p2;
  30. int x, y, w, h, gray;
  31. w = i1->w < i2->w ? i1->w : i2->w;
  32. h = i1->h < i2->h ? i1->h : i2->h;
  33. f1 = i1->last_modified;
  34. f2 = i2->last_modified;
  35. gray = f1 == PIPI_PIXELS_Y_F32 && f2 == PIPI_PIXELS_Y_F32;
  36. /* FIXME: this is not right */
  37. if(gray)
  38. {
  39. p1 = (float *)i1->p[PIPI_PIXELS_Y_F32].pixels;
  40. p2 = (float *)i2->p[PIPI_PIXELS_Y_F32].pixels;
  41. }
  42. else
  43. {
  44. pipi_get_pixels(i1, PIPI_PIXELS_RGBA_F32);
  45. pipi_get_pixels(i2, PIPI_PIXELS_RGBA_F32);
  46. p1 = (float *)i1->p[PIPI_PIXELS_RGBA_F32].pixels;
  47. p2 = (float *)i2->p[PIPI_PIXELS_RGBA_F32].pixels;
  48. }
  49. if(gray)
  50. {
  51. for(y = 0; y < h; y++)
  52. for(x = 0; x < w; x++)
  53. {
  54. float a = p1[y * i1->w + x];
  55. float b = p2[y * i2->w + x];
  56. ret += (a - b) * (a - b);
  57. }
  58. }
  59. else
  60. {
  61. for(y = 0; y < h; y++)
  62. for(x = 0; x < w; x++)
  63. {
  64. float a, b, sum = 0.0;
  65. a = p1[(y * i1->w + x) * 4];
  66. b = p2[(y * i2->w + x) * 4];
  67. sum += (a - b) * (a - b);
  68. a = p1[(y * i1->w + x) * 4 + 1];
  69. b = p2[(y * i2->w + x) * 4 + 1];
  70. sum += (a - b) * (a - b);
  71. a = p1[(y * i1->w + x) * 4 + 2];
  72. b = p2[(y * i2->w + x) * 4 + 2];
  73. sum += (a - b) * (a - b);
  74. ret += sum / 3;
  75. }
  76. }
  77. /* TODO: free pixels if they were allocated */
  78. /* Restore original image formats */
  79. i1->last_modified = f1;
  80. i2->last_modified = f2;
  81. return ret / (w * h);
  82. }