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 11 KiB

4 years ago
4 years ago
4 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  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 "offsets.h"
  25. #include "messages.h"
  26. typedef struct {
  27. float r, g, b;
  28. } rgb_t;
  29. typedef struct {
  30. float *prow[MAX_HEIGHT]; // Row buffers
  31. int nrow; // Number of rows
  32. int chA, chB; // ID of each channel
  33. char name[256]; // Stripped filename
  34. } image_t;
  35. typedef struct {
  36. char *type; // Output image type
  37. char *effects;
  38. int satnum; // The satellite number
  39. char *map; // Path to a map file
  40. char *path; // Output directory
  41. int realtime;
  42. } options_t;
  43. extern int zenith;
  44. extern char PrecipPalette[256*3];
  45. extern rgb_t applyPalette(char *palette, int val);
  46. extern rgb_t RGBcomposite(rgb_t top, float top_a, rgb_t bottom, float bottom_a);
  47. extern rgb_t falsecolor(float vis, float temp);
  48. int mapOverlay(char *filename, rgb_t **crow, int nrow, int zenith, int MCIR) {
  49. FILE *fp = fopen(filename, "rb");
  50. // Create reader
  51. png_structp png = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
  52. if(!png) return 0;
  53. png_infop info = png_create_info_struct(png);
  54. if(!info) return 0;
  55. png_init_io(png, fp);
  56. // Read info from header
  57. png_read_info(png, info);
  58. int width = png_get_image_width(png, info);
  59. int height = png_get_image_height(png, info);
  60. png_byte color_type = png_get_color_type(png, info);
  61. png_byte bit_depth = png_get_bit_depth(png, info);
  62. // Check the image
  63. if(width != 1040){
  64. fprintf(stderr, "Map must be 1040px wide.\n");
  65. return 0;
  66. }else if(bit_depth != 16){
  67. fprintf(stderr, "Map must be 16 bit color.\n");
  68. return 0;
  69. }else if(color_type != PNG_COLOR_TYPE_RGB){
  70. fprintf(stderr, "Map must be RGB.\n");
  71. return 0;
  72. }else if(zenith > height/2 || nrow-zenith > height/2){
  73. fprintf(stderr, "WARNING: Map is too short to cover entire image\n");
  74. }
  75. // Create row buffers
  76. png_bytep *mapRows = NULL;
  77. mapRows = (png_bytep *) malloc(sizeof(png_bytep) * height);
  78. for(int y = 0; y < height; y++)
  79. mapRows[y] = (png_byte *) malloc(png_get_rowbytes(png, info));
  80. // Read image
  81. png_read_image(png, mapRows);
  82. // Tidy up
  83. fclose(fp);
  84. png_destroy_read_struct(&png, &info, NULL);
  85. // Map overlay / MCIR / Precipitation
  86. int mapOffset = (height/2)-zenith;
  87. for(int y = 0; y < nrow; y++) {
  88. for(int x = 49; x < width - 82; x++){
  89. // Maps are 16 bit / channel
  90. png_bytep px = &mapRows[CLIP(y + mapOffset, 0, height)][x * 6];
  91. rgb_t map = {
  92. (px[0] << 8) | px[1],
  93. (px[2] << 8) | px[3],
  94. (px[4] << 8) | px[5]
  95. };
  96. // Pixel offsets
  97. int chb = x + CHB_OFFSET - 49;
  98. int cha = x + 36;
  99. // Fill in map
  100. if(MCIR){
  101. if(map.b < 128 && map.g > 128){
  102. // Land
  103. float green = CLIP((map.g-256)/32.0, 0, 1);
  104. float blue = 1-CLIP((map.b-32)/64.0, 0, 1);
  105. crow[y][cha] = (rgb_t){50 + blue*50, 80 + green*70, 64};
  106. }else{
  107. // Sea
  108. crow[y][cha] = (rgb_t){12, 30, 85};
  109. }
  110. }
  111. // Color -> alpha: composite
  112. int composite = map.r + map.g + map.b;
  113. // Color -> alpha: flattern and convert to 8 bits / channel
  114. float factor = (255 * 255 * 2.0) / composite;
  115. map.r *= factor/257.0; map.g *= factor/257.0; map.b *= factor/257.0;
  116. // Color -> alpha: convert black to alpha
  117. float alpha = CLIP(composite / 65535.0, 0, 1);
  118. // Map overlay on channel A
  119. crow[y][cha] = RGBcomposite(map, alpha, crow[y][cha], 1);
  120. // Map overlay on channel B
  121. if(!MCIR)
  122. crow[y][chb] = RGBcomposite(map, alpha, crow[y][chb], 1);
  123. // Cloud overlay on channel A
  124. if(MCIR){
  125. float cloud = CLIP((crow[y][chb].r - 115) / 107, 0, 1);
  126. crow[y][cha] = RGBcomposite((rgb_t){240, 250, 255}, cloud, crow[y][cha], 1);
  127. }
  128. }
  129. }
  130. return 1;
  131. }
  132. int readRawImage(char *filename, float **prow, int *nrow) {
  133. FILE *fp = fopen(filename, "r");
  134. // Create reader
  135. png_structp png = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
  136. if(!png) return 0;
  137. png_infop info = png_create_info_struct(png);
  138. if(!info) return 0;
  139. png_init_io(png, fp);
  140. // Read info from header
  141. png_read_info(png, info);
  142. int width = png_get_image_width(png, info);
  143. int height = png_get_image_height(png, info);
  144. png_byte color_type = png_get_color_type(png, info);
  145. png_byte bit_depth = png_get_bit_depth(png, info);
  146. // Check the image
  147. if(width != IMG_WIDTH){
  148. fprintf(stderr, "Raw image must be %ipx wide.\n", IMG_WIDTH);
  149. return 0;
  150. }else if(bit_depth != 8){
  151. fprintf(stderr, "Raw image must have 8 bit color.\n");
  152. return 0;
  153. }else if(color_type != PNG_COLOR_TYPE_GRAY){
  154. fprintf(stderr, "Raw image must be grayscale.\n");
  155. return 0;
  156. }
  157. // Create row buffers
  158. png_bytep *PNGrows = NULL;
  159. PNGrows = (png_bytep *) malloc(sizeof(png_bytep) * height);
  160. for(int y = 0; y < height; y++) PNGrows[y] = (png_byte *)
  161. malloc(png_get_rowbytes(png, info));
  162. // Read image
  163. png_read_image(png, PNGrows);
  164. // Tidy up
  165. fclose(fp);
  166. png_destroy_read_struct(&png, &info, NULL);
  167. // Put into prow
  168. *nrow = height;
  169. for(int y = 0; y < height; y++) {
  170. prow[y] = (float *) malloc(sizeof(float) * width);
  171. for(int x = 0; x < width; x++)
  172. prow[y][x] = (float)PNGrows[y][x];
  173. }
  174. return 1;
  175. }
  176. png_text text_ptr[] = {
  177. {PNG_TEXT_COMPRESSION_NONE, "Software", VERSION},
  178. {PNG_TEXT_COMPRESSION_NONE, "Channel", "Unknown", 7},
  179. {PNG_TEXT_COMPRESSION_NONE, "Description", "NOAA satellite image", 20}
  180. };
  181. int ImageOut(options_t *opts, image_t *img, int offset, int width, char *desc, char *chid, char *palette){
  182. char outName[384];
  183. sprintf(outName, "%s/%s-%s.png", opts->path, img->name, chid);
  184. text_ptr[1].text = desc;
  185. text_ptr[1].text_length = sizeof(desc);
  186. FILE *pngfile;
  187. // Reduce the width of the image to componsate for the missing telemetry
  188. int fcimage = strcmp(desc, "False Color") == 0;
  189. int skiptele = 0;
  190. if(opts->effects != NULL && CONTAINS(opts->effects, 't')){
  191. width -= TOTAL_TELE;
  192. skiptele = 1;
  193. }
  194. // Create writer
  195. png_structp png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
  196. if (!png_ptr) {
  197. png_destroy_write_struct(&png_ptr, (png_infopp) NULL);
  198. fprintf(stderr, ERR_PNG_WRITE);
  199. return 0;
  200. }
  201. png_infop info_ptr = png_create_info_struct(png_ptr);
  202. if (!info_ptr) {
  203. png_destroy_write_struct(&png_ptr, (png_infopp) NULL);
  204. fprintf(stderr, ERR_PNG_INFO);
  205. return 0;
  206. }
  207. int greyscale = 0;
  208. if(palette == NULL && !CONTAINS(opts->effects, 'p') && !fcimage && opts->map[0] == '\0' && strcmp(chid, "MCIR") != 0){
  209. greyscale = 1;
  210. // Greyscale image
  211. png_set_IHDR(png_ptr, info_ptr, width, img->nrow,
  212. 8, PNG_COLOR_TYPE_GRAY, PNG_INTERLACE_NONE,
  213. PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);
  214. }else{
  215. // 8 bit RGB image
  216. png_set_IHDR(png_ptr, info_ptr, width, img->nrow,
  217. 8, PNG_COLOR_TYPE_RGB, PNG_INTERLACE_NONE,
  218. PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);
  219. }
  220. png_set_text(png_ptr, info_ptr, text_ptr, 3);
  221. png_set_pHYs(png_ptr, info_ptr, 3636, 3636, PNG_RESOLUTION_METER);
  222. // Init I/O
  223. pngfile = fopen(outName, "wb");
  224. if (!pngfile) {
  225. fprintf(stderr, ERR_FILE_WRITE, outName);
  226. return 1;
  227. }
  228. png_init_io(png_ptr, pngfile);
  229. png_write_info(png_ptr, info_ptr);
  230. // Move prow into crow, crow ~ color rows
  231. rgb_t *crow[img->nrow];
  232. if(!greyscale){
  233. for(int y = 0; y < img->nrow; y++){
  234. crow[y] = (rgb_t *) malloc(sizeof(rgb_t) * IMG_WIDTH);
  235. for(int x = 0; x < IMG_WIDTH; x++){
  236. if(palette == NULL)
  237. crow[y][x].r = crow[y][x].g = crow[y][x].b = CLIP(img->prow[y][x], 0, 255);
  238. else
  239. crow[y][x] = applyPalette(palette, img->prow[y][x]);
  240. }
  241. }
  242. }
  243. // Precipitation
  244. // TODO: use temperature calibration
  245. if(CONTAINS(opts->effects, 'p')){
  246. for(int y = 0; y < img->nrow; y++){
  247. for(int x = 0; x < CH_WIDTH; x++){
  248. if(img->prow[y][x + CHB_OFFSET] > 191)
  249. crow[y][x + CHB_OFFSET] = applyPalette(PrecipPalette, img->prow[y][x + CHB_OFFSET]);
  250. }
  251. }
  252. }
  253. // Map stuff
  254. if(opts->map != NULL && opts->map[0] != '\0'){
  255. if(mapOverlay(opts->map, crow, img->nrow, zenith, strcmp(chid, "MCIR") == 0) == 0){
  256. fprintf(stderr, "Skipping MCIR generation; see above.\n");
  257. return 0;
  258. }
  259. }else if(strcmp(chid, "MCIR") == 0){
  260. fprintf(stderr, "Skipping MCIR generation; no map provided.\n");
  261. return 0;
  262. }
  263. printf("Writing %s", outName);
  264. // Build image
  265. for (int y = 0; y < img->nrow; y++) {
  266. png_color pix[width];
  267. png_byte gpix[width];
  268. int skip = 0;
  269. for (int x = 0; x < width; x++) {
  270. if(skiptele){
  271. switch (x) {
  272. case 0:
  273. skip += SYNC_WIDTH + SPC_WIDTH;
  274. break;
  275. case CH_WIDTH:
  276. skip += TELE_WIDTH + SYNC_WIDTH + SPC_WIDTH;
  277. break;
  278. }
  279. }
  280. if(greyscale){
  281. gpix[x] = img->prow[y][x + skip + offset];
  282. }else if(fcimage){
  283. rgb_t pixel = falsecolor(img->prow[y][x + CHA_OFFSET], img->prow[y][x + CHB_OFFSET]);
  284. pix[x] = (png_color){pixel.r, pixel.g, pixel.b};
  285. }else{
  286. pix[x] = (png_color){crow[y][x + skip + offset].r, crow[y][x + skip + offset].g, crow[y][x + skip + offset].b};
  287. }
  288. }
  289. if(greyscale){
  290. png_write_row(png_ptr, (png_bytep) gpix);
  291. }else{
  292. png_write_row(png_ptr, (png_bytep) pix);
  293. }
  294. }
  295. // Tidy up
  296. png_write_end(png_ptr, info_ptr);
  297. fclose(pngfile);
  298. printf("\nDone\n");
  299. png_destroy_write_struct(&png_ptr, &info_ptr);
  300. return 1;
  301. }
  302. // TODO: remove these from the global scope
  303. png_structp rt_png_ptr;
  304. png_infop rt_info_ptr;
  305. FILE *rt_pngfile;
  306. int initWriter(options_t *opts, image_t *img, int width, int height, char *desc, char *chid){
  307. char outName[384];
  308. sprintf(outName, "%s/%s-%s.png", opts->path, img->name, chid);
  309. text_ptr[1].text = desc;
  310. text_ptr[1].text_length = sizeof(desc);
  311. // Create writer
  312. rt_png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
  313. if (!rt_png_ptr) {
  314. png_destroy_write_struct(&rt_png_ptr, (png_infopp) NULL);
  315. fprintf(stderr, ERR_PNG_WRITE);
  316. return 0;
  317. }
  318. rt_info_ptr = png_create_info_struct(rt_png_ptr);
  319. if (!rt_info_ptr) {
  320. png_destroy_write_struct(&rt_png_ptr, (png_infopp) NULL);
  321. fprintf(stderr, ERR_PNG_INFO);
  322. return 0;
  323. }
  324. // Greyscale image
  325. png_set_IHDR(rt_png_ptr, rt_info_ptr, width, height,
  326. 8, PNG_COLOR_TYPE_GRAY, PNG_INTERLACE_NONE,
  327. PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);
  328. png_set_text(rt_png_ptr, rt_info_ptr, text_ptr, 3);
  329. // Channel = 25cm wide
  330. png_set_pHYs(rt_png_ptr, rt_info_ptr, 3636, 3636, PNG_RESOLUTION_METER);
  331. // Init I/O
  332. rt_pngfile = fopen(outName, "wb");
  333. if (!rt_pngfile) {
  334. fprintf(stderr, ERR_FILE_WRITE, outName);
  335. return 0;
  336. }
  337. png_init_io(rt_png_ptr, rt_pngfile);
  338. png_write_info(rt_png_ptr, rt_info_ptr);
  339. return 1;
  340. }
  341. void pushRow(float *row, int width){
  342. png_byte pix[width];
  343. for(int i = 0; i < width; i++)
  344. pix[i] = row[i];
  345. png_write_row(rt_png_ptr, (png_bytep) pix);
  346. }
  347. void closeWriter(){
  348. png_write_end(rt_png_ptr, rt_info_ptr);
  349. fclose(rt_pngfile);
  350. png_destroy_write_struct(&rt_png_ptr, &rt_info_ptr);
  351. }