Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.
 
 
 
 
 

314 Zeilen
8.9 KiB

  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. // Precipitation
  114. if(crow[y][chb].r > 191){
  115. float ramp = CLIP((crow[y][chb].r - 191) / 5.0, 0, 1);
  116. crow[y][cha] = RGBcomposite(applyPalette(PrecipPalette, crow[y][chb].r), ramp, crow[y][cha], 1);
  117. }
  118. }
  119. }
  120. return 1;
  121. }
  122. int readRawImage(char *filename, float **prow, int *nrow) {
  123. FILE *fp = fopen(filename, "r");
  124. // Create reader
  125. png_structp png = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
  126. if(!png) return 0;
  127. png_infop info = png_create_info_struct(png);
  128. if(!info) return 0;
  129. png_init_io(png, fp);
  130. // Read info from header
  131. png_read_info(png, info);
  132. int width = png_get_image_width(png, info);
  133. int height = png_get_image_height(png, info);
  134. png_byte color_type = png_get_color_type(png, info);
  135. png_byte bit_depth = png_get_bit_depth(png, info);
  136. // Check the image
  137. if(width != IMG_WIDTH){
  138. fprintf(stderr, "Raw image must be %ipx wide.\n", IMG_WIDTH);
  139. return 0;
  140. }else if(bit_depth != 8){
  141. fprintf(stderr, "Raw image must have 8 bit color.\n");
  142. return 0;
  143. }else if(color_type != PNG_COLOR_TYPE_GRAY){
  144. fprintf(stderr, "Raw image must be grayscale.\n");
  145. return 0;
  146. }
  147. // Create row buffers
  148. png_bytep *PNGrows = NULL;
  149. PNGrows = (png_bytep *) malloc(sizeof(png_bytep) * height);
  150. for(int y = 0; y < height; y++) PNGrows[y] = (png_byte *)
  151. malloc(png_get_rowbytes(png, info));
  152. // Read image
  153. png_read_image(png, PNGrows);
  154. // Tidy up
  155. fclose(fp);
  156. png_destroy_read_struct(&png, &info, NULL);
  157. // Put into prow
  158. *nrow = height;
  159. for(int y = 0; y < height; y++) {
  160. prow[y] = (float *) malloc(sizeof(float) * width);
  161. for(int x = 0; x < width; x++)
  162. prow[y][x] = (float)PNGrows[y][x];
  163. }
  164. return 1;
  165. }
  166. typedef struct {
  167. float *prow[3000]; // Row buffers
  168. int nrow; // Number of rows
  169. int chA, chB; // ID of each channel
  170. char name[256]; // Stripped filename
  171. } image_t;
  172. typedef struct {
  173. char *type; // Output image type
  174. char *effects;
  175. int satnum; // The satellite number
  176. char *map; // Path to a map file
  177. char *path; // Output directory
  178. } options_t;
  179. //int ImageOut(char *filename, char *chid, float **prow, int nrow, int width, int offset, char *palette, char *effects, char *mapFile) {
  180. int ImageOut(options_t *opts, image_t *img, int offset, int width, char *desc, char *chid, char *palette){
  181. char outName[384];
  182. sprintf(outName, "%s/%s-%s.png", opts->path, img->name, chid);
  183. FILE *pngfile;
  184. png_text text_ptr[] = {
  185. {PNG_TEXT_COMPRESSION_NONE, "Software", VERSION},
  186. {PNG_TEXT_COMPRESSION_NONE, "Channel", desc, sizeof(desc)},
  187. {PNG_TEXT_COMPRESSION_NONE, "Description", "NOAA satellite image", 20}
  188. };
  189. // Reduce the width of the image to componsate for the missing telemetry
  190. if(opts->effects != NULL && CONTAINS(opts->effects, 't'))
  191. width -= TOTAL_TELE;
  192. // Create writer
  193. png_structp png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
  194. if (!png_ptr) {
  195. png_destroy_write_struct(&png_ptr, (png_infopp) NULL);
  196. fprintf(stderr, ERR_PNG_WRITE);
  197. return(0);
  198. }
  199. png_infop info_ptr = png_create_info_struct(png_ptr);
  200. if (!info_ptr) {
  201. png_destroy_write_struct(&png_ptr, (png_infopp) NULL);
  202. fprintf(stderr, ERR_PNG_INFO);
  203. return(0);
  204. }
  205. // 8 bit RGB image
  206. png_set_IHDR(png_ptr, info_ptr, width, img->nrow,
  207. 8, PNG_COLOR_TYPE_RGB, PNG_INTERLACE_NONE,
  208. PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);
  209. png_set_text(png_ptr, info_ptr, text_ptr, 3);
  210. // Channel = 25cm wide
  211. png_set_pHYs(png_ptr, info_ptr, 3636, 3636, PNG_RESOLUTION_METER);
  212. // Init I/O
  213. pngfile = fopen(outName, "wb");
  214. if (!pngfile) {
  215. fprintf(stderr, ERR_FILE_WRITE, outName);
  216. return(1);
  217. }
  218. png_init_io(png_ptr, pngfile);
  219. png_write_info(png_ptr, info_ptr);
  220. // Move prow into crow, crow ~ color rows
  221. rgb_t *crow[img->nrow];
  222. for(int y = 0; y < img->nrow; y++){
  223. crow[y] = (rgb_t *) malloc(sizeof(rgb_t) * IMG_WIDTH);
  224. for(int x = 0; x < IMG_WIDTH; x++){
  225. if(palette == NULL){
  226. crow[y][x].r = crow[y][x].g = crow[y][x].b = CLIP(img->prow[y][x], 0, 255);
  227. }else{
  228. crow[y][x] = applyPalette(palette, img->prow[y][x]);
  229. }
  230. }
  231. }
  232. if(opts->map != NULL && opts->map[0] != '\0'){
  233. if(mapOverlay(opts->map, crow, img->nrow, zenith, strcmp(chid, "MCIR") == 0) == 0){
  234. fprintf(stderr, "Skipping MCIR generation; see above.\n");
  235. return 0;
  236. }
  237. }else if(strcmp(chid, "MCIR") == 0){
  238. fprintf(stderr, "Skipping MCIR generation; no map provided.\n");
  239. return 0;
  240. }
  241. //extern void falsecolor(float vis, float temp, float *r, float *g, float *b);
  242. printf("Writing %s", outName);
  243. int fcimage = strcmp(chid, "False Color") == 0;
  244. // Build RGB image
  245. for (int y = 0; y < img->nrow; y++) {
  246. png_color pix[width];
  247. for (int x = 0; x < width; x++) {
  248. pix[x].red = (int)crow[y][x + offset].r;
  249. pix[x].green = (int)crow[y][x + offset].g;
  250. pix[x].blue = (int)crow[y][x + offset].b;
  251. }
  252. png_write_row(png_ptr, (png_bytep) pix);
  253. }
  254. // Tidy up
  255. png_write_end(png_ptr, info_ptr);
  256. fclose(pngfile);
  257. printf("\nDone\n");
  258. png_destroy_write_struct(&png_ptr, &info_ptr);
  259. return(1);
  260. }