Não pode escolher mais do que 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

há 4 anos
há 4 anos
há 4 anos
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  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 fcimage = strcmp(desc, "False Color") == 0;
  186. int skiptele = 0;
  187. if(opts->effects != NULL && CONTAINS(opts->effects, 't')){
  188. width -= TOTAL_TELE;
  189. skiptele = 1;
  190. }
  191. // Create writer
  192. png_structp png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
  193. if (!png_ptr) {
  194. png_destroy_write_struct(&png_ptr, (png_infopp) NULL);
  195. fprintf(stderr, ERR_PNG_WRITE);
  196. return(0);
  197. }
  198. png_infop info_ptr = png_create_info_struct(png_ptr);
  199. if (!info_ptr) {
  200. png_destroy_write_struct(&png_ptr, (png_infopp) NULL);
  201. fprintf(stderr, ERR_PNG_INFO);
  202. return(0);
  203. }
  204. int greyscale = 0;
  205. if(palette == NULL && !CONTAINS(opts->effects, 'p') && !fcimage && opts->map[0] == '\0' && strcmp(chid, "MCIR") != 0){
  206. greyscale = 1;
  207. // Greyscale image
  208. png_set_IHDR(png_ptr, info_ptr, width, img->nrow,
  209. 8, PNG_COLOR_TYPE_GRAY, PNG_INTERLACE_NONE,
  210. PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);
  211. }else{
  212. // 8 bit RGB image
  213. png_set_IHDR(png_ptr, info_ptr, width, img->nrow,
  214. 8, PNG_COLOR_TYPE_RGB, PNG_INTERLACE_NONE,
  215. PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);
  216. }
  217. png_set_text(png_ptr, info_ptr, text_ptr, 3);
  218. // Channel = 25cm wide
  219. png_set_pHYs(png_ptr, info_ptr, 3636, 3636, PNG_RESOLUTION_METER);
  220. // Init I/O
  221. pngfile = fopen(outName, "wb");
  222. if (!pngfile) {
  223. fprintf(stderr, ERR_FILE_WRITE, outName);
  224. return(1);
  225. }
  226. png_init_io(png_ptr, pngfile);
  227. png_write_info(png_ptr, info_ptr);
  228. // Move prow into crow, crow ~ color rows
  229. rgb_t *crow[img->nrow];
  230. for(int y = 0; y < img->nrow; y++){
  231. crow[y] = (rgb_t *) malloc(sizeof(rgb_t) * IMG_WIDTH);
  232. for(int x = 0; x < IMG_WIDTH; x++){
  233. if(palette == NULL){
  234. crow[y][x].r = crow[y][x].g = crow[y][x].b = CLIP(img->prow[y][x], 0, 255);
  235. }else{
  236. crow[y][x] = applyPalette(palette, img->prow[y][x]);
  237. }
  238. }
  239. }
  240. // Precipitation
  241. // TODO: use temperature calibration for accuracy
  242. if(CONTAINS(opts->effects, 'p')){
  243. for(int y = 0; y < img->nrow; y++){
  244. for(int x = 0; x < CH_WIDTH; x++){
  245. if(img->prow[y][x + CHB_OFFSET] > 191)
  246. crow[y][x + CHB_OFFSET] = applyPalette(PrecipPalette, img->prow[y][x + CHB_OFFSET]);
  247. }
  248. }
  249. }
  250. if(opts->map != NULL && opts->map[0] != '\0'){
  251. if(mapOverlay(opts->map, crow, img->nrow, zenith, strcmp(chid, "MCIR") == 0) == 0){
  252. fprintf(stderr, "Skipping MCIR generation; see above.\n");
  253. return 0;
  254. }
  255. }else if(strcmp(chid, "MCIR") == 0){
  256. fprintf(stderr, "Skipping MCIR generation; no map provided.\n");
  257. return 0;
  258. }
  259. printf("Writing %s", outName);
  260. extern rgb_t falsecolor(float vis, float temp);
  261. // Build image
  262. for (int y = 0; y < img->nrow; y++) {
  263. png_color pix[width];
  264. int skip = 0;
  265. for (int x = 0; x < width; x++) {
  266. if(skiptele){
  267. switch (x) {
  268. case 0:
  269. skip += SYNC_WIDTH + SPC_WIDTH;
  270. break;
  271. case CH_WIDTH:
  272. skip += TELE_WIDTH + SYNC_WIDTH + SPC_WIDTH;
  273. break;
  274. case CH_WIDTH*2:
  275. skip += TELE_WIDTH;
  276. break;
  277. }
  278. }
  279. if(greyscale){
  280. // Horrific but works
  281. if(x % 3 == 0){
  282. pix[x/3].red = img->prow[y][x + skip + offset ];
  283. pix[x/3].green = img->prow[y][x + skip + offset+1];
  284. pix[x/3].blue = img->prow[y][x + skip + offset+2];
  285. }
  286. }else if(fcimage){
  287. rgb_t pixel = falsecolor(img->prow[y][x + CHA_OFFSET], img->prow[y][x + CHB_OFFSET]);
  288. pix[x].red = pixel.r;
  289. pix[x].green = pixel.g;
  290. pix[x].blue = pixel.b;
  291. }else{
  292. pix[x].red = crow[y][x + skip + offset].r;
  293. pix[x].green = crow[y][x + skip + offset].g;
  294. pix[x].blue = crow[y][x + skip + offset].b;
  295. }
  296. }
  297. png_write_row(png_ptr, (png_bytep) pix);
  298. }
  299. // Tidy up
  300. png_write_end(png_ptr, info_ptr);
  301. fclose(pngfile);
  302. printf("\nDone\n");
  303. png_destroy_write_struct(&png_ptr, &info_ptr);
  304. return(1);
  305. }