Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

před 4 roky
před 4 roky
před 4 roky
před 4 roky
před 4 roky
před 4 roky
před 4 roky
před 4 roky
před 4 roky
před 4 roky
před 4 roky
před 4 roky
před 4 roky
před 4 roky
před 4 roky
před 4 roky
před 4 roky
před 4 roky
před 4 roky
před 4 roky
před 4 roky
před 4 roky
před 4 roky
před 4 roky
před 4 roky
před 4 roky
před 4 roky
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521
  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");
  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 cbb = prow[y][x + CHB_OFFSET];
  239. crow[y][x + CHA_OFFSET] = pal_row[cbb][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. FILE *pngfile;
  294. // Create writer
  295. png_structp png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
  296. if (!png_ptr) {
  297. png_destroy_write_struct(&png_ptr, (png_infopp) NULL);
  298. fprintf(stderr, "Could not create a PNG writer\n");
  299. return 0;
  300. }
  301. png_infop info_ptr = png_create_info_struct(png_ptr);
  302. if (!info_ptr) {
  303. png_destroy_write_struct(&png_ptr, (png_infopp) NULL);
  304. fprintf(stderr, "Could not create a PNG writer\n");
  305. return 0;
  306. }
  307. if(greyscale){
  308. // Greyscale image
  309. png_set_IHDR(png_ptr, info_ptr, width, img->nrow,
  310. 8, PNG_COLOR_TYPE_GRAY, PNG_INTERLACE_NONE,
  311. PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);
  312. }else{
  313. // 8 bit RGB image
  314. png_set_IHDR(png_ptr, info_ptr, width, img->nrow,
  315. 8, PNG_COLOR_TYPE_RGB, PNG_INTERLACE_NONE,
  316. PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);
  317. }
  318. png_set_text(png_ptr, info_ptr, meta, 3);
  319. png_set_pHYs(png_ptr, info_ptr, 3636, 3636, PNG_RESOLUTION_METER);
  320. // Init I/O
  321. pngfile = fopen(outName, "wb");
  322. if (!pngfile) {
  323. fprintf(stderr, "Could not open %s for writing\n", outName);
  324. return 1;
  325. }
  326. png_init_io(png_ptr, pngfile);
  327. png_write_info(png_ptr, info_ptr);
  328. // Move prow into crow, crow ~ color rows, if required
  329. rgb_t *crow[img->nrow];
  330. if(!greyscale)
  331. prow2crow(img->prow, img->nrow, palette, crow);
  332. // Apply a user provided color palette
  333. if(CONTAINS(opts->type, Palleted))
  334. applyUserPalette(img->prow, img->nrow, opts->palette, crow);
  335. // Precipitation overlay
  336. if(CONTAINS(opts->effects, Precipitation_Overlay)){
  337. for(int y = 0; y < img->nrow; y++){
  338. for(int x = 0; x < CH_WIDTH; x++){
  339. if(img->prow[y][x + CHB_OFFSET] >= 198)
  340. crow[y][x + CHB_OFFSET] = crow[y][x + CHA_OFFSET] = applyPalette(PrecipPalette, img->prow[y][x + CHB_OFFSET]-198);
  341. }
  342. }
  343. }
  344. // Map stuff
  345. if(opts->map != NULL && opts->map[0] != '\0'){
  346. if(!mapOverlay(opts->map, crow, img->nrow, img->zenith, CONTAINS(opts->type, MCIR))){
  347. fprintf(stderr, "Skipping MCIR generation.\n");
  348. return 0;
  349. }
  350. }else if(CONTAINS(opts->type, MCIR)){
  351. fprintf(stderr, "Skipping MCIR generation; no map provided.\n");
  352. return 0;
  353. }
  354. printf("Writing %s", outName);
  355. // Float power macro (for gamma adjustment)
  356. #define POWF(a, b) (b == 1.0 ? a : exp(b * log(a)))
  357. float a = POWF(255, opts->gamma)/255;
  358. // Build image
  359. for (int y = 0; y < img->nrow; y++) {
  360. png_color pix[width]; // Color
  361. png_byte mpix[width]; // Mono
  362. int skip = 0;
  363. for (int x = 0; x < width; x++) {
  364. if(crop_telemetry && x == CH_WIDTH)
  365. skip += TELE_WIDTH + SYNC_WIDTH + SPC_WIDTH;
  366. if(greyscale){
  367. mpix[x] = POWF(img->prow[y][x + skip + offset], opts->gamma)/a;
  368. }else{
  369. pix[x] = (png_color){
  370. POWF(crow[y][x + skip + offset].r, opts->gamma)/a,
  371. POWF(crow[y][x + skip + offset].g, opts->gamma)/a,
  372. POWF(crow[y][x + skip + offset].b, opts->gamma)/a
  373. };
  374. }
  375. }
  376. if(greyscale){
  377. png_write_row(png_ptr, (png_bytep) mpix);
  378. }else{
  379. png_write_row(png_ptr, (png_bytep) pix);
  380. }
  381. }
  382. // Tidy up
  383. png_write_end(png_ptr, info_ptr);
  384. fclose(pngfile);
  385. printf("\nDone\n");
  386. png_destroy_write_struct(&png_ptr, &info_ptr);
  387. return 1;
  388. }
  389. // TODO: clean up everthing below this comment
  390. png_structp rt_png_ptr;
  391. png_infop rt_info_ptr;
  392. FILE *rt_pngfile;
  393. int initWriter(options_t *opts, image_t *img, int width, int height, char *desc, char *chid){
  394. char outName[384];
  395. sprintf(outName, "%s/%s-%s.png", opts->path, img->name, chid);
  396. png_text meta[] = {
  397. {PNG_TEXT_COMPRESSION_NONE, "Software", VERSION},
  398. {PNG_TEXT_COMPRESSION_NONE, "Channel", desc, sizeof(desc)},
  399. {PNG_TEXT_COMPRESSION_NONE, "Description", "NOAA satellite image", 20}
  400. };
  401. // Create writer
  402. rt_png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
  403. if (!rt_png_ptr) {
  404. png_destroy_write_struct(&rt_png_ptr, (png_infopp) NULL);
  405. fprintf(stderr, "Could not create a PNG writer\n");
  406. return 0;
  407. }
  408. rt_info_ptr = png_create_info_struct(rt_png_ptr);
  409. if (!rt_info_ptr) {
  410. png_destroy_write_struct(&rt_png_ptr, (png_infopp) NULL);
  411. fprintf(stderr, "Could not create a PNG writer\n");
  412. return 0;
  413. }
  414. // Greyscale image
  415. png_set_IHDR(rt_png_ptr, rt_info_ptr, width, height,
  416. 8, PNG_COLOR_TYPE_GRAY, PNG_INTERLACE_NONE,
  417. PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);
  418. png_set_text(rt_png_ptr, rt_info_ptr, meta, 3);
  419. // Channel = 25cm wide
  420. png_set_pHYs(rt_png_ptr, rt_info_ptr, 3636, 3636, PNG_RESOLUTION_METER);
  421. // Init I/O
  422. rt_pngfile = fopen(outName, "wb");
  423. if (!rt_pngfile) {
  424. fprintf(stderr, "Could not open %s for writing\n", outName);
  425. return 0;
  426. }
  427. png_init_io(rt_png_ptr, rt_pngfile);
  428. png_write_info(rt_png_ptr, rt_info_ptr);
  429. // Turn off compression
  430. png_set_compression_level(rt_png_ptr, 0);
  431. return 1;
  432. }
  433. void pushRow(float *row, int width){
  434. png_byte pix[width];
  435. for(int i = 0; i < width; i++)
  436. pix[i] = row[i];
  437. png_write_row(rt_png_ptr, (png_bytep) pix);
  438. }
  439. void closeWriter(){
  440. png_write_end(rt_png_ptr, rt_info_ptr);
  441. fclose(rt_pngfile);
  442. png_destroy_write_struct(&rt_png_ptr, &rt_info_ptr);
  443. }