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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167 | /* GATE PROJECT LICENSE:
+----------------------------------------------------------------------------+
| Copyright(c) 2018-2025, Stefan Meislinger |
| All rights reserved. |
| |
| Redistribution and use in source and binary forms, with or without |
| modification, are permitted provided that the following conditions are met:|
| |
| 1. Redistributions of source code must retain the above copyright notice, |
| this list of conditions and the following disclaimer. |
| 2. Redistributions in binary form must reproduce the above copyright |
| notice, this list of conditions and the following disclaimer in the |
| documentation and/or other materials provided with the distribution. |
| |
| THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"|
| AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| ARE DISCLAIMED.IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE |
| LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF |
| THE POSSIBILITY OF SUCH DAMAGE. |
+----------------------------------------------------------------------------+
*/
#include "gate/graphics/imageformats.h"
#include "gate/results.h"
#include "gate/files.h"
#include "gate/graphics/bitmapimages.h"
#include "gate/graphics/jpegimages.h"
#include "gate/graphics/pixmapimages.h"
#include "gate/graphics/pngimages.h"
#include "gate/graphics/gifimages.h"
struct ext_mapping
{
gate_enumint_t id;
char const* ext;
gate_image_reader_t reader;
gate_image_writer_t writer;
};
static struct ext_mapping const gate_ext_mapping[] =
{
{ GATE_IMAGEFORMAT_BITMAP, "bmp", &gate_bitmapimage_load, &gate_bitmapimage_save },
{ GATE_IMAGEFORMAT_PIXMAP, "xpm", &gate_pixmapimage_load, &gate_pixmapimage_save },
{ GATE_IMAGEFORMAT_JPEG, "jpg", &gate_jpegimage_load, &gate_jpegimage_save },
{ GATE_IMAGEFORMAT_JPEG, "jpeg", &gate_jpegimage_load, &gate_jpegimage_save },
{ GATE_IMAGEFORMAT_GIF, "gif", &gate_gifimage_load, &gate_gifimage_save },
{ GATE_IMAGEFORMAT_PNG, "png", &gate_pngimage_load, &gate_pngimage_save }
};
static gate_size_t const gate_ext_mapping_count = sizeof(gate_ext_mapping) / sizeof(gate_ext_mapping[0]);
gate_enumint_t gate_imageformat_parse_file_extension(gate_string_t const* file_ext)
{
gate_size_t ndx;
char const* ext;<--- The scope of the variable 'ext' can be reduced. [+]The scope of the variable 'ext' can be reduced. Warning: Be careful when fixing this message, especially when there are inner loops. Here is an example where cppcheck will write that the scope for 'i' can be reduced:
void f(int x)
{
int i = 0;
if (x) {
// it's safe to move 'int i = 0;' here
for (int n = 0; n < 10; ++n) {
// it is possible but not safe to move 'int i = 0;' here
do_something(&i);
}
}
}
When you see this message it is always safe to reduce the variable scope 1 level.
gate_size_t ext_len;
for (ndx = 0; ndx != gate_ext_mapping_count; ++ndx)
{
ext = gate_ext_mapping[ndx].ext;
ext_len = gate_str_length(ext);
if (gate_str_compare(file_ext->str, file_ext->length, ext, ext_len) == 0)
{
return gate_ext_mapping[ndx].id;
}
if (gate_str_ends_with_ic(file_ext->str, file_ext->length, ext, ext_len))
{
if (file_ext->str[file_ext->length - ext_len - 1] == '.')
{
return gate_ext_mapping[ndx].id;
}
}
}
return GATE_IMAGEFORMAT_UNKNOWN;
}
char const* gate_imageformat_get_file_extension(gate_enumint_t image_format)
{
gate_size_t ndx;
for (ndx = 0; ndx != gate_ext_mapping_count; ++ndx)
{
if (gate_ext_mapping[ndx].id == image_format)
{
return gate_ext_mapping[ndx].ext;
}
}
return NULL;
}
gate_result_t gate_imageformat_load(gate_enumint_t image_format, gate_stream_t* srcstream, gate_rasterimage_t* image, gate_enumint_t flags)
{
gate_result_t result = GATE_RESULT_NOTSUPPORTED;
gate_size_t ndx;
for (ndx = 0; ndx != gate_ext_mapping_count; ++ndx)
{
if (gate_ext_mapping[ndx].id == image_format)
{
result = gate_ext_mapping[ndx].reader(srcstream, image, flags);
break;
}
}
return result;
}
gate_result_t gate_imageformat_save(gate_enumint_t image_format, gate_rasterimage_t const* image, gate_stream_t* deststream, gate_enumint_t flags)
{
gate_result_t result = GATE_RESULT_FAILED;
gate_size_t ndx;
for (ndx = 0; ndx != gate_ext_mapping_count; ++ndx)
{
if (gate_ext_mapping[ndx].id == image_format)
{
result = gate_ext_mapping[ndx].writer(image, deststream, flags);
break;
}
}
return result;
}
gate_result_t gate_imageformat_load_file(gate_string_t const* file_path, gate_rasterimage_t* image, gate_enumint_t flags)
{
gate_result_t result;
gate_int32_t file_id = gate_imageformat_parse_file_extension(file_path);
gate_controlstream_t* filestream = NULL;
do
{
if (file_id == GATE_IMAGEFORMAT_UNKNOWN)
{
result = GATE_RESULT_NOTSUPPORTED;
break;
}
result = gate_file_openstream(file_path, GATE_STREAM_OPEN_READ, &filestream);
GATE_BREAK_IF_FAILED(result);
result = gate_imageformat_load(file_id, (gate_stream_t*)filestream, image, flags);
} while (0);
if (filestream != NULL)
{
gate_object_release(filestream);
}
return result;
}
gate_result_t gate_imageformat_save_file(gate_string_t const* file_path, gate_rasterimage_t const* image, gate_enumint_t flags)
{
gate_result_t result;
gate_int32_t file_id = gate_imageformat_parse_file_extension(file_path);
gate_filestream_t* filestream = NULL;
do
{
if (file_id == GATE_IMAGEFORMAT_UNKNOWN)
{
result = GATE_RESULT_NOTSUPPORTED;
break;
}
result = gate_file_openstream(file_path, GATE_STREAM_OPEN_WRITE, &filestream);
GATE_BREAK_IF_FAILED(result);
result = gate_imageformat_save(file_id, image, (gate_stream_t*)filestream, flags);
} while (0);
if (filestream != NULL)
{
gate_object_release(filestream);
}
return result;
}
|