aboutsummaryrefslogtreecommitdiff
path: root/src/ImageUtils.cpp
blob: a0b2c1eda926fddee57ef71817d21a5af6b154c2 (plain)
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
#include "../include/ImageUtils.hpp"
#include <string.h>
#include <arpa/inet.h>

namespace QuickMedia {
    static bool gif_get_size(unsigned char *data, size_t data_size, int *width, int *height) {
        if(data_size >= 10 && memcmp(data, "GIF", 3) == 0) {
            *width = ((int)data[7] << 8) | data[6];
            *height = ((int)data[9] << 8) | data[8];
            return true;
        }
        return false;
    }

    static bool png_get_size(unsigned char *data, size_t data_size, int *width, int *height) {
        if(data_size >= 24 && memcmp(data, "\x89\x50\x4E\x47\x0D\x0A\x1A\x0A", 8) == 0) {
            memcpy(width, data + 16, sizeof(int));
            memcpy(height, data + 16 + sizeof(int), sizeof(int));
            *width = htonl(*width);
            *height = htonl(*height);
            return true;
        }
        return false;
    }

#if 0
    static bool is_cpu_little_endian() {
        unsigned short i;
        memcpy(&i, "LE", sizeof(i));
        return (i & 0xF) == 'E';
    }

    static unsigned short read_uint16(unsigned char *data, bool cpu_little_endian, bool file_little_endian) {
        unsigned short result;
        memcpy(&result, data, sizeof(result));
        if(cpu_little_endian != file_little_endian)
            result = __builtin_bswap16(result);
        return result;
    }

    static unsigned int read_uint32(unsigned char *data, bool cpu_little_endian, bool file_little_endian) {
        unsigned int result;
        memcpy(&result, data, sizeof(result));
        if(cpu_little_endian != file_little_endian)
            result = __builtin_bswap32(result);
        return result;
    }
#endif
#if 0
    static bool tiff_get_size(unsigned char *data, size_t data_size, int *width, int *height) {
        if(data_size < 8)
            return false;

        bool cpu_little_endian = is_cpu_little_endian();
        bool file_little_endian = true;
        if(memcmp(data, "II", 2) == 0)
            file_little_endian = true;
        else if(memcmp(data, "MM", 2) == 0)
            file_little_endian = false;
        else
            return false;

        unsigned short id = read_uint16(data, cpu_little_endian, file_little_endian);
        if(id != 42)
            return false;

        unsigned int offset_to_ifd = read_uint32(data, cpu_little_endian, file_little_endian);
        if(offset_to_ifd )
    }
#endif

    static bool jpeg_get_size(unsigned char *data, size_t data_size, int *width, int *height) {
        if(data_size < 11 || memcmp(data, "\xFF\xD8\xFF\xE0", 4) != 0)
            return false;

        unsigned short block_length = ((int)data[4] << 8) | data[5];
        if(memcmp(data + 6, "JFIF\0", 5) != 0)
            return false;

        size_t index = 4;
        while(index < data_size) {
            index += block_length;
            if(index + 1 >= data_size) return false;
            if(data[index] != 0xFF) return false; // Check if start of block
            if((data[index + 1] == 0xC0 || data[index + 1] == 0xC2) && index + 8 < data_size) { // Start of frame marker
                *height = ((int)data[index + 5] << 8) | data[index + 6];
                *width = ((int)data[index + 7] << 8) | data[index + 8];
                return true;
            } else {
                if(index + 2 >= data_size) return false;
                index += 2;
                block_length = ((int)data[index] << 8) | data[index + 1];
            }
        }

        return false;
    }

    // TODO: Also support exif files. Exif files are containers for jpeg, tiff etc so once tiff has been implemented so can exif.
    // Exif header is similar to jpeg.
    bool image_get_resolution(const Path &path, int *width, int *height, ImageType *image_type) {
        FILE *file = fopen(path.data.c_str(), "rb");
        if(!file)
            return false;

        unsigned char data[512];
        size_t bytes_read = fread(data, 1, sizeof(data), file);
        fclose(file);

        if(png_get_size(data, bytes_read, width, height)) {
            if(image_type)
                *image_type = ImageType::PNG;
            return true;
        } else if(gif_get_size(data, bytes_read, width, height)) {
            if(image_type)
                *image_type = ImageType::GIF;
            return true;
        } else if(jpeg_get_size(data, bytes_read, width, height)) {
            if(image_type)
                *image_type = ImageType::JPG;
            return true;
        }
        return false;
    }

    bool is_image_ext(const char *ext) {
        return strcasecmp(ext, ".jpg") == 0 || strcasecmp(ext, ".jpeg") == 0 || strcasecmp(ext, ".png") == 0 || strcasecmp(ext, ".gif") == 0;
    }
}