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

Contents of /public/pc/shared_libraries/freeimage/v3.12.0/Examples/Generic/LoadFromHandle.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: 4265 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 Handle Example
3 //
4 // Design and implementation by
5 // - Hervé Drolon
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 own risk!
20 // ==========================================================
21
22 // This example shows how to load a bitmap from a
23 // user allocated FILE pointer.
24 //
25 // Functions used in this sample :
26 // FreeImage_GetFormatFromFIF, FreeImage_GetFileTypeFromHandle, FreeImage_LoadFromHandle,
27 // FreeImage_GetFIFFromFilename, FreeImage_Save, FreeImage_Unload
28 // FreeImage_GetVersion, FreeImage_GetCopyrightMessage, FreeImage_SetOutputMessage
29 //
30 // ==========================================================
31
32 #include <assert.h>
33 #include <stdio.h>
34 #include <stdlib.h>
35
36 #include "FreeImage.h"
37
38 // ----------------------------------------------------------
39
40 /**
41 FreeImage error handler
42 @param fif Format / Plugin responsible for the error
43 @param message Error message
44 */
45 void FreeImageErrorHandler(FREE_IMAGE_FORMAT fif, const char *message) {
46 printf("\n*** ");
47 if(fif != FIF_UNKNOWN) {
48 printf("%s Format\n", FreeImage_GetFormatFromFIF(fif));
49 }
50 printf(message);
51 printf(" ***\n");
52 }
53
54 // ----------------------------------------------------------
55
56 unsigned DLL_CALLCONV
57 myReadProc(void *buffer, unsigned size, unsigned count, fi_handle handle) {
58 return fread(buffer, size, count, (FILE *)handle);
59 }
60
61 unsigned DLL_CALLCONV
62 myWriteProc(void *buffer, unsigned size, unsigned count, fi_handle handle) {
63 return fwrite(buffer, size, count, (FILE *)handle);
64 }
65
66 int DLL_CALLCONV
67 mySeekProc(fi_handle handle, long offset, int origin) {
68 return fseek((FILE *)handle, offset, origin);
69 }
70
71 long DLL_CALLCONV
72 myTellProc(fi_handle handle) {
73 return ftell((FILE *)handle);
74 }
75
76 // ----------------------------------------------------------
77
78 int
79 main(int argc, char *argv[]) {
80
81 // call this ONLY when linking with FreeImage as a static library
82 #ifdef FREEIMAGE_LIB
83 FreeImage_Initialise();
84 #endif // FREEIMAGE_LIB
85
86 // initialize your own FreeImage error handler
87
88 FreeImage_SetOutputMessage(FreeImageErrorHandler);
89
90 // print version & copyright infos
91
92 printf(FreeImage_GetVersion());
93 printf("\n");
94 printf(FreeImage_GetCopyrightMessage());
95 printf("\n");
96
97
98 if(argc != 2) {
99 printf("Usage : LoadFromHandle <input file name>\n");
100 return 0;
101 }
102
103 // initialize your own IO functions
104
105 FreeImageIO io;
106
107 io.read_proc = myReadProc;
108 io.write_proc = myWriteProc;
109 io.seek_proc = mySeekProc;
110 io.tell_proc = myTellProc;
111
112 FILE *file = fopen(argv[1], "rb");
113
114 if (file != NULL) {
115 // find the buffer format
116 FREE_IMAGE_FORMAT fif = FreeImage_GetFileTypeFromHandle(&io, (fi_handle)file, 0);
117
118 if(fif != FIF_UNKNOWN) {
119 // load from the file handle
120 FIBITMAP *dib = FreeImage_LoadFromHandle(fif, &io, (fi_handle)file, 0);
121
122 // save the bitmap as a PNG ...
123 const char *output_filename = "test.png";
124
125 // first, check the output format from the file name or file extension
126 FREE_IMAGE_FORMAT out_fif = FreeImage_GetFIFFromFilename(output_filename);
127
128 if(out_fif != FIF_UNKNOWN) {
129 // then save the file
130 FreeImage_Save(out_fif, dib, output_filename, 0);
131 }
132
133 // free the loaded FIBITMAP
134 FreeImage_Unload(dib);
135 }
136 fclose(file);
137 }
138
139 // call this ONLY when linking with FreeImage as a static library
140 #ifdef FREEIMAGE_LIB
141 FreeImage_DeInitialise();
142 #endif // FREEIMAGE_LIB
143
144 return 0;
145 }

  ViewVC Help
Powered by ViewVC 1.1.26