1 // ==========================================================
2 // fipMemoryIO class implementation
3 //
4 // Design and implementation by
5 // - Herv� Drolon (drolon@infonie.fr)
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 "FreeImagePlus.h"
23 #include <stdlib.h>
24 #include <string.h>
25 #include <assert.h>
26 #include <sys/types.h>
27 #include <sys/stat.h>
28 #include <stdio.h>
29 
fipMemoryIO(BYTE * data,DWORD size_in_bytes)30 fipMemoryIO::fipMemoryIO(BYTE *data, DWORD size_in_bytes) {
31 	_hmem = FreeImage_OpenMemory(data, size_in_bytes);
32 }
33 
~fipMemoryIO()34 fipMemoryIO::~fipMemoryIO() {
35 	if(_hmem != NULL) {
36 		FreeImage_CloseMemory(_hmem);
37 	}
38 }
39 
close()40 void fipMemoryIO::close() {
41 	if(_hmem != NULL) {
42 		FreeImage_CloseMemory(_hmem);
43 		_hmem = NULL;
44 	}
45 }
46 
isValid() const47 BOOL fipMemoryIO::isValid() const {
48 	return (_hmem != NULL);
49 }
50 
getFileType() const51 FREE_IMAGE_FORMAT fipMemoryIO::getFileType() const {
52 	if(_hmem != NULL) {
53 		return FreeImage_GetFileTypeFromMemory(_hmem, 0);
54 	}
55 
56 	return FIF_UNKNOWN;
57 }
58 
load(FREE_IMAGE_FORMAT fif,int flags) const59 FIBITMAP* fipMemoryIO::load(FREE_IMAGE_FORMAT fif, int flags) const {
60 	return FreeImage_LoadFromMemory(fif, _hmem, flags);
61 }
62 
loadMultiPage(FREE_IMAGE_FORMAT fif,int flags) const63 FIMULTIBITMAP* fipMemoryIO::loadMultiPage(FREE_IMAGE_FORMAT fif, int flags) const {
64 	return FreeImage_LoadMultiBitmapFromMemory(fif, _hmem, flags);
65 }
66 
save(FREE_IMAGE_FORMAT fif,FIBITMAP * dib,int flags)67 BOOL fipMemoryIO::save(FREE_IMAGE_FORMAT fif, FIBITMAP *dib, int flags) {
68 	return FreeImage_SaveToMemory(fif, dib, _hmem, flags);
69 }
70 
saveMultiPage(FREE_IMAGE_FORMAT fif,FIMULTIBITMAP * bitmap,int flags)71 BOOL fipMemoryIO::saveMultiPage(FREE_IMAGE_FORMAT fif, FIMULTIBITMAP *bitmap, int flags) {
72 	return FreeImage_SaveMultiBitmapToMemory(fif, bitmap, _hmem, flags);
73 }
74 
read(void * buffer,unsigned size,unsigned count) const75 unsigned fipMemoryIO::read(void *buffer, unsigned size, unsigned count) const {
76 	return FreeImage_ReadMemory(buffer, size, count, _hmem);
77 }
78 
write(const void * buffer,unsigned size,unsigned count)79 unsigned fipMemoryIO::write(const void *buffer, unsigned size, unsigned count) {
80 	return FreeImage_WriteMemory(buffer, size, count, _hmem);
81 }
82 
tell() const83 long fipMemoryIO::tell() const {
84 	return FreeImage_TellMemory(_hmem);
85 }
86 
seek(long offset,int origin)87 BOOL fipMemoryIO::seek(long offset, int origin) {
88 	return FreeImage_SeekMemory(_hmem, offset, origin);
89 }
90 
acquire(BYTE ** data,DWORD * size_in_bytes)91 BOOL fipMemoryIO::acquire(BYTE **data, DWORD *size_in_bytes) {
92 	return FreeImage_AcquireMemory(_hmem, data, size_in_bytes);
93 }
94 
95 
96