Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

effects.c 5.5 KiB

1 rok temu
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. /*
  2. * aptdec - A lightweight FOSS (NOAA) APT decoder
  3. * Copyright (C) 2019-2023 Xerbo (xerbo@protonmail.com)
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  17. */
  18. #include <aptdec.h>
  19. #include <string.h>
  20. #include <stdio.h>
  21. #include <math.h>
  22. #include <stdlib.h>
  23. #include "algebra.h"
  24. #include "util.h"
  25. #include "filter.h"
  26. void aptdec_equalize(aptdec_image_t *img, aptdec_region_t region) {
  27. // Plot histogram
  28. size_t histogram[256] = {0};
  29. for (size_t y = 0; y < img->rows; y++) {
  30. for (size_t x = 0; x < region.width; x++) {
  31. histogram[img->data[y * APTDEC_IMG_WIDTH + x + region.offset]]++;
  32. }
  33. }
  34. // Calculate cumulative frequency
  35. size_t sum = 0, cf[256] = {0};
  36. for (int i = 0; i < 256; i++) {
  37. sum += histogram[i];
  38. cf[i] = sum;
  39. }
  40. // Apply histogram
  41. int area = img->rows * region.width;
  42. for (size_t y = 0; y < img->rows; y++) {
  43. for (size_t x = 0; x < region.width; x++) {
  44. int k = (int)img->data[y * APTDEC_IMG_WIDTH + x + region.offset];
  45. img->data[y * APTDEC_IMG_WIDTH + x + region.offset] = (255.0f / area) * cf[k];
  46. }
  47. }
  48. }
  49. // Brightness calibrate, including telemetry
  50. static void image_apply_linear(uint8_t *data, int rows, int offset, int width, linear_t regr) {
  51. for (int y = 0; y < rows; y++) {
  52. for (int x = 0; x < width; x++) {
  53. float pv = linear_calc(data[y * APTDEC_IMG_WIDTH + x + offset], regr);
  54. data[y * APTDEC_IMG_WIDTH + x + offset] = clamp_int(roundf(pv), 0, 255);
  55. }
  56. }
  57. }
  58. void aptdec_stretch(aptdec_image_t *img, aptdec_region_t region) {
  59. // Plot histogram
  60. size_t histogram[256] = { 0 };
  61. for (size_t y = 0; y < img->rows; y++) {
  62. for (size_t x = 0; x < region.width; x++) {
  63. histogram[img->data[y*APTDEC_IMG_WIDTH + x + region.offset]]++;
  64. }
  65. }
  66. // Calculate cumulative frequency
  67. size_t sum = 0;
  68. size_t cf[256] = { 0 };
  69. for (size_t i = 0; i < 256; i++) {
  70. sum += histogram[i];
  71. cf[i] = sum;
  72. }
  73. // Find min/max points (1st percentile)
  74. int min = -1, max = -1;
  75. for (size_t i = 0; i < 256; i++) {
  76. if ((float)cf[i] / (float)sum < 0.01f && min == -1) {
  77. min = i;
  78. }
  79. if ((float)cf[i] / (float)sum > 0.99f && max == -1) {
  80. max = i;
  81. break;
  82. }
  83. }
  84. float a = 255.0f / (max - min);
  85. float b = a * -min;
  86. image_apply_linear(img->data, img->rows, region.offset, region.width, (linear_t){a, b});
  87. }
  88. // Median denoise (with deviation threshold)
  89. void aptdec_denoise(aptdec_image_t *img, aptdec_region_t region) {
  90. for (size_t y = 1; y < img->rows - 1; y++) {
  91. for (size_t x = 1; x < region.width - 1; x++) {
  92. float pixels[9] = { 0.0f };
  93. int pixeln = 0;
  94. for (int y2 = -1; y2 < 2; y2++) {
  95. for (int x2 = -1; x2 < 2; x2++) {
  96. pixels[pixeln++] = img->data[(y + y2) * APTDEC_IMG_WIDTH + (x + region.offset) + x2];
  97. }
  98. }
  99. if (standard_deviation(pixels, 9) > 15) {
  100. img->data[y * APTDEC_IMG_WIDTH + x + region.offset] = medianf(pixels, 9);
  101. }
  102. }
  103. }
  104. }
  105. // Flips a channel, for northbound passes
  106. void aptdec_flip(aptdec_image_t *img, aptdec_region_t region) {
  107. for (size_t y = 1; y < img->rows; y++) {
  108. for (int x = 1; x < ceil(region.width / 2.0f); x++) {
  109. // Flip top-left & bottom-right
  110. swap_uint8(
  111. &img->data[(img->rows - y) * APTDEC_IMG_WIDTH + region.offset + x],
  112. &img->data[y * APTDEC_IMG_WIDTH + region.offset + (region.width - x)]
  113. );
  114. }
  115. }
  116. }
  117. // Calculate crop to remove noise from the start and end of an image
  118. #define NOISE_THRESH 2600.0
  119. #include "filter.h"
  120. int aptdec_crop(aptdec_image_t *img) {
  121. const float sync_pattern[] = {-1, -1, -1, -1, 1, 1, -1, -1, 1, 1, -1, -1, 1, 1, -1, -1, 1, 1, -1, -1,
  122. 1, 1, -1, -1, 1, 1, -1, -1, 1, 1, -1, -1, -1, -1, -1, -1, -1, -1, 0};
  123. float *spc_rows = calloc(img->rows, sizeof(float));
  124. int startCrop = 0;
  125. int endCrop = img->rows;
  126. for (size_t y = 0; y < img->rows; y++) {
  127. float temp[39] = { 0.0f };
  128. for (size_t i = 0; i < 39; i++) {
  129. temp[i] = img->data[y * APTDEC_IMG_WIDTH + i];
  130. }
  131. spc_rows[y] = convolve(temp, &sync_pattern[0], 39);
  132. }
  133. // Find ends
  134. for (size_t y = 0; y < img->rows - 1; y++) {
  135. if (spc_rows[y] > NOISE_THRESH) {
  136. endCrop = y;
  137. }
  138. }
  139. for (size_t y = img->rows; y > 0; y--) {
  140. if (spc_rows[y] > NOISE_THRESH) {
  141. startCrop = y;
  142. }
  143. }
  144. printf("Crop rows: %i -> %i\n", startCrop, endCrop);
  145. // Ignore the noisy rows at the end
  146. img->rows = (endCrop - startCrop);
  147. // Remove the noisy rows at start
  148. memmove(img->data, &img->data[startCrop * APTDEC_IMG_WIDTH], img->rows * APTDEC_IMG_WIDTH * sizeof(float));
  149. free(spc_rows);
  150. return startCrop;
  151. }