25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

effects.c 5.4 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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 "algebra.h"
  23. #include "util.h"
  24. #include "filter.h"
  25. void apt_equalize(apt_image_t *img, apt_region_t region) {
  26. // Plot histogram
  27. size_t histogram[256] = {0};
  28. for (size_t y = 0; y < img->rows; y++) {
  29. for (size_t x = 0; x < region.width; x++) {
  30. histogram[img->data[y * APT_IMG_WIDTH + x + region.offset]]++;
  31. }
  32. }
  33. // Calculate cumulative frequency
  34. size_t sum = 0, cf[256] = {0};
  35. for (int i = 0; i < 256; i++) {
  36. sum += histogram[i];
  37. cf[i] = sum;
  38. }
  39. // Apply histogram
  40. int area = img->rows * region.width;
  41. for (size_t y = 0; y < img->rows; y++) {
  42. for (size_t x = 0; x < region.width; x++) {
  43. int k = (int)img->data[y * APT_IMG_WIDTH + x + region.offset];
  44. img->data[y * APT_IMG_WIDTH + x + region.offset] = (255.0f / area) * cf[k];
  45. }
  46. }
  47. }
  48. // Brightness calibrate, including telemetry
  49. static void image_apply_linear(uint8_t *data, int rows, int offset, int width, linear_t regr) {
  50. for (int y = 0; y < rows; y++) {
  51. for (int x = 0; x < width; x++) {
  52. float pv = linear_calc(data[y * APT_IMG_WIDTH + x + offset], regr);
  53. data[y * APT_IMG_WIDTH + x + offset] = clamp_int(roundf(pv), 0, 255);
  54. }
  55. }
  56. }
  57. void apt_stretch(apt_image_t *img, apt_region_t region) {
  58. // Plot histogram
  59. size_t histogram[256] = { 0 };
  60. for (size_t y = 0; y < img->rows; y++) {
  61. for (size_t x = 0; x < region.width; x++) {
  62. histogram[img->data[y*APT_IMG_WIDTH + x + region.offset]]++;
  63. }
  64. }
  65. // Calculate cumulative frequency
  66. size_t sum = 0;
  67. size_t cf[256] = { 0 };
  68. for (size_t i = 0; i < 256; i++) {
  69. sum += histogram[i];
  70. cf[i] = sum;
  71. }
  72. // Find min/max points (1st percentile)
  73. int min = -1, max = -1;
  74. for (size_t i = 0; i < 256; i++) {
  75. if ((float)cf[i] / (float)sum < 0.01f && min == -1) {
  76. min = i;
  77. }
  78. if ((float)cf[i] / (float)sum > 0.99f && max == -1) {
  79. max = i;
  80. break;
  81. }
  82. }
  83. float a = 255.0f / (max - min);
  84. float b = a * -min;
  85. image_apply_linear(img->data, img->rows, region.offset, region.width, (linear_t){a, b});
  86. }
  87. // Median denoise (with deviation threshold)
  88. void apt_denoise(apt_image_t *img, apt_region_t region) {
  89. for (size_t y = 1; y < img->rows - 1; y++) {
  90. for (size_t x = 1; x < region.width - 1; x++) {
  91. float pixels[9];
  92. int pixeln = 0;
  93. for (int y2 = -1; y2 < 2; y2++) {
  94. for (int x2 = -1; x2 < 2; x2++) {
  95. pixels[pixeln++] = img->data[(y + y2) * APT_IMG_WIDTH + (x + region.offset) + x2];
  96. }
  97. }
  98. if (standard_deviation(pixels, 9) > 15) {
  99. img->data[y * APT_IMG_WIDTH + x + region.offset] = medianf(pixels, 9);
  100. }
  101. }
  102. }
  103. }
  104. // Flips a channel, for northbound passes
  105. void apt_flip(apt_image_t *img, apt_region_t region) {
  106. for (size_t y = 1; y < img->rows; y++) {
  107. for (int x = 1; x < ceil(region.width / 2.0f); x++) {
  108. // Flip top-left & bottom-right
  109. swap_uint8(
  110. &img->data[(img->rows - y) * APT_IMG_WIDTH + region.offset + x],
  111. &img->data[y * APT_IMG_WIDTH + region.offset + (region.width - x)]
  112. );
  113. }
  114. }
  115. }
  116. // Calculate crop to remove noise from the start and end of an image
  117. #define NOISE_THRESH 2600.0
  118. #include "filter.h"
  119. int apt_crop(apt_image_t *img) {
  120. const float sync_pattern[] = {-1, -1, -1, -1, 1, 1, -1, -1, 1, 1, -1, -1, 1, 1, -1, -1, 1, 1, -1, -1,
  121. 1, 1, -1, -1, 1, 1, -1, -1, 1, 1, -1, -1, -1, -1, -1, -1, -1, -1, 0};
  122. float spc_rows[img->rows];
  123. int startCrop = 0;
  124. int endCrop = img->rows;
  125. for (size_t y = 0; y < img->rows; y++) {
  126. float temp[39];
  127. for (size_t i = 0; i < 39; i++) {
  128. temp[i] = img->data[y * APT_IMG_WIDTH + i];
  129. }
  130. spc_rows[y] = convolve(temp, &sync_pattern[0], 39);
  131. }
  132. // Find ends
  133. for (size_t y = 0; y < img->rows - 1; y++) {
  134. if (spc_rows[y] > NOISE_THRESH) {
  135. endCrop = y;
  136. }
  137. }
  138. for (size_t y = img->rows; y > 0; y--) {
  139. if (spc_rows[y] > NOISE_THRESH) {
  140. startCrop = y;
  141. }
  142. }
  143. printf("Crop rows: %i -> %i\n", startCrop, endCrop);
  144. // Ignore the noisy rows at the end
  145. img->rows = (endCrop - startCrop);
  146. // Remove the noisy rows at start
  147. memmove(img->data, &img->data[startCrop * APT_IMG_WIDTH], img->rows * APT_IMG_WIDTH * sizeof(float));
  148. return startCrop;
  149. }