Não pode escolher mais do que 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

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