/[projet1]/public/pc/shared_libraries/freeimage/v3.12.0/Source/FreeImage/FreeImageIO.cpp
Defence Force logotype

Contents of /public/pc/shared_libraries/freeimage/v3.12.0/Source/FreeImage/FreeImageIO.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 126 - (show annotations)
Mon Jul 13 12:20:10 2009 UTC (10 years, 7 months ago) by dbug
File size: 5024 byte(s)
Added some shared libraries (UnitTest++, and FreeImage) to avoid having every single project brings its own libraries.
Ideally people should add them in a way we can upgrade versions without breaking things:
-> public/pc/shared_libraries/library_name/library_version/actuall_content
1 // ==========================================================
2 // Input/Output functions
3 //
4 // Design and implementation by
5 // - Floris van den Berg (flvdberg@wxs.nl)
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 #include "FreeImage.h"
23 #include "Utilities.h"
24 #include "FreeImageIO.h"
25
26 // ----------------------------------------------------------
27
28 unsigned DLL_CALLCONV
29 _ReadProc(void *buffer, unsigned size, unsigned count, fi_handle handle) {
30 return (unsigned)fread(buffer, size, count, (FILE *)handle);
31 }
32
33 unsigned DLL_CALLCONV
34 _WriteProc(void *buffer, unsigned size, unsigned count, fi_handle handle) {
35 return (unsigned)fwrite(buffer, size, count, (FILE *)handle);
36 }
37
38 int DLL_CALLCONV
39 _SeekProc(fi_handle handle, long offset, int origin) {
40 return fseek((FILE *)handle, offset, origin);
41 }
42
43 long DLL_CALLCONV
44 _TellProc(fi_handle handle) {
45 return ftell((FILE *)handle);
46 }
47
48 // ----------------------------------------------------------
49
50 void
51 SetDefaultIO(FreeImageIO *io) {
52 io->read_proc = _ReadProc;
53 io->seek_proc = _SeekProc;
54 io->tell_proc = _TellProc;
55 io->write_proc = _WriteProc;
56 }
57
58 // =====================================================================
59 // Memory IO functions
60 // =====================================================================
61
62 unsigned DLL_CALLCONV
63 _MemoryReadProc(void *buffer, unsigned size, unsigned count, fi_handle handle) {
64 unsigned x;
65
66 FIMEMORYHEADER *mem_header = (FIMEMORYHEADER*)(((FIMEMORY*)handle)->data);
67
68 for(x = 0; x < count; x++) {
69 //if there isnt size bytes left to read, set pos to eof and return a short count
70 if( (mem_header->filelen - mem_header->curpos) < (long)size ) {
71 mem_header->curpos = mem_header->filelen;
72 break;
73 }
74 //copy size bytes count times
75 memcpy( buffer, (char *)mem_header->data + mem_header->curpos, size );
76 mem_header->curpos += size;
77 buffer = (char *)buffer + size;
78 }
79 return x;
80 }
81
82 unsigned DLL_CALLCONV
83 _MemoryWriteProc(void *buffer, unsigned size, unsigned count, fi_handle handle) {
84 void *newdata;
85 long newdatalen;
86
87 FIMEMORYHEADER *mem_header = (FIMEMORYHEADER*)(((FIMEMORY*)handle)->data);
88
89 //double the data block size if we need to
90 while( (mem_header->curpos + (long)(size*count)) >= mem_header->datalen ) {
91 //if we are at or above 1G, we cant double without going negative
92 if( mem_header->datalen & 0x40000000 ) {
93 //max 2G
94 if( mem_header->datalen == 0x7FFFFFFF ) {
95 return 0;
96 }
97 newdatalen = 0x7FFFFFFF;
98 } else if( mem_header->datalen == 0 ) {
99 //default to 4K if nothing yet
100 newdatalen = 4096;
101 } else {
102 //double size
103 newdatalen = mem_header->datalen << 1;
104 }
105 newdata = realloc( mem_header->data, newdatalen );
106 if( !newdata ) {
107 return 0;
108 }
109 mem_header->data = newdata;
110 mem_header->datalen = newdatalen;
111 }
112 memcpy( (char *)mem_header->data + mem_header->curpos, buffer, size*count );
113 mem_header->curpos += size*count;
114 if( mem_header->curpos > mem_header->filelen ) {
115 mem_header->filelen = mem_header->curpos;
116 }
117 return count;
118 }
119
120 int DLL_CALLCONV
121 _MemorySeekProc(fi_handle handle, long offset, int origin) {
122 FIMEMORYHEADER *mem_header = (FIMEMORYHEADER*)(((FIMEMORY*)handle)->data);
123
124 switch(origin) { //0 to filelen-1 are 'inside' the file
125 default:
126 case SEEK_SET: //can fseek() to 0-7FFFFFFF always
127 if( offset >= 0 ) {
128 mem_header->curpos = offset;
129 return 0;
130 }
131 break;
132
133 case SEEK_CUR:
134 if( mem_header->curpos + offset >= 0 ) {
135 mem_header->curpos += offset;
136 return 0;
137 }
138 break;
139
140 case SEEK_END:
141 if( mem_header->filelen + offset >= 0 ) {
142 mem_header->curpos = mem_header->filelen + offset;
143 return 0;
144 }
145 break;
146 }
147
148 return -1;
149 }
150
151 long DLL_CALLCONV
152 _MemoryTellProc(fi_handle handle) {
153 FIMEMORYHEADER *mem_header = (FIMEMORYHEADER*)(((FIMEMORY*)handle)->data);
154
155 return mem_header->curpos;
156 }
157
158 // ----------------------------------------------------------
159
160 void
161 SetMemoryIO(FreeImageIO *io) {
162 io->read_proc = _MemoryReadProc;
163 io->seek_proc = _MemorySeekProc;
164 io->tell_proc = _MemoryTellProc;
165 io->write_proc = _MemoryWriteProc;
166 }

  ViewVC Help
Powered by ViewVC 1.1.26