Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.
 
 
 
 
 

517 рядки
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 "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-256)/32.0, 0, 1);
  90. float blue = 1 - CLIP((map.b-32)/64.0, 0, 1);
  91. crow[y][cha] = (rgb_t){blue*127, 30+green*80, 40};
  92. }else{
  93. // Sea
  94. crow[y][cha] = (rgb_t){12, 30, 85};
  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 - 115) / 107, 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. // Build image
  355. for (int y = 0; y < img->nrow; y++) {
  356. png_color pix[width]; // Color
  357. png_byte mpix[width]; // Mono
  358. int skip = 0;
  359. for (int x = 0; x < width; x++) {
  360. if(crop_telemetry && x == CH_WIDTH){
  361. skip += TELE_WIDTH + SYNC_WIDTH + SPC_WIDTH;
  362. }
  363. if(greyscale){
  364. mpix[x] = img->prow[y][x + skip + offset];
  365. }else{
  366. pix[x] = (png_color){
  367. crow[y][x + skip + offset].r,
  368. crow[y][x + skip + offset].g,
  369. crow[y][x + skip + offset].b
  370. };
  371. }
  372. }
  373. if(greyscale){
  374. png_write_row(png_ptr, (png_bytep) mpix);
  375. }else{
  376. png_write_row(png_ptr, (png_bytep) pix);
  377. }
  378. }
  379. // Tidy up
  380. png_write_end(png_ptr, info_ptr);
  381. fclose(pngfile);
  382. printf("\nDone\n");
  383. png_destroy_write_struct(&png_ptr, &info_ptr);
  384. return 1;
  385. }
  386. // TODO: clean up everthing below this comment
  387. png_structp rt_png_ptr;
  388. png_infop rt_info_ptr;
  389. FILE *rt_pngfile;
  390. int initWriter(options_t *opts, image_t *img, int width, int height, char *desc, char *chid){
  391. char outName[384];
  392. sprintf(outName, "%s/%s-%s.png", opts->path, img->name, chid);
  393. png_text meta[] = {
  394. {PNG_TEXT_COMPRESSION_NONE, "Software", VERSION},
  395. {PNG_TEXT_COMPRESSION_NONE, "Channel", desc, sizeof(desc)},
  396. {PNG_TEXT_COMPRESSION_NONE, "Description", "NOAA satellite image", 20}
  397. };
  398. // Create writer
  399. rt_png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
  400. if (!rt_png_ptr) {
  401. png_destroy_write_struct(&rt_png_ptr, (png_infopp) NULL);
  402. fprintf(stderr, "Could not create a PNG writer\n");
  403. return 0;
  404. }
  405. rt_info_ptr = png_create_info_struct(rt_png_ptr);
  406. if (!rt_info_ptr) {
  407. png_destroy_write_struct(&rt_png_ptr, (png_infopp) NULL);
  408. fprintf(stderr, "Could not create a PNG writer\n");
  409. return 0;
  410. }
  411. // Greyscale image
  412. png_set_IHDR(rt_png_ptr, rt_info_ptr, width, height,
  413. 8, PNG_COLOR_TYPE_GRAY, PNG_INTERLACE_NONE,
  414. PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);
  415. png_set_text(rt_png_ptr, rt_info_ptr, meta, 3);
  416. // Channel = 25cm wide
  417. png_set_pHYs(rt_png_ptr, rt_info_ptr, 3636, 3636, PNG_RESOLUTION_METER);
  418. // Init I/O
  419. rt_pngfile = fopen(outName, "wb");
  420. if (!rt_pngfile) {
  421. fprintf(stderr, "Could not open %s for writing\n", outName);
  422. return 0;
  423. }
  424. png_init_io(rt_png_ptr, rt_pngfile);
  425. png_write_info(rt_png_ptr, rt_info_ptr);
  426. // Turn off compression
  427. png_set_compression_level(rt_png_ptr, 0);
  428. return 1;
  429. }
  430. void pushRow(float *row, int width){
  431. png_byte pix[width];
  432. for(int i = 0; i < width; i++)
  433. pix[i] = row[i];
  434. png_write_row(rt_png_ptr, (png_bytep) pix);
  435. }
  436. void closeWriter(){
  437. png_write_end(rt_png_ptr, rt_info_ptr);
  438. fclose(rt_pngfile);
  439. png_destroy_write_struct(&rt_png_ptr, &rt_info_ptr);
  440. }