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