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.

4 jaren geleden
4 jaren geleden
4 jaren geleden
4 jaren geleden
4 jaren geleden
4 jaren geleden
4 jaren geleden
4 jaren geleden
4 jaren geleden
4 jaren geleden
4 jaren geleden
4 jaren geleden
4 jaren geleden
4 jaren geleden
4 jaren geleden
4 jaren geleden
4 jaren geleden
4 jaren geleden
4 jaren geleden
4 jaren geleden
4 jaren geleden
4 jaren geleden
4 jaren geleden
4 jaren geleden
4 jaren geleden
4 jaren geleden
4 jaren geleden
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556
  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. char buffer[1024];
  188. // PALETTE_DIR is set through CMake
  189. sprintf(buffer, PALETTE_DIR"/%s", filename);
  190. fp = fopen(buffer, "r");
  191. if(!fp){
  192. fprintf(stderr, "Cannot open %s\n", filename);
  193. return 0;
  194. }
  195. }
  196. // Create reader
  197. png_structp png = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
  198. if(!png) {
  199. fclose(fp);
  200. return 0;
  201. }
  202. png_infop info = png_create_info_struct(png);
  203. if(!info) {
  204. fclose(fp);
  205. return 0;
  206. }
  207. png_init_io(png, fp);
  208. // Read info from header
  209. png_read_info(png, info);
  210. int width = png_get_image_width(png, info);
  211. int height = png_get_image_height(png, info);
  212. png_byte color_type = png_get_color_type(png, info);
  213. png_byte bit_depth = png_get_bit_depth(png, info);
  214. // Check the image
  215. if(width != 256 && height != 256){
  216. fprintf(stderr, "Palette must be 256x256.\n");
  217. return 0;
  218. }else if(bit_depth != 8){
  219. fprintf(stderr, "Palette must be 8 bit color.\n");
  220. return 0;
  221. }else if(color_type != PNG_COLOR_TYPE_RGB){
  222. fprintf(stderr, "Palette must be RGB.\n");
  223. return 0;
  224. }
  225. // Create row buffers
  226. png_bytep *PNGrows = NULL;
  227. PNGrows = (png_bytep *) malloc(sizeof(png_bytep) * height);
  228. for(int y = 0; y < height; y++)
  229. PNGrows[y] = (png_byte *) malloc(png_get_rowbytes(png, info));
  230. // Read image
  231. png_read_image(png, PNGrows);
  232. // Tidy up
  233. fclose(fp);
  234. png_destroy_read_struct(&png, &info, NULL);
  235. // Put into crow
  236. for(int y = 0; y < height; y++) {
  237. pixels[y] = (rgb_t *) malloc(sizeof(rgb_t) * width);
  238. for(int x = 0; x < width; x++)
  239. pixels[y][x] = (rgb_t){
  240. PNGrows[y][x*3],
  241. PNGrows[y][x*3 + 1],
  242. PNGrows[y][x*3 + 2]
  243. };
  244. }
  245. return 1;
  246. }
  247. void prow2crow(float **prow, int nrow, char *palette, rgb_t **crow){
  248. for(int y = 0; y < nrow; y++){
  249. crow[y] = (rgb_t *) malloc(sizeof(rgb_t) * IMG_WIDTH);
  250. for(int x = 0; x < IMG_WIDTH; x++){
  251. if(palette == NULL)
  252. crow[y][x].r = crow[y][x].g = crow[y][x].b = prow[y][x];
  253. else
  254. crow[y][x] = applyPalette(palette, prow[y][x]);
  255. }
  256. }
  257. }
  258. int applyUserPalette(float **prow, int nrow, char *filename, rgb_t **crow){
  259. rgb_t *pal_row[256];
  260. if(!readPalette(filename, pal_row)){
  261. fprintf(stderr, "Could not read palette\n");
  262. return 0;
  263. }
  264. for(int y = 0; y < nrow; y++){
  265. for(int x = 0; x < CH_WIDTH; x++){
  266. int cha = CLIP(prow[y][x + CHA_OFFSET], 0, 255);
  267. int chb = CLIP(prow[y][x + CHB_OFFSET], 0, 255);
  268. crow[y][x + CHA_OFFSET] = pal_row[chb][cha];
  269. }
  270. }
  271. return 1;
  272. }
  273. int ImageOut(options_t *opts, image_t *img, int offset, int width, char *desc, char chid, char *palette){
  274. char outName[512];
  275. if(opts->filename == NULL || opts->filename[0] == '\0'){
  276. sprintf(outName, "%s/%s-%c.png", opts->path, img->name, chid);
  277. }else{
  278. sprintf(outName, "%s/%s", opts->path, opts->filename);
  279. }
  280. png_text meta[] = {
  281. {PNG_TEXT_COMPRESSION_NONE, "Software", VERSION},
  282. {PNG_TEXT_COMPRESSION_NONE, "Channel", desc, sizeof(desc)},
  283. {PNG_TEXT_COMPRESSION_NONE, "Description", "NOAA satellite image", 20}
  284. };
  285. // Parse image type
  286. int greyscale = 1;
  287. switch (chid){
  288. case Palleted:
  289. greyscale = 0;
  290. break;
  291. case Temperature:
  292. greyscale = 0;
  293. break;
  294. case MCIR:
  295. greyscale = 0;
  296. break;
  297. case Raw_Image: break;
  298. case Channel_A: break;
  299. case Channel_B: break;
  300. }
  301. // Parse effects
  302. int crop_telemetry = 0;
  303. for(unsigned long int i = 0; i < strlen(opts->effects); i++){
  304. switch (opts->effects[i]) {
  305. case Crop_Telemetry:
  306. width -= TOTAL_TELE;
  307. offset += SYNC_WIDTH + SPC_WIDTH;
  308. crop_telemetry = 1;
  309. break;
  310. case Precipitation_Overlay:
  311. greyscale = 0;
  312. break;
  313. case Flip_Image: break;
  314. case Denoise: break;
  315. case Histogram_Equalise: break;
  316. case Linear_Equalise: break;
  317. case Crop_Noise: break;
  318. default:
  319. fprintf(stderr, "NOTICE: Unrecognised effect, \"%c\"\n", opts->effects[i]);
  320. break;
  321. }
  322. }
  323. if(opts->map != NULL && opts->map[0] != '\0'){
  324. greyscale = 0;
  325. }
  326. FILE *pngfile;
  327. // Create writer
  328. png_structp png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
  329. if (!png_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. png_infop info_ptr = png_create_info_struct(png_ptr);
  335. if (!info_ptr) {
  336. png_destroy_write_struct(&png_ptr, (png_infopp) NULL);
  337. fprintf(stderr, "Could not create a PNG writer\n");
  338. return 0;
  339. }
  340. if(greyscale){
  341. // Greyscale image
  342. png_set_IHDR(png_ptr, info_ptr, width, img->nrow,
  343. 8, PNG_COLOR_TYPE_GRAY, PNG_INTERLACE_NONE,
  344. PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);
  345. }else{
  346. // 8 bit RGB image
  347. png_set_IHDR(png_ptr, info_ptr, width, img->nrow,
  348. 8, PNG_COLOR_TYPE_RGB, PNG_INTERLACE_NONE,
  349. PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);
  350. }
  351. png_set_text(png_ptr, info_ptr, meta, 3);
  352. png_set_pHYs(png_ptr, info_ptr, 3636, 3636, PNG_RESOLUTION_METER);
  353. // Init I/O
  354. pngfile = fopen(outName, "wb");
  355. if (!pngfile) {
  356. fprintf(stderr, "Could not open %s for writing\n", outName);
  357. return 1;
  358. }
  359. png_init_io(png_ptr, pngfile);
  360. png_write_info(png_ptr, info_ptr);
  361. // Move prow into crow, crow ~ color rows, if required
  362. rgb_t *crow[MAX_HEIGHT];
  363. if(!greyscale){
  364. prow2crow(img->prow, img->nrow, palette, crow);
  365. }
  366. // Apply a user provided color palette
  367. if(chid == Palleted){
  368. applyUserPalette(img->prow, img->nrow, opts->palette, crow);
  369. }
  370. // Precipitation overlay
  371. if(CONTAINS(opts->effects, Precipitation_Overlay)){
  372. for(int y = 0; y < img->nrow; y++){
  373. for(int x = 0; x < CH_WIDTH; x++){
  374. if(img->prow[y][x + CHB_OFFSET] >= 198)
  375. crow[y][x + CHB_OFFSET] = crow[y][x + CHA_OFFSET] = applyPalette(PrecipPalette, img->prow[y][x + CHB_OFFSET]-198);
  376. }
  377. }
  378. }
  379. // Map stuff
  380. if(opts->map != NULL && opts->map[0] != '\0'){
  381. if(!mapOverlay(opts->map, crow, img->nrow, img->zenith+opts->mapOffset, CONTAINS(opts->type, MCIR))){
  382. fprintf(stderr, "Skipping MCIR generation.\n");
  383. return 0;
  384. }
  385. }else if(CONTAINS(opts->type, MCIR)){
  386. fprintf(stderr, "Skipping MCIR generation; no map provided.\n");
  387. return 0;
  388. }
  389. printf("Writing %s", outName);
  390. // Float power macro (for gamma adjustment)
  391. #define POWF(a, b) (b == 1.0 ? a : exp(b * log(a)))
  392. float a = POWF(255, opts->gamma)/255;
  393. // Build image
  394. for (int y = 0; y < img->nrow; y++) {
  395. png_color pix[width]; // Color
  396. png_byte mpix[width]; // Mono
  397. int skip = 0;
  398. for (int x = 0; x < width; x++) {
  399. if(crop_telemetry && x == CH_WIDTH)
  400. skip += TELE_WIDTH + SYNC_WIDTH + SPC_WIDTH;
  401. if(greyscale){
  402. mpix[x] = POWF(img->prow[y][x + skip + offset], opts->gamma)/a;
  403. }else{
  404. pix[x] = (png_color){
  405. POWF(crow[y][x + skip + offset].r, opts->gamma)/a,
  406. POWF(crow[y][x + skip + offset].g, opts->gamma)/a,
  407. POWF(crow[y][x + skip + offset].b, opts->gamma)/a
  408. };
  409. }
  410. }
  411. if(greyscale){
  412. png_write_row(png_ptr, (png_bytep) mpix);
  413. }else{
  414. png_write_row(png_ptr, (png_bytep) pix);
  415. }
  416. }
  417. // Tidy up
  418. png_write_end(png_ptr, info_ptr);
  419. fclose(pngfile);
  420. printf("\nDone\n");
  421. png_destroy_write_struct(&png_ptr, &info_ptr);
  422. return 1;
  423. }
  424. // TODO: clean up everthing below this comment
  425. png_structp rt_png_ptr;
  426. png_infop rt_info_ptr;
  427. FILE *rt_pngfile;
  428. int initWriter(options_t *opts, image_t *img, int width, int height, char *desc, char *chid){
  429. char outName[384];
  430. sprintf(outName, "%s/%s-%s.png", opts->path, img->name, chid);
  431. png_text meta[] = {
  432. {PNG_TEXT_COMPRESSION_NONE, "Software", VERSION},
  433. {PNG_TEXT_COMPRESSION_NONE, "Channel", desc, sizeof(desc)},
  434. {PNG_TEXT_COMPRESSION_NONE, "Description", "NOAA satellite image", 20}
  435. };
  436. // Create writer
  437. rt_png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
  438. if (!rt_png_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. rt_info_ptr = png_create_info_struct(rt_png_ptr);
  444. if (!rt_info_ptr) {
  445. png_destroy_write_struct(&rt_png_ptr, (png_infopp) NULL);
  446. fprintf(stderr, "Could not create a PNG writer\n");
  447. return 0;
  448. }
  449. // Greyscale image
  450. png_set_IHDR(rt_png_ptr, rt_info_ptr, width, height,
  451. 8, PNG_COLOR_TYPE_GRAY, PNG_INTERLACE_NONE,
  452. PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);
  453. png_set_text(rt_png_ptr, rt_info_ptr, meta, 3);
  454. // Channel = 25cm wide
  455. png_set_pHYs(rt_png_ptr, rt_info_ptr, 3636, 3636, PNG_RESOLUTION_METER);
  456. // Init I/O
  457. rt_pngfile = fopen(outName, "wb");
  458. if (!rt_pngfile) {
  459. fprintf(stderr, "Could not open %s for writing\n", outName);
  460. return 0;
  461. }
  462. png_init_io(rt_png_ptr, rt_pngfile);
  463. png_write_info(rt_png_ptr, rt_info_ptr);
  464. // Turn off compression
  465. png_set_compression_level(rt_png_ptr, 0);
  466. return 1;
  467. }
  468. void pushRow(float *row, int width){
  469. png_byte pix[width];
  470. for(int i = 0; i < width; i++)
  471. pix[i] = row[i];
  472. png_write_row(rt_png_ptr, (png_bytep) pix);
  473. }
  474. void closeWriter(){
  475. png_write_end(rt_png_ptr, rt_info_ptr);
  476. fclose(rt_pngfile);
  477. png_destroy_write_struct(&rt_png_ptr, &rt_info_ptr);
  478. }