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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544
  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 = prow[y][x + CHA_OFFSET];
  256. int chb = prow[y][x + CHB_OFFSET];
  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. default:
  307. fprintf(stderr, "NOTICE: Unrecognised effect, \"%c\"\n", opts->effects[i]);
  308. break;
  309. }
  310. }
  311. if(opts->map != NULL && opts->map[0] != '\0'){
  312. greyscale = 0;
  313. }
  314. FILE *pngfile;
  315. // Create writer
  316. png_structp png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
  317. if (!png_ptr) {
  318. png_destroy_write_struct(&png_ptr, (png_infopp) NULL);
  319. fprintf(stderr, "Could not create a PNG writer\n");
  320. return 0;
  321. }
  322. png_infop info_ptr = png_create_info_struct(png_ptr);
  323. if (!info_ptr) {
  324. png_destroy_write_struct(&png_ptr, (png_infopp) NULL);
  325. fprintf(stderr, "Could not create a PNG writer\n");
  326. return 0;
  327. }
  328. if(greyscale){
  329. // Greyscale image
  330. png_set_IHDR(png_ptr, info_ptr, width, img->nrow,
  331. 8, PNG_COLOR_TYPE_GRAY, PNG_INTERLACE_NONE,
  332. PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);
  333. }else{
  334. // 8 bit RGB image
  335. png_set_IHDR(png_ptr, info_ptr, width, img->nrow,
  336. 8, PNG_COLOR_TYPE_RGB, PNG_INTERLACE_NONE,
  337. PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);
  338. }
  339. png_set_text(png_ptr, info_ptr, meta, 3);
  340. png_set_pHYs(png_ptr, info_ptr, 3636, 3636, PNG_RESOLUTION_METER);
  341. // Init I/O
  342. pngfile = fopen(outName, "wb");
  343. if (!pngfile) {
  344. fprintf(stderr, "Could not open %s for writing\n", outName);
  345. return 1;
  346. }
  347. png_init_io(png_ptr, pngfile);
  348. png_write_info(png_ptr, info_ptr);
  349. // Move prow into crow, crow ~ color rows, if required
  350. rgb_t *crow[MAX_HEIGHT];
  351. if(!greyscale){
  352. prow2crow(img->prow, img->nrow, palette, crow);
  353. }
  354. // Apply a user provided color palette
  355. if(chid == Palleted){
  356. applyUserPalette(img->prow, img->nrow, opts->palette, crow);
  357. }
  358. // Precipitation overlay
  359. if(CONTAINS(opts->effects, Precipitation_Overlay)){
  360. for(int y = 0; y < img->nrow; y++){
  361. for(int x = 0; x < CH_WIDTH; x++){
  362. if(img->prow[y][x + CHB_OFFSET] >= 198)
  363. crow[y][x + CHB_OFFSET] = crow[y][x + CHA_OFFSET] = applyPalette(PrecipPalette, img->prow[y][x + CHB_OFFSET]-198);
  364. }
  365. }
  366. }
  367. // Map stuff
  368. if(opts->map != NULL && opts->map[0] != '\0'){
  369. if(!mapOverlay(opts->map, crow, img->nrow, img->zenith, CONTAINS(opts->type, MCIR))){
  370. fprintf(stderr, "Skipping MCIR generation.\n");
  371. return 0;
  372. }
  373. }else if(CONTAINS(opts->type, MCIR)){
  374. fprintf(stderr, "Skipping MCIR generation; no map provided.\n");
  375. return 0;
  376. }
  377. printf("Writing %s", outName);
  378. // Float power macro (for gamma adjustment)
  379. #define POWF(a, b) (b == 1.0 ? a : exp(b * log(a)))
  380. float a = POWF(255, opts->gamma)/255;
  381. // Build image
  382. for (int y = 0; y < img->nrow; y++) {
  383. png_color pix[width]; // Color
  384. png_byte mpix[width]; // Mono
  385. int skip = 0;
  386. for (int x = 0; x < width; x++) {
  387. if(crop_telemetry && x == CH_WIDTH)
  388. skip += TELE_WIDTH + SYNC_WIDTH + SPC_WIDTH;
  389. if(greyscale){
  390. mpix[x] = POWF(img->prow[y][x + skip + offset], opts->gamma)/a;
  391. }else{
  392. pix[x] = (png_color){
  393. POWF(crow[y][x + skip + offset].r, opts->gamma)/a,
  394. POWF(crow[y][x + skip + offset].g, opts->gamma)/a,
  395. POWF(crow[y][x + skip + offset].b, opts->gamma)/a
  396. };
  397. }
  398. }
  399. if(greyscale){
  400. png_write_row(png_ptr, (png_bytep) mpix);
  401. }else{
  402. png_write_row(png_ptr, (png_bytep) pix);
  403. }
  404. }
  405. // Tidy up
  406. png_write_end(png_ptr, info_ptr);
  407. fclose(pngfile);
  408. printf("\nDone\n");
  409. png_destroy_write_struct(&png_ptr, &info_ptr);
  410. return 1;
  411. }
  412. // TODO: clean up everthing below this comment
  413. png_structp rt_png_ptr;
  414. png_infop rt_info_ptr;
  415. FILE *rt_pngfile;
  416. int initWriter(options_t *opts, image_t *img, int width, int height, char *desc, char *chid){
  417. char outName[384];
  418. sprintf(outName, "%s/%s-%s.png", opts->path, img->name, chid);
  419. png_text meta[] = {
  420. {PNG_TEXT_COMPRESSION_NONE, "Software", VERSION},
  421. {PNG_TEXT_COMPRESSION_NONE, "Channel", desc, sizeof(desc)},
  422. {PNG_TEXT_COMPRESSION_NONE, "Description", "NOAA satellite image", 20}
  423. };
  424. // Create writer
  425. rt_png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
  426. if (!rt_png_ptr) {
  427. png_destroy_write_struct(&rt_png_ptr, (png_infopp) NULL);
  428. fprintf(stderr, "Could not create a PNG writer\n");
  429. return 0;
  430. }
  431. rt_info_ptr = png_create_info_struct(rt_png_ptr);
  432. if (!rt_info_ptr) {
  433. png_destroy_write_struct(&rt_png_ptr, (png_infopp) NULL);
  434. fprintf(stderr, "Could not create a PNG writer\n");
  435. return 0;
  436. }
  437. // Greyscale image
  438. png_set_IHDR(rt_png_ptr, rt_info_ptr, width, height,
  439. 8, PNG_COLOR_TYPE_GRAY, PNG_INTERLACE_NONE,
  440. PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);
  441. png_set_text(rt_png_ptr, rt_info_ptr, meta, 3);
  442. // Channel = 25cm wide
  443. png_set_pHYs(rt_png_ptr, rt_info_ptr, 3636, 3636, PNG_RESOLUTION_METER);
  444. // Init I/O
  445. rt_pngfile = fopen(outName, "wb");
  446. if (!rt_pngfile) {
  447. fprintf(stderr, "Could not open %s for writing\n", outName);
  448. return 0;
  449. }
  450. png_init_io(rt_png_ptr, rt_pngfile);
  451. png_write_info(rt_png_ptr, rt_info_ptr);
  452. // Turn off compression
  453. png_set_compression_level(rt_png_ptr, 0);
  454. return 1;
  455. }
  456. void pushRow(float *row, int width){
  457. png_byte pix[width];
  458. for(int i = 0; i < width; i++)
  459. pix[i] = row[i];
  460. png_write_row(rt_png_ptr, (png_bytep) pix);
  461. }
  462. void closeWriter(){
  463. png_write_end(rt_png_ptr, rt_info_ptr);
  464. fclose(rt_pngfile);
  465. png_destroy_write_struct(&rt_png_ptr, &rt_info_ptr);
  466. }