/src/freeimage-svn/FreeImage/trunk/Source/FreeImage/MemoryIO.cpp
Line | Count | Source (jump to first uncovered line) |
1 | | // ========================================================== |
2 | | // Memory Input/Output functions |
3 | | // |
4 | | // Design and implementation by |
5 | | // - Ryan Rubley <ryan@lostreality.org> |
6 | | // - Hervé Drolon (drolon@infonie.fr) |
7 | | // |
8 | | // This file is part of FreeImage 3 |
9 | | // |
10 | | // COVERED CODE IS PROVIDED UNDER THIS LICENSE ON AN "AS IS" BASIS, WITHOUT WARRANTY |
11 | | // OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, WITHOUT LIMITATION, WARRANTIES |
12 | | // THAT THE COVERED CODE IS FREE OF DEFECTS, MERCHANTABLE, FIT FOR A PARTICULAR PURPOSE |
13 | | // OR NON-INFRINGING. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE COVERED |
14 | | // CODE IS WITH YOU. SHOULD ANY COVERED CODE PROVE DEFECTIVE IN ANY RESPECT, YOU (NOT |
15 | | // THE INITIAL DEVELOPER OR ANY OTHER CONTRIBUTOR) ASSUME THE COST OF ANY NECESSARY |
16 | | // SERVICING, REPAIR OR CORRECTION. THIS DISCLAIMER OF WARRANTY CONSTITUTES AN ESSENTIAL |
17 | | // PART OF THIS LICENSE. NO USE OF ANY COVERED CODE IS AUTHORIZED HEREUNDER EXCEPT UNDER |
18 | | // THIS DISCLAIMER. |
19 | | // |
20 | | // Use at your own risk! |
21 | | // ========================================================== |
22 | | |
23 | | #include "FreeImage.h" |
24 | | #include "Utilities.h" |
25 | | #include "FreeImageIO.h" |
26 | | |
27 | | // ===================================================================== |
28 | | |
29 | | |
30 | | // ===================================================================== |
31 | | // Open and close a memory handle |
32 | | // ===================================================================== |
33 | | |
34 | | FIMEMORY * DLL_CALLCONV |
35 | 22.8k | FreeImage_OpenMemory(BYTE *data, DWORD size_in_bytes) { |
36 | | // allocate a memory handle |
37 | 22.8k | FIMEMORY *stream = (FIMEMORY*)malloc(sizeof(FIMEMORY)); |
38 | 22.8k | if(stream) { |
39 | 22.8k | stream->data = (BYTE*)malloc(sizeof(FIMEMORYHEADER)); |
40 | | |
41 | 22.8k | if(stream->data) { |
42 | 22.8k | FIMEMORYHEADER *mem_header = (FIMEMORYHEADER*)(stream->data); |
43 | | |
44 | | // initialize the memory header |
45 | 22.8k | memset(mem_header, 0, sizeof(FIMEMORYHEADER)); |
46 | | |
47 | 22.8k | if(data && size_in_bytes) { |
48 | | // wrap a user buffer |
49 | 22.8k | mem_header->delete_me = FALSE; |
50 | 22.8k | mem_header->data = (BYTE*)data; |
51 | 22.8k | mem_header->file_length = size_in_bytes; |
52 | 22.8k | mem_header->data_length = size_in_bytes; |
53 | 22.8k | } else { |
54 | 0 | mem_header->delete_me = TRUE; |
55 | 0 | } |
56 | | |
57 | 22.8k | return stream; |
58 | 22.8k | } |
59 | 0 | free(stream); |
60 | 0 | } |
61 | | |
62 | 0 | return NULL; |
63 | 22.8k | } |
64 | | |
65 | | |
66 | | void DLL_CALLCONV |
67 | 22.8k | FreeImage_CloseMemory(FIMEMORY *stream) { |
68 | 22.8k | if(stream && stream->data) { |
69 | 22.8k | FIMEMORYHEADER *mem_header = (FIMEMORYHEADER*)(stream->data); |
70 | 22.8k | if(mem_header->delete_me) { |
71 | 0 | free(mem_header->data); |
72 | 0 | } |
73 | 22.8k | free(mem_header); |
74 | 22.8k | free(stream); |
75 | 22.8k | } |
76 | 22.8k | } |
77 | | |
78 | | // ===================================================================== |
79 | | // Memory stream load/save functions |
80 | | // ===================================================================== |
81 | | |
82 | | FIBITMAP * DLL_CALLCONV |
83 | 97 | FreeImage_LoadFromMemory(FREE_IMAGE_FORMAT fif, FIMEMORY *stream, int flags) { |
84 | 97 | if (stream && stream->data) { |
85 | 97 | FreeImageIO io; |
86 | 97 | SetMemoryIO(&io); |
87 | | |
88 | 97 | return FreeImage_LoadFromHandle(fif, &io, (fi_handle)stream, flags); |
89 | 97 | } |
90 | | |
91 | 0 | return NULL; |
92 | 97 | } |
93 | | |
94 | | |
95 | | BOOL DLL_CALLCONV |
96 | 0 | FreeImage_SaveToMemory(FREE_IMAGE_FORMAT fif, FIBITMAP *dib, FIMEMORY *stream, int flags) { |
97 | 0 | if (stream) { |
98 | 0 | FreeImageIO io; |
99 | 0 | SetMemoryIO(&io); |
100 | |
|
101 | 0 | FIMEMORYHEADER *mem_header = (FIMEMORYHEADER*)(stream->data); |
102 | |
|
103 | 0 | if(mem_header->delete_me == TRUE) { |
104 | 0 | return FreeImage_SaveToHandle(fif, dib, &io, (fi_handle)stream, flags); |
105 | 0 | } else { |
106 | | // do not save in a user buffer |
107 | 0 | FreeImage_OutputMessageProc(fif, "Memory buffer is read only"); |
108 | 0 | } |
109 | 0 | } |
110 | | |
111 | 0 | return FALSE; |
112 | 0 | } |
113 | | |
114 | | // ===================================================================== |
115 | | // Memory stream buffer access |
116 | | // ===================================================================== |
117 | | |
118 | | BOOL DLL_CALLCONV |
119 | 0 | FreeImage_AcquireMemory(FIMEMORY *stream, BYTE **data, DWORD *size_in_bytes) { |
120 | 0 | if (stream) { |
121 | 0 | FIMEMORYHEADER *mem_header = (FIMEMORYHEADER*)(stream->data); |
122 | |
|
123 | 0 | *data = (BYTE*)mem_header->data; |
124 | 0 | *size_in_bytes = mem_header->file_length; |
125 | 0 | return TRUE; |
126 | 0 | } |
127 | | |
128 | 0 | return FALSE; |
129 | 0 | } |
130 | | |
131 | | // ===================================================================== |
132 | | // Seeking in Memory stream |
133 | | // ===================================================================== |
134 | | |
135 | | /** |
136 | | Moves the memory pointer to a specified location |
137 | | @param stream Pointer to FIMEMORY structure |
138 | | @param offset Number of bytes from origin |
139 | | @param origin Initial position |
140 | | @return Returns TRUE if successful, returns FALSE otherwise |
141 | | */ |
142 | | BOOL DLL_CALLCONV |
143 | 0 | FreeImage_SeekMemory(FIMEMORY *stream, long offset, int origin) { |
144 | 0 | FreeImageIO io; |
145 | 0 | SetMemoryIO(&io); |
146 | |
|
147 | 0 | if (stream != NULL) { |
148 | 0 | int success = io.seek_proc((fi_handle)stream, offset, origin); |
149 | 0 | return (success == 0) ? TRUE : FALSE; |
150 | 0 | } |
151 | | |
152 | 0 | return FALSE; |
153 | 0 | } |
154 | | |
155 | | /** |
156 | | Gets the current position of a memory pointer |
157 | | @param stream Target FIMEMORY structure |
158 | | @return Returns the current file position if successful, -1 otherwise |
159 | | */ |
160 | | long DLL_CALLCONV |
161 | 0 | FreeImage_TellMemory(FIMEMORY *stream) { |
162 | 0 | FreeImageIO io; |
163 | 0 | SetMemoryIO(&io); |
164 | |
|
165 | 0 | if (stream != NULL) { |
166 | 0 | return io.tell_proc((fi_handle)stream); |
167 | 0 | } |
168 | | |
169 | 0 | return -1L; |
170 | 0 | } |
171 | | |
172 | | // ===================================================================== |
173 | | // Reading or Writing in Memory stream |
174 | | // ===================================================================== |
175 | | |
176 | | /** |
177 | | Reads data from a memory stream |
178 | | @param buffer Storage location for data |
179 | | @param size Item size in bytes |
180 | | @param count Maximum number of items to be read |
181 | | @param stream Pointer to FIMEMORY structure |
182 | | @return Returns the number of full items actually read, which may be less than count if an error occurs |
183 | | */ |
184 | | unsigned DLL_CALLCONV |
185 | 0 | FreeImage_ReadMemory(void *buffer, unsigned size, unsigned count, FIMEMORY *stream) { |
186 | 0 | FreeImageIO io; |
187 | 0 | SetMemoryIO(&io); |
188 | |
|
189 | 0 | if (stream != NULL) { |
190 | 0 | return io.read_proc(buffer, size, count, stream); |
191 | 0 | } |
192 | | |
193 | 0 | return 0; |
194 | 0 | } |
195 | | |
196 | | /** |
197 | | Writes data to a memory stream. |
198 | | @param buffer Pointer to data to be written |
199 | | @param size Item size in bytes |
200 | | @param count Maximum number of items to be written |
201 | | @param stream Pointer to FIMEMORY structure |
202 | | @return Returns the number of full items actually written, which may be less than count if an error occurs |
203 | | */ |
204 | | unsigned DLL_CALLCONV |
205 | 0 | FreeImage_WriteMemory(const void *buffer, unsigned size, unsigned count, FIMEMORY *stream) { |
206 | 0 | if (stream != NULL) { |
207 | 0 | FreeImageIO io; |
208 | 0 | SetMemoryIO(&io); |
209 | |
|
210 | 0 | FIMEMORYHEADER *mem_header = (FIMEMORYHEADER*)(((FIMEMORY*)stream)->data); |
211 | |
|
212 | 0 | if(mem_header->delete_me == TRUE) { |
213 | 0 | return io.write_proc((void *)buffer, size, count, stream); |
214 | 0 | } else { |
215 | | // do not write in a user buffer |
216 | 0 | FreeImage_OutputMessageProc(FIF_UNKNOWN, "Memory buffer is read only"); |
217 | 0 | } |
218 | 0 | } |
219 | | |
220 | 0 | return 0; |
221 | 0 | } |
222 | | |