File: main.cpp

package info (click to toggle)
libxisf 0.2.13-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 4,612 kB
  • sloc: ansic: 23,072; cpp: 14,790; ada: 1,681; pascal: 1,139; sh: 996; cs: 879; asm: 281; xml: 95; makefile: 12
file content (131 lines) | stat: -rw-r--r-- 6,164 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
/************************************************************************
 * LibXISF - library to load and save XISF files                        *
 * Copyright (C) 2025 DuĊĦan Poizl                                       *
 *                                                                      *
 * This program is free software: you can redistribute it and/or modify *
 * it under the terms of the GNU General Public License as published by *
 * the Free Software Foundation, either version 3 of the License, or    *
 * (at your option) any later version.                                  *
 *                                                                      *
 * This program is distributed in the hope that it will be useful,      *
 * but WITHOUT ANY WARRANTY; without even the implied warranty of       *
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the        *
 * GNU General Public License for more details.                         *
 *                                                                      *
 * You should have received a copy of the GNU General Public License    *
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.*
 ************************************************************************/

#include <iostream>
#include "libxisf.h"

using namespace LibXISF;

void benchmark();

#define TEST(cond, msg) if(cond){ std::cerr << msg << std::endl; return 1; }

int main(int argc, char **argv)
{
    try
    {
        if (argc < 2)
        {
            XISFWriter writer;
            Image image(5, 7);
            image.setImageType(Image::Light);
            image.addProperty(Property("PropertyString", "Hello XISF"));
            image.addProperty(Property("PropertyBoolean", (Boolean)true));
            image.addProperty(Property("PropertyInt8", (Int8)(8)));
            image.addProperty(Property("PropertyInt16", (Int16)16));
            image.addProperty(Property("PropertyInt32", 32));
            image.addProperty(Property("PropertyUInt8", (UInt8)8));
            image.addProperty(Property("PropertyUInt16", (UInt16)(16)));
            image.addProperty(Property("PropertyUInt32", (uint32_t)32));
            image.addProperty(Property("PropertyFloat32", (Float32) 0.32));
            image.addProperty(Property("PropertyFloat64", (Float64) 0.64));
            image.addProperty(Property("PropertyComplex32", Complex32{3.0, -2.0}));
            image.addProperty(Property("PropertyComplex64", Complex64{-3.0, 2.0}));
            image.addProperty(Property("VectorUInt16", UI16Vector({23, 45, 86})));
            image.addProperty(Property("VectorComplex32", C32Vector({{1, 2}, {3, 4}, {5, 6}})));
            UI16Matrix m(2, 3);
            m(0, 0) = 0;
            m(0, 1) = 1;
            m(0, 2) = 2;
            m(1, 0) = 10;
            image.addProperty(Property("UI16Matrix", m));
            std::tm tm = {12, 22, 23, 1, 2, 2023, 0, 0, 0};
            image.addProperty(Property("TimeObs", tm));
            image.addFITSKeyword({"RA", "226.9751163116387", "Right ascension of the center of the image (deg)"});
            image.addFITSKeyword({"DEC", "62.02302376908295", "Declination of the center of the image (deg)"});
            image.setCompression(DataBlock::Zlib, 9);
            writer.writeImage(image);

            image.setImageType(Image::Flat);
            image.setCompression(DataBlock::LZ4);
            image.setByteshuffling(true);
            writer.writeImage(image);
            ByteArray data;
            std::cout << "Saving image" << std::endl;
            writer.save(data);

            XISFReader reader;
            std::cout << "Loading image" << std::endl;
            reader.open(data);
            const Image &img0 = reader.getImage(0);
            const Image &img1 = reader.getImage(1);

            TEST(image.imageProperties().size() != img0.imageProperties().size(), "Property count doesn't match");
            TEST(std::memcmp(image.imageData(), img0.imageData(), image.imageDataSize()), "Images doesn't match");
            TEST(std::memcmp(image.imageData(), img1.imageData(), image.imageDataSize()), "Images doesn't match");

            XISFModify mod;
            mod.open(data);
            mod.addFITSKeyword(0, {"NEWKEY", "1.0", ""});
            mod.updateFITSKeyword(0, {"OBJECT", "M42", ""}, true);

            ByteArray data2;
            mod.save(data2);
            reader.open(data2);
            const Image &imgMod = reader.getImage(0, false);
            auto fitsKeywords = imgMod.fitsKeywords();
            TEST(fitsKeywords.size() != 4, "FITS keyword were not added");
            TEST(fitsKeywords[0].name != "RA", "Incorrect FITS RA keyword");
            TEST(fitsKeywords[1].name != "DEC", "Incorrect FITS DEC keyword");
            TEST(fitsKeywords[2].name != "NEWKEY", "Incorrect FITS NEWKEY keyword");
            TEST(fitsKeywords[3].name != "OBJECT", "Incorrect FITS OBJECT keyword");
        }
        else if(argc == 2 && std::strcmp(argv[1], "bench") == 0)
        {
            benchmark();
        }
        else
        {
            LibXISF::XISFReader reader;
            reader.open(argv[1]);
            TEST(reader.imagesCount() != 1, "No image");

            const LibXISF::Image &image = reader.getImage(0);
            TEST(image.width() != 8, "Invalid width")
            TEST(image.height() != 10, "Invalid height");
            TEST(image.colorSpace() != LibXISF::Image::Gray, "Invalid color space");
            TEST(image.pixelStorage() != LibXISF::Image::Planar, "Invalid pixel storage");
            if(std::strstr(argv[1], "lz4"))
            {
                TEST(image.compression() != LibXISF::DataBlock::LZ4, "Invalid compression codec");
            }
            else
            {
                TEST(image.compression() != LibXISF::DataBlock::None, "Invalid compression codec");
            }
            //TEST(!image.dataBlock.embedded, "Not embedded");
            TEST(image.imageDataSize() != 80*2, "Invalid data size");
        }
    }
    catch (const LibXISF::Error &e)
    {
        std::cout << e.what() << std::endl;
        return 2;
    }
    return 0;
}