File: ReadImage.cpp

package info (click to toggle)
xplanet 1.3.0-5.1
  • links: PTS
  • area: main
  • in suites: bullseye, buster
  • size: 4,348 kB
  • sloc: cpp: 24,131; ansic: 6,295; sh: 3,247; makefile: 349
file content (131 lines) | stat: -rw-r--r-- 3,454 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
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
#include <cstdio>
#include <cstring>
using namespace std;

#include "config.h"

extern "C"
{
    int
    read_bmp(const char *filename, int *width, int *height, 
             unsigned char **rgb);
#ifdef HAVE_LIBGIF
    int
    read_gif(const char *filename, int *width, int *height, 
             unsigned char **rgb);
#endif
    
#ifdef HAVE_LIBJPEG
    int read_jpeg(const char *filename, int *width, int *height, 
                  unsigned char **rgb);
#endif
    
#ifdef HAVE_LIBPNG
    int
    read_png(const char *filename, int *width, int *height, 
             unsigned char **rgb, unsigned char **alpha);
#endif
    
#ifdef HAVE_LIBPNM
    int
    read_pnm(const char *filename, int *width, int *height, 
             unsigned char **rgb);
#endif
    
#ifdef HAVE_LIBTIFF
    int
    read_tiff(const char *filename, int *width, int *height, 
              unsigned char **rgb);
#endif
}

bool 
ReadImage(const char *filename, int &width, int &height, 
          unsigned char *&rgb_data, unsigned char *&png_alpha)
{
    char buf[4];
    unsigned char *ubuf = (unsigned char *) buf;
    size_t ret = 0;
    int success = 0;

    FILE *file;
    file = fopen(filename, "rb");
    if (file == NULL) return(false);
  
    /* see what kind of file we have */

    ret = fread(buf, 1, 4, file);
    fclose(file);
    if (ret != 4)
        return 0;

    if (!strncmp("BM", buf, 2))
    {
        success = read_bmp(filename, &width, &height, &rgb_data);
    }
    else if (!strncmp("GIF8", buf, 4))
    {
#ifdef HAVE_LIBGIF
        success = read_gif(filename, &width, &height, &rgb_data);
#else
        fprintf(stderr, 
                "Sorry, this program was not compiled with GIF support\n");
        success = 0;
#endif /* HAVE_LIBGIF */
    }
    else if ((ubuf[0] == 0xff) && (ubuf[1] == 0xd8))
    {
#ifdef HAVE_LIBJPEG
        success = read_jpeg(filename, &width, &height, &rgb_data);
#else
        fprintf(stderr, 
                "Sorry, this program was not compiled with JPEG support\n");
        success = 0;
#endif /* HAVE_LIBJPEG */
    }
    else if ((ubuf[0] == 0x89) && !strncmp("PNG", buf+1, 3))
    {
#ifdef HAVE_LIBPNG
        success = read_png(filename, &width, &height, &rgb_data, &png_alpha);
#else
        fprintf(stderr, 
                "Sorry, this program was not compiled with PNG support\n");
        success = 0;
#endif /* HAVE_LIBPNG */
    }
    else if ((   !strncmp("P6\n", buf, 3))
             || (!strncmp("P5\n", buf, 3))
             || (!strncmp("P4\n", buf, 3))
             || (!strncmp("P3\n", buf, 3))
             || (!strncmp("P2\n", buf, 3))
             || (!strncmp("P1\n", buf, 3)))
    {
#ifdef HAVE_LIBPNM
        success = read_pnm(filename, &width, &height, &rgb_data);
#else
        fprintf(stderr, 
                "Sorry, this program was not compiled with PNM support\n");
        success = 0;
#endif /* HAVE_LIBPNM */
    }
    else if (((!strncmp ("MM", buf, 2)) && (ubuf[2] == 0x00) 
              && (ubuf[3] == 0x2a))
             || ((!strncmp ("II", buf, 2)) && (ubuf[2] == 0x2a) 
                 && (ubuf[3] == 0x00)))
    {
#ifdef HAVE_LIBTIFF
        success = read_tiff(filename, &width, &height, &rgb_data);
#else
        fprintf(stderr, 
                "Sorry, this program was not compiled with TIFF support\n");
        success = 0;
#endif
    }
    else
    {
        fprintf(stderr, "Unknown image format\n");
        success = 0;
    }

    return(success == 1);
}