File: io.cpp

package info (click to toggle)
satdump 1.2.2%2Bgb79af48-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 81,648 kB
  • sloc: cpp: 276,768; ansic: 164,598; lisp: 1,219; sh: 283; xml: 106; makefile: 7
file content (95 lines) | stat: -rw-r--r-- 3,996 bytes parent folder | download | duplicates (2)
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
#include "io.h"
#include <fstream>
#include "logger.h"
#include "core/config.h"

namespace image
{
    void load_img(Image &img, std::string file)
    {
        std::ifstream file_sigature_src(file, std::ios::binary);
        uint8_t signature[10];
        file_sigature_src.read((char *)signature, 10);
        if (signature[0] == 0xFF && signature[1] == 0xD8)
            load_jpeg(img, file);
        else if (signature[0] == 0x89 && signature[1] == 0x50 && signature[2] == 0x4E && signature[3] == 0x47)
            load_png(img, file);
        else if (signature[0] == 0xff && signature[1] == 0x4f && signature[2] == 0xff && signature[3] == 0x51)
            load_j2k(img, file);
        else if (signature[0] == 'P' && (signature[1] == '5' || signature[1] == '6'))
            load_pbm(img, file);
        else if (signature[0] == 'I' && signature[1] == 'I' && signature[2] == '*')
            load_tiff(img, file);
        else if (signature[0] == 'q' && signature[1] == 'o' && signature[2] == 'i' && signature[3] == 'f')
            load_qoi(img, file);
    }

    void load_img(Image &img, uint8_t *buffer, int size)
    {
        if (buffer[0] == 0xFF && buffer[1] == 0xD8)
            load_jpeg(img, buffer, size);
        else if (buffer[0] == 0x89 && buffer[1] == 0x50 && buffer[2] == 0x4E && buffer[3] == 0x47)
            load_png(img, buffer, size);
    }

    void save_img(Image &img, std::string file, bool fast)
    {
        if (!append_ext(&file))
            return;
        logger->info("Saving " + file + "...");
        if (file.find(".png") != std::string::npos)
            save_png(img, file, fast);
        else if (file.find(".jpeg") != std::string::npos || file.find(".jpg") != std::string::npos)
            save_jpeg(img, file);
        else if (file.find(".j2k") != std::string::npos)
            save_j2k(img, file);
        else if ((file.find(".ppm") != std::string::npos) || (file.find(".pgm") != std::string::npos) || (file.find(".pbm") != std::string::npos))
            save_pbm(img, file);
        else if ((file.find(".tif") != std::string::npos) || (file.find(".gtif") != std::string::npos) || (file.find(".tiff") != std::string::npos))
            save_tiff(img, file);
        else if (file.find(".qoi") != std::string::npos)
            save_qoi(img, file);
    }

    // Append selected file extension
    bool append_ext(std::string *file, bool prod)
    {
        // Do nothing if there's already an extension
        if (file->find(".png") != std::string::npos ||
            file->find(".jpeg") != std::string::npos ||
            file->find(".jpg") != std::string::npos ||
            file->find(".j2k") != std::string::npos ||
            file->find(".pgm") != std::string::npos ||
            file->find(".pbm") != std::string::npos ||
            file->find(".ppm") != std::string::npos ||
            file->find(".tif") != std::string::npos ||
            file->find(".tiff") != std::string::npos ||
            file->find(".gtif") != std::string::npos ||
            file->find(".qoi") != std::string::npos)
            return true;

        // Otherwise, load the user setting
        std::string image_format;
        try
        {
            if (prod)
                image_format = satdump::config::main_cfg["satdump_general"]["product_format"]["value"];
            else
                image_format = satdump::config::main_cfg["satdump_general"]["image_format"]["value"];
        }
        catch (std::exception &e)
        {
            logger->error("Image format not specified, and default format cannot be found! %s", e.what());
            return false;
        }

        if (image_format != "png" && image_format != "jpg" && image_format != "j2k" && image_format != "pbm" && image_format != "tif" && image_format != "qoi")
        {
            logger->error("Image format not specified, and default format is invalid!");
            return false;
        }

        *file += "." + image_format;
        return true;
    }
}