-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtestWrappedBuffer.cpp
163 lines (126 loc) · 4.99 KB
/
testWrappedBuffer.cpp
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
// ==========================================================
// FreeImage 3 Test Script
//
// Design and implementation by
// - Hervé Drolon ([email protected])
//
// This file is part of FreeImage 3
//
// COVERED CODE IS PROVIDED UNDER THIS LICENSE ON AN "AS IS" BASIS, WITHOUT WARRANTY
// OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, WITHOUT LIMITATION, WARRANTIES
// THAT THE COVERED CODE IS FREE OF DEFECTS, MERCHANTABLE, FIT FOR A PARTICULAR PURPOSE
// OR NON-INFRINGING. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE COVERED
// CODE IS WITH YOU. SHOULD ANY COVERED CODE PROVE DEFECTIVE IN ANY RESPECT, YOU (NOT
// THE INITIAL DEVELOPER OR ANY OTHER CONTRIBUTOR) ASSUME THE COST OF ANY NECESSARY
// SERVICING, REPAIR OR CORRECTION. THIS DISCLAIMER OF WARRANTY CONSTITUTES AN ESSENTIAL
// PART OF THIS LICENSE. NO USE OF ANY COVERED CODE IS AUTHORIZED HEREUNDER EXCEPT UNDER
// THIS DISCLAIMER.
//
// Use at your own risk!
// ==========================================================
#include "TestSuite.h"
// Local test functions
// ----------------------------------------------------------
static void testBasicWrapper(FIBOOL copySource, uint8_t *bits, FREE_IMAGE_TYPE type, int width, int height, int pitch, unsigned bpp) {
FIBITMAP *src = NULL;
FIBITMAP *clone = NULL;
FIBITMAP *dst = NULL;
// allocate a wrapper
src = FreeImage_ConvertFromRawBitsEx(copySource, bits, type, width, height, pitch, bpp, FI_RGBA_RED_MASK, FI_RGBA_GREEN_MASK, FI_RGBA_BLUE_MASK, FALSE);
assert(src != NULL);
// test clone
clone = FreeImage_Clone(src);
assert(clone != NULL);
// test in-place processing
FreeImage_Invert(src);
// test processing
dst = FreeImage_ConvertToFloat(src);
assert(dst != NULL);
FreeImage_Unload(dst);
FreeImage_Unload(clone);
// unload the wrapper
FreeImage_Unload(src);
}
static void testViewport(FIBITMAP *dib) {
FIBITMAP *src = NULL;
// define a viewport as [vp_x, vp_y, vp_width, vp_height]
// (assume the image is larger than the viewport)
int vp_width = 300;
int vp_height = 200;
int vp_x = FreeImage_GetWidth(dib) / 2 - vp_width / 2;
int vp_y = FreeImage_GetHeight(dib) / 2 - vp_height / 2;
// point the viewport data
unsigned bytes_per_pixel = FreeImage_GetLine(dib) / FreeImage_GetWidth(dib);
uint8_t *data = FreeImage_GetBits(dib) + vp_y * FreeImage_GetPitch(dib) + vp_x * bytes_per_pixel;
// wrap a section (no copy)
src = FreeImage_ConvertFromRawBitsEx(FALSE/*copySource*/, data, FIT_BITMAP, vp_width, vp_height, FreeImage_GetPitch(dib), FreeImage_GetBPP(dib), FI_RGBA_RED_MASK, FI_RGBA_GREEN_MASK, FI_RGBA_BLUE_MASK);
// save the section (note that the image is inverted due to previous processing)
FreeImage_Save(FIF_PNG, src, "viewport.png");
// unload the wrapper
FreeImage_Unload(src);
}
// Main test functions
// ----------------------------------------------------------
void testWrappedBuffer(const char *lpszPathName, int flags) {
FIBITMAP *dib = NULL;
// simulate a user provided buffer
// -------------------------------
// load the dib
FREE_IMAGE_FORMAT fif = FreeImage_GetFileType(lpszPathName);
dib = FreeImage_Load(fif, lpszPathName, flags);
assert(dib != NULL);
// get data info
FREE_IMAGE_TYPE type = FreeImage_GetImageType(dib);
unsigned width = FreeImage_GetWidth(dib);
unsigned height = FreeImage_GetHeight(dib);
unsigned pitch = FreeImage_GetPitch(dib);
unsigned bpp = FreeImage_GetBPP(dib);
uint8_t *bits = FreeImage_GetBits(dib);
// test wrapped buffer manipulations
// -------------------------------
testBasicWrapper(TRUE /*copySource*/, bits, type, width, height, pitch, bpp);
testBasicWrapper(FALSE /*copySource*/, bits, type, width, height, pitch, bpp);
// test another use-case : viewport
testViewport(dib);
// unload the user provided buffer
// -------------------------------
FreeImage_Unload(dib);
}
void testCreateView(const char *lpszPathName, int flags) {
FIBITMAP *dib = NULL;
FIBITMAP *view = NULL;
// test working with views
// -------------------------------
// load the dib
FREE_IMAGE_FORMAT fif = FreeImage_GetFileType(lpszPathName);
dib = FreeImage_Load(fif, lpszPathName, flags);
assert(dib != NULL);
// get data info
FREE_IMAGE_TYPE type = FreeImage_GetImageType(dib);
assert(type == FIT_BITMAP);
unsigned width = FreeImage_GetWidth(dib);
unsigned height = FreeImage_GetHeight(dib);
unsigned pitch = FreeImage_GetPitch(dib);
unsigned bpp = FreeImage_GetBPP(dib);
// create a view around the center of the image
int cx = width / 2;
int cy = height / 2;
int ext_x = width / 4;
int ext_y = height / 4;
unsigned left = cx - ext_x;
unsigned right = cx + ext_x;
unsigned top = cy - ext_y;
unsigned bottom = cy + ext_y;
// make pitch odd
if ((right - left) % 2 == 0) {
right++;
}
view = FreeImage_CreateView(dib, left, top, right, bottom);
assert(FreeImage_GetPitch(view) == pitch);
assert(FreeImage_GetBPP(view) == bpp);
// save as BMP - check correct pitch handling
FreeImage_Save(FIF_BMP, view, "test_view.bmp", 0);
// no longer needed
FreeImage_Unload(view);
FreeImage_Unload(dib);
}