/[projet1]/public/pc/shared_libraries/freeimage/v3.12.0/Examples/Generic/FIIO_Mem.cpp
Defence Force logotype

Contents of /public/pc/shared_libraries/freeimage/v3.12.0/Examples/Generic/FIIO_Mem.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 126 - (show annotations)
Mon Jul 13 12:20:10 2009 UTC (10 years, 10 months ago) by dbug
File size: 3832 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 || fiio_mem.cpp by Ryan Rubley <ryan@lostreality.org> ||
3 || ||
4 || (v1.02) 4-28-2004 ||
5 || FreeImageIO to memory ||
6 || ||
7 \*--------------------------------------------------------------------------*/
8
9 #include <string.h>
10 #include <stdlib.h>
11 #include "fiio_mem.h"
12
13 #ifdef __cplusplus
14 extern "C" {
15 #endif
16
17 FIBITMAP *
18 FreeImage_LoadFromMem(FREE_IMAGE_FORMAT fif, fiio_mem_handle *handle, int flags) {
19 FreeImageIO io;
20 SetMemIO(&io);
21
22 if (handle && handle->data) {
23 handle->curpos = 0;
24 return FreeImage_LoadFromHandle(fif, &io, (fi_handle)handle, flags);
25 }
26
27 return NULL;
28 }
29
30 BOOL
31 FreeImage_SaveToMem(FREE_IMAGE_FORMAT fif, FIBITMAP *dib, fiio_mem_handle *handle, int flags) {
32 FreeImageIO io;
33 SetMemIO(&io);
34
35 if (handle) {
36 handle->filelen = 0;
37 handle->curpos = 0;
38 return FreeImage_SaveToHandle(fif, dib, &io, (fi_handle)handle, flags);
39 }
40
41 return FALSE;
42 }
43
44 // ----------------------------------------------------------
45
46 void
47 SetMemIO(FreeImageIO *io) {
48 io->read_proc = fiio_mem_ReadProc;
49 io->seek_proc = fiio_mem_SeekProc;
50 io->tell_proc = fiio_mem_TellProc;
51 io->write_proc = fiio_mem_WriteProc;
52 }
53
54 // ----------------------------------------------------------
55
56 #define FIIOMEM(member) (((fiio_mem_handle *)handle)->member)
57
58 unsigned
59 fiio_mem_ReadProc(void *buffer, unsigned size, unsigned count, fi_handle handle) {
60 unsigned x;
61 for( x=0; x<count; x++ ) {
62 //if there isnt size bytes left to read, set pos to eof and return a short count
63 if( FIIOMEM(filelen)-FIIOMEM(curpos) < (long)size ) {
64 FIIOMEM(curpos) = FIIOMEM(filelen);
65 break;
66 }
67 //copy size bytes count times
68 memcpy( buffer, (char *)FIIOMEM(data) + FIIOMEM(curpos), size );
69 FIIOMEM(curpos) += size;
70 buffer = (char *)buffer + size;
71 }
72 return x;
73 }
74
75 unsigned
76 fiio_mem_WriteProc(void *buffer, unsigned size, unsigned count, fi_handle handle) {
77 void *newdata;
78 long newdatalen;
79 //double the data block size if we need to
80 while( FIIOMEM(curpos)+(long)(size*count) >= FIIOMEM(datalen) ) {
81 //if we are at or above 1G, we cant double without going negative
82 if( FIIOMEM(datalen) & 0x40000000 ) {
83 //max 2G
84 if( FIIOMEM(datalen) == 0x7FFFFFFF ) {
85 return 0;
86 }
87 newdatalen = 0x7FFFFFFF;
88 } else if( FIIOMEM(datalen) == 0 ) {
89 //default to 4K if nothing yet
90 newdatalen = 4096;
91 } else {
92 //double size
93 newdatalen = FIIOMEM(datalen) << 1;
94 }
95 newdata = realloc( FIIOMEM(data), newdatalen );
96 if( !newdata ) {
97 return 0;
98 }
99 FIIOMEM(data) = newdata;
100 FIIOMEM(datalen) = newdatalen;
101 }
102 memcpy( (char *)FIIOMEM(data) + FIIOMEM(curpos), buffer, size*count );
103 FIIOMEM(curpos) += size*count;
104 if( FIIOMEM(curpos) > FIIOMEM(filelen) ) {
105 FIIOMEM(filelen) = FIIOMEM(curpos);
106 }
107 return count;
108 }
109
110 int
111 fiio_mem_SeekProc(fi_handle handle, long offset, int origin) {
112 switch(origin) { //0 to filelen-1 are 'inside' the file
113 default:
114 case SEEK_SET: //can fseek() to 0-7FFFFFFF always
115 if( offset >= 0 ) {
116 FIIOMEM(curpos) = offset;
117 return 0;
118 }
119 break;
120
121 case SEEK_CUR:
122 if( FIIOMEM(curpos)+offset >= 0 ) {
123 FIIOMEM(curpos) += offset;
124 return 0;
125 }
126 break;
127
128 case SEEK_END:
129 if( FIIOMEM(filelen)+offset >= 0 ) {
130 FIIOMEM(curpos) = FIIOMEM(filelen)+offset;
131 return 0;
132 }
133 break;
134 }
135
136 return -1;
137 }
138
139 long
140 fiio_mem_TellProc(fi_handle handle) {
141 return FIIOMEM(curpos);
142 }
143
144 #ifdef __cplusplus
145 }
146 #endif

  ViewVC Help
Powered by ViewVC 1.1.26