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.

217 lines
7.0KB

  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. switch(header->bitpix) {
  127. case 8:
  128. case 16:
  129. case 32: case -32:
  130. case 64: case -64: break;
  131. default:
  132. av_log(avcl, AV_LOG_ERROR, "invalid value of BITPIX %d\n", header->bitpix); \
  133. return AVERROR_INVALIDDATA;
  134. }
  135. dict_set_if_not_null(metadata, keyword, value);
  136. header->state = STATE_NAXIS;
  137. break;
  138. case STATE_NAXIS:
  139. CHECK_KEYWORD("NAXIS");
  140. CHECK_VALUE("NAXIS", naxis);
  141. dict_set_if_not_null(metadata, keyword, value);
  142. if (header->naxis) {
  143. header->state = STATE_NAXIS_N;
  144. } else {
  145. header->state = STATE_REST;
  146. }
  147. break;
  148. case STATE_NAXIS_N:
  149. ret = sscanf(keyword, "NAXIS%d", &dim_no);
  150. if (ret != 1 || dim_no != header->naxis_index + 1) {
  151. av_log(avcl, AV_LOG_ERROR, "expected NAXIS%d keyword, found %s = %s\n", header->naxis_index + 1, keyword, value);
  152. return AVERROR_INVALIDDATA;
  153. }
  154. if (sscanf(value, "%d", &header->naxisn[header->naxis_index]) != 1) {
  155. av_log(avcl, AV_LOG_ERROR, "invalid value of NAXIS%d keyword, %s = %s\n", header->naxis_index + 1, keyword, value);
  156. return AVERROR_INVALIDDATA;
  157. }
  158. dict_set_if_not_null(metadata, keyword, value);
  159. header->naxis_index++;
  160. if (header->naxis_index == header->naxis) {
  161. header->state = STATE_REST;
  162. }
  163. break;
  164. case STATE_REST:
  165. if (!strcmp(keyword, "BLANK") && sscanf(value, "%"SCNd64"", &t) == 1) {
  166. header->blank = t;
  167. header->blank_found = 1;
  168. } else if (!strcmp(keyword, "BSCALE") && sscanf(value, "%lf", &d) == 1) {
  169. if (d <= 0)
  170. return AVERROR_INVALIDDATA;
  171. header->bscale = d;
  172. } else if (!strcmp(keyword, "BZERO") && sscanf(value, "%lf", &d) == 1) {
  173. header->bzero = d;
  174. } else if (!strcmp(keyword, "CTYPE3") && !strncmp(value, "'RGB", 4)) {
  175. header->rgb = 1;
  176. } else if (!strcmp(keyword, "DATAMAX") && sscanf(value, "%lf", &d) == 1) {
  177. header->data_max_found = 1;
  178. header->data_max = d;
  179. } else if (!strcmp(keyword, "DATAMIN") && sscanf(value, "%lf", &d) == 1) {
  180. header->data_min_found = 1;
  181. header->data_min = d;
  182. } else if (!strcmp(keyword, "END")) {
  183. return 1;
  184. } else if (!strcmp(keyword, "GROUPS") && sscanf(value, "%c", &c) == 1) {
  185. header->groups = (c == 'T');
  186. } else if (!strcmp(keyword, "GCOUNT") && sscanf(value, "%"SCNd64"", &t) == 1) {
  187. header->gcount = t;
  188. } else if (!strcmp(keyword, "PCOUNT") && sscanf(value, "%"SCNd64"", &t) == 1) {
  189. header->pcount = t;
  190. }
  191. dict_set_if_not_null(metadata, keyword, value);
  192. break;
  193. }
  194. return 0;
  195. }