Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.
 
 
 
 
 

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