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.
 
 
 
 
 

495 lines
13 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 int zenith;
  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-256)/32.0, 0, 1);
  91. float blue = 1 - CLIP((map.b-32)/64.0, 0, 1);
  92. crow[y][cha] = (rgb_t){blue*127, 30+green*80, 40};
  93. }else{
  94. // Sea
  95. crow[y][cha] = (rgb_t){12, 30, 85};
  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 - 115) / 107, 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 **crow) {
  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_RGBA){
  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. crow[y] = (rgb_t *) malloc(sizeof(rgb_t) * width);
  209. for(int x = 0; x < width; x++)
  210. crow[y][x] = (rgb_t){
  211. PNGrows[y][x*4],
  212. PNGrows[y][x*4 + 1],
  213. PNGrows[y][x*4 + 2]
  214. };
  215. }
  216. return 1;
  217. }
  218. png_text meta[] = {
  219. {PNG_TEXT_COMPRESSION_NONE, "Software", VERSION},
  220. {PNG_TEXT_COMPRESSION_NONE, "Channel", "Unknown", 7},
  221. {PNG_TEXT_COMPRESSION_NONE, "Description", "NOAA satellite image", 20}
  222. };
  223. int ImageOut(options_t *opts, image_t *img, int offset, int width, char *desc, char *chid, char *palette){
  224. char outName[384];
  225. if(opts->filename == NULL || opts->filename[0] == '\0'){
  226. sprintf(outName, "%s/%s-%s.png", opts->path, img->name, chid);
  227. }else{
  228. sprintf(outName, "%s/%s", opts->path, opts -> filename);
  229. }
  230. meta[1].text = desc;
  231. meta[1].text_length = sizeof(desc);
  232. FILE *pngfile;
  233. // Reduce the width of the image to componsate for the missing telemetry
  234. int fc = (chid[0] == 'c');
  235. int greyscale = 0;
  236. int skiptele = 0;
  237. int imgpalette = (chid[0] == 'p');
  238. if(opts->effects != NULL && CONTAINS(opts->effects, 't')){
  239. width -= TOTAL_TELE;
  240. skiptele = 1;
  241. }
  242. // Create writer
  243. png_structp png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
  244. if (!png_ptr) {
  245. png_destroy_write_struct(&png_ptr, (png_infopp) NULL);
  246. fprintf(stderr, "Could not create a PNG writer\n");
  247. return 0;
  248. }
  249. png_infop info_ptr = png_create_info_struct(png_ptr);
  250. if (!info_ptr) {
  251. png_destroy_write_struct(&png_ptr, (png_infopp) NULL);
  252. fprintf(stderr, "Could not create a PNG writer\n");
  253. return 0;
  254. }
  255. if(palette == NULL && !CONTAINS(opts->effects, 'p') && !fc && opts->map[0] == '\0' && chid[0] != 'm' && !imgpalette){
  256. greyscale = 1;
  257. // Greyscale image
  258. png_set_IHDR(png_ptr, info_ptr, width, img->nrow,
  259. 8, PNG_COLOR_TYPE_GRAY, PNG_INTERLACE_NONE,
  260. PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);
  261. }else{
  262. // 8 bit RGB image
  263. png_set_IHDR(png_ptr, info_ptr, width, img->nrow,
  264. 8, PNG_COLOR_TYPE_RGB, PNG_INTERLACE_NONE,
  265. PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);
  266. }
  267. png_set_text(png_ptr, info_ptr, meta, 3);
  268. png_set_pHYs(png_ptr, info_ptr, 3636, 3636, PNG_RESOLUTION_METER);
  269. // Init I/O
  270. pngfile = fopen(outName, "wb");
  271. if (!pngfile) {
  272. fprintf(stderr, "Could not open %s for writing\n", outName);
  273. return 1;
  274. }
  275. png_init_io(png_ptr, pngfile);
  276. png_write_info(png_ptr, info_ptr);
  277. // Move prow into crow, crow ~ color rows
  278. rgb_t *crow[img->nrow];
  279. if(!greyscale && !fc){
  280. for(int y = 0; y < img->nrow; y++){
  281. crow[y] = (rgb_t *) malloc(sizeof(rgb_t) * IMG_WIDTH);
  282. for(int x = 0; x < IMG_WIDTH; x++){
  283. if(palette == NULL)
  284. crow[y][x].r = crow[y][x].g = crow[y][x].b = img->prow[y][x];
  285. else
  286. crow[y][x] = applyPalette(palette, img->prow[y][x]);
  287. }
  288. }
  289. }
  290. // Precipitation
  291. // TODO: use temperature calibration
  292. if(CONTAINS(opts->effects, 'p')){
  293. for(int y = 0; y < img->nrow; y++){
  294. for(int x = 0; x < CH_WIDTH; x++){
  295. if(img->prow[y][x + CHB_OFFSET] > 191)
  296. crow[y][x + CHB_OFFSET] = applyPalette(PrecipPalette, img->prow[y][x + CHB_OFFSET]);
  297. }
  298. }
  299. }
  300. // Map stuff
  301. if(opts->map != NULL && opts->map[0] != '\0'){
  302. if(mapOverlay(opts->map, crow, img->nrow, zenith, (chid[0] == 'm')) == 0){
  303. fprintf(stderr, "Skipping MCIR generation.\n");
  304. return 0;
  305. }
  306. }else if(chid[0] == 'm'){
  307. fprintf(stderr, "Skipping MCIR generation; no map provided.\n");
  308. return 0;
  309. }
  310. printf("Writing %s", outName);
  311. rgb_t *pal_row[256];
  312. if(imgpalette){
  313. if(!readPalette(img->palette, pal_row)){
  314. return 0;
  315. }
  316. }
  317. // Build image
  318. for (int y = 0; y < img->nrow; y++) {
  319. png_color pix[width]; // Color
  320. png_byte mpix[width]; // Mono
  321. int skip = 0;
  322. for (int x = 0; x < width; x++) {
  323. if(skiptele){
  324. switch (x) {
  325. case 0:
  326. skip += SYNC_WIDTH + SPC_WIDTH;
  327. break;
  328. case CH_WIDTH:
  329. skip += TELE_WIDTH + SYNC_WIDTH + SPC_WIDTH;
  330. break;
  331. }
  332. }
  333. if(greyscale){
  334. mpix[x] = img->prow[y][x + skip + offset];
  335. }else if(fc){
  336. pix[x] = (png_color){
  337. CLIP(img->prow[y][x + CHA_OFFSET], 0, 255),
  338. CLIP(img->prow[y][x + CHA_OFFSET], 0, 255),
  339. CLIP(img->prow[y][x + CHB_OFFSET], 0, 255)
  340. };
  341. }else if(imgpalette){
  342. int cha = img->prow[y][x + CHA_OFFSET];
  343. int cbb = img->prow[y][x + CHB_OFFSET];
  344. pix[x] = (png_color){
  345. pal_row[cbb][cha].r,
  346. pal_row[cbb][cha].g,
  347. pal_row[cbb][cha].b
  348. };
  349. }else{
  350. pix[x] = (png_color){
  351. crow[y][x + skip + offset].r,
  352. crow[y][x + skip + offset].g,
  353. crow[y][x + skip + offset].b
  354. };
  355. }
  356. }
  357. if(greyscale){
  358. png_write_row(png_ptr, (png_bytep) mpix);
  359. }else{
  360. png_write_row(png_ptr, (png_bytep) pix);
  361. }
  362. }
  363. // Tidy up
  364. png_write_end(png_ptr, info_ptr);
  365. fclose(pngfile);
  366. printf("\nDone\n");
  367. png_destroy_write_struct(&png_ptr, &info_ptr);
  368. return 1;
  369. }
  370. // TODO: clean up everthing below this comment
  371. png_structp rt_png_ptr;
  372. png_infop rt_info_ptr;
  373. FILE *rt_pngfile;
  374. int initWriter(options_t *opts, image_t *img, int width, int height, char *desc, char *chid){
  375. char outName[384];
  376. sprintf(outName, "%s/%s-%s.png", opts->path, img->name, chid);
  377. meta[1].text = desc;
  378. meta[1].text_length = sizeof(desc);
  379. // Create writer
  380. rt_png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
  381. if (!rt_png_ptr) {
  382. png_destroy_write_struct(&rt_png_ptr, (png_infopp) NULL);
  383. fprintf(stderr, "Could not create a PNG writer\n");
  384. return 0;
  385. }
  386. rt_info_ptr = png_create_info_struct(rt_png_ptr);
  387. if (!rt_info_ptr) {
  388. png_destroy_write_struct(&rt_png_ptr, (png_infopp) NULL);
  389. fprintf(stderr, "Could not create a PNG writer\n");
  390. return 0;
  391. }
  392. // Greyscale image
  393. png_set_IHDR(rt_png_ptr, rt_info_ptr, width, height,
  394. 8, PNG_COLOR_TYPE_GRAY, PNG_INTERLACE_NONE,
  395. PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);
  396. png_set_text(rt_png_ptr, rt_info_ptr, meta, 3);
  397. // Channel = 25cm wide
  398. png_set_pHYs(rt_png_ptr, rt_info_ptr, 3636, 3636, PNG_RESOLUTION_METER);
  399. // Init I/O
  400. rt_pngfile = fopen(outName, "wb");
  401. if (!rt_pngfile) {
  402. fprintf(stderr, "Could not open %s for writing\n", outName);
  403. return 0;
  404. }
  405. png_init_io(rt_png_ptr, rt_pngfile);
  406. png_write_info(rt_png_ptr, rt_info_ptr);
  407. // Turn off compression
  408. png_set_compression_level(rt_png_ptr, 0);
  409. return 1;
  410. }
  411. void pushRow(float *row, int width){
  412. png_byte pix[width];
  413. for(int i = 0; i < width; i++)
  414. pix[i] = row[i];
  415. png_write_row(rt_png_ptr, (png_bytep) pix);
  416. }
  417. void closeWriter(){
  418. png_write_end(rt_png_ptr, rt_info_ptr);
  419. fclose(rt_pngfile);
  420. png_destroy_write_struct(&rt_png_ptr, &rt_info_ptr);
  421. }