avformat/fitsenc: write DATAMIN/DATAMAX to encoded output

There is no point in doing normalization when such files are decoded.

Update fate test with new results.
This commit is contained in:
Paul B Mahol 2021-02-08 18:46:36 +01:00
parent 4aef642cfd
commit e0fd35d867
9 changed files with 42 additions and 23 deletions

View File

@ -45,7 +45,8 @@ static int fits_write_header(AVFormatContext *s)
* @param lines_written to keep track of lines written so far
* @return 0
*/
static int write_keyword_value(AVFormatContext *s, const char *keyword, int value, int *lines_written)
static int write_keyword_value(AVFormatContext *s, const char *fmt,
const char *keyword, void *value, int *lines_written)
{
int len, ret;
uint8_t header[80];
@ -57,7 +58,12 @@ static int write_keyword_value(AVFormatContext *s, const char *keyword, int valu
header[8] = '=';
header[9] = ' ';
ret = snprintf(header + 10, 70, "%d", value);
if (!strcmp(fmt, "%d")) {
ret = snprintf(header + 10, 70, fmt, *(int *)value);
} else {
ret = snprintf(header + 10, 70, fmt, *(float *)value);
}
memset(&header[ret + 10], ' ', sizeof(header) - (ret + 10));
avio_write(s->pb, header, sizeof(header));
@ -72,16 +78,22 @@ static int write_image_header(AVFormatContext *s)
FITSContext *fitsctx = s->priv_data;
uint8_t buffer[80];
int bitpix, naxis, naxis3 = 1, bzero = 0, rgb = 0, lines_written = 0, lines_left;
int pcount = 0, gcount = 1;
float datamax, datamin;
switch (encctx->format) {
case AV_PIX_FMT_GRAY8:
bitpix = 8;
naxis = 2;
datamin = 0;
datamax = 255;
break;
case AV_PIX_FMT_GRAY16BE:
bitpix = 16;
naxis = 2;
bzero = 32768;
datamin = 0;
datamax = 65535;
break;
case AV_PIX_FMT_GBRP:
case AV_PIX_FMT_GBRAP:
@ -93,6 +105,8 @@ static int write_image_header(AVFormatContext *s)
} else {
naxis3 = 4;
}
datamin = 0;
datamax = 255;
break;
case AV_PIX_FMT_GBRP16BE:
case AV_PIX_FMT_GBRAP16BE:
@ -105,6 +119,8 @@ static int write_image_header(AVFormatContext *s)
naxis3 = 4;
}
bzero = 32768;
datamin = 0;
datamax = 65535;
break;
default:
return AVERROR(EINVAL);
@ -122,28 +138,31 @@ static int write_image_header(AVFormatContext *s)
}
lines_written++;
write_keyword_value(s, "BITPIX", bitpix, &lines_written); // no of bits per pixel
write_keyword_value(s, "NAXIS", naxis, &lines_written); // no of dimensions of image
write_keyword_value(s, "NAXIS1", encctx->width, &lines_written); // first dimension i.e. width
write_keyword_value(s, "NAXIS2", encctx->height, &lines_written); // second dimension i.e. height
write_keyword_value(s, "%d", "BITPIX", &bitpix, &lines_written); // no of bits per pixel
write_keyword_value(s, "%d", "NAXIS", &naxis, &lines_written); // no of dimensions of image
write_keyword_value(s, "%d", "NAXIS1", &encctx->width, &lines_written); // first dimension i.e. width
write_keyword_value(s, "%d", "NAXIS2", &encctx->height, &lines_written); // second dimension i.e. height
if (rgb)
write_keyword_value(s, "NAXIS3", naxis3, &lines_written); // third dimension to store RGBA planes
write_keyword_value(s, "%d", "NAXIS3", &naxis3, &lines_written); // third dimension to store RGBA planes
if (!fitsctx->first_image) {
write_keyword_value(s, "PCOUNT", 0, &lines_written);
write_keyword_value(s, "GCOUNT", 1, &lines_written);
write_keyword_value(s, "%d", "PCOUNT", &pcount, &lines_written);
write_keyword_value(s, "%d", "GCOUNT", &gcount, &lines_written);
} else {
fitsctx->first_image = 0;
}
write_keyword_value(s, "%g", "DATAMIN", &datamin, &lines_written);
write_keyword_value(s, "%g", "DATAMAX", &datamax, &lines_written);
/*
* Since FITS does not support unsigned 16 bit integers,
* BZERO = 32768 is used to store unsigned 16 bit integers as
* signed integers so that it can be read properly.
*/
if (bitpix == 16)
write_keyword_value(s, "BZERO", bzero, &lines_written);
write_keyword_value(s, "%d", "BZERO", &bzero, &lines_written);
if (rgb) {
memcpy(buffer, "CTYPE3 = 'RGB '", 20);

View File

@ -3,8 +3,8 @@
#codec_id 0: fits
#dimensions 0: 72x36
#sar 0: 0/1
0, 0, 0, 1, 14320, 0x0ecf72e0
0, 1, 1, 1, 14320, 0xd94af6eb
0, 2, 2, 1, 14320, 0x15c21892
0, 3, 3, 1, 14320, 0xb18adc01
0, 4, 4, 1, 14320, 0xc2be706d
0, 0, 0, 1, 14320, 0xa9ee75a4
0, 1, 1, 1, 14320, 0xb9daf9af
0, 2, 2, 1, 14320, 0xf6431b56
0, 3, 3, 1, 14320, 0x921adec5
0, 4, 4, 1, 14320, 0xa34e7331

View File

@ -3,4 +3,4 @@
#codec_id 0: rawvideo
#dimensions 0: 128x128
#sar 0: 0/1
0, 0, 0, 1, 16384, 0x353dbacd
0, 0, 0, 1, 16384, 0xeff50901

View File

@ -1,3 +1,3 @@
28eb102547b82acca57ef097a6c639d8 *tests/data/lavf/lavf.gbrap.fits
d953a6a2c719de9d922d0624a7eb796b *tests/data/lavf/lavf.gbrap.fits
10224000 tests/data/lavf/lavf.gbrap.fits
tests/data/lavf/lavf.gbrap.fits CRC=0x883af247

View File

@ -1,3 +1,3 @@
ff5fb24a67aeabd4f56088ca8b03d8b0 *tests/data/lavf/lavf.gbrap16be.fits
e9a04d25104fc43ddc62b58eb33ecd08 *tests/data/lavf/lavf.gbrap16be.fits
20376000 tests/data/lavf/lavf.gbrap16be.fits
tests/data/lavf/lavf.gbrap16be.fits CRC=0xa981271b

View File

@ -1,3 +1,3 @@
dae49b5f6eb58981ba91e3e108355717 *tests/data/lavf/lavf.gbrp.fits
3952247f7f9669f968826c909852bbd7 *tests/data/lavf/lavf.gbrp.fits
7704000 tests/data/lavf/lavf.gbrp.fits
tests/data/lavf/lavf.gbrp.fits CRC=0x80745c5e

View File

@ -1,3 +1,3 @@
693ea80c33eb9b348db27a0bc4a5cc8a *tests/data/lavf/lavf.gbrp16be.fits
caf72fec125df9c7a1d59c9d1bc70b80 *tests/data/lavf/lavf.gbrp16be.fits
15336000 tests/data/lavf/lavf.gbrp16be.fits
tests/data/lavf/lavf.gbrp16be.fits CRC=0x9573fb2b

View File

@ -1,3 +1,3 @@
d76b46a5a336b56f73451817cdf3897c *tests/data/lavf/lavf.gray.fits
e690dc6db533b87f5f843737007ed070 *tests/data/lavf/lavf.gray.fits
2664000 tests/data/lavf/lavf.gray.fits
tests/data/lavf/lavf.gray.fits CRC=0x7aa0122f

View File

@ -1,3 +1,3 @@
15e85a553bbd07783f92377ed369308b *tests/data/lavf/lavf.gray16be.fits
262658f437a256cd843db2b401bc20a9 *tests/data/lavf/lavf.gray16be.fits
5184000 tests/data/lavf/lavf.gray16be.fits
tests/data/lavf/lavf.gray16be.fits CRC=0x8cdcbeb2
tests/data/lavf/lavf.gray16be.fits CRC=0x737e8998