選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

pngio.c 9.0 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  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. extern int zenith;
  30. extern char PrecipPalette[256*3];
  31. extern rgb_t applyPalette(char *palette, int val);
  32. extern rgb_t RGBcomposite(rgb_t top, float top_a, rgb_t bottom, float bottom_a);
  33. int mapOverlay(char *filename, rgb_t **crow, int nrow, int zenith, int MCIR) {
  34. FILE *fp = fopen(filename, "rb");
  35. // Create reader
  36. png_structp png = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
  37. if(!png) return 0;
  38. png_infop info = png_create_info_struct(png);
  39. if(!info) return 0;
  40. png_init_io(png, fp);
  41. // Read info from header
  42. png_read_info(png, info);
  43. int width = png_get_image_width(png, info);
  44. int height = png_get_image_height(png, info);
  45. png_byte color_type = png_get_color_type(png, info);
  46. png_byte bit_depth = png_get_bit_depth(png, info);
  47. // Check the image
  48. if(width != 1040){
  49. fprintf(stderr, "Map must be 1040px wide.\n");
  50. return 0;
  51. }else if(bit_depth != 16){
  52. fprintf(stderr, "Map must be 16 bit color.\n");
  53. return 0;
  54. }else if(color_type != PNG_COLOR_TYPE_RGB){
  55. fprintf(stderr, "Map must be RGB.\n");
  56. return 0;
  57. }else if(zenith > height/2 || nrow-zenith > height/2){
  58. fprintf(stderr, "WARNING: Map is too short to cover entire image\n");
  59. }
  60. // Create row buffers
  61. png_bytep *mapRows = NULL;
  62. mapRows = (png_bytep *) malloc(sizeof(png_bytep) * height);
  63. for(int y = 0; y < height; y++)
  64. mapRows[y] = (png_byte *) malloc(png_get_rowbytes(png, info));
  65. // Read image
  66. png_read_image(png, mapRows);
  67. // Tidy up
  68. fclose(fp);
  69. png_destroy_read_struct(&png, &info, NULL);
  70. // Map overlay / MCIR / Precipitation
  71. int mapOffset = (height/2)-zenith;
  72. for(int y = 0; y < nrow; y++) {
  73. for(int x = 49; x < width - 82; x++){
  74. // Maps are 16 bit / channel
  75. png_bytep px = &mapRows[CLIP(y + mapOffset, 0, height)][x * 6];
  76. rgb_t map = {
  77. (px[0] << 8) | px[1],
  78. (px[2] << 8) | px[3],
  79. (px[4] << 8) | px[5]
  80. };
  81. // Pixel offsets
  82. int chb = x + CHB_OFFSET - 49;
  83. int cha = x + 36;
  84. // Fill in map
  85. if(MCIR){
  86. if(map.b < 128 && map.g > 128){
  87. // Land
  88. float green = CLIP((map.g-256)/32.0, 0, 1);
  89. float blue = 1-CLIP((map.b-32)/64.0, 0, 1);
  90. crow[y][cha] = (rgb_t){50 + blue*50, 80 + green*70, 64};
  91. }else{
  92. // Sea
  93. crow[y][cha] = (rgb_t){12, 30, 85};
  94. }
  95. }
  96. // Color -> alpha: composite
  97. int composite = map.r + map.g + map.b;
  98. // Color -> alpha: flattern and convert to 8 bits / channel
  99. float factor = (255 * 255 * 2.0) / composite;
  100. map.r *= factor/257.0; map.g *= factor/257.0; map.b *= factor/257.0;
  101. // Color -> alpha: convert black to alpha
  102. float alpha = CLIP(composite / 65535.0, 0, 1);
  103. // Map overlay on channel A
  104. crow[y][cha] = RGBcomposite(map, alpha, crow[y][cha], 1);
  105. // Map overlay on channel B
  106. if(!MCIR)
  107. crow[y][chb] = RGBcomposite(map, alpha, crow[y][chb], 1);
  108. // Cloud overlay on channel A
  109. if(MCIR){
  110. float cloud = CLIP((crow[y][chb].r - 115) / 107, 0, 1);
  111. crow[y][cha] = RGBcomposite((rgb_t){240, 250, 255}, cloud, crow[y][cha], 1);
  112. }
  113. }
  114. }
  115. return 1;
  116. }
  117. int readRawImage(char *filename, float **prow, int *nrow) {
  118. FILE *fp = fopen(filename, "r");
  119. // Create reader
  120. png_structp png = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
  121. if(!png) return 0;
  122. png_infop info = png_create_info_struct(png);
  123. if(!info) return 0;
  124. png_init_io(png, fp);
  125. // Read info from header
  126. png_read_info(png, info);
  127. int width = png_get_image_width(png, info);
  128. int height = png_get_image_height(png, info);
  129. png_byte color_type = png_get_color_type(png, info);
  130. png_byte bit_depth = png_get_bit_depth(png, info);
  131. // Check the image
  132. if(width != IMG_WIDTH){
  133. fprintf(stderr, "Raw image must be %ipx wide.\n", IMG_WIDTH);
  134. return 0;
  135. }else if(bit_depth != 8){
  136. fprintf(stderr, "Raw image must have 8 bit color.\n");
  137. return 0;
  138. }else if(color_type != PNG_COLOR_TYPE_GRAY){
  139. fprintf(stderr, "Raw image must be grayscale.\n");
  140. return 0;
  141. }
  142. // Create row buffers
  143. png_bytep *PNGrows = NULL;
  144. PNGrows = (png_bytep *) malloc(sizeof(png_bytep) * height);
  145. for(int y = 0; y < height; y++) PNGrows[y] = (png_byte *)
  146. malloc(png_get_rowbytes(png, info));
  147. // Read image
  148. png_read_image(png, PNGrows);
  149. // Tidy up
  150. fclose(fp);
  151. png_destroy_read_struct(&png, &info, NULL);
  152. // Put into prow
  153. *nrow = height;
  154. for(int y = 0; y < height; y++) {
  155. prow[y] = (float *) malloc(sizeof(float) * width);
  156. for(int x = 0; x < width; x++)
  157. prow[y][x] = (float)PNGrows[y][x];
  158. }
  159. return 1;
  160. }
  161. typedef struct {
  162. float *prow[3000]; // Row buffers
  163. int nrow; // Number of rows
  164. int chA, chB; // ID of each channel
  165. char name[256]; // Stripped filename
  166. } image_t;
  167. typedef struct {
  168. char *type; // Output image type
  169. char *effects;
  170. int satnum; // The satellite number
  171. char *map; // Path to a map file
  172. char *path; // Output directory
  173. } options_t;
  174. //int ImageOut(char *filename, char *chid, float **prow, int nrow, int width, int offset, char *palette, char *effects, char *mapFile) {
  175. int ImageOut(options_t *opts, image_t *img, int offset, int width, char *desc, char *chid, char *palette){
  176. char outName[384];
  177. sprintf(outName, "%s/%s-%s.png", opts->path, img->name, chid);
  178. FILE *pngfile;
  179. png_text text_ptr[] = {
  180. {PNG_TEXT_COMPRESSION_NONE, "Software", VERSION},
  181. {PNG_TEXT_COMPRESSION_NONE, "Channel", desc, sizeof(desc)},
  182. {PNG_TEXT_COMPRESSION_NONE, "Description", "NOAA satellite image", 20}
  183. };
  184. // Reduce the width of the image to componsate for the missing telemetry
  185. if(opts->effects != NULL && CONTAINS(opts->effects, 't'))
  186. width -= TOTAL_TELE;
  187. // Create writer
  188. png_structp png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
  189. if (!png_ptr) {
  190. png_destroy_write_struct(&png_ptr, (png_infopp) NULL);
  191. fprintf(stderr, ERR_PNG_WRITE);
  192. return(0);
  193. }
  194. png_infop info_ptr = png_create_info_struct(png_ptr);
  195. if (!info_ptr) {
  196. png_destroy_write_struct(&png_ptr, (png_infopp) NULL);
  197. fprintf(stderr, ERR_PNG_INFO);
  198. return(0);
  199. }
  200. // 8 bit RGB image
  201. png_set_IHDR(png_ptr, info_ptr, width, img->nrow,
  202. 8, PNG_COLOR_TYPE_RGB, PNG_INTERLACE_NONE,
  203. PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);
  204. png_set_text(png_ptr, info_ptr, text_ptr, 3);
  205. // Channel = 25cm wide
  206. png_set_pHYs(png_ptr, info_ptr, 3636, 3636, PNG_RESOLUTION_METER);
  207. // Init I/O
  208. pngfile = fopen(outName, "wb");
  209. if (!pngfile) {
  210. fprintf(stderr, ERR_FILE_WRITE, outName);
  211. return(1);
  212. }
  213. png_init_io(png_ptr, pngfile);
  214. png_write_info(png_ptr, info_ptr);
  215. // Move prow into crow, crow ~ color rows
  216. rgb_t *crow[img->nrow];
  217. for(int y = 0; y < img->nrow; y++){
  218. crow[y] = (rgb_t *) malloc(sizeof(rgb_t) * IMG_WIDTH);
  219. for(int x = 0; x < IMG_WIDTH; x++){
  220. if(palette == NULL){
  221. crow[y][x].r = crow[y][x].g = crow[y][x].b = CLIP(img->prow[y][x], 0, 255);
  222. }else{
  223. crow[y][x] = applyPalette(palette, img->prow[y][x]);
  224. }
  225. }
  226. }
  227. // Precipitation
  228. // TODO: use temperature calibration for accuracy
  229. if(CONTAINS(opts->effects, 'p')){
  230. for(int y = 0; y < img->nrow; y++){
  231. for(int x = 0; x < CH_WIDTH; x++){
  232. if(img->prow[y][x + CHB_OFFSET] > 191)
  233. crow[y][x + CHB_OFFSET] = applyPalette(PrecipPalette, img->prow[y][x + CHB_OFFSET]);
  234. }
  235. }
  236. }
  237. if(opts->map != NULL && opts->map[0] != '\0'){
  238. if(mapOverlay(opts->map, crow, img->nrow, zenith, strcmp(chid, "MCIR") == 0) == 0){
  239. fprintf(stderr, "Skipping MCIR generation; see above.\n");
  240. return 0;
  241. }
  242. }else if(strcmp(chid, "MCIR") == 0){
  243. fprintf(stderr, "Skipping MCIR generation; no map provided.\n");
  244. return 0;
  245. }
  246. //extern void falsecolor(float vis, float temp, float *r, float *g, float *b);
  247. printf("Writing %s", outName);
  248. int fcimage = strcmp(chid, "False Color") == 0;
  249. // Build RGB image
  250. for (int y = 0; y < img->nrow; y++) {
  251. png_color pix[width];
  252. for (int x = 0; x < width; x++) {
  253. pix[x].red = (int)crow[y][x + offset].r;
  254. pix[x].green = (int)crow[y][x + offset].g;
  255. pix[x].blue = (int)crow[y][x + offset].b;
  256. }
  257. png_write_row(png_ptr, (png_bytep) pix);
  258. }
  259. // Tidy up
  260. png_write_end(png_ptr, info_ptr);
  261. fclose(pngfile);
  262. printf("\nDone\n");
  263. png_destroy_write_struct(&png_ptr, &info_ptr);
  264. return(1);
  265. }