1
// ==========================================================
2
// FreeImagePlus Test Script
4
// Design and implementation by
5
// - Herv� Drolon (drolon@infonie.fr)
7
// This file is part of FreeImage 3
9
// COVERED CODE IS PROVIDED UNDER THIS LICENSE ON AN "AS IS" BASIS, WITHOUT WARRANTY
10
// OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, WITHOUT LIMITATION, WARRANTIES
11
// THAT THE COVERED CODE IS FREE OF DEFECTS, MERCHANTABLE, FIT FOR A PARTICULAR PURPOSE
12
// OR NON-INFRINGING. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE COVERED
13
// CODE IS WITH YOU. SHOULD ANY COVERED CODE PROVE DEFECTIVE IN ANY RESPECT, YOU (NOT
14
// THE INITIAL DEVELOPER OR ANY OTHER CONTRIBUTOR) ASSUME THE COST OF ANY NECESSARY
15
// SERVICING, REPAIR OR CORRECTION. THIS DISCLAIMER OF WARRANTY CONSTITUTES AN ESSENTIAL
16
// PART OF THIS LICENSE. NO USE OF ANY COVERED CODE IS AUTHORIZED HEREUNDER EXCEPT UNDER
19
// Use at your own risk!
20
// ==========================================================
25
// --------------------------------------------------------------------------
26
// Memory IO test scripts
29
Test saving to a memory stream
31
void testSaveMemIO(const char *lpszPathName) {
33
// load a regular file
34
FREE_IMAGE_FORMAT fif = FreeImage_GetFileType(lpszPathName);
35
FIBITMAP *dib = FreeImage_Load(fif, lpszPathName, 0);
37
// open a memory handle
40
// save the file to memory
41
memIO.write(fif, dib, 0);
43
// at this point, memIO contains the entire PNG data in memory.
44
// the amount of space used by the memory is equal to file_size
45
long file_size = memIO.tell();
46
assert(file_size != 0);
48
// its easy load an image from memory as well
50
// seek to the start of the memory stream
51
memIO.seek(0L, SEEK_SET);
54
FREE_IMAGE_FORMAT mem_fif = memIO.getFileType();
56
// load an image from the memory handle
57
FIBITMAP *check = memIO.read(mem_fif, 0);
59
// save as a regular file
60
FreeImage_Save(FIF_PNG, check, "dump.png", PNG_DEFAULT);
62
FreeImage_Unload(check);
63
FreeImage_Unload(dib);
65
// The memIO object will be destroyed automatically
69
Test loading from a buffer attached to a memory stream
71
void testLoadMemIO(const char *lpszPathName) {
75
// get data associated with lpszPathName
76
result = stat(lpszPathName, &buf);
78
// allocate a memory buffer and load temporary data
79
BYTE *mem_buffer = (BYTE*)malloc(buf.st_size * sizeof(BYTE));
81
FILE *stream = fopen(lpszPathName, "rb");
83
fread(mem_buffer, sizeof(BYTE), buf.st_size, stream);
86
// attach the binary data to a memory stream
87
fipMemoryIO memIO(mem_buffer, buf.st_size);
90
FREE_IMAGE_FORMAT fif = memIO.getFileType();
92
// load an image from the memory stream
93
FIBITMAP *check = memIO.read(fif, PNG_DEFAULT);
95
// save as a regular file
96
FreeImage_Save(FIF_PNG, check, "blob.png", PNG_DEFAULT);
98
// close the stream (memIO is destroyed)
101
// user is responsible for freeing the data
108
Test extracting a memory buffer from a memory stream
110
void testAcquireMemIO(const char *lpszPathName) {
112
// load a regular file
113
FREE_IMAGE_FORMAT fif = FreeImage_GetFileType(lpszPathName);
114
FIBITMAP *dib = FreeImage_Load(fif, lpszPathName, 0);
116
// open and allocate a memory stream
119
// save the file to memory
120
memIO.write(FIF_PNG, dib, PNG_DEFAULT);
122
// get the buffer from the memory stream
123
BYTE *mem_buffer = NULL;
124
DWORD size_in_bytes = 0;
126
memIO.acquire(&mem_buffer, &size_in_bytes);
128
// save the buffer in a file stream
129
FILE *stream = fopen("buffer.png", "wb");
131
fwrite(mem_buffer, sizeof(BYTE), size_in_bytes, stream);
135
// close and free the memory stream (memIO is destroyed)
139
Test Loading / Saving from / to a memory stream using fipImage
141
void testImageMemIO(const char *lpszPathName) {
142
BOOL bSuccess = FALSE;
147
// load a regular file
148
bSuccess = image.load(lpszPathName);
150
// save the file to a memory stream
151
bSuccess = image.saveToMemory(FIF_PNG, memIO, PNG_DEFAULT);
153
// load the file from the memory stream
154
memIO.seek(0L, SEEK_SET);
155
bSuccess = image.loadFromMemory(memIO, 0);
159
void testMemIO(const char *lpszPathName) {
160
testSaveMemIO(lpszPathName);
161
testLoadMemIO(lpszPathName);
162
testAcquireMemIO(lpszPathName);
163
testImageMemIO(lpszPathName);