Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

4 роки тому
4 роки тому
4 роки тому
4 роки тому
4 роки тому
4 роки тому
4 роки тому
4 роки тому
4 роки тому
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  1. /*
  2. * This file is part of Aptdec.
  3. * Copyright (c) 2004-2009 Thierry Leconte (F4DWV), Xerbo (xerbo@protonmail.com) 2019-2020
  4. *
  5. * Aptdec 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. */
  19. #include <png.h>
  20. #include <stdlib.h>
  21. #include <stdio.h>
  22. #include <string.h>
  23. #include <stdint.h>
  24. #include "offsets.h"
  25. #include "messages.h"
  26. typedef struct {
  27. float r, g, b;
  28. } rgb_t;
  29. extern int zenith;
  30. extern char PrecipPalette[256*3];
  31. extern rgb_t applyPalette(char *palette, int val);
  32. extern rgb_t RGBcomposite(rgb_t top, float top_a, rgb_t bottom, float bottom_a);
  33. int mapOverlay(char *filename, rgb_t **crow, int nrow, int zenith, int MCIR) {
  34. FILE *fp = fopen(filename, "rb");
  35. // Create reader
  36. png_structp png = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
  37. if(!png) return 0;
  38. png_infop info = png_create_info_struct(png);
  39. if(!info) return 0;
  40. png_init_io(png, fp);
  41. // Read info from header
  42. png_read_info(png, info);
  43. int width = png_get_image_width(png, info);
  44. int height = png_get_image_height(png, info);
  45. png_byte color_type = png_get_color_type(png, info);
  46. png_byte bit_depth = png_get_bit_depth(png, info);
  47. // Check the image
  48. if(width != 1040){
  49. fprintf(stderr, "Map must be 1040px wide.\n");
  50. return 0;
  51. }else if(bit_depth != 16){
  52. fprintf(stderr, "Map must be 16 bit color.\n");
  53. return 0;
  54. }else if(color_type != PNG_COLOR_TYPE_RGB){
  55. fprintf(stderr, "Map must be RGB.\n");
  56. return 0;
  57. }else if(zenith > height/2 || nrow-zenith > height/2){
  58. fprintf(stderr, "WARNING: Map is too short to cover entire image\n");
  59. }
  60. // Create row buffers
  61. png_bytep *mapRows = NULL;
  62. mapRows = (png_bytep *) malloc(sizeof(png_bytep) * height);
  63. for(int y = 0; y < height; y++)
  64. mapRows[y] = (png_byte *) malloc(png_get_rowbytes(png, info));
  65. // Read image
  66. png_read_image(png, mapRows);
  67. // Tidy up
  68. fclose(fp);
  69. png_destroy_read_struct(&png, &info, NULL);
  70. // Map overlay / MCIR / Precipitation
  71. int mapOffset = (height/2)-zenith;
  72. for(int y = 0; y < nrow; y++) {
  73. for(int x = 49; x < width - 82; x++){
  74. // Maps are 16 bit / channel
  75. png_bytep px = &mapRows[CLIP(y + mapOffset, 0, height)][x * 6];
  76. rgb_t map = {
  77. (px[0] << 8) | px[1],
  78. (px[2] << 8) | px[3],
  79. (px[4] << 8) | px[5]
  80. };
  81. // Pixel offsets
  82. int chb = x + CHB_OFFSET - 49;
  83. int cha = x + 36;
  84. // Fill in map
  85. if(MCIR){
  86. if(map.b < 128 && map.g > 128){
  87. // Land
  88. float green = CLIP((map.g-256)/32.0, 0, 1);
  89. float blue = 1-CLIP((map.b-32)/64.0, 0, 1);
  90. crow[y][cha] = (rgb_t){50 + blue*50, 80 + green*70, 64};
  91. }else{
  92. // Sea
  93. crow[y][cha] = (rgb_t){12, 30, 85};
  94. }
  95. }
  96. // Color -> alpha: composite
  97. int composite = map.r + map.g + map.b;
  98. // Color -> alpha: flattern and convert to 8 bits / channel
  99. float factor = (255 * 255 * 2.0) / composite;
  100. map.r *= factor/257.0; map.g *= factor/257.0; map.b *= factor/257.0;
  101. // Color -> alpha: convert black to alpha
  102. float alpha = CLIP(composite / 65535.0, 0, 1);
  103. // Map overlay on channel A
  104. crow[y][cha] = RGBcomposite(map, alpha, crow[y][cha], 1);
  105. // Map overlay on channel B
  106. if(!MCIR)
  107. crow[y][chb] = RGBcomposite(map, alpha, crow[y][chb], 1);
  108. // Cloud overlay on channel A
  109. if(MCIR){
  110. float cloud = CLIP((crow[y][chb].r - 115) / 107, 0, 1);
  111. crow[y][cha] = RGBcomposite((rgb_t){240, 250, 255}, cloud, crow[y][cha], 1);
  112. }
  113. }
  114. }
  115. return 1;
  116. }
  117. int readRawImage(char *filename, float **prow, int *nrow) {
  118. FILE *fp = fopen(filename, "r");
  119. // Create reader
  120. png_structp png = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
  121. if(!png) return 0;
  122. png_infop info = png_create_info_struct(png);
  123. if(!info) return 0;
  124. png_init_io(png, fp);
  125. // Read info from header
  126. png_read_info(png, info);
  127. int width = png_get_image_width(png, info);
  128. int height = png_get_image_height(png, info);
  129. png_byte color_type = png_get_color_type(png, info);
  130. png_byte bit_depth = png_get_bit_depth(png, info);
  131. // Check the image
  132. if(width != IMG_WIDTH){
  133. fprintf(stderr, "Raw image must be %ipx wide.\n", IMG_WIDTH);
  134. return 0;
  135. }else if(bit_depth != 8){
  136. fprintf(stderr, "Raw image must have 8 bit color.\n");
  137. return 0;
  138. }else if(color_type != PNG_COLOR_TYPE_GRAY){
  139. fprintf(stderr, "Raw image must be grayscale.\n");
  140. return 0;
  141. }
  142. // Create row buffers
  143. png_bytep *PNGrows = NULL;
  144. PNGrows = (png_bytep *) malloc(sizeof(png_bytep) * height);
  145. for(int y = 0; y < height; y++) PNGrows[y] = (png_byte *)
  146. malloc(png_get_rowbytes(png, info));
  147. // Read image
  148. png_read_image(png, PNGrows);
  149. // Tidy up
  150. fclose(fp);
  151. png_destroy_read_struct(&png, &info, NULL);
  152. // Put into prow
  153. *nrow = height;
  154. for(int y = 0; y < height; y++) {
  155. prow[y] = (float *) malloc(sizeof(float) * width);
  156. for(int x = 0; x < width; x++)
  157. prow[y][x] = (float)PNGrows[y][x];
  158. }
  159. return 1;
  160. }
  161. typedef struct {
  162. float *prow[3000]; // Row buffers
  163. int nrow; // Number of rows
  164. int chA, chB; // ID of each channel
  165. char name[256]; // Stripped filename
  166. } image_t;
  167. typedef struct {
  168. char *type; // Output image type
  169. char *effects;
  170. int satnum; // The satellite number
  171. char *map; // Path to a map file
  172. char *path; // Output directory
  173. } options_t;
  174. //int ImageOut(char *filename, char *chid, float **prow, int nrow, int width, int offset, char *palette, char *effects, char *mapFile) {
  175. int ImageOut(options_t *opts, image_t *img, int offset, int width, char *desc, char *chid, char *palette){
  176. char outName[384];
  177. sprintf(outName, "%s/%s-%s.png", opts->path, img->name, chid);
  178. FILE *pngfile;
  179. png_text text_ptr[] = {
  180. {PNG_TEXT_COMPRESSION_NONE, "Software", VERSION},
  181. {PNG_TEXT_COMPRESSION_NONE, "Channel", desc, sizeof(desc)},
  182. {PNG_TEXT_COMPRESSION_NONE, "Description", "NOAA satellite image", 20}
  183. };
  184. // Reduce the width of the image to componsate for the missing telemetry
  185. int skiptele = 0;
  186. if(opts->effects != NULL && CONTAINS(opts->effects, 't')){
  187. width -= TOTAL_TELE;
  188. skiptele = 1;
  189. }
  190. // Create writer
  191. png_structp png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
  192. if (!png_ptr) {
  193. png_destroy_write_struct(&png_ptr, (png_infopp) NULL);
  194. fprintf(stderr, ERR_PNG_WRITE);
  195. return(0);
  196. }
  197. png_infop info_ptr = png_create_info_struct(png_ptr);
  198. if (!info_ptr) {
  199. png_destroy_write_struct(&png_ptr, (png_infopp) NULL);
  200. fprintf(stderr, ERR_PNG_INFO);
  201. return(0);
  202. }
  203. // 8 bit RGB image
  204. png_set_IHDR(png_ptr, info_ptr, width, img->nrow,
  205. 8, PNG_COLOR_TYPE_RGB, PNG_INTERLACE_NONE,
  206. PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);
  207. png_set_text(png_ptr, info_ptr, text_ptr, 3);
  208. // Channel = 25cm wide
  209. png_set_pHYs(png_ptr, info_ptr, 3636, 3636, PNG_RESOLUTION_METER);
  210. // Init I/O
  211. pngfile = fopen(outName, "wb");
  212. if (!pngfile) {
  213. fprintf(stderr, ERR_FILE_WRITE, outName);
  214. return(1);
  215. }
  216. png_init_io(png_ptr, pngfile);
  217. png_write_info(png_ptr, info_ptr);
  218. // Move prow into crow, crow ~ color rows
  219. rgb_t *crow[img->nrow];
  220. for(int y = 0; y < img->nrow; y++){
  221. crow[y] = (rgb_t *) malloc(sizeof(rgb_t) * IMG_WIDTH);
  222. for(int x = 0; x < IMG_WIDTH; x++){
  223. if(palette == NULL){
  224. crow[y][x].r = crow[y][x].g = crow[y][x].b = CLIP(img->prow[y][x], 0, 255);
  225. }else{
  226. crow[y][x] = applyPalette(palette, img->prow[y][x]);
  227. }
  228. }
  229. }
  230. // Precipitation
  231. // TODO: use temperature calibration for accuracy
  232. if(CONTAINS(opts->effects, 'p')){
  233. for(int y = 0; y < img->nrow; y++){
  234. for(int x = 0; x < CH_WIDTH; x++){
  235. if(img->prow[y][x + CHB_OFFSET] > 191)
  236. crow[y][x + CHB_OFFSET] = applyPalette(PrecipPalette, img->prow[y][x + CHB_OFFSET]);
  237. }
  238. }
  239. }
  240. if(opts->map != NULL && opts->map[0] != '\0'){
  241. if(mapOverlay(opts->map, crow, img->nrow, zenith, strcmp(chid, "MCIR") == 0) == 0){
  242. fprintf(stderr, "Skipping MCIR generation; see above.\n");
  243. return 0;
  244. }
  245. }else if(strcmp(chid, "MCIR") == 0){
  246. fprintf(stderr, "Skipping MCIR generation; no map provided.\n");
  247. return 0;
  248. }
  249. printf("Writing %s", outName);
  250. extern rgb_t falsecolor(float vis, float temp);
  251. int fcimage = strcmp(desc, "False Color") == 0;
  252. // Build RGB image
  253. int skip;
  254. for (int y = 0; y < img->nrow; y++) {
  255. png_color pix[width];
  256. for (int x = 0, skip = 0; x < width; x++) {
  257. if(skiptele){
  258. switch (x) {
  259. case 0:
  260. skip += SYNC_WIDTH + SPC_WIDTH;
  261. break;
  262. case CH_WIDTH:
  263. skip += TELE_WIDTH + SYNC_WIDTH + SPC_WIDTH;
  264. break;
  265. case CH_WIDTH*2:
  266. skip += TELE_WIDTH;
  267. break;
  268. }
  269. }
  270. if(fcimage){
  271. rgb_t pixel = falsecolor(img->prow[y][x + CHA_OFFSET], img->prow[y][x + CHB_OFFSET]);
  272. pix[x].red = pixel.r;
  273. pix[x].green = pixel.g;
  274. pix[x].blue = pixel.b;
  275. }else{
  276. pix[x].red = (int)crow[y][x + skip + offset].r;
  277. pix[x].green = (int)crow[y][x + skip + offset].g;
  278. pix[x].blue = (int)crow[y][x + skip + offset].b;
  279. }
  280. }
  281. png_write_row(png_ptr, (png_bytep) pix);
  282. }
  283. // Tidy up
  284. png_write_end(png_ptr, info_ptr);
  285. fclose(pngfile);
  286. printf("\nDone\n");
  287. png_destroy_write_struct(&png_ptr, &info_ptr);
  288. return(1);
  289. }