~ubuntu-branches/ubuntu/jaunty/freeimage/jaunty

« back to all changes in this revision

Viewing changes to FreeImage/Wrapper/FreeImagePlus/test/fipTestMemIO.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Federico Di Gregorio
  • Date: 2007-05-07 15:35:21 UTC
  • Revision ID: james.westby@ubuntu.com-20070507153521-m4lx765bzxxug9qf
Tags: upstream-3.9.3
Import upstream version 3.9.3

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// ==========================================================
 
2
// FreeImagePlus Test Script
 
3
//
 
4
// Design and implementation by
 
5
// - Herv� Drolon (drolon@infonie.fr)
 
6
//
 
7
// This file is part of FreeImage 3
 
8
//
 
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
 
17
// THIS DISCLAIMER.
 
18
//
 
19
// Use at your own risk!
 
20
// ==========================================================
 
21
 
 
22
 
 
23
#include "fipTest.h"
 
24
 
 
25
// --------------------------------------------------------------------------
 
26
// Memory IO test scripts
 
27
 
 
28
/**
 
29
Test saving to a memory stream
 
30
*/
 
31
void testSaveMemIO(const char *lpszPathName) {
 
32
 
 
33
        // load a regular file
 
34
        FREE_IMAGE_FORMAT fif = FreeImage_GetFileType(lpszPathName);
 
35
        FIBITMAP *dib = FreeImage_Load(fif, lpszPathName, 0);
 
36
        
 
37
        // open a memory handle
 
38
        fipMemoryIO memIO;
 
39
 
 
40
        // save the file to memory
 
41
        memIO.write(fif, dib, 0);
 
42
 
 
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);
 
47
 
 
48
        // its easy load an image from memory as well
 
49
 
 
50
        // seek to the start of the memory stream
 
51
        memIO.seek(0L, SEEK_SET);
 
52
        
 
53
        // get the file type    
 
54
        FREE_IMAGE_FORMAT mem_fif = memIO.getFileType();
 
55
        
 
56
        // load an image from the memory handle         
 
57
        FIBITMAP *check = memIO.read(mem_fif, 0);
 
58
 
 
59
        // save as a regular file
 
60
        FreeImage_Save(FIF_PNG, check, "dump.png", PNG_DEFAULT);
 
61
 
 
62
        FreeImage_Unload(check);
 
63
        FreeImage_Unload(dib);
 
64
 
 
65
        // The memIO object will be destroyed automatically
 
66
}
 
67
 
 
68
/**
 
69
Test loading from a buffer attached to a memory stream
 
70
*/
 
71
void testLoadMemIO(const char *lpszPathName) {
 
72
        struct stat buf;
 
73
        int result;
 
74
 
 
75
        // get data associated with lpszPathName
 
76
        result = stat(lpszPathName, &buf);
 
77
        if(result == 0) {
 
78
                // allocate a memory buffer and load temporary data
 
79
                BYTE *mem_buffer = (BYTE*)malloc(buf.st_size * sizeof(BYTE));
 
80
                if(mem_buffer) {
 
81
                        FILE *stream = fopen(lpszPathName, "rb");
 
82
                        if(stream) {
 
83
                                fread(mem_buffer, sizeof(BYTE), buf.st_size, stream);
 
84
                                fclose(stream);
 
85
 
 
86
                                // attach the binary data to a memory stream
 
87
                                fipMemoryIO memIO(mem_buffer, buf.st_size);
 
88
 
 
89
                                // get the file type
 
90
                                FREE_IMAGE_FORMAT fif = memIO.getFileType();
 
91
 
 
92
                                // load an image from the memory stream
 
93
                                FIBITMAP *check = memIO.read(fif, PNG_DEFAULT);
 
94
 
 
95
                                // save as a regular file
 
96
                                FreeImage_Save(FIF_PNG, check, "blob.png", PNG_DEFAULT);
 
97
                                
 
98
                                // close the stream (memIO is destroyed)
 
99
                        }
 
100
 
 
101
                        // user is responsible for freeing the data
 
102
                        free(mem_buffer);
 
103
                }
 
104
        }
 
105
}
 
106
 
 
107
/**
 
108
Test extracting a memory buffer from a memory stream
 
109
*/
 
110
void testAcquireMemIO(const char *lpszPathName) {
 
111
 
 
112
        // load a regular file
 
113
        FREE_IMAGE_FORMAT fif = FreeImage_GetFileType(lpszPathName);
 
114
        FIBITMAP *dib = FreeImage_Load(fif, lpszPathName, 0);
 
115
 
 
116
        // open and allocate a memory stream
 
117
        fipMemoryIO memIO;
 
118
 
 
119
        // save the file to memory
 
120
        memIO.write(FIF_PNG, dib, PNG_DEFAULT);
 
121
 
 
122
        // get the buffer from the memory stream
 
123
        BYTE *mem_buffer = NULL;
 
124
        DWORD size_in_bytes = 0;
 
125
 
 
126
        memIO.acquire(&mem_buffer, &size_in_bytes);
 
127
 
 
128
        // save the buffer in a file stream
 
129
        FILE *stream = fopen("buffer.png", "wb");
 
130
        if(stream) {
 
131
                fwrite(mem_buffer, sizeof(BYTE), size_in_bytes, stream);
 
132
                fclose(stream);
 
133
        }
 
134
 
 
135
        // close and free the memory stream (memIO is destroyed)
 
136
}
 
137
 
 
138
/**
 
139
Test Loading / Saving from / to a memory stream using fipImage
 
140
*/
 
141
void testImageMemIO(const char *lpszPathName) {
 
142
        BOOL bSuccess = FALSE;
 
143
 
 
144
        fipMemoryIO memIO;
 
145
        fipImage image;
 
146
 
 
147
        // load a regular file
 
148
        bSuccess = image.load(lpszPathName);
 
149
        if(bSuccess) {
 
150
                // save the file to a memory stream
 
151
                bSuccess = image.saveToMemory(FIF_PNG, memIO, PNG_DEFAULT);
 
152
                
 
153
                // load the file from the memory stream
 
154
                memIO.seek(0L, SEEK_SET);
 
155
                bSuccess = image.loadFromMemory(memIO, 0);
 
156
        }
 
157
}
 
158
 
 
159
void testMemIO(const char *lpszPathName) {
 
160
        testSaveMemIO(lpszPathName);
 
161
        testLoadMemIO(lpszPathName);
 
162
        testAcquireMemIO(lpszPathName);
 
163
        testImageMemIO(lpszPathName);
 
164
}
 
165