Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.
 
 
 
 
 

529 lignes
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) 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. #pragma GCC diagnostic ignored "-Wunused-result"
  252. png_text meta[] = {
  253. {PNG_TEXT_COMPRESSION_NONE, "Software", VERSION},
  254. {PNG_TEXT_COMPRESSION_NONE, "Channel", desc, sizeof(desc)},
  255. {PNG_TEXT_COMPRESSION_NONE, "Description", "NOAA satellite image", 20}
  256. };
  257. // Parse image type
  258. int greyscale = 1;
  259. switch (chid){
  260. case Palleted:
  261. greyscale = 0;
  262. break;
  263. case Temperature:
  264. greyscale = 0;
  265. break;
  266. case MCIR:
  267. greyscale = 0;
  268. break;
  269. case Raw_Image: break;
  270. case Channel_A: break;
  271. case Channel_B: break;
  272. }
  273. // Parse effects
  274. int crop_telemetry = 0;
  275. for(unsigned long int i = 0; i < strlen(opts->effects); i++){
  276. switch (opts->effects[i]) {
  277. case Crop_Telemetry:
  278. width -= TOTAL_TELE;
  279. offset += SYNC_WIDTH + SPC_WIDTH;
  280. crop_telemetry = 1;
  281. break;
  282. case Precipitation_Overlay:
  283. greyscale = 0;
  284. break;
  285. case Flip_Image: break;
  286. case Denoise: break;
  287. case Histogram_Equalise: break;
  288. case Linear_Equalise: break;
  289. default:
  290. fprintf(stderr, "NOTICE: Unrecognised effect, \"%c\"\n", opts->effects[i]);
  291. break;
  292. }
  293. }
  294. if(opts->map != NULL && opts->map[0] != '\0'){
  295. greyscale = 0;
  296. }
  297. FILE *pngfile;
  298. // Create writer
  299. png_structp png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
  300. if (!png_ptr) {
  301. png_destroy_write_struct(&png_ptr, (png_infopp) NULL);
  302. fprintf(stderr, "Could not create a PNG writer\n");
  303. return 0;
  304. }
  305. png_infop info_ptr = png_create_info_struct(png_ptr);
  306. if (!info_ptr) {
  307. png_destroy_write_struct(&png_ptr, (png_infopp) NULL);
  308. fprintf(stderr, "Could not create a PNG writer\n");
  309. return 0;
  310. }
  311. if(greyscale){
  312. // Greyscale image
  313. png_set_IHDR(png_ptr, info_ptr, width, img->nrow,
  314. 8, PNG_COLOR_TYPE_GRAY, PNG_INTERLACE_NONE,
  315. PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);
  316. }else{
  317. // 8 bit RGB image
  318. png_set_IHDR(png_ptr, info_ptr, width, img->nrow,
  319. 8, PNG_COLOR_TYPE_RGB, PNG_INTERLACE_NONE,
  320. PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);
  321. }
  322. png_set_text(png_ptr, info_ptr, meta, 3);
  323. png_set_pHYs(png_ptr, info_ptr, 3636, 3636, PNG_RESOLUTION_METER);
  324. // Init I/O
  325. pngfile = fopen(outName, "wb");
  326. if (!pngfile) {
  327. fprintf(stderr, "Could not open %s for writing\n", outName);
  328. return 1;
  329. }
  330. png_init_io(png_ptr, pngfile);
  331. png_write_info(png_ptr, info_ptr);
  332. // Move prow into crow, crow ~ color rows, if required
  333. rgb_t *crow[img->nrow];
  334. if(!greyscale){
  335. prow2crow(img->prow, img->nrow, palette, crow);
  336. }
  337. // Apply a user provided color palette
  338. if(CONTAINS(opts->type, Palleted)){
  339. applyUserPalette(img->prow, img->nrow, opts->palette, crow);
  340. }
  341. // Precipitation overlay
  342. if(CONTAINS(opts->effects, Precipitation_Overlay)){
  343. for(int y = 0; y < img->nrow; y++){
  344. for(int x = 0; x < CH_WIDTH; x++){
  345. if(img->prow[y][x + CHB_OFFSET] >= 198)
  346. crow[y][x + CHB_OFFSET] = crow[y][x + CHA_OFFSET] = applyPalette(PrecipPalette, img->prow[y][x + CHB_OFFSET]-198);
  347. }
  348. }
  349. }
  350. // Map stuff
  351. if(opts->map != NULL && opts->map[0] != '\0'){
  352. if(!mapOverlay(opts->map, crow, img->nrow, img->zenith, CONTAINS(opts->type, MCIR))){
  353. fprintf(stderr, "Skipping MCIR generation.\n");
  354. return 0;
  355. }
  356. }else if(CONTAINS(opts->type, MCIR)){
  357. fprintf(stderr, "Skipping MCIR generation; no map provided.\n");
  358. return 0;
  359. }
  360. printf("Writing %s", outName);
  361. // Float power macro (for gamma adjustment)
  362. #define POWF(a, b) (b == 1.0 ? a : exp(b * log(a)))
  363. float a = POWF(255, opts->gamma)/255;
  364. // Build image
  365. for (int y = 0; y < img->nrow; y++) {
  366. png_color pix[width]; // Color
  367. png_byte mpix[width]; // Mono
  368. int skip = 0;
  369. for (int x = 0; x < width; x++) {
  370. if(crop_telemetry && x == CH_WIDTH)
  371. skip += TELE_WIDTH + SYNC_WIDTH + SPC_WIDTH;
  372. if(greyscale){
  373. mpix[x] = POWF(img->prow[y][x + skip + offset], opts->gamma)/a;
  374. }else{
  375. pix[x] = (png_color){
  376. POWF(crow[y][x + skip + offset].r, opts->gamma)/a,
  377. POWF(crow[y][x + skip + offset].g, opts->gamma)/a,
  378. POWF(crow[y][x + skip + offset].b, opts->gamma)/a
  379. };
  380. }
  381. }
  382. if(greyscale){
  383. png_write_row(png_ptr, (png_bytep) mpix);
  384. }else{
  385. png_write_row(png_ptr, (png_bytep) pix);
  386. }
  387. }
  388. // Tidy up
  389. png_write_end(png_ptr, info_ptr);
  390. fclose(pngfile);
  391. printf("\nDone\n");
  392. png_destroy_write_struct(&png_ptr, &info_ptr);
  393. return 1;
  394. }
  395. // TODO: clean up everthing below this comment
  396. png_structp rt_png_ptr;
  397. png_infop rt_info_ptr;
  398. FILE *rt_pngfile;
  399. int initWriter(options_t *opts, image_t *img, int width, int height, char *desc, char *chid){
  400. char outName[384];
  401. sprintf(outName, "%s/%s-%s.png", opts->path, img->name, chid);
  402. png_text meta[] = {
  403. {PNG_TEXT_COMPRESSION_NONE, "Software", VERSION},
  404. {PNG_TEXT_COMPRESSION_NONE, "Channel", desc, sizeof(desc)},
  405. {PNG_TEXT_COMPRESSION_NONE, "Description", "NOAA satellite image", 20}
  406. };
  407. // Create writer
  408. rt_png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
  409. if (!rt_png_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. rt_info_ptr = png_create_info_struct(rt_png_ptr);
  415. if (!rt_info_ptr) {
  416. png_destroy_write_struct(&rt_png_ptr, (png_infopp) NULL);
  417. fprintf(stderr, "Could not create a PNG writer\n");
  418. return 0;
  419. }
  420. // Greyscale image
  421. png_set_IHDR(rt_png_ptr, rt_info_ptr, width, height,
  422. 8, PNG_COLOR_TYPE_GRAY, PNG_INTERLACE_NONE,
  423. PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);
  424. png_set_text(rt_png_ptr, rt_info_ptr, meta, 3);
  425. // Channel = 25cm wide
  426. png_set_pHYs(rt_png_ptr, rt_info_ptr, 3636, 3636, PNG_RESOLUTION_METER);
  427. // Init I/O
  428. rt_pngfile = fopen(outName, "wb");
  429. if (!rt_pngfile) {
  430. fprintf(stderr, "Could not open %s for writing\n", outName);
  431. return 0;
  432. }
  433. png_init_io(rt_png_ptr, rt_pngfile);
  434. png_write_info(rt_png_ptr, rt_info_ptr);
  435. // Turn off compression
  436. png_set_compression_level(rt_png_ptr, 0);
  437. return 1;
  438. }
  439. void pushRow(float *row, int width){
  440. png_byte pix[width];
  441. for(int i = 0; i < width; i++)
  442. pix[i] = row[i];
  443. png_write_row(rt_png_ptr, (png_bytep) pix);
  444. }
  445. void closeWriter(){
  446. png_write_end(rt_png_ptr, rt_info_ptr);
  447. fclose(rt_pngfile);
  448. png_destroy_write_struct(&rt_png_ptr, &rt_info_ptr);
  449. }