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

Contents of /public/pc/shared_libraries/freeimage/v3.12.0/Examples/Generic/LoadFromMemory.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: 2760 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 // Load From Memory Example
3 //
4 // Design and implementation by Floris van den Berg
5 //
6 // This file is part of FreeImage 3
7 //
8 // Use at own risk!
9 // ==========================================================
10 //
11 // This example shows how to load a bitmap from memory
12 // rather than from a file. To do this we make use of the
13 // FreeImage_LoadFromHandle functions where we override
14 // the i/o functions to simulate FILE* access in memory.
15 //
16 // For seeking purposes the fi_handle passed to the i/o
17 // functions contain the start of the data block where the
18 // bitmap is stored.
19 //
20 // ==========================================================
21
22 #include <assert.h>
23 #include <stdio.h>
24 #include <stdlib.h>
25
26 #include "FreeImage.h"
27
28 // ----------------------------------------------------------
29
30 fi_handle g_load_address;
31
32 // ----------------------------------------------------------
33
34 inline unsigned _stdcall
35 _ReadProc(void *buffer, unsigned size, unsigned count, fi_handle handle) {
36 BYTE *tmp = (BYTE *)buffer;
37
38 for (unsigned c = 0; c < count; c++) {
39 memcpy(tmp, g_load_address, size);
40
41 g_load_address = (BYTE *)g_load_address + size;
42
43 tmp += size;
44 }
45
46 return count;
47 }
48
49 inline unsigned _stdcall
50 _WriteProc(void *buffer, unsigned size, unsigned count, fi_handle handle) {
51 // there's not much use for saving the bitmap into memory now, is there?
52
53 return size;
54 }
55
56 inline int _stdcall
57 _SeekProc(fi_handle handle, long offset, int origin) {
58 assert(origin != SEEK_END);
59
60 if (origin == SEEK_SET) {
61 g_load_address = (BYTE *)handle + offset;
62 } else {
63 g_load_address = (BYTE *)g_load_address + offset;
64 }
65
66 return 0;
67 }
68
69 inline long _stdcall
70 _TellProc(fi_handle handle) {
71 assert((int)handle > (int)g_load_address);
72
73 return ((int)g_load_address - (int)handle);
74 }
75
76 // ----------------------------------------------------------
77
78 int
79 main(int argc, char *argv[]) {
80 FreeImageIO io;
81
82 io.read_proc = _ReadProc;
83 io.write_proc = _WriteProc;
84 io.tell_proc = _TellProc;
85 io.seek_proc = _SeekProc;
86
87 // allocate some memory for the bitmap
88
89 BYTE *test = new BYTE[159744];
90
91 if (test != NULL) {
92 // load the bitmap into memory. ofcourse you can do this any way you want
93
94 FILE *file = fopen("e:\\projects\\images\\money-256.tif", "rb");
95 fread(test, 159744, 1, file);
96 fclose(file);
97
98 // we store the load address of the bitmap for internal reasons
99
100 g_load_address = test;
101
102 // convert the bitmap
103
104 FIBITMAP *dib = FreeImage_LoadFromHandle(FIF_TIFF, &io, (fi_handle)test);
105
106 // don't forget to free the dib !
107 FreeImage_Unload(dib);
108
109 delete [] test;
110 }
111
112 return 0;
113 }

  ViewVC Help
Powered by ViewVC 1.1.26