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.
 
 
 
 
 

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