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.
 
 
 
 
 

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