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.

pngio.c 14 KiB

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