forked from hunter-packages/arrayfire
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathimageio2.cpp
More file actions
389 lines (332 loc) · 14.8 KB
/
imageio2.cpp
File metadata and controls
389 lines (332 loc) · 14.8 KB
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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
/*******************************************************
* Copyright (c) 2014, ArrayFire
* All rights reserved.
*
* This file is distributed under 3-clause BSD license.
* The complete license agreement can be obtained at:
* http://arrayfire.com/licenses/BSD-3-Clause
********************************************************/
#if defined(WITH_FREEIMAGE)
#include "imageio_helper.h"
#include <af/array.h>
#include <af/index.h>
#include <af/dim4.hpp>
#include <af/arith.h>
#include <af/algorithm.h>
#include <af/blas.h>
#include <af/data.h>
#include <af/image.h>
#include <backend.hpp>
#include <ArrayInfo.hpp>
#include <traits.hpp>
#include <memory.hpp>
#include <err_common.hpp>
#include <string>
#include <cstring>
#include <cstdio>
#include <cstdlib>
using af::dim4;
using namespace detail;
template<typename T, FI_CHANNELS fi_color>
static af_err readImage_t(af_array *rImage, const uchar* pSrcLine, const int nSrcPitch,
const uint fi_w, const uint fi_h)
{
// create an array to receive the loaded image data.
AF_CHECK(af_init());
T *pDst = pinnedAlloc<T>(fi_w * fi_h * 4); // 4 channels is max
T* pDst0 = pDst;
T* pDst1 = pDst + (fi_w * fi_h * 1);
T* pDst2 = pDst + (fi_w * fi_h * 2);
T* pDst3 = pDst + (fi_w * fi_h * 3);
uint indx = 0;
uint step = fi_color;
for (uint x = 0; x < fi_w; ++x) {
for (uint y = 0; y < fi_h; ++y) {
const T *src = (T*)((uchar*)pSrcLine - y * nSrcPitch);
if(fi_color == 1) {
pDst0[indx] = (T) *(src + (x * step));
} else if(fi_color >= 3) {
if((af_dtype) af::dtype_traits<T>::af_type == u8) {
pDst0[indx] = (T) *(src + (x * step + FI_RGBA_RED));
pDst1[indx] = (T) *(src + (x * step + FI_RGBA_GREEN));
pDst2[indx] = (T) *(src + (x * step + FI_RGBA_BLUE));
} else {
// Non 8-bit types do not use ordering
// See Pixel Access Functions Chapter in FreeImage Doc
pDst0[indx] = (T) *(src + (x * step + 0));
pDst1[indx] = (T) *(src + (x * step + 1));
pDst2[indx] = (T) *(src + (x * step + 2));
}
if (fi_color == 4) pDst3[indx] = (T) *(src + (x * step + FI_RGBA_ALPHA));
}
indx++;
}
}
// TODO
af::dim4 dims(fi_h, fi_w, fi_color, 1);
af_err err = af_create_array(rImage, pDst, dims.ndims(), dims.get(),
(af_dtype) af::dtype_traits<T>::af_type);
pinnedFree(pDst);
return err;
}
FREE_IMAGE_TYPE getFIT(FI_CHANNELS channels, af_dtype type)
{
if(channels == AFFI_GRAY) {
if(type == u8 ) return FIT_BITMAP;
else if(type == u16) return FIT_UINT16;
else if(type == f32) return FIT_FLOAT;
} else if(channels == AFFI_RGB) {
if(type == u8 ) return FIT_BITMAP;
else if(type == u16) return FIT_RGB16;
else if(type == f32) return FIT_RGBF;
} else if(channels == AFFI_RGBA) {
if(type == u8 ) return FIT_BITMAP;
else if(type == u16) return FIT_RGBA16;
else if(type == f32) return FIT_RGBAF;
}
return FIT_BITMAP;
}
////////////////////////////////////////////////////////////////////////////////
// File IO
////////////////////////////////////////////////////////////////////////////////
// Load image from disk.
af_err af_load_image_native(af_array *out, const char* filename)
{
try {
ARG_ASSERT(1, filename != NULL);
// for statically linked FI
FI_Init();
// set your own FreeImage error handler
FreeImage_SetOutputMessage(FreeImageErrorHandler);
// try to guess the file format from the file extension
FREE_IMAGE_FORMAT fif = FreeImage_GetFileType(filename);
if (fif == FIF_UNKNOWN) {
fif = FreeImage_GetFIFFromFilename(filename);
}
if(fif == FIF_UNKNOWN) {
AF_ERROR("FreeImage Error: Unknown File or Filetype", AF_ERR_NOT_SUPPORTED);
}
int flags = 0;
if(fif == FIF_JPEG) flags = flags | JPEG_ACCURATE;
// check that the plugin has reading capabilities ...
FIBITMAP* pBitmap = NULL;
if (FreeImage_FIFSupportsReading(fif)) {
pBitmap = FreeImage_Load(fif, filename, flags);
}
if(pBitmap == NULL) {
AF_ERROR("FreeImage Error: Error reading image or file does not exist", AF_ERR_RUNTIME);
}
// make sure pBitmap is unleaded automatically, no matter how we exit this function
FI_BitmapResource bitmapUnloader(pBitmap);
// check image color type
uint color_type = FreeImage_GetColorType(pBitmap);
const uint fi_bpp = FreeImage_GetBPP(pBitmap);
//int fi_color = (int)((fi_bpp / 8.0) + 0.5); //ceil
int fi_color;
switch(color_type) {
case 0: // FIC_MINISBLACK
case 1: // FIC_MINISWHITE
fi_color = 1; break;
case 2: // FIC_PALETTE
case 3: // FIC_RGB
fi_color = 3; break;
case 4: // FIC_RGBALPHA
case 5: // FIC_CMYK
fi_color = 4; break;
default: // Should not come here
fi_color = 3; break;
}
const int fi_bpc = fi_bpp / fi_color;
if(fi_bpc != 8 && fi_bpc != 16 && fi_bpc != 32) {
AF_ERROR("FreeImage Error: Bits per channel not supported", AF_ERR_NOT_SUPPORTED);
}
// sizes
uint fi_w = FreeImage_GetWidth(pBitmap);
uint fi_h = FreeImage_GetHeight(pBitmap);
// FI = row major | AF = column major
uint nSrcPitch = FreeImage_GetPitch(pBitmap);
const uchar* pSrcLine = FreeImage_GetBits(pBitmap) + nSrcPitch * (fi_h - 1);
// result image
af_array rImage;
if(fi_color == 4) { //4 channel image
if(fi_bpc == 8)
AF_CHECK((readImage_t<uchar, AFFI_RGBA>)(&rImage, pSrcLine, nSrcPitch, fi_w, fi_h));
else if(fi_bpc == 16)
AF_CHECK((readImage_t<ushort, AFFI_RGBA>)(&rImage, pSrcLine, nSrcPitch, fi_w, fi_h));
else if(fi_bpc == 32)
AF_CHECK((readImage_t<float, AFFI_RGBA>)(&rImage, pSrcLine, nSrcPitch, fi_w, fi_h));
} else if (fi_color == 1) {
if(fi_bpc == 8)
AF_CHECK((readImage_t<uchar, AFFI_GRAY>)(&rImage, pSrcLine, nSrcPitch, fi_w, fi_h));
else if(fi_bpc == 16)
AF_CHECK((readImage_t<ushort, AFFI_GRAY>)(&rImage, pSrcLine, nSrcPitch, fi_w, fi_h));
else if(fi_bpc == 32)
AF_CHECK((readImage_t<float, AFFI_GRAY>)(&rImage, pSrcLine, nSrcPitch, fi_w, fi_h));
} else { //3 channel imag
if(fi_bpc == 8)
AF_CHECK((readImage_t<uchar, AFFI_RGB >)(&rImage, pSrcLine, nSrcPitch, fi_w, fi_h));
else if(fi_bpc == 16)
AF_CHECK((readImage_t<ushort, AFFI_RGB >)(&rImage, pSrcLine, nSrcPitch, fi_w, fi_h));
else if(fi_bpc == 32)
AF_CHECK((readImage_t<float, AFFI_RGB >)(&rImage, pSrcLine, nSrcPitch, fi_w, fi_h));
}
std::swap(*out,rImage);
} CATCHALL;
return AF_SUCCESS;
}
template<typename T, FI_CHANNELS channels>
static void save_t(T* pDstLine, const af_array in, const dim4 dims, uint nDstPitch)
{
af_array rr = 0, gg = 0, bb = 0, aa = 0;
AF_CHECK(channel_split(in, dims, &rr, &gg, &bb, &aa)); // convert array to 3 channels if needed
af_array rrT = 0, ggT = 0, bbT = 0, aaT = 0;
T *pSrc0 = 0, *pSrc1 = 0, *pSrc2 = 0, *pSrc3 = 0;
uint step = channels; // force 3 channels saving
uint indx = 0;
AF_CHECK(af_transpose(&rrT, rr, false));
if(channels >= 3) AF_CHECK(af_transpose(&ggT, gg, false));
if(channels >= 3) AF_CHECK(af_transpose(&bbT, bb, false));
if(channels >= 4) AF_CHECK(af_transpose(&aaT, aa, false));
ArrayInfo cinfo = getInfo(rrT);
pSrc0 = pinnedAlloc<T>(cinfo.elements());
if(channels >= 3) pSrc1 = pinnedAlloc<T>(cinfo.elements());
if(channels >= 3) pSrc2 = pinnedAlloc<T>(cinfo.elements());
if(channels >= 4) pSrc3 = pinnedAlloc<T>(cinfo.elements());
AF_CHECK(af_get_data_ptr((void*)pSrc0, rrT));
if(channels >= 3) AF_CHECK(af_get_data_ptr((void*)pSrc1, ggT));
if(channels >= 3) AF_CHECK(af_get_data_ptr((void*)pSrc2, bbT));
if(channels >= 4) AF_CHECK(af_get_data_ptr((void*)pSrc3, aaT));
const uint fi_w = dims[1];
const uint fi_h = dims[0];
// Copy the array into FreeImage buffer
for (uint y = 0; y < fi_h; ++y) {
for (uint x = 0; x < fi_w; ++x) {
if(channels == 1) {
*(pDstLine + x * step + FI_RGBA_RED) = (T) pSrc0[indx]; // r -> 0
} else if(channels >=3) {
if((af_dtype) af::dtype_traits<T>::af_type == u8) {
*(pDstLine + x * step + FI_RGBA_BLUE) = (T) pSrc2[indx]; // b -> 0
*(pDstLine + x * step + FI_RGBA_GREEN) = (T) pSrc1[indx]; // g -> 1
*(pDstLine + x * step + FI_RGBA_RED) = (T) pSrc0[indx]; // r -> 2
} else {
// Non 8-bit types do not use ordering
// See Pixel Access Functions Chapter in FreeImage Doc
*(pDstLine + x * step + 0) = (T) pSrc0[indx]; // r -> 0
*(pDstLine + x * step + 1) = (T) pSrc1[indx]; // g -> 1
*(pDstLine + x * step + 2) = (T) pSrc2[indx]; // b -> 2
}
}
if(channels >= 4) *(pDstLine + x * step + FI_RGBA_ALPHA) = (T) pSrc3[indx]; // a
++indx;
}
pDstLine = (T*)(((uchar*)pDstLine) - nDstPitch);
}
pinnedFree(pSrc0);
if(channels >= 3) pinnedFree(pSrc1);
if(channels >= 3) pinnedFree(pSrc2);
if(channels >= 4) pinnedFree(pSrc3);
if(rr != 0) AF_CHECK(af_release_array(rr ));
if(gg != 0) AF_CHECK(af_release_array(gg ));
if(bb != 0) AF_CHECK(af_release_array(bb ));
if(aa != 0) AF_CHECK(af_release_array(aa ));
if(rrT!= 0) AF_CHECK(af_release_array(rrT));
if(ggT!= 0) AF_CHECK(af_release_array(ggT));
if(bbT!= 0) AF_CHECK(af_release_array(bbT));
if(aaT!= 0) AF_CHECK(af_release_array(aaT));
}
// Save an image to disk.
af_err af_save_image_native(const char* filename, const af_array in)
{
try {
ARG_ASSERT(0, filename != NULL);
FI_Init();
// set your own FreeImage error handler
FreeImage_SetOutputMessage(FreeImageErrorHandler);
// try to guess the file format from the file extension
FREE_IMAGE_FORMAT fif = FreeImage_GetFileType(filename);
if (fif == FIF_UNKNOWN) {
fif = FreeImage_GetFIFFromFilename(filename);
}
if(fif == FIF_UNKNOWN) {
AF_ERROR("FreeImage Error: Unknown Filetype", AF_ERR_NOT_SUPPORTED);
}
ArrayInfo info = getInfo(in);
// check image color type
FI_CHANNELS channels = (FI_CHANNELS)info.dims()[2];
DIM_ASSERT(1, channels <= 4);
DIM_ASSERT(1, channels != 2);
// sizes
uint fi_w = info.dims()[1];
uint fi_h = info.dims()[0];
af_dtype type = info.getType();
// FI assumes [0-255] for u8
// FI assumes [0-65k] for u16
// FI assumes [0-1] for f32
int fi_bpp = 0;
switch(type) {
case u8: fi_bpp = channels * 8; break;
case u16: fi_bpp = channels * 16; break;
case f32: fi_bpp = channels * 32; break;
default: TYPE_ERROR(1, type);
}
FREE_IMAGE_TYPE fit_type = getFIT(channels, type);
// create the result image storage using FreeImage
FIBITMAP* pResultBitmap = NULL;
switch(type) {
case u8: pResultBitmap = FreeImage_AllocateT(fit_type, fi_w, fi_h, fi_bpp); break;
case u16: pResultBitmap = FreeImage_AllocateT(fit_type, fi_w, fi_h, fi_bpp); break;
case f32: pResultBitmap = FreeImage_AllocateT(fit_type, fi_w, fi_h, fi_bpp); break;
default: TYPE_ERROR(1, type);
}
if(pResultBitmap == NULL) {
AF_ERROR("FreeImage Error: Error creating image or file", AF_ERR_RUNTIME);
}
// make sure pResultBitmap is unloaded automatically, no matter how we exit this function
FI_BitmapResource resultBitmapUnloader(pResultBitmap);
// FI = row major | AF = column major
uint nDstPitch = FreeImage_GetPitch(pResultBitmap);
void* pDstLine = FreeImage_GetBits(pResultBitmap) + nDstPitch * (fi_h - 1);
if(channels == AFFI_GRAY) {
switch(type) {
case u8: save_t<uchar , AFFI_GRAY>((uchar *)pDstLine, in, info.dims(), nDstPitch); break;
case u16: save_t<ushort, AFFI_GRAY>((ushort*)pDstLine, in, info.dims(), nDstPitch); break;
case f32: save_t<float , AFFI_GRAY>((float *)pDstLine, in, info.dims(), nDstPitch); break;
default: TYPE_ERROR(1, type);
}
} else if(channels == AFFI_RGB) {
switch(type) {
case u8: save_t<uchar , AFFI_RGB >((uchar *)pDstLine, in, info.dims(), nDstPitch); break;
case u16: save_t<ushort, AFFI_RGB >((ushort*)pDstLine, in, info.dims(), nDstPitch); break;
case f32: save_t<float , AFFI_RGB >((float *)pDstLine, in, info.dims(), nDstPitch); break;
default: TYPE_ERROR(1, type);
}
} else {
switch(type) {
case u8: save_t<uchar , AFFI_RGBA>((uchar *)pDstLine, in, info.dims(), nDstPitch); break;
case u16: save_t<ushort, AFFI_RGBA>((ushort*)pDstLine, in, info.dims(), nDstPitch); break;
case f32: save_t<float , AFFI_RGBA>((float *)pDstLine, in, info.dims(), nDstPitch); break;
default: TYPE_ERROR(1, type);
}
}
int flags = 0;
if(fif == FIF_JPEG) flags = flags | JPEG_QUALITYSUPERB;
// now save the result image
if (!(FreeImage_Save(fif, pResultBitmap, filename, flags) == TRUE)) {
AF_ERROR("FreeImage Error: Failed to save image", AF_ERR_RUNTIME);
}
} CATCHALL
return AF_SUCCESS;
}
#else // WITH_FREEIMAGE
#include <af/image.h>
#include <stdio.h>
#include <err_common.hpp>
af_err af_load_image_native(af_array *out, const char* filename)
{
AF_RETURN_ERROR("ArrayFire compiled without Image IO (FreeImage) support", AF_ERR_NOT_CONFIGURED);
}
af_err af_save_image_native(const char* filename, const af_array in)
{
AF_RETURN_ERROR("ArrayFire compiled without Image IO (FreeImage) support", AF_ERR_NOT_CONFIGURED);
}
#endif // WITH_FREEIMAGE