aaa.c 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. #include <stdio.h>
  2. #include "libmegapixels.h"
  3. float
  4. clamp_float(float value, float min, float max)
  5. {
  6. if (value > max)
  7. return max;
  8. if (value < min)
  9. return min;
  10. return value;
  11. }
  12. void
  13. libmegapixels_aaa_init(libmegapixels_aaa_stats *stats)
  14. {
  15. float identity[] = {1.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 1.0f};
  16. libmegapixels_aaa_set_matrix(stats, identity, identity);
  17. }
  18. void
  19. libmegapixels_aaa_set_matrix(libmegapixels_aaa_stats *stats, const float matrix1[9], const float matrix2[9])
  20. {
  21. for (size_t i = 0; i < 9; i++) {
  22. stats->matrix1[i] = matrix1[i];
  23. stats->matrix2[i] = matrix2[i];
  24. }
  25. }
  26. void
  27. libmegapixels_aaa_software_statistics(libmegapixels_aaa_stats *stats, const unsigned int *frame, const int width,
  28. const int height)
  29. {
  30. unsigned int bright = 0;
  31. unsigned int too_bright = 0;
  32. unsigned int too_dark = 0;
  33. unsigned int total = width * height;
  34. unsigned long long sum_r = 0, sum_g = 0, sum_b = 0;
  35. for (ssize_t p = 0; p < width * height; p++) {
  36. unsigned int r = (frame[p] >> 0) & 0xff;
  37. unsigned int g = (frame[p] >> 8) & 0xff;
  38. unsigned int b = (frame[p] >> 16) & 0xff;
  39. unsigned int y = (r + g + b) / 3;
  40. if (y > 220) {
  41. too_bright++;
  42. }
  43. if (y > 180) {
  44. bright++;
  45. }
  46. if (y < 2) {
  47. too_dark++;
  48. }
  49. // Whitebalance on the midrange pixels only
  50. if (y > 75 && y < 200) {
  51. sum_r += r;
  52. sum_g += g;
  53. sum_b += b;
  54. }
  55. }
  56. // Exposure calculations
  57. unsigned int p_bright = (bright * 100) / total;
  58. unsigned int p_too_bright = (too_bright * 100) / total;
  59. unsigned int p_dark = (too_dark * 100) / total;
  60. stats->exposure = 0;
  61. if (p_bright < 1) {
  62. stats->exposure = 1;
  63. }
  64. if (p_too_bright > 8) {
  65. stats->exposure = -1;
  66. }
  67. stats->blacklevel = 0;
  68. if (p_dark < 1) {
  69. stats->blacklevel = -1;
  70. }
  71. if (p_dark > 3) {
  72. stats->blacklevel = 1;
  73. }
  74. // Whitebalance calculations
  75. float r = (float) sum_r / total;
  76. float g = (float) sum_g / total;
  77. float b = (float) sum_b / total;
  78. stats->avg_r = (r * stats->matrix1[0]) + (g * stats->matrix1[1]) + (b * stats->matrix1[2]);
  79. stats->avg_g = (r * stats->matrix1[3]) + (g * stats->matrix1[4]) + (b * stats->matrix1[5]);
  80. stats->avg_b = (r * stats->matrix1[6]) + (g * stats->matrix1[7]) + (b * stats->matrix1[8]);
  81. }