File: fipTestMemIO.cpp

package info (click to toggle)
freeimage 3.18.0%2Bds2-11
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 9,824 kB
  • sloc: cpp: 42,339; cs: 36,178; pascal: 4,629; ansic: 1,294; makefile: 101; sh: 84
file content (182 lines) | stat: -rw-r--r-- 5,172 bytes parent folder | download | duplicates (5)
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
// ==========================================================
// FreeImagePlus Test Script
//
// Design and implementation by
// - Herv Drolon (drolon@infonie.fr)
//
// 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 "fipTest.h"

using namespace std;

// --------------------------------------------------------------------------
// Memory IO test scripts

/**
Test saving to a memory stream
*/
void testSaveMemIO(const char *lpszPathName) {
	BOOL bSuccess = FALSE;

	// load a regular file
	FREE_IMAGE_FORMAT fif = FreeImage_GetFileType(lpszPathName);
	FIBITMAP *dib = FreeImage_Load(fif, lpszPathName, 0);
	
	// open a memory handle
	fipMemoryIO memIO;

	// save the file to memory
	bSuccess = memIO.save(fif, dib, 0);
	assert(bSuccess == TRUE);

	// at this point, memIO contains the entire PNG data in memory. 
	// the amount of space used by the memory is equal to file_size	
	long file_size = memIO.tell();
	assert(file_size != 0);

	// its easy load an image from memory as well

	// seek to the start of the memory stream
	memIO.seek(0L, SEEK_SET);
	
	// get the file type	
	FREE_IMAGE_FORMAT mem_fif = memIO.getFileType();
	
	// load an image from the memory handle 	
	FIBITMAP *check = memIO.load(mem_fif, 0);
	assert(check != NULL);

	// save as a regular file
	bSuccess = FreeImage_Save(FIF_PNG, check, "dump.png", PNG_DEFAULT);
	assert(bSuccess == TRUE);

	FreeImage_Unload(check);
	FreeImage_Unload(dib);

	// The memIO object will be destroyed automatically
}

/**
Test loading from a buffer attached to a memory stream
*/
void testLoadMemIO(const char *lpszPathName) {
	struct stat buf;
	int result;
	BOOL bSuccess = FALSE;

	// get data associated with lpszPathName
	result = stat(lpszPathName, &buf);
	if(result == 0) {
		// allocate a memory buffer and load temporary data
		BYTE *mem_buffer = (BYTE*)malloc(buf.st_size * sizeof(BYTE));
		if(mem_buffer) {
			FILE *stream = fopen(lpszPathName, "rb");
			if(stream) {
				fread(mem_buffer, sizeof(BYTE), buf.st_size, stream);
				fclose(stream);

				// attach the binary data to a memory stream
				fipMemoryIO memIO(mem_buffer, buf.st_size);

				// get the file type
				FREE_IMAGE_FORMAT fif = memIO.getFileType();

				// load an image from the memory stream
				FIBITMAP *check = memIO.load(fif, PNG_DEFAULT);
				assert(check != NULL);

				// save as a regular file
				bSuccess = FreeImage_Save(FIF_PNG, check, "blob.png", PNG_DEFAULT);
				assert(bSuccess == TRUE);
				
				// close the stream (memIO is destroyed)
			}

			// user is responsible for freeing the data
			free(mem_buffer);
		}
	}
}

/**
Test extracting a memory buffer from a memory stream
*/
void testAcquireMemIO(const char *lpszPathName) {
	BOOL bSuccess = FALSE;

	// load a regular file
	FREE_IMAGE_FORMAT fif = FreeImage_GetFileType(lpszPathName);
	FIBITMAP *dib = FreeImage_Load(fif, lpszPathName, 0);

	// open and allocate a memory stream
	fipMemoryIO memIO;

	// save the file to memory
	bSuccess = memIO.save(FIF_PNG, dib, PNG_DEFAULT);
	assert(bSuccess == TRUE);

	// get the buffer from the memory stream
	BYTE *mem_buffer = NULL;
	DWORD size_in_bytes = 0;

	bSuccess = memIO.acquire(&mem_buffer, &size_in_bytes);
	assert(bSuccess == TRUE);

	// save the buffer in a file stream
	FILE *stream = fopen("buffer.png", "wb");
	if(stream) {
		fwrite(mem_buffer, sizeof(BYTE), size_in_bytes, stream);
		fclose(stream);
	}

	// close and free the memory stream (memIO is destroyed)
}

/**
Test Loading / Saving from / to a memory stream using fipImage
*/
void testImageMemIO(const char *lpszPathName) {
	BOOL bSuccess = FALSE;

	fipMemoryIO memIO;
	fipImage image;

	// load a regular file
	bSuccess = image.load(lpszPathName);
	assert(bSuccess == TRUE);
	if(bSuccess) {
		// save the file to a memory stream
		bSuccess = image.saveToMemory(FIF_PNG, memIO, PNG_DEFAULT);
		assert(bSuccess);
		
		// load the file from the memory stream
		memIO.seek(0L, SEEK_SET);
		bSuccess = image.loadFromMemory(memIO, 0);
		assert(bSuccess);
	}
}

void testMemIO(const char *lpszPathName) {
	cout << "testMemIO ...\n";

	testSaveMemIO(lpszPathName);
	testLoadMemIO(lpszPathName);
	testAcquireMemIO(lpszPathName);
	testImageMemIO(lpszPathName);
}