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.
 
 
 
 
 

412 lines
12 KiB

  1. /*
  2. * This file is part of Aptdec.
  3. * Copyright (c) 2004-2009 Thierry Leconte (F4DWV), Xerbo (xerbo@protonmail.com) 2019-2022
  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 "pngio.h"
  20. #include <math.h>
  21. #include <png.h>
  22. #include <stdint.h>
  23. #include <stdio.h>
  24. #include <stdlib.h>
  25. #include <string.h>
  26. #include "util.h"
  27. int readRawImage(char *filename, float **prow, int *nrow) {
  28. FILE *fp = fopen(filename, "rb");
  29. printf("%s", filename);
  30. if (!fp) {
  31. error_noexit("Cannot open image");
  32. return 0;
  33. }
  34. // Create reader
  35. png_structp png = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
  36. if (!png) {
  37. fclose(fp);
  38. return 0;
  39. }
  40. png_infop info = png_create_info_struct(png);
  41. if (!info) {
  42. fclose(fp);
  43. return 0;
  44. }
  45. png_init_io(png, fp);
  46. // Read info from header
  47. png_read_info(png, info);
  48. int width = png_get_image_width(png, info);
  49. int height = png_get_image_height(png, info);
  50. png_byte color_type = png_get_color_type(png, info);
  51. png_byte bit_depth = png_get_bit_depth(png, info);
  52. // Check the image
  53. if (width != APT_IMG_WIDTH) {
  54. error_noexit("Raw image must be 2080px wide");
  55. return 0;
  56. } else if (bit_depth != 8) {
  57. error_noexit("Raw image must have 8 bit color");
  58. return 0;
  59. } else if (color_type != PNG_COLOR_TYPE_GRAY) {
  60. error_noexit("Raw image must be grayscale");
  61. return 0;
  62. }
  63. // Create row buffers
  64. png_bytep *PNGrows = NULL;
  65. PNGrows = (png_bytep *)malloc(sizeof(png_bytep) * height);
  66. for (int y = 0; y < height; y++) PNGrows[y] = (png_byte *)malloc(png_get_rowbytes(png, info));
  67. // Read image
  68. png_read_image(png, PNGrows);
  69. // Tidy up
  70. fclose(fp);
  71. png_destroy_read_struct(&png, &info, NULL);
  72. // Put into prow
  73. *nrow = height;
  74. for (int y = 0; y < height; y++) {
  75. prow[y] = (float *)malloc(sizeof(float) * width);
  76. for (int x = 0; x < width; x++) prow[y][x] = (float)PNGrows[y][x];
  77. }
  78. return 1;
  79. }
  80. int readPalette(char *filename, apt_rgb_t **pixels) {
  81. FILE *fp = fopen(filename, "rb");
  82. if (!fp) {
  83. error_noexit("Cannot open palette");
  84. return 0;
  85. }
  86. // Create reader
  87. png_structp png = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
  88. if (!png) {
  89. fclose(fp);
  90. return 0;
  91. }
  92. png_infop info = png_create_info_struct(png);
  93. if (!info) {
  94. fclose(fp);
  95. return 0;
  96. }
  97. png_init_io(png, fp);
  98. // Read info from header
  99. png_read_info(png, info);
  100. int width = png_get_image_width(png, info);
  101. int height = png_get_image_height(png, info);
  102. png_byte color_type = png_get_color_type(png, info);
  103. png_byte bit_depth = png_get_bit_depth(png, info);
  104. // Check the image
  105. if (width != 256 && height != 256) {
  106. error_noexit("Palette must be 256x256");
  107. return 0;
  108. } else if (bit_depth != 8) {
  109. error_noexit("Palette must be 8 bit color");
  110. return 0;
  111. } else if (color_type != PNG_COLOR_TYPE_RGB) {
  112. error_noexit("Palette must be RGB");
  113. return 0;
  114. }
  115. // Create row buffers
  116. png_bytep *PNGrows = NULL;
  117. PNGrows = (png_bytep *)malloc(sizeof(png_bytep) * height);
  118. for (int y = 0; y < height; y++) PNGrows[y] = (png_byte *)malloc(png_get_rowbytes(png, info));
  119. // Read image
  120. png_read_image(png, PNGrows);
  121. // Tidy up
  122. fclose(fp);
  123. png_destroy_read_struct(&png, &info, NULL);
  124. // Put into crow
  125. for (int y = 0; y < height; y++) {
  126. pixels[y] = (apt_rgb_t *)malloc(sizeof(apt_rgb_t) * width);
  127. for (int x = 0; x < width; x++)
  128. pixels[y][x] = (apt_rgb_t){PNGrows[y][x * 3], PNGrows[y][x * 3 + 1], PNGrows[y][x * 3 + 2]};
  129. }
  130. return 1;
  131. }
  132. void prow2crow(float **prow, int nrow, char *palette, apt_rgb_t **crow) {
  133. for (int y = 0; y < nrow; y++) {
  134. crow[y] = (apt_rgb_t *)malloc(sizeof(apt_rgb_t) * APT_IMG_WIDTH);
  135. for (int x = 0; x < APT_IMG_WIDTH; x++) {
  136. if (palette == NULL)
  137. crow[y][x].r = crow[y][x].g = crow[y][x].b = prow[y][x];
  138. else
  139. crow[y][x] = apt_applyPalette(palette, prow[y][x]);
  140. }
  141. }
  142. }
  143. int applyUserPalette(float **prow, int nrow, char *filename, apt_rgb_t **crow) {
  144. apt_rgb_t *pal_row[256];
  145. if (!readPalette(filename, pal_row)) {
  146. error_noexit("Could not read palette");
  147. return 0;
  148. }
  149. for (int y = 0; y < nrow; y++) {
  150. for (int x = 0; x < APT_CH_WIDTH; x++) {
  151. int cha = CLIP(prow[y][x + APT_CHA_OFFSET], 0, 255);
  152. int chb = CLIP(prow[y][x + APT_CHB_OFFSET], 0, 255);
  153. crow[y][x + APT_CHA_OFFSET] = pal_row[chb][cha];
  154. }
  155. }
  156. return 1;
  157. }
  158. int ImageOut(options_t *opts, apt_image_t *img, int offset, int width, char *desc, char chid, char *palette) {
  159. char outName[512];
  160. if (opts->filename == NULL || opts->filename[0] == '\0') {
  161. sprintf(outName, "%s/%s-%c.png", opts->path, img->name, chid);
  162. } else {
  163. sprintf(outName, "%s/%s", opts->path, opts->filename);
  164. }
  165. png_text meta[] = {{PNG_TEXT_COMPRESSION_NONE, "Software", VERSION, sizeof(VERSION)},
  166. {PNG_TEXT_COMPRESSION_NONE, "Channel", desc, sizeof(desc)},
  167. {PNG_TEXT_COMPRESSION_NONE, "Description", "NOAA satellite image", 20}};
  168. // Parse image type
  169. int greyscale = 1;
  170. switch (chid) {
  171. case Palleted:
  172. greyscale = 0;
  173. break;
  174. case Temperature:
  175. greyscale = 0;
  176. break;
  177. case Raw_Image:
  178. break;
  179. case Channel_A:
  180. break;
  181. case Channel_B:
  182. break;
  183. }
  184. // Parse effects
  185. int crop_telemetry = 0;
  186. for (unsigned long int i = 0; i < strlen(opts->effects); i++) {
  187. switch (opts->effects[i]) {
  188. case Crop_Telemetry:
  189. if (width == 2080) {
  190. width -= APT_TOTAL_TELE;
  191. offset += APT_SYNC_WIDTH + APT_SPC_WIDTH;
  192. crop_telemetry = 1;
  193. }
  194. break;
  195. case Precipitation_Overlay:
  196. greyscale = 0;
  197. break;
  198. case Flip_Image:
  199. break;
  200. case Denoise:
  201. break;
  202. case Histogram_Equalise:
  203. break;
  204. case Linear_Equalise:
  205. break;
  206. case Crop_Noise:
  207. break;
  208. default: {
  209. char text[100];
  210. sprintf(text, "Unrecognised effect, \"%c\"", opts->effects[i]);
  211. warning(text);
  212. break;
  213. }
  214. }
  215. }
  216. FILE *pngfile;
  217. // Create writer
  218. png_structp png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
  219. if (!png_ptr) {
  220. png_destroy_write_struct(&png_ptr, (png_infopp)NULL);
  221. error_noexit("Could not create a PNG writer");
  222. return 0;
  223. }
  224. png_infop info_ptr = png_create_info_struct(png_ptr);
  225. if (!info_ptr) {
  226. png_destroy_write_struct(&png_ptr, (png_infopp)NULL);
  227. error_noexit("Could not create a PNG writer");
  228. return 0;
  229. }
  230. if (greyscale) {
  231. // Greyscale image
  232. png_set_IHDR(png_ptr, info_ptr, width, img->nrow, 8, PNG_COLOR_TYPE_GRAY, PNG_INTERLACE_NONE,
  233. PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);
  234. } else {
  235. // 8 bit RGB image
  236. png_set_IHDR(png_ptr, info_ptr, width, img->nrow, 8, PNG_COLOR_TYPE_RGB, PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_DEFAULT,
  237. PNG_FILTER_TYPE_DEFAULT);
  238. }
  239. png_set_text(png_ptr, info_ptr, meta, 3);
  240. png_set_pHYs(png_ptr, info_ptr, 3636, 3636, PNG_RESOLUTION_METER);
  241. // Init I/O
  242. pngfile = fopen(outName, "wb");
  243. if (!pngfile) {
  244. error_noexit("Could not open PNG for writing");
  245. return 1;
  246. }
  247. png_init_io(png_ptr, pngfile);
  248. png_write_info(png_ptr, info_ptr);
  249. // Move prow into crow, crow ~ color rows, if required
  250. apt_rgb_t *crow[APT_MAX_HEIGHT];
  251. if (!greyscale) {
  252. prow2crow(img->prow, img->nrow, palette, crow);
  253. }
  254. // Apply a user provided color palette
  255. if (chid == Palleted) {
  256. applyUserPalette(img->prow, img->nrow, opts->palette, crow);
  257. }
  258. // Precipitation overlay
  259. if (CONTAINS(opts->effects, Precipitation_Overlay)) {
  260. for (int y = 0; y < img->nrow; y++) {
  261. for (int x = 0; x < APT_CH_WIDTH; x++) {
  262. if (img->prow[y][x + APT_CHB_OFFSET] >= 198)
  263. crow[y][x + APT_CHB_OFFSET] = crow[y][x + APT_CHA_OFFSET] =
  264. apt_applyPalette(apt_PrecipPalette, img->prow[y][x + APT_CHB_OFFSET] - 198);
  265. }
  266. }
  267. }
  268. printf("Writing %s", outName);
  269. // Float power macro (for gamma adjustment)
  270. #define POWF(a, b) (b == 1.0 ? a : exp(b * log(a)))
  271. float a = POWF(255, opts->gamma) / 255;
  272. // Build image
  273. for (int y = 0; y < img->nrow; y++) {
  274. png_color pix[APT_IMG_WIDTH]; // Color
  275. png_byte mpix[APT_IMG_WIDTH]; // Mono
  276. int skip = 0;
  277. for (int x = 0; x < width; x++) {
  278. if (crop_telemetry && x == APT_CH_WIDTH) skip += APT_TELE_WIDTH + APT_SYNC_WIDTH + APT_SPC_WIDTH;
  279. if (greyscale) {
  280. mpix[x] = POWF(img->prow[y][x + skip + offset], opts->gamma) / a;
  281. } else {
  282. pix[x] = (png_color){POWF(crow[y][x + skip + offset].r, opts->gamma) / a,
  283. POWF(crow[y][x + skip + offset].g, opts->gamma) / a,
  284. POWF(crow[y][x + skip + offset].b, opts->gamma) / a};
  285. }
  286. }
  287. if (greyscale) {
  288. png_write_row(png_ptr, (png_bytep)mpix);
  289. } else {
  290. png_write_row(png_ptr, (png_bytep)pix);
  291. }
  292. }
  293. // Tidy up
  294. png_write_end(png_ptr, info_ptr);
  295. fclose(pngfile);
  296. printf("\nDone\n");
  297. png_destroy_write_struct(&png_ptr, &info_ptr);
  298. return 1;
  299. }
  300. // TODO: clean up everthing below this comment
  301. png_structp rt_png_ptr;
  302. png_infop rt_info_ptr;
  303. FILE *rt_pngfile;
  304. int initWriter(options_t *opts, apt_image_t *img, int width, int height, char *desc, char *chid) {
  305. char outName[384];
  306. sprintf(outName, "%s/%s-%s.png", opts->path, img->name, chid);
  307. png_text meta[] = {{PNG_TEXT_COMPRESSION_NONE, "Software", VERSION, sizeof(VERSION)},
  308. {PNG_TEXT_COMPRESSION_NONE, "Channel", desc, sizeof(desc)},
  309. {PNG_TEXT_COMPRESSION_NONE, "Description", "NOAA satellite image", 20}};
  310. // Create writer
  311. rt_png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
  312. if (!rt_png_ptr) {
  313. png_destroy_write_struct(&rt_png_ptr, (png_infopp)NULL);
  314. error_noexit("Could not create a PNG writer");
  315. return 0;
  316. }
  317. rt_info_ptr = png_create_info_struct(rt_png_ptr);
  318. if (!rt_info_ptr) {
  319. png_destroy_write_struct(&rt_png_ptr, (png_infopp)NULL);
  320. error_noexit("Could not create a PNG writer");
  321. return 0;
  322. }
  323. // Greyscale image
  324. png_set_IHDR(rt_png_ptr, rt_info_ptr, width, height, 8, PNG_COLOR_TYPE_GRAY, PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_DEFAULT,
  325. PNG_FILTER_TYPE_DEFAULT);
  326. png_set_text(rt_png_ptr, rt_info_ptr, meta, 3);
  327. // Channel = 25cm wide
  328. png_set_pHYs(rt_png_ptr, rt_info_ptr, 3636, 3636, PNG_RESOLUTION_METER);
  329. // Init I/O
  330. rt_pngfile = fopen(outName, "wb");
  331. if (!rt_pngfile) {
  332. error_noexit("Could not open PNG for writing");
  333. return 0;
  334. }
  335. png_init_io(rt_png_ptr, rt_pngfile);
  336. png_write_info(rt_png_ptr, rt_info_ptr);
  337. // Turn off compression
  338. png_set_compression_level(rt_png_ptr, 0);
  339. return 1;
  340. }
  341. void pushRow(float *row, int width) {
  342. png_byte pix[APT_IMG_WIDTH];
  343. for (int i = 0; i < width; i++) pix[i] = row[i];
  344. png_write_row(rt_png_ptr, (png_bytep)pix);
  345. }
  346. void closeWriter() {
  347. png_write_end(rt_png_ptr, rt_info_ptr);
  348. fclose(rt_pngfile);
  349. png_destroy_write_struct(&rt_png_ptr, &rt_info_ptr);
  350. }