File: LoadFromMemory.cpp

package info (click to toggle)
freeimage 3.10.0-1
  • links: PTS, VCS
  • area: main
  • in suites: lenny
  • size: 18,864 kB
  • ctags: 21,425
  • sloc: ansic: 150,658; cpp: 57,288; pascal: 2,899; cs: 786; sh: 574; makefile: 394; asm: 284
file content (113 lines) | stat: -rw-r--r-- 2,760 bytes parent folder | download | duplicates (11)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
// ==========================================================
// Load From Memory Example
//
// Design and implementation by Floris van den Berg
//
// This file is part of FreeImage 3
//
// Use at own risk!
// ==========================================================
//
//  This example shows how to load a bitmap from memory
//  rather than from a file. To do this we make use of the
//  FreeImage_LoadFromHandle functions where we override
//  the i/o functions to simulate FILE* access in memory.
//
//  For seeking purposes the fi_handle passed to the i/o
//  functions contain the start of the data block where the
//  bitmap is stored.
//
// ==========================================================

#include <assert.h>
#include <stdio.h>
#include <stdlib.h>

#include "FreeImage.h"

// ----------------------------------------------------------

fi_handle g_load_address;

// ----------------------------------------------------------

inline unsigned _stdcall
_ReadProc(void *buffer, unsigned size, unsigned count, fi_handle handle) {
	BYTE *tmp = (BYTE *)buffer;

	for (unsigned c = 0; c < count; c++) {
		memcpy(tmp, g_load_address, size);

		g_load_address = (BYTE *)g_load_address + size;

		tmp += size;
	}

	return count;
}

inline unsigned _stdcall
_WriteProc(void *buffer, unsigned size, unsigned count, fi_handle handle) {
	// there's not much use for saving the bitmap into memory now, is there?

	return size;
}

inline int _stdcall
_SeekProc(fi_handle handle, long offset, int origin) {
	assert(origin != SEEK_END);

	if (origin == SEEK_SET) {
		g_load_address = (BYTE *)handle + offset;
	} else {
		g_load_address = (BYTE *)g_load_address + offset;
	}

	return 0;
}

inline long _stdcall
_TellProc(fi_handle handle) {
	assert((int)handle > (int)g_load_address);

	return ((int)g_load_address - (int)handle);
}

// ----------------------------------------------------------

int 
main(int argc, char *argv[]) {
	FreeImageIO io;

	io.read_proc  = _ReadProc;
	io.write_proc = _WriteProc;
	io.tell_proc  = _TellProc;
	io.seek_proc  = _SeekProc;

	// allocate some memory for the bitmap

	BYTE *test = new BYTE[159744];

	if (test != NULL) {
		// load the bitmap into memory. ofcourse you can do this any way you want

		FILE *file = fopen("e:\\projects\\images\\money-256.tif", "rb");
		fread(test, 159744, 1, file);
		fclose(file);

		// we store the load address of the bitmap for internal reasons

		g_load_address = test;

		// convert the bitmap
		
		FIBITMAP *dib = FreeImage_LoadFromHandle(FIF_TIFF, &io, (fi_handle)test);

		// don't forget to free the dib !
		FreeImage_Unload(dib);

		delete [] test;
	}

	return 0;
}