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