2 * FITS implementation of common functions
3 * Copyright (c) 2017 Paras Chadha
5 * This file is part of FFmpeg.
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.
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.
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
23 #include "libavutil/dict.h"
26 int avpriv_fits_header_init(FITSHeader *header, FITSHeaderState state)
28 header->state = state;
29 header->naxis_index = 0;
30 header->blank_found = 0;
35 header->image_extension = 0;
38 header->data_min_found = 0;
39 header->data_max_found = 0;
43 static int dict_set_if_not_null(AVDictionary ***metadata, char *keyword, char *value)
46 av_dict_set(*metadata, keyword, value, 0);
51 * Extract keyword and value from a header line (80 bytes) and store them in keyword and value strings respectively
52 * @param ptr8 pointer to the data
53 * @param keyword pointer to the char array in which keyword is to be stored
54 * @param value pointer to the char array in which value is to be stored
55 * @return 0 if calculated successfully otherwise AVERROR_INVALIDDATA
57 static int read_keyword_value(const uint8_t *ptr8, char *keyword, char *value)
61 for (i = 0; i < 8 && ptr8[i] != ' '; i++) {
68 while (i < 80 && ptr8[i] == ' ') {
75 if (ptr8[i-1] == '\'') {
76 for (; i < 80 && ptr8[i] != '\''; i++) {
80 } else if (ptr8[i-1] == '(') {
81 for (; i < 80 && ptr8[i] != ')'; i++) {
86 for (; i < 80 && ptr8[i] != ' ' && ptr8[i] != '/'; i++) {
96 #define CHECK_KEYWORD(key) \
97 if (strcmp(keyword, key)) { \
98 av_log(avcl, AV_LOG_ERROR, "expected %s keyword, found %s = %s\n", key, keyword, value); \
99 return AVERROR_INVALIDDATA; \
102 #define CHECK_VALUE(key, val) \
103 if (sscanf(value, "%d", &header->val) != 1) { \
104 av_log(avcl, AV_LOG_ERROR, "invalid value of %s keyword, %s = %s\n", key, keyword, value); \
105 return AVERROR_INVALIDDATA; \
108 int avpriv_fits_header_parse_line(void *avcl, FITSHeader *header, const uint8_t line[80], AVDictionary ***metadata)
113 char keyword[10], value[72], c;
115 read_keyword_value(line, keyword, value);
116 switch (header->state) {
118 CHECK_KEYWORD("SIMPLE");
120 if (value[0] == 'F') {
121 av_log(avcl, AV_LOG_WARNING, "not a standard FITS file\n");
122 } else if (value[0] != 'T') {
123 av_log(avcl, AV_LOG_ERROR, "invalid value of SIMPLE keyword, SIMPLE = %c\n", value[0]);
124 return AVERROR_INVALIDDATA;
127 header->state = STATE_BITPIX;
130 CHECK_KEYWORD("XTENSION");
132 if (!strcmp(value, "'IMAGE '")) {
133 header->image_extension = 1;
136 header->state = STATE_BITPIX;
139 CHECK_KEYWORD("BITPIX");
140 CHECK_VALUE("BITPIX", bitpix);
141 dict_set_if_not_null(metadata, keyword, value);
143 header->state = STATE_NAXIS;
146 CHECK_KEYWORD("NAXIS");
147 CHECK_VALUE("NAXIS", naxis);
148 dict_set_if_not_null(metadata, keyword, value);
151 header->state = STATE_NAXIS_N;
153 header->state = STATE_REST;
157 ret = sscanf(keyword, "NAXIS%d", &dim_no);
158 if (ret != 1 || dim_no != header->naxis_index + 1) {
159 av_log(avcl, AV_LOG_ERROR, "expected NAXIS%d keyword, found %s = %s\n", header->naxis_index + 1, keyword, value);
160 return AVERROR_INVALIDDATA;
163 if (sscanf(value, "%d", &header->naxisn[header->naxis_index]) != 1) {
164 av_log(avcl, AV_LOG_ERROR, "invalid value of NAXIS%d keyword, %s = %s\n", header->naxis_index + 1, keyword, value);
165 return AVERROR_INVALIDDATA;
168 dict_set_if_not_null(metadata, keyword, value);
169 header->naxis_index++;
170 if (header->naxis_index == header->naxis) {
171 header->state = STATE_REST;
175 if (!strcmp(keyword, "BLANK") && sscanf(value, "%"SCNd64"", &t) == 1) {
177 header->blank_found = 1;
178 } else if (!strcmp(keyword, "BSCALE") && sscanf(value, "%lf", &d) == 1) {
180 } else if (!strcmp(keyword, "BZERO") && sscanf(value, "%lf", &d) == 1) {
182 } else if (!strcmp(keyword, "CTYPE3") && !strncmp(value, "'RGB", 4)) {
184 } else if (!strcmp(keyword, "DATAMAX") && sscanf(value, "%lf", &d) == 1) {
185 header->data_max_found = 1;
186 header->data_max = d;
187 } else if (!strcmp(keyword, "DATAMIN") && sscanf(value, "%lf", &d) == 1) {
188 header->data_min_found = 1;
189 header->data_min = d;
190 } else if (!strcmp(keyword, "END")) {
192 } else if (!strcmp(keyword, "GROUPS") && sscanf(value, "%c", &c) == 1) {
193 header->groups = (c == 'T');
194 } else if (!strcmp(keyword, "GCOUNT") && sscanf(value, "%"SCNd64"", &t) == 1) {
196 } else if (!strcmp(keyword, "PCOUNT") && sscanf(value, "%"SCNd64"", &t) == 1) {
199 dict_set_if_not_null(metadata, keyword, value);