1 /*
2 * FITS image decoder
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
22 /**
23 * @file
24 * FITS image decoder
25 *
26 * Specification: https://fits.gsfc.nasa.gov/fits_standard.html Version 3.0
27 *
28 * Support all 2d images alongwith, bzero, bscale and blank keywords.
29 * RGBA images are supported as NAXIS3 = 3 or 4 i.e. Planes in RGBA order. Also CTYPE = 'RGB ' should be present.
30 * Also to interpret data, values are linearly scaled using min-max scaling but not RGB images.
31 */
32
41
46
47 /**
48 * Calculate the data_min and data_max values from the data.
49 * This is called if the values are not present in the header.
50 * @param ptr8 pointer to the data
51 * @param header pointer to the header
52 * @param end pointer to end of packet
53 * @return 0 if calculated successfully otherwise AVERROR_INVALIDDATA
54 */
56 {
58 int16_t t16;
60 int64_t t64;
61 float tflt;
62 double tdbl;
64
65 header->data_min = DBL_MAX;
66 header->data_max = -DBL_MAX;
68 #define CASE_N(a, t, rd) \
69 case a: \
70 for (i = 0; i < header->naxisn[1]; i++) { \
71 for (j = 0; j < header->naxisn[0]; j++) { \
72 t = rd; \
73 if (!header->blank_found || t != header->blank) { \
74 if (t > header->data_max) \
75 header->data_max = t; \
76 if (t < header->data_min) \
77 header->data_min = t; \
78 } \
79 ptr8 += abs(a) >> 3; \
80 } \
81 } \
82 break
83
90 default:
92 }
93 return 0;
94 }
95
96 /**
97 * Read the fits header and store the values in FITSHeader pointed by header
98 * @param avctx AVCodec context
99 * @param ptr pointer to pointer to the data
100 * @param header pointer to the FITSHeader
101 * @param end pointer to end of packet
102 * @param metadata pointer to pointer to AVDictionary to store metadata
103 * @return 0 if calculated successfully otherwise AVERROR_INVALIDDATA
104 */
107 {
108 const uint8_t *ptr8 = *ptr;
109 int lines_read, bytes_left,
i,
ret;
111
112 lines_read = 1; // to account for first header line, SIMPLE or XTENSION which is not included in packet...
114 do {
115 if (end - ptr8 < 80)
118 ptr8 += 80;
119 lines_read++;
123
124 bytes_left = (((lines_read + 35) / 36) * 36 - lines_read) * 80;
125 if (end - ptr8 < bytes_left)
127 ptr8 += bytes_left;
128
132 }
133
137 }
138
142 }
143
149 }
151 }
152
153 if (end - ptr8 <
size)
155 *ptr = ptr8;
156
162 }
163 } else {
164 /*
165 * instead of applying bscale and bzero to every element,
166 * we can do inverse transformation on data_min and data_max
167 */
170 }
175 }
178 }
179
180 return 0;
181 }
182
184 {
186 const uint8_t *ptr8 = avpkt->
data, *end;
188 int16_t t16;
190 int64_t t64;
191 float tflt;
192 double tdbl;
194 const int map[] = {2, 0, 1, 3};
// mapping from GBRA -> RGBA as RGBA is to be stored in FITS file..
195 uint8_t *dst8;
196 uint16_t *dst16;
197 uint64_t t;
200
201 end = ptr8 + avpkt->
size;
206
209 if (
header.naxisn[2] == 3) {
211 } else {
213 }
214 }
else if (
header.bitpix == 16) {
215 if (
header.naxisn[2] == 3) {
217 } else {
219 }
220 } else {
223 }
224 } else {
227 } else {
229 }
230 }
231
234
237
238 /*
239 * FITS stores images with bottom row first. Therefore we have
240 * to fill the image from bottom to top.
241 */
244 #define CASE_RGB(cas, dst, type, dref) \
245 case cas: \
246 for (k = 0; k < header.naxisn[2]; k++) { \
247 for (i = 0; i < avctx->height; i++) { \
248 dst = (type *) (p->data[map[k]] + (avctx->height - i - 1) * p->linesize[map[k]]); \
249 for (j = 0; j < avctx->width; j++) { \
250 t32 = dref(ptr8); \
251 if (!header.blank_found || t32 != header.blank) { \
252 t = t32 * header.bscale + header.bzero; \
253 } else { \
254 t = fitsctx->blank_val; \
255 } \
256 *dst++ = (type) t; \
257 ptr8 += cas >> 3; \
258 } \
259 } \
260 } \
261 break
262
265 }
266 } else {
268
271 }
273
275 #define CASE_GRAY(cas, dst, type, t, rd) \
276 case cas: \
277 for (i = 0; i < avctx->height; i++) { \
278 dst = (type *) (p->data[0] + (avctx->height-i-1)* p->linesize[0]); \
279 for (j = 0; j < avctx->width; j++) { \
280 t = rd; \
281 if (!header.blank_found || t != header.blank) { \
282 *dst++ = lrint(((t - header.data_min) * ((1 << (sizeof(type) * 8)) - 1)) * scale); \
283 } else { \
284 *dst++ = fitsctx->blank_val; \
285 } \
286 ptr8 += abs(cas) >> 3; \
287 } \
288 } \
289 break
290
297 default:
300 }
301 }
302
305
306 *got_frame = 1;
307
309 }
310
314 };
315
321 };
322
332 };