-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathimage.c
318 lines (273 loc) · 9.34 KB
/
image.c
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
// Copyright (c) 2017-2023 California Institute of Technology ("Caltech"). U.S.
// Government sponsorship acknowledged. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
#include <FreeImage.h>
#include <string.h>
#include <stdlib.h>
#include "mrcal-image.h"
#include "util.h"
// for diagnostics
__attribute__((unused))
static void report_image_details(/* const; FreeImage doesn't support that */
FIBITMAP* fib, const char* what)
{
MSG("%s colortype = %d bpp = %d dimensions: (%d,%d), pitch = %d",
what,
(int)FreeImage_GetColorType(fib),
(int)FreeImage_GetBPP (fib),
(int)FreeImage_GetWidth (fib),
(int)FreeImage_GetHeight (fib),
(int)FreeImage_GetPitch (fib));
}
static
bool generic_save(const char* filename,
const void* _image,
int bits_per_pixel)
{
bool result = false;
FIBITMAP* fib = NULL;
// This may actually be a different mrcal_image_xxx_t type, but all the
// fields line up anyway
const mrcal_image_uint8_t* image = (const mrcal_image_uint8_t*)_image;
if(image->w == 0 || image->h == 0)
{
MSG("Asked to save an empty image: dimensions (%d,%d)!",
image->w, image->h);
goto done;
}
#if defined HAVE_OLD_LIBFREEIMAGE && HAVE_OLD_LIBFREEIMAGE
if(bits_per_pixel == 16)
MSG("WARNING: you have an old build of libfreeimage. It has trouble writing 16bpp images, so '%s' will probably be written incorrectly. You should upgrade your libfreeimage and rebuild",
filename);
fib = FreeImage_ConvertFromRawBits( (BYTE*)image->data,
image->width, image->height, image->stride,
bits_per_pixel,
0,0,0,
// Top row is stored first
true);
#else
// I do NOT reuse the input data because this function actually changes the
// input buffer to flip it upside-down instead of accessing the bits in the
// correct order
fib = FreeImage_ConvertFromRawBitsEx(true,
(BYTE*)image->data,
(bits_per_pixel == 16) ? FIT_UINT16 : FIT_BITMAP,
image->width, image->height, image->stride,
bits_per_pixel,
0,0,0,
// Top row is stored first
true);
#endif
FREE_IMAGE_FORMAT format = FreeImage_GetFIFFromFilename(filename);
if(format == FIF_UNKNOWN)
{
MSG("FreeImage doesn't know how to save '%s'", filename);
goto done;
}
int flags = format == FIF_JPEG ? 96 : 0;
if(!FreeImage_Save(format, fib, filename, flags))
{
MSG("FreeImage couldn't save '%s'", filename);
goto done;
}
result = true;
done:
if(fib != NULL)
FreeImage_Unload(fib);
return result;
}
bool mrcal_image_uint8_save (const char* filename, const mrcal_image_uint8_t* image)
{
return generic_save(filename, image, 8);
}
bool mrcal_image_uint16_save(const char* filename, const mrcal_image_uint16_t* image)
{
return generic_save(filename, image, 16);
}
bool mrcal_image_bgr_save(const char* filename, const mrcal_image_bgr_t* image)
{
return generic_save(filename, image, 24);
}
static
bool generic_load(// output
// mrcal_image_uint8_t if bits_per_pixel == 8
// mrcal_image_uint16_t if bits_per_pixel == 16
// mrcal_image_bgr_t if bits_per_pixel == 24
void* _image,
// if >0: this is the requested bits_per_pixel. If == 0: we
// get this from the input image, and set the value on the
// return
int* bits_per_pixel,
// input
const char* filename)
{
bool result = false;
FIBITMAP* fib = NULL;
FIBITMAP* fib_converted = NULL;
FREE_IMAGE_FORMAT format = FreeImage_GetFileType(filename,0);
if(format == FIF_UNKNOWN)
{
MSG("Couldn't load '%s': FreeImage_GetFileType() failed", filename);
goto done;
}
fib = FreeImage_Load(format, filename, 0);
if(fib == NULL)
{
MSG("Couldn't load '%s': FreeImage_Load() failed", filename);
goto done;
}
// FreeImage loads images upside-down, so I flip it around
if(!FreeImage_FlipVertical(fib))
{
MSG("Couldn't flip the image");
goto done;
}
// might not be "uint8_t" necessarily, but all the fields still line up
mrcal_image_uint8_t* image = NULL;
FREE_IMAGE_COLOR_TYPE color_type_expected;
const char* what_expected;
if(*bits_per_pixel == 0)
{
// autodetect
FREE_IMAGE_COLOR_TYPE color_type_have = FreeImage_GetColorType(fib);
if(color_type_have == FIC_RGB ||
color_type_have == FIC_PALETTE)
{
*bits_per_pixel = 24;
}
else if(color_type_have == FIC_MINISBLACK)
{
unsigned int bpp_have = FreeImage_GetBPP(fib);
if(bpp_have == 16)
*bits_per_pixel = 16;
else
*bits_per_pixel = 8;
}
else
{
MSG("Couldn't auto-detect image type. I only know about FIC_RGB, FIC_PALETTE, FIC_MINISBLACK");
goto done;
}
}
if(*bits_per_pixel == 8)
{
color_type_expected = FIC_MINISBLACK;
what_expected = "grayscale";
fib_converted = FreeImage_ConvertToGreyscale(fib);
if(fib_converted == NULL)
{
MSG("Couldn't FreeImage_ConvertToGreyscale()");
goto done;
}
}
else if(*bits_per_pixel == 16)
{
color_type_expected = FIC_MINISBLACK;
what_expected = "16-bit grayscale";
// At this time, 16bpp grayscale images can only be read directly from
// the input. I cannot be given a different kind of input, and convert
// the images to 16bpp grayscale
fib_converted = fib;
}
else if(*bits_per_pixel == 24)
{
color_type_expected = FIC_RGB;
what_expected = "bgr 24-bit";
fib_converted = FreeImage_ConvertTo24Bits(fib);
if(fib_converted == NULL)
{
MSG("Couldn't FreeImage_ConvertTo24Bits()");
goto done;
}
}
else
{
MSG("Input bits_per_pixel must be 8 or 16 or 24; got %d", *bits_per_pixel);
goto done;
}
if(!(FreeImage_GetColorType(fib_converted) == color_type_expected &&
FreeImage_GetBPP(fib_converted) == (unsigned)*bits_per_pixel))
{
MSG("Loaded and preprocessed image isn't %s",
what_expected);
goto done;
}
// This may actually be a different mrcal_image_xxx_t type, but all the
// fields line up anyway
image = (mrcal_image_uint8_t*)_image;
image->width = (int)FreeImage_GetWidth (fib_converted);
image->height = (int)FreeImage_GetHeight(fib_converted);
image->stride = (int)FreeImage_GetPitch (fib_converted);
int size = image->stride*image->height;
if(posix_memalign((void**)&image->data, 16UL, size) != 0)
{
MSG("%s('%s') couldn't allocate image: malloc(%d) failed",
__func__, filename, size);
goto done;
}
memcpy( image->data,
FreeImage_GetBits(fib_converted),
size );
result = true;
done:
if(fib != NULL)
FreeImage_Unload(fib);
if(fib_converted != NULL &&
fib_converted != fib)
FreeImage_Unload(fib_converted);
return result;
}
bool mrcal_image_uint8_load(// output
mrcal_image_uint8_t* image,
// input
const char* filename)
{
int bits_per_pixel = 8;
return generic_load(image, &bits_per_pixel, filename);
}
bool mrcal_image_uint16_load(// output
mrcal_image_uint16_t* image,
// input
const char* filename)
{
int bits_per_pixel = 16;
return generic_load(image, &bits_per_pixel, filename);
}
bool mrcal_image_bgr_load (// output
mrcal_image_bgr_t* image,
// input
const char* filename)
{
int bits_per_pixel = 24;
return generic_load(image, &bits_per_pixel, filename);
}
bool mrcal_image_anytype_load(// output
// This is ONE of the known types
mrcal_image_uint8_t* image,
int* bits_per_pixel,
int* channels,
// input
const char* filename)
{
*bits_per_pixel = 0;
if(!generic_load(image, bits_per_pixel, filename))
return false;
switch(*bits_per_pixel)
{
case 8:
case 16:
*channels = 1;
break;
case 24:
*channels = 3;
break;
default:
MSG("Getting here is a bug");
return false;
}
return true;
}