You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

402 lines
11 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 "common.h"
  25. #include "offsets.h"
  26. extern int zenith;
  27. extern char PrecipPalette[256*3];
  28. extern rgb_t applyPalette(char *palette, int val);
  29. extern rgb_t RGBcomposite(rgb_t top, float top_a, rgb_t bottom, float bottom_a);
  30. int mapOverlay(char *filename, rgb_t **crow, int nrow, int zenith, int MCIR) {
  31. FILE *fp = fopen(filename, "rb");
  32. // Create reader
  33. png_structp png = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
  34. if(!png) return 0;
  35. png_infop info = png_create_info_struct(png);
  36. if(!info) return 0;
  37. png_init_io(png, fp);
  38. // Read info from header
  39. png_read_info(png, info);
  40. int width = png_get_image_width(png, info);
  41. int height = png_get_image_height(png, info);
  42. png_byte color_type = png_get_color_type(png, info);
  43. png_byte bit_depth = png_get_bit_depth(png, info);
  44. // Check the image
  45. if(width != 1040){
  46. fprintf(stderr, "Map must be 1040px wide.\n");
  47. return 0;
  48. }else if(bit_depth != 16){
  49. fprintf(stderr, "Map must be 16 bit color.\n");
  50. return 0;
  51. }else if(color_type != PNG_COLOR_TYPE_RGB){
  52. fprintf(stderr, "Map must be RGB.\n");
  53. return 0;
  54. }else if(zenith > height/2 || nrow-zenith > height/2){
  55. fprintf(stderr, "WARNING: Map is too short to cover entire image\n");
  56. }
  57. // Create row buffers
  58. png_bytep *mapRows = NULL;
  59. mapRows = (png_bytep *) malloc(sizeof(png_bytep) * height);
  60. for(int y = 0; y < height; y++)
  61. mapRows[y] = (png_byte *) malloc(png_get_rowbytes(png, info));
  62. // Read image
  63. png_read_image(png, mapRows);
  64. // Tidy up
  65. fclose(fp);
  66. png_destroy_read_struct(&png, &info, NULL);
  67. printf("Adding map overlay\n");
  68. // Map overlay / MCIR / Precipitation
  69. int mapOffset = (height/2)-zenith;
  70. for(int y = 0; y < nrow; y++) {
  71. for(int x = 49; x < width - 82; x++){
  72. // Maps are 16 bit / channel
  73. png_bytep px = &mapRows[CLIP(y + mapOffset, 0, height)][x * 6];
  74. rgb_t map = {
  75. (px[0] << 8) | px[1],
  76. (px[2] << 8) | px[3],
  77. (px[4] << 8) | px[5]
  78. };
  79. // Pixel offsets
  80. int chb = x + CHB_OFFSET - 49;
  81. int cha = x + 36;
  82. // Fill in map
  83. if(MCIR){
  84. if(map.b < 128 && map.g > 128){
  85. // Land
  86. float green = CLIP((map.g-256)/32.0, 0, 1);
  87. float blue = 1 - CLIP((map.b-32)/64.0, 0, 1);
  88. crow[y][cha] = (rgb_t){blue*127, 30+green*80, 40};
  89. }else{
  90. // Sea
  91. crow[y][cha] = (rgb_t){12, 30, 85};
  92. }
  93. }
  94. // Color -> alpha: composite
  95. int composite = map.r + map.g + map.b;
  96. // Color -> alpha: flattern and convert to 8 bits / channel
  97. float factor = (255 * 255 * 2.0) / composite;
  98. map.r *= factor/257.0; map.g *= factor/257.0; map.b *= factor/257.0;
  99. // Color -> alpha: convert black to alpha
  100. float alpha = CLIP(composite / 65535.0, 0, 1);
  101. // Map overlay on channel A
  102. crow[y][cha] = RGBcomposite(map, alpha, crow[y][cha], 1);
  103. // Map overlay on channel B
  104. if(!MCIR)
  105. crow[y][chb] = RGBcomposite(map, alpha, crow[y][chb], 1);
  106. // Cloud overlay on channel A
  107. if(MCIR){
  108. float cloud = CLIP((crow[y][chb].r - 115) / 107, 0, 1);
  109. crow[y][cha] = RGBcomposite((rgb_t){240, 250, 255}, cloud, crow[y][cha], 1);
  110. }
  111. }
  112. }
  113. return 1;
  114. }
  115. int readRawImage(char *filename, float **prow, int *nrow) {
  116. FILE *fp = fopen(filename, "r");
  117. // Create reader
  118. png_structp png = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
  119. if(!png) return 0;
  120. png_infop info = png_create_info_struct(png);
  121. if(!info) return 0;
  122. png_init_io(png, fp);
  123. // Read info from header
  124. png_read_info(png, info);
  125. int width = png_get_image_width(png, info);
  126. int height = png_get_image_height(png, info);
  127. png_byte color_type = png_get_color_type(png, info);
  128. png_byte bit_depth = png_get_bit_depth(png, info);
  129. // Check the image
  130. if(width != IMG_WIDTH){
  131. fprintf(stderr, "Raw image must be %ipx wide.\n", IMG_WIDTH);
  132. return 0;
  133. }else if(bit_depth != 8){
  134. fprintf(stderr, "Raw image must have 8 bit color.\n");
  135. return 0;
  136. }else if(color_type != PNG_COLOR_TYPE_GRAY){
  137. fprintf(stderr, "Raw image must be grayscale.\n");
  138. return 0;
  139. }
  140. // Create row buffers
  141. png_bytep *PNGrows = NULL;
  142. PNGrows = (png_bytep *) malloc(sizeof(png_bytep) * height);
  143. for(int y = 0; y < height; y++) PNGrows[y] = (png_byte *)
  144. malloc(png_get_rowbytes(png, info));
  145. // Read image
  146. png_read_image(png, PNGrows);
  147. // Tidy up
  148. fclose(fp);
  149. png_destroy_read_struct(&png, &info, NULL);
  150. // Put into prow
  151. *nrow = height;
  152. for(int y = 0; y < height; y++) {
  153. prow[y] = (float *) malloc(sizeof(float) * width);
  154. for(int x = 0; x < width; x++)
  155. prow[y][x] = (float)PNGrows[y][x];
  156. }
  157. return 1;
  158. }
  159. png_text meta[] = {
  160. {PNG_TEXT_COMPRESSION_NONE, "Software", VERSION},
  161. {PNG_TEXT_COMPRESSION_NONE, "Channel", "Unknown", 7},
  162. {PNG_TEXT_COMPRESSION_NONE, "Description", "NOAA satellite image", 20}
  163. };
  164. int ImageOut(options_t *opts, image_t *img, int offset, int width, char *desc, char *chid, char *palette){
  165. char outName[384];
  166. sprintf(outName, "%s/%s-%s.png", opts->path, img->name, chid);
  167. meta[1].text = desc;
  168. meta[1].text_length = sizeof(desc);
  169. FILE *pngfile;
  170. // Reduce the width of the image to componsate for the missing telemetry
  171. int fc = strcmp(desc, "False Color") == 0;
  172. int greyscale = 0;
  173. int skiptele = 0;
  174. if(opts->effects != NULL && CONTAINS(opts->effects, 't')){
  175. width -= TOTAL_TELE;
  176. skiptele = 1;
  177. }
  178. // Create writer
  179. png_structp png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
  180. if (!png_ptr) {
  181. png_destroy_write_struct(&png_ptr, (png_infopp) NULL);
  182. fprintf(stderr, "Could not create a PNG writer\n");
  183. return 0;
  184. }
  185. png_infop info_ptr = png_create_info_struct(png_ptr);
  186. if (!info_ptr) {
  187. png_destroy_write_struct(&png_ptr, (png_infopp) NULL);
  188. fprintf(stderr, "Could not create a PNG writer\n");
  189. return 0;
  190. }
  191. if(palette == NULL && !CONTAINS(opts->effects, 'p') && !fc && opts->map[0] == '\0' && strcmp(chid, "MCIR") != 0){
  192. greyscale = 1;
  193. // Greyscale image
  194. png_set_IHDR(png_ptr, info_ptr, width, img->nrow,
  195. 8, PNG_COLOR_TYPE_GRAY, PNG_INTERLACE_NONE,
  196. PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);
  197. }else{
  198. // 8 bit RGB image
  199. png_set_IHDR(png_ptr, info_ptr, width, img->nrow,
  200. 8, PNG_COLOR_TYPE_RGB, PNG_INTERLACE_NONE,
  201. PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);
  202. }
  203. png_set_text(png_ptr, info_ptr, meta, 3);
  204. png_set_pHYs(png_ptr, info_ptr, 3636, 3636, PNG_RESOLUTION_METER);
  205. // Init I/O
  206. pngfile = fopen(outName, "wb");
  207. if (!pngfile) {
  208. fprintf(stderr, "Could not open %s for writing\n", outName);
  209. return 1;
  210. }
  211. png_init_io(png_ptr, pngfile);
  212. png_write_info(png_ptr, info_ptr);
  213. // Move prow into crow, crow ~ color rows
  214. rgb_t *crow[img->nrow];
  215. if(!greyscale && !fc){
  216. for(int y = 0; y < img->nrow; y++){
  217. crow[y] = (rgb_t *) malloc(sizeof(rgb_t) * IMG_WIDTH);
  218. for(int x = 0; x < IMG_WIDTH; x++){
  219. if(palette == NULL)
  220. crow[y][x].r = crow[y][x].g = crow[y][x].b = img->prow[y][x];
  221. else
  222. crow[y][x] = applyPalette(palette, img->prow[y][x]);
  223. }
  224. }
  225. }
  226. // Precipitation
  227. // TODO: use temperature calibration
  228. if(CONTAINS(opts->effects, 'p')){
  229. for(int y = 0; y < img->nrow; y++){
  230. for(int x = 0; x < CH_WIDTH; x++){
  231. if(img->prow[y][x + CHB_OFFSET] > 191)
  232. crow[y][x + CHB_OFFSET] = applyPalette(PrecipPalette, img->prow[y][x + CHB_OFFSET]);
  233. }
  234. }
  235. }
  236. // Map stuff
  237. if(opts->map != NULL && opts->map[0] != '\0'){
  238. if(mapOverlay(opts->map, crow, img->nrow, zenith, strcmp(desc, "MCIR") == 0) == 0){
  239. fprintf(stderr, "Skipping MCIR generation; see above.\n");
  240. return 0;
  241. }
  242. }else if(strcmp(chid, "MCIR") == 0){
  243. fprintf(stderr, "Skipping MCIR generation; no map provided.\n");
  244. return 0;
  245. }
  246. printf("Writing %s", outName);
  247. // Build image
  248. for (int y = 0; y < img->nrow; y++) {
  249. png_color pix[width]; // Color
  250. png_byte mpix[width]; // Mono
  251. int skip = 0;
  252. for (int x = 0; x < width; x++) {
  253. if(skiptele){
  254. switch (x) {
  255. case 0:
  256. skip += SYNC_WIDTH + SPC_WIDTH;
  257. break;
  258. case CH_WIDTH:
  259. skip += TELE_WIDTH + SYNC_WIDTH + SPC_WIDTH;
  260. break;
  261. }
  262. }
  263. if(greyscale){
  264. mpix[x] = img->prow[y][x + skip + offset];
  265. }else if(fc){
  266. pix[x] = (png_color){
  267. CLIP(img->prow[y][x + CHA_OFFSET], 0, 255),
  268. CLIP(img->prow[y][x + CHA_OFFSET], 0, 255),
  269. CLIP(img->prow[y][x + CHB_OFFSET], 0, 255)
  270. };
  271. }else{
  272. pix[x] = (png_color){
  273. crow[y][x + skip + offset].r,
  274. crow[y][x + skip + offset].g,
  275. crow[y][x + skip + offset].b
  276. };
  277. }
  278. }
  279. if(greyscale){
  280. png_write_row(png_ptr, (png_bytep) mpix);
  281. }else{
  282. png_write_row(png_ptr, (png_bytep) pix);
  283. }
  284. }
  285. // Tidy up
  286. png_write_end(png_ptr, info_ptr);
  287. fclose(pngfile);
  288. printf("\nDone\n");
  289. png_destroy_write_struct(&png_ptr, &info_ptr);
  290. return 1;
  291. }
  292. // TODO: clean up everthing below this comment
  293. png_structp rt_png_ptr;
  294. png_infop rt_info_ptr;
  295. FILE *rt_pngfile;
  296. int initWriter(options_t *opts, image_t *img, int width, int height, char *desc, char *chid){
  297. char outName[384];
  298. sprintf(outName, "%s/%s-%s.png", opts->path, img->name, chid);
  299. meta[1].text = desc;
  300. meta[1].text_length = sizeof(desc);
  301. // Create writer
  302. rt_png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
  303. if (!rt_png_ptr) {
  304. png_destroy_write_struct(&rt_png_ptr, (png_infopp) NULL);
  305. fprintf(stderr, "Could not create a PNG writer\n");
  306. return 0;
  307. }
  308. rt_info_ptr = png_create_info_struct(rt_png_ptr);
  309. if (!rt_info_ptr) {
  310. png_destroy_write_struct(&rt_png_ptr, (png_infopp) NULL);
  311. fprintf(stderr, "Could not create a PNG writer\n");
  312. return 0;
  313. }
  314. // Greyscale image
  315. png_set_IHDR(rt_png_ptr, rt_info_ptr, width, height,
  316. 8, PNG_COLOR_TYPE_GRAY, PNG_INTERLACE_NONE,
  317. PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);
  318. png_set_text(rt_png_ptr, rt_info_ptr, meta, 3);
  319. // Channel = 25cm wide
  320. png_set_pHYs(rt_png_ptr, rt_info_ptr, 3636, 3636, PNG_RESOLUTION_METER);
  321. // Init I/O
  322. rt_pngfile = fopen(outName, "wb");
  323. if (!rt_pngfile) {
  324. fprintf(stderr, "Could not open %s for writing\n", outName);
  325. return 0;
  326. }
  327. png_init_io(rt_png_ptr, rt_pngfile);
  328. png_write_info(rt_png_ptr, rt_info_ptr);
  329. return 1;
  330. }
  331. void pushRow(float *row, int width){
  332. png_byte pix[width];
  333. for(int i = 0; i < width; i++)
  334. pix[i] = row[i];
  335. png_write_row(rt_png_ptr, (png_bytep) pix);
  336. }
  337. void closeWriter(){
  338. png_write_end(rt_png_ptr, rt_info_ptr);
  339. fclose(rt_pngfile);
  340. png_destroy_write_struct(&rt_png_ptr, &rt_info_ptr);
  341. }