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.
 
 
 
 
 

527 lines
14 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 <math.h>
  25. #include "common.h"
  26. #include "offsets.h"
  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. if(!fp) {
  33. fprintf(stderr, "Cannot open %s\n", filename);
  34. return 0;
  35. }
  36. // Create reader
  37. png_structp png = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
  38. if(!png) return 0;
  39. png_infop info = png_create_info_struct(png);
  40. if(!info) return 0;
  41. png_init_io(png, fp);
  42. // Read info from header
  43. png_read_info(png, info);
  44. int width = png_get_image_width(png, info);
  45. int height = png_get_image_height(png, info);
  46. png_byte color_type = png_get_color_type(png, info);
  47. png_byte bit_depth = png_get_bit_depth(png, info);
  48. // Check the image
  49. if(width != 1040){
  50. fprintf(stderr, "Map must be 1040px wide.\n");
  51. return 0;
  52. }else if(bit_depth != 16){
  53. fprintf(stderr, "Map must be 16 bit color.\n");
  54. return 0;
  55. }else if(color_type != PNG_COLOR_TYPE_RGB){
  56. fprintf(stderr, "Map must be RGB.\n");
  57. return 0;
  58. }else if(zenith > height/2 || nrow-zenith > height/2){
  59. fprintf(stderr, "WARNING: Map is too short to cover entire image\n");
  60. }
  61. // Create row buffers
  62. png_bytep *mapRows = NULL;
  63. mapRows = (png_bytep *) malloc(sizeof(png_bytep) * height);
  64. for(int y = 0; y < height; y++)
  65. mapRows[y] = (png_byte *) malloc(png_get_rowbytes(png, info));
  66. // Read image
  67. png_read_image(png, mapRows);
  68. // Tidy up
  69. fclose(fp);
  70. png_destroy_read_struct(&png, &info, NULL);
  71. printf("Adding map overlay\n");
  72. // Map overlay / MCIR / Precipitation
  73. int mapOffset = (height/2)-zenith;
  74. for(int y = 0; y < nrow; y++) {
  75. for(int x = 49; x < width - 82; x++){
  76. // Maps are 16 bit / channel
  77. png_bytep px = &mapRows[CLIP(y + mapOffset, 0, height-1)][x * 6];
  78. rgb_t map = {
  79. (px[0] << 8) | px[1],
  80. (px[2] << 8) | px[3],
  81. (px[4] << 8) | px[5]
  82. };
  83. // Pixel offsets
  84. int chb = x + CHB_OFFSET - 49;
  85. int cha = x + 36;
  86. // Fill in map
  87. if(MCIR){
  88. if(map.b < 128 && map.g > 128){
  89. // Land
  90. float green = CLIP(map.g/300, 0, 1);
  91. float blue = 0.15 - CLIP(map.b/960.0, 0, 1);
  92. crow[y][cha] = (rgb_t){blue*1000, green*98, blue*500.0};
  93. }else{
  94. // Sea
  95. crow[y][cha] = (rgb_t){9, 17, 74};
  96. }
  97. }
  98. // Color -> alpha: composite
  99. int composite = map.r + map.g + map.b;
  100. // Color -> alpha: flattern and convert to 8 bits / channel
  101. float factor = (255 * 255 * 2.0) / composite;
  102. map.r *= factor/257.0; map.g *= factor/257.0; map.b *= factor/257.0;
  103. // Color -> alpha: convert black to alpha
  104. float alpha = CLIP(composite / 65535.0, 0, 1);
  105. // Map overlay on channel A
  106. crow[y][cha] = RGBcomposite(map, alpha, crow[y][cha], 1);
  107. // Map overlay on channel B
  108. if(!MCIR)
  109. crow[y][chb] = RGBcomposite(map, alpha, crow[y][chb], 1);
  110. // Cloud overlay on channel A
  111. if(MCIR){
  112. float cloud = CLIP((crow[y][chb].r - 105) / 150, 0, 1);
  113. crow[y][cha] = RGBcomposite((rgb_t){240, 250, 255}, cloud, crow[y][cha], 1);
  114. }
  115. }
  116. }
  117. return 1;
  118. }
  119. int readRawImage(char *filename, float **prow, int *nrow) {
  120. FILE *fp = fopen(filename, "r");
  121. if(!fp) {
  122. fprintf(stderr, "Cannot open %s\n", filename);
  123. return 0;
  124. }
  125. // Create reader
  126. png_structp png = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
  127. if(!png) return 0;
  128. png_infop info = png_create_info_struct(png);
  129. if(!info) return 0;
  130. png_init_io(png, fp);
  131. // Read info from header
  132. png_read_info(png, info);
  133. int width = png_get_image_width(png, info);
  134. int height = png_get_image_height(png, info);
  135. png_byte color_type = png_get_color_type(png, info);
  136. png_byte bit_depth = png_get_bit_depth(png, info);
  137. // Check the image
  138. if(width != IMG_WIDTH){
  139. fprintf(stderr, "Raw image must be %ipx wide.\n", IMG_WIDTH);
  140. return 0;
  141. }else if(bit_depth != 8){
  142. fprintf(stderr, "Raw image must have 8 bit color.\n");
  143. return 0;
  144. }else if(color_type != PNG_COLOR_TYPE_GRAY){
  145. fprintf(stderr, "Raw image must be grayscale.\n");
  146. return 0;
  147. }
  148. // Create row buffers
  149. png_bytep *PNGrows = NULL;
  150. PNGrows = (png_bytep *) malloc(sizeof(png_bytep) * height);
  151. for(int y = 0; y < height; y++) PNGrows[y] = (png_byte *)
  152. malloc(png_get_rowbytes(png, info));
  153. // Read image
  154. png_read_image(png, PNGrows);
  155. // Tidy up
  156. fclose(fp);
  157. png_destroy_read_struct(&png, &info, NULL);
  158. // Put into prow
  159. *nrow = height;
  160. for(int y = 0; y < height; y++) {
  161. prow[y] = (float *) malloc(sizeof(float) * width);
  162. for(int x = 0; x < width; x++)
  163. prow[y][x] = (float)PNGrows[y][x];
  164. }
  165. return 1;
  166. }
  167. int readPalette(char *filename, rgb_t **pixels) {
  168. FILE *fp = fopen(filename, "r");
  169. if(!fp) {
  170. fprintf(stderr, "Cannot open %s\n", filename);
  171. return 0;
  172. }
  173. // Create reader
  174. png_structp png = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
  175. if(!png) return 0;
  176. png_infop info = png_create_info_struct(png);
  177. if(!info) return 0;
  178. png_init_io(png, fp);
  179. // Read info from header
  180. png_read_info(png, info);
  181. int width = png_get_image_width(png, info);
  182. int height = png_get_image_height(png, info);
  183. png_byte color_type = png_get_color_type(png, info);
  184. png_byte bit_depth = png_get_bit_depth(png, info);
  185. // Check the image
  186. if(width != 256 && height != 256){
  187. fprintf(stderr, "Palette must be 256x256.\n");
  188. return 0;
  189. }else if(bit_depth != 8){
  190. fprintf(stderr, "Palette must be 8 bit color.\n");
  191. return 0;
  192. }else if(color_type != PNG_COLOR_TYPE_RGB){
  193. fprintf(stderr, "Palette must be RGB.\n");
  194. return 0;
  195. }
  196. // Create row buffers
  197. png_bytep *PNGrows = NULL;
  198. PNGrows = (png_bytep *) malloc(sizeof(png_bytep) * height);
  199. for(int y = 0; y < height; y++)
  200. PNGrows[y] = (png_byte *) malloc(png_get_rowbytes(png, info));
  201. // Read image
  202. png_read_image(png, PNGrows);
  203. // Tidy up
  204. fclose(fp);
  205. png_destroy_read_struct(&png, &info, NULL);
  206. // Put into crow
  207. for(int y = 0; y < height; y++) {
  208. pixels[y] = (rgb_t *) malloc(sizeof(rgb_t) * width);
  209. for(int x = 0; x < width; x++)
  210. pixels[y][x] = (rgb_t){
  211. PNGrows[y][x*3],
  212. PNGrows[y][x*3 + 1],
  213. PNGrows[y][x*3 + 2]
  214. };
  215. }
  216. return 1;
  217. }
  218. void prow2crow(float **prow, int nrow, char *palette, rgb_t **crow){
  219. for(int y = 0; y < nrow; y++){
  220. crow[y] = (rgb_t *) malloc(sizeof(rgb_t) * IMG_WIDTH);
  221. for(int x = 0; x < IMG_WIDTH; x++){
  222. if(palette == NULL)
  223. crow[y][x].r = crow[y][x].g = crow[y][x].b = prow[y][x];
  224. else
  225. crow[y][x] = applyPalette(palette, prow[y][x]);
  226. }
  227. }
  228. }
  229. int applyUserPalette(float **prow, int nrow, char *filename, rgb_t **crow){
  230. rgb_t *pal_row[256];
  231. if(!readPalette(filename, pal_row)){
  232. fprintf(stderr, "Could not read palette\n");
  233. return 0;
  234. }
  235. for(int y = 0; y < nrow; y++){
  236. for(int x = 0; x < CH_WIDTH; x++){
  237. int cha = prow[y][x + CHA_OFFSET];
  238. int chb = prow[y][x + CHB_OFFSET];
  239. crow[y][x + CHA_OFFSET] = pal_row[chb][cha];
  240. }
  241. }
  242. return 1;
  243. }
  244. int ImageOut(options_t *opts, image_t *img, int offset, int width, char *desc, char chid, char *palette){
  245. char outName[512];
  246. if(opts->filename == NULL || opts->filename[0] == '\0'){
  247. sprintf(outName, "%s/%s-%c.png", opts->path, img->name, chid);
  248. }else{
  249. sprintf(outName, "%s/%s", opts->path, opts->filename);
  250. }
  251. png_text meta[] = {
  252. {PNG_TEXT_COMPRESSION_NONE, "Software", VERSION},
  253. {PNG_TEXT_COMPRESSION_NONE, "Channel", desc, sizeof(desc)},
  254. {PNG_TEXT_COMPRESSION_NONE, "Description", "NOAA satellite image", 20}
  255. };
  256. // Parse image type
  257. int greyscale = 1;
  258. switch (chid){
  259. case Palleted:
  260. greyscale = 0;
  261. break;
  262. case Temperature:
  263. greyscale = 0;
  264. break;
  265. case MCIR:
  266. greyscale = 0;
  267. break;
  268. case Raw_Image: break;
  269. case Channel_A: break;
  270. case Channel_B: break;
  271. }
  272. // Parse effects
  273. int crop_telemetry = 0;
  274. for(unsigned long int i = 0; i < strlen(opts->effects); i++){
  275. switch (opts->effects[i]) {
  276. case Crop_Telemetry:
  277. width -= TOTAL_TELE;
  278. offset += SYNC_WIDTH + SPC_WIDTH;
  279. crop_telemetry = 1;
  280. break;
  281. case Precipitation_Overlay:
  282. greyscale = 0;
  283. break;
  284. case Flip_Image: break;
  285. case Denoise: break;
  286. case Histogram_Equalise: break;
  287. case Linear_Equalise: break;
  288. default:
  289. fprintf(stderr, "NOTICE: Unrecognised effect, \"%c\"\n", opts->effects[i]);
  290. break;
  291. }
  292. }
  293. if(opts->map != NULL && opts->map[0] != '\0'){
  294. greyscale = 0;
  295. }
  296. FILE *pngfile;
  297. // Create writer
  298. png_structp png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
  299. if (!png_ptr) {
  300. png_destroy_write_struct(&png_ptr, (png_infopp) NULL);
  301. fprintf(stderr, "Could not create a PNG writer\n");
  302. return 0;
  303. }
  304. png_infop info_ptr = png_create_info_struct(png_ptr);
  305. if (!info_ptr) {
  306. png_destroy_write_struct(&png_ptr, (png_infopp) NULL);
  307. fprintf(stderr, "Could not create a PNG writer\n");
  308. return 0;
  309. }
  310. if(greyscale){
  311. // Greyscale image
  312. png_set_IHDR(png_ptr, info_ptr, width, img->nrow,
  313. 8, PNG_COLOR_TYPE_GRAY, PNG_INTERLACE_NONE,
  314. PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);
  315. }else{
  316. // 8 bit RGB image
  317. png_set_IHDR(png_ptr, info_ptr, width, img->nrow,
  318. 8, PNG_COLOR_TYPE_RGB, PNG_INTERLACE_NONE,
  319. PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);
  320. }
  321. png_set_text(png_ptr, info_ptr, meta, 3);
  322. png_set_pHYs(png_ptr, info_ptr, 3636, 3636, PNG_RESOLUTION_METER);
  323. // Init I/O
  324. pngfile = fopen(outName, "wb");
  325. if (!pngfile) {
  326. fprintf(stderr, "Could not open %s for writing\n", outName);
  327. return 1;
  328. }
  329. png_init_io(png_ptr, pngfile);
  330. png_write_info(png_ptr, info_ptr);
  331. // Move prow into crow, crow ~ color rows, if required
  332. rgb_t *crow[MAX_HEIGHT];
  333. if(!greyscale){
  334. prow2crow(img->prow, img->nrow, palette, crow);
  335. }
  336. // Apply a user provided color palette
  337. if(chid == Palleted){
  338. applyUserPalette(img->prow, img->nrow, opts->palette, crow);
  339. }
  340. // Precipitation overlay
  341. if(CONTAINS(opts->effects, Precipitation_Overlay)){
  342. for(int y = 0; y < img->nrow; y++){
  343. for(int x = 0; x < CH_WIDTH; x++){
  344. if(img->prow[y][x + CHB_OFFSET] >= 198)
  345. crow[y][x + CHB_OFFSET] = crow[y][x + CHA_OFFSET] = applyPalette(PrecipPalette, img->prow[y][x + CHB_OFFSET]-198);
  346. }
  347. }
  348. }
  349. // Map stuff
  350. if(opts->map != NULL && opts->map[0] != '\0'){
  351. if(!mapOverlay(opts->map, crow, img->nrow, img->zenith, CONTAINS(opts->type, MCIR))){
  352. fprintf(stderr, "Skipping MCIR generation.\n");
  353. return 0;
  354. }
  355. }else if(CONTAINS(opts->type, MCIR)){
  356. fprintf(stderr, "Skipping MCIR generation; no map provided.\n");
  357. return 0;
  358. }
  359. printf("Writing %s", outName);
  360. // Float power macro (for gamma adjustment)
  361. #define POWF(a, b) (b == 1.0 ? a : exp(b * log(a)))
  362. float a = POWF(255, opts->gamma)/255;
  363. // Build image
  364. for (int y = 0; y < img->nrow; y++) {
  365. png_color pix[width]; // Color
  366. png_byte mpix[width]; // Mono
  367. int skip = 0;
  368. for (int x = 0; x < width; x++) {
  369. if(crop_telemetry && x == CH_WIDTH)
  370. skip += TELE_WIDTH + SYNC_WIDTH + SPC_WIDTH;
  371. if(greyscale){
  372. mpix[x] = POWF(img->prow[y][x + skip + offset], opts->gamma)/a;
  373. }else{
  374. pix[x] = (png_color){
  375. POWF(crow[y][x + skip + offset].r, opts->gamma)/a,
  376. POWF(crow[y][x + skip + offset].g, opts->gamma)/a,
  377. POWF(crow[y][x + skip + offset].b, opts->gamma)/a
  378. };
  379. }
  380. }
  381. if(greyscale){
  382. png_write_row(png_ptr, (png_bytep) mpix);
  383. }else{
  384. png_write_row(png_ptr, (png_bytep) pix);
  385. }
  386. }
  387. // Tidy up
  388. png_write_end(png_ptr, info_ptr);
  389. fclose(pngfile);
  390. printf("\nDone\n");
  391. png_destroy_write_struct(&png_ptr, &info_ptr);
  392. return 1;
  393. }
  394. // TODO: clean up everthing below this comment
  395. png_structp rt_png_ptr;
  396. png_infop rt_info_ptr;
  397. FILE *rt_pngfile;
  398. int initWriter(options_t *opts, image_t *img, int width, int height, char *desc, char *chid){
  399. char outName[384];
  400. sprintf(outName, "%s/%s-%s.png", opts->path, img->name, chid);
  401. png_text meta[] = {
  402. {PNG_TEXT_COMPRESSION_NONE, "Software", VERSION},
  403. {PNG_TEXT_COMPRESSION_NONE, "Channel", desc, sizeof(desc)},
  404. {PNG_TEXT_COMPRESSION_NONE, "Description", "NOAA satellite image", 20}
  405. };
  406. // Create writer
  407. rt_png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
  408. if (!rt_png_ptr) {
  409. png_destroy_write_struct(&rt_png_ptr, (png_infopp) NULL);
  410. fprintf(stderr, "Could not create a PNG writer\n");
  411. return 0;
  412. }
  413. rt_info_ptr = png_create_info_struct(rt_png_ptr);
  414. if (!rt_info_ptr) {
  415. png_destroy_write_struct(&rt_png_ptr, (png_infopp) NULL);
  416. fprintf(stderr, "Could not create a PNG writer\n");
  417. return 0;
  418. }
  419. // Greyscale image
  420. png_set_IHDR(rt_png_ptr, rt_info_ptr, width, height,
  421. 8, PNG_COLOR_TYPE_GRAY, PNG_INTERLACE_NONE,
  422. PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);
  423. png_set_text(rt_png_ptr, rt_info_ptr, meta, 3);
  424. // Channel = 25cm wide
  425. png_set_pHYs(rt_png_ptr, rt_info_ptr, 3636, 3636, PNG_RESOLUTION_METER);
  426. // Init I/O
  427. rt_pngfile = fopen(outName, "wb");
  428. if (!rt_pngfile) {
  429. fprintf(stderr, "Could not open %s for writing\n", outName);
  430. return 0;
  431. }
  432. png_init_io(rt_png_ptr, rt_pngfile);
  433. png_write_info(rt_png_ptr, rt_info_ptr);
  434. // Turn off compression
  435. png_set_compression_level(rt_png_ptr, 0);
  436. return 1;
  437. }
  438. void pushRow(float *row, int width){
  439. png_byte pix[width];
  440. for(int i = 0; i < width; i++)
  441. pix[i] = row[i];
  442. png_write_row(rt_png_ptr, (png_bytep) pix);
  443. }
  444. void closeWriter(){
  445. png_write_end(rt_png_ptr, rt_info_ptr);
  446. fclose(rt_pngfile);
  447. png_destroy_write_struct(&rt_png_ptr, &rt_info_ptr);
  448. }