File: fipTestMPageMemory.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 (277 lines) | stat: -rw-r--r-- 7,867 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
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
// ==========================================================
// 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;

// --------------------------------------------------------------------------

static BOOL  
loadBuffer(const char *lpszPathName, BYTE **buffer, DWORD *length) {
	struct stat file_info;
	int result;

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

	return FALSE;
}

static BOOL 
extractPagesFromMemory(FREE_IMAGE_FORMAT fif, fipMemoryIO& memIO) {
	BOOL bMemoryCache = TRUE;

	char filename[256];
	fipImage image;

	// open the multipage bitmap stream as read-only
	fipMultiPage src(bMemoryCache);

	src.open(memIO);

	if(src.isValid()) {
		// get the page count
		int count = src.getPageCount();
		// extract all pages
		for(int page = 0; page < count; page++) {
			// load the bitmap at position 'page'
			image = src.lockPage(page);
			if(image.isValid()) {
				// save the page
				sprintf(filename, "page%d.%s", page, FreeImage_GetFormatFromFIF(fif));
				image.save(filename, 0);
				// Unload the bitmap (do not apply any change to src)
				src.unlockPage(image, FALSE);
			} else {
				// an error occured: free the multipage bitmap handle (fipMultiPage destructor) and return
				src.close(0);
				return FALSE;
			}
		}
	}
	// make sure to close the multipage bitmap handle on exit (fipMultiPage destructor or direct call to src.close(0))
	return src.close(0);
}

BOOL testLoadMultiBitmapFromMemory(const char *lpszPathName) {
	BOOL bSuccess = FALSE;

	BYTE *buffer = NULL;
	DWORD buffer_size = 0;

	// load source stream as a buffer, i.e. 
	// allocate a memory buffer and load temporary data
	bSuccess = loadBuffer(lpszPathName, &buffer, &buffer_size);
	assert(bSuccess);

	if(bSuccess) {
		// attach the binary data to a memory stream
		fipMemoryIO memIO(buffer, buffer_size);

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

		// extract pages
		bSuccess = extractPagesFromMemory(fif, memIO);
		assert(bSuccess);

		// close the memory stream (memIO destructor)
	}

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

	return bSuccess;
}

// --------------------------------------------------------------------------

BOOL testSaveMultiBitmapToMemory(const char *input, const char *output, int output_flag) {
	BOOL bSuccess;

	BOOL bCreateNew = FALSE;
	BOOL bReadOnly = TRUE;
	BOOL bMemoryCache = TRUE;

	// Open src file (read-only, use memory cache)
	fipMultiPage src(bMemoryCache);
	src.open(input, bCreateNew, bReadOnly, 0);

	if(src.isValid()) {
		// open and allocate a memory stream
		fipMemoryIO memIO;
		
		// save the file to memory
		FREE_IMAGE_FORMAT fif = fipImage::identifyFIF(output);
		bSuccess = src.saveToMemory(fif, memIO, output_flag);
		assert(bSuccess);

		// src is no longer needed: close and free src file
		src.close(0);

		// 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);

		// save the buffer in a file stream
		FILE *stream = fopen(output, "wb");
		if(stream) {
			fwrite(mem_buffer, sizeof(BYTE), size_in_bytes, stream);
			fclose(stream);
		}
		
		// close and free the memory stream (memIO destructor)
		
		return TRUE;
	}

	return FALSE;
}

BOOL testMemoryStreamMultiPageOpenSave(const char *lpszPathName, char *output, int input_flag, int output_flag) {
	BOOL bSuccess = FALSE;

	BYTE *buffer = NULL;
	DWORD buffer_size = 0;

	// load source stream as a buffer, i.e. 
	// allocate a memory buffer and load temporary data
	bSuccess = loadBuffer(lpszPathName, &buffer, &buffer_size);
	assert(bSuccess);

	// attach the binary data to a memory stream
	fipMemoryIO src_stream(buffer, buffer_size);
	assert(src_stream.isValid());

	// open the multipage bitmap stream
	fipMultiPage src;
	src.open(src_stream, input_flag);

	// apply some modifications (everything being stored to the cache) ...

	if(src.isValid()) {
		fipImage image;

		// get the page count
		int count = src.getPageCount();
		assert(count > 2);

		// Load the bitmap at position '2'
		image = src.lockPage(2);
		if(image.isValid()) {
			image.invert();
			// Unload the bitmap (apply change to src, modifications are stored to the cache)
			src.unlockPage(image, TRUE);
		}

		// delete page 0 (modifications are stored to the cache)
		src.deletePage(0);

		// insert a new page at position '0' (modifications are stored to the cache)
		image.load("test.jpg");
		src.insertPage(0, image);
	}

	// save the modification into the output stream ...

	if(src.isValid()) {
		// open and allocate a memory stream
		fipMemoryIO dst_stream;
		assert(dst_stream.isValid());
		
		// save the file to memory
		FREE_IMAGE_FORMAT fif = fipImage::identifyFIF(output);
		src.saveToMemory(fif, dst_stream, output_flag);

		// src is no longer needed
		// close and free the memory stream
		src_stream.close();
		// close and free src file (nothing is done, the cache is cleared)
		src.close(0);

		// at this point, the input buffer is no longer needed
		// !!! user is responsible for freeing the initial source buffer !!!
		free(buffer); buffer = NULL;
		
		// get the dst buffer from the memory stream
		BYTE *dst_buffer = NULL;
		DWORD size_in_bytes = 0;
		
		dst_stream.acquire(&dst_buffer, &size_in_bytes);
		
		// save the buffer in a file stream
		FILE *stream = fopen(output, "wb");
		if(stream) {
			fwrite(dst_buffer, sizeof(BYTE), size_in_bytes, stream);
			fclose(stream);
		}
		
		// close and free the memory stream (destructor is called)

		return TRUE;
	}

	if(buffer) {
		free(buffer);
	}

	return FALSE;
}

// --------------------------------------------------------------------------

void testMultiPageMemory(const char *lpszPathName) {
	BOOL bSuccess;

	cout << "testMultiPageMemory ...\n";
	
	// test FreeImage_LoadMultiBitmapFromMemory
	bSuccess = testLoadMultiBitmapFromMemory(lpszPathName);
	assert(bSuccess);

	// test FreeImage_SaveMultiBitmapToMemory
	bSuccess = testSaveMultiBitmapToMemory(lpszPathName, "mpage-mstream.tif", 0);
	assert(bSuccess);

	// test FreeImage_LoadMultiBitmapFromMemory & FreeImage_SaveMultiBitmapToMemory
	bSuccess = testMemoryStreamMultiPageOpenSave(lpszPathName, "mpage-mstream-redirect.tif", 0, 0);
	assert(bSuccess);

}