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.

204 lines
6.7KB

  1. /*
  2. * FITS implementation of common functions
  3. * Copyright (c) 2017 Paras Chadha
  4. *
  5. * This file is part of FFmpeg.
  6. *
  7. * FFmpeg is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * FFmpeg is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with FFmpeg; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. #include "avcodec.h"
  22. #include "libavutil/dict.h"
  23. #include "fits.h"
  24. int avpriv_fits_header_init(FITSHeader *header, FITSHeaderState state)
  25. {
  26. header->state = state;
  27. header->naxis_index = 0;
  28. header->blank_found = 0;
  29. header->pcount = 0;
  30. header->gcount = 1;
  31. header->groups = 0;
  32. header->rgb = 0;
  33. header->image_extension = 0;
  34. header->bscale = 1.0;
  35. header->bzero = 0;
  36. header->data_min_found = 0;
  37. header->data_max_found = 0;
  38. return 0;
  39. }
  40. static int dict_set_if_not_null(AVDictionary ***metadata, char *keyword, char *value)
  41. {
  42. if (metadata)
  43. av_dict_set(*metadata, keyword, value, 0);
  44. return 0;
  45. }
  46. /**
  47. * Extract keyword and value from a header line (80 bytes) and store them in keyword and value strings respectively
  48. * @param ptr8 pointer to the data
  49. * @param keyword pointer to the char array in which keyword is to be stored
  50. * @param value pointer to the char array in which value is to be stored
  51. * @return 0 if calculated successfully otherwise AVERROR_INVALIDDATA
  52. */
  53. static int read_keyword_value(const uint8_t *ptr8, char *keyword, char *value)
  54. {
  55. int i;
  56. for (i = 0; i < 8 && ptr8[i] != ' '; i++) {
  57. keyword[i] = ptr8[i];
  58. }
  59. keyword[i] = '\0';
  60. if (ptr8[8] == '=') {
  61. i = 10;
  62. while (i < 80 && ptr8[i] == ' ') {
  63. i++;
  64. }
  65. if (i < 80) {
  66. *value++ = ptr8[i];
  67. i++;
  68. if (ptr8[i-1] == '\'') {
  69. for (; i < 80 && ptr8[i] != '\''; i++) {
  70. *value++ = ptr8[i];
  71. }
  72. *value++ = '\'';
  73. } else if (ptr8[i-1] == '(') {
  74. for (; i < 80 && ptr8[i] != ')'; i++) {
  75. *value++ = ptr8[i];
  76. }
  77. *value++ = ')';
  78. } else {
  79. for (; i < 80 && ptr8[i] != ' ' && ptr8[i] != '/'; i++) {
  80. *value++ = ptr8[i];
  81. }
  82. }
  83. }
  84. }
  85. *value = '\0';
  86. return 0;
  87. }
  88. #define CHECK_KEYWORD(key) \
  89. if (strcmp(keyword, key)) { \
  90. av_log(avcl, AV_LOG_ERROR, "expected %s keyword, found %s = %s\n", key, keyword, value); \
  91. return AVERROR_INVALIDDATA; \
  92. }
  93. #define CHECK_VALUE(key, val) \
  94. if (sscanf(value, "%d", &header->val) != 1) { \
  95. av_log(avcl, AV_LOG_ERROR, "invalid value of %s keyword, %s = %s\n", key, keyword, value); \
  96. return AVERROR_INVALIDDATA; \
  97. }
  98. int avpriv_fits_header_parse_line(void *avcl, FITSHeader *header, const uint8_t line[80], AVDictionary ***metadata)
  99. {
  100. int dim_no, ret;
  101. int64_t t;
  102. double d;
  103. char keyword[10], value[72], c;
  104. read_keyword_value(line, keyword, value);
  105. switch (header->state) {
  106. case STATE_SIMPLE:
  107. CHECK_KEYWORD("SIMPLE");
  108. if (value[0] == 'F') {
  109. av_log(avcl, AV_LOG_WARNING, "not a standard FITS file\n");
  110. } else if (value[0] != 'T') {
  111. av_log(avcl, AV_LOG_ERROR, "invalid value of SIMPLE keyword, SIMPLE = %c\n", value[0]);
  112. return AVERROR_INVALIDDATA;
  113. }
  114. header->state = STATE_BITPIX;
  115. break;
  116. case STATE_XTENSION:
  117. CHECK_KEYWORD("XTENSION");
  118. if (!strcmp(value, "'IMAGE '")) {
  119. header->image_extension = 1;
  120. }
  121. header->state = STATE_BITPIX;
  122. break;
  123. case STATE_BITPIX:
  124. CHECK_KEYWORD("BITPIX");
  125. CHECK_VALUE("BITPIX", bitpix);
  126. dict_set_if_not_null(metadata, keyword, value);
  127. header->state = STATE_NAXIS;
  128. break;
  129. case STATE_NAXIS:
  130. CHECK_KEYWORD("NAXIS");
  131. CHECK_VALUE("NAXIS", naxis);
  132. dict_set_if_not_null(metadata, keyword, value);
  133. if (header->naxis) {
  134. header->state = STATE_NAXIS_N;
  135. } else {
  136. header->state = STATE_REST;
  137. }
  138. break;
  139. case STATE_NAXIS_N:
  140. ret = sscanf(keyword, "NAXIS%d", &dim_no);
  141. if (ret != 1 || dim_no != header->naxis_index + 1) {
  142. av_log(avcl, AV_LOG_ERROR, "expected NAXIS%d keyword, found %s = %s\n", header->naxis_index + 1, keyword, value);
  143. return AVERROR_INVALIDDATA;
  144. }
  145. if (sscanf(value, "%d", &header->naxisn[header->naxis_index]) != 1) {
  146. av_log(avcl, AV_LOG_ERROR, "invalid value of NAXIS%d keyword, %s = %s\n", header->naxis_index + 1, keyword, value);
  147. return AVERROR_INVALIDDATA;
  148. }
  149. dict_set_if_not_null(metadata, keyword, value);
  150. header->naxis_index++;
  151. if (header->naxis_index == header->naxis) {
  152. header->state = STATE_REST;
  153. }
  154. break;
  155. case STATE_REST:
  156. if (!strcmp(keyword, "BLANK") && sscanf(value, "%"SCNd64"", &t) == 1) {
  157. header->blank = t;
  158. header->blank_found = 1;
  159. } else if (!strcmp(keyword, "BSCALE") && sscanf(value, "%lf", &d) == 1) {
  160. header->bscale = d;
  161. } else if (!strcmp(keyword, "BZERO") && sscanf(value, "%lf", &d) == 1) {
  162. header->bzero = d;
  163. } else if (!strcmp(keyword, "CTYPE3") && !strncmp(value, "'RGB", 4)) {
  164. header->rgb = 1;
  165. } else if (!strcmp(keyword, "DATAMAX") && sscanf(value, "%lf", &d) == 1) {
  166. header->data_max_found = 1;
  167. header->data_max = d;
  168. } else if (!strcmp(keyword, "DATAMIN") && sscanf(value, "%lf", &d) == 1) {
  169. header->data_min_found = 1;
  170. header->data_min = d;
  171. } else if (!strcmp(keyword, "END")) {
  172. return 1;
  173. } else if (!strcmp(keyword, "GROUPS") && sscanf(value, "%c", &c) == 1) {
  174. header->groups = (c == 'T');
  175. } else if (!strcmp(keyword, "GCOUNT") && sscanf(value, "%"SCNd64"", &t) == 1) {
  176. header->gcount = t;
  177. } else if (!strcmp(keyword, "PCOUNT") && sscanf(value, "%"SCNd64"", &t) == 1) {
  178. header->pcount = t;
  179. }
  180. dict_set_if_not_null(metadata, keyword, value);
  181. break;
  182. }
  183. return 0;
  184. }