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

Contents of /public/pc/shared_libraries/freeimage/v3.12.0/Examples/Generic/BatchLoad.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: 6507 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 // Batch loader
3 //
4 // Design and implementation by
5 // - Floris van den Berg
6 // - Hervé Drolon
7 //
8 // This file is part of FreeImage 3
9 //
10 // COVERED CODE IS PROVIDED UNDER THIS LICENSE ON AN "AS IS" BASIS, WITHOUT WARRANTY
11 // OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, WITHOUT LIMITATION, WARRANTIES
12 // THAT THE COVERED CODE IS FREE OF DEFECTS, MERCHANTABLE, FIT FOR A PARTICULAR PURPOSE
13 // OR NON-INFRINGING. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE COVERED
14 // CODE IS WITH YOU. SHOULD ANY COVERED CODE PROVE DEFECTIVE IN ANY RESPECT, YOU (NOT
15 // THE INITIAL DEVELOPER OR ANY OTHER CONTRIBUTOR) ASSUME THE COST OF ANY NECESSARY
16 // SERVICING, REPAIR OR CORRECTION. THIS DISCLAIMER OF WARRANTY CONSTITUTES AN ESSENTIAL
17 // PART OF THIS LICENSE. NO USE OF ANY COVERED CODE IS AUTHORIZED HEREUNDER EXCEPT UNDER
18 // THIS DISCLAIMER.
19 //
20 // Use at own risk!
21 // ==========================================================
22
23 //
24 // This example shows how to easily batch load a directory
25 // full of images. Because not all formats can be identified
26 // by their header (some images don't have a header or one
27 // at the end of the file) we make use of the
28 // FreeImage_GetFIFFromFilename function. This function
29 // receives a file name, for example 'myfile.bmp', and returns
30 // a FREE_IMAGE_TYPE enum which identifies that bitmap.
31 //
32 // Functions used in this sample :
33 // FreeImage_GetFileType, FreeImage_GetFIFFromFilename, FreeImage_FIFSupportsReading,
34 // FreeImage_Load, FreeImage_GetBPP, FreeImage_FIFSupportsWriting, FreeImage_GetFormatFromFIF
35 // FreeImage_FIFSupportsExportBPP, FreeImage_Save, FreeImage_Unload,
36 // FreeImage_SetOutputMessage, FreeImage_GetVersion, FreeImage_GetCopyrightMessage
37 //
38 // ==========================================================
39
40 #include <assert.h>
41 #include <stdio.h>
42 #include <io.h>
43 #include <string.h>
44 #include <stdlib.h>
45
46 #include "FreeImage.h"
47
48 // ----------------------------------------------------------
49
50 /** Generic image loader
51 @param lpszPathName Pointer to the full file name
52 @param flag Optional load flag constant
53 @return Returns the loaded dib if successful, returns NULL otherwise
54 */
55 FIBITMAP* GenericLoader(const char* lpszPathName, int flag) {
56 FREE_IMAGE_FORMAT fif = FIF_UNKNOWN;
57
58 // check the file signature and deduce its format
59 // (the second argument is currently not used by FreeImage)
60 fif = FreeImage_GetFileType(lpszPathName, 0);
61 if(fif == FIF_UNKNOWN) {
62 // no signature ?
63 // try to guess the file format from the file extension
64 fif = FreeImage_GetFIFFromFilename(lpszPathName);
65 }
66 // check that the plugin has reading capabilities ...
67 if((fif != FIF_UNKNOWN) && FreeImage_FIFSupportsReading(fif)) {
68 // ok, let's load the file
69 FIBITMAP *dib = FreeImage_Load(fif, lpszPathName, flag);
70 // unless a bad file format, we are done !
71 return dib;
72 }
73 return NULL;
74 }
75
76 /** Generic image writer
77 @param dib Pointer to the dib to be saved
78 @param lpszPathName Pointer to the full file name
79 @param flag Optional save flag constant
80 @return Returns true if successful, returns false otherwise
81 */
82 bool GenericWriter(FIBITMAP* dib, const char* lpszPathName, int flag) {
83 FREE_IMAGE_FORMAT fif = FIF_UNKNOWN;
84 BOOL bSuccess = FALSE;
85
86 if(dib) {
87 // try to guess the file format from the file extension
88 fif = FreeImage_GetFIFFromFilename(lpszPathName);
89 if(fif != FIF_UNKNOWN ) {
90 // check that the plugin has sufficient writing and export capabilities ...
91 WORD bpp = FreeImage_GetBPP(dib);
92 if(FreeImage_FIFSupportsWriting(fif) && FreeImage_FIFSupportsExportBPP(fif, bpp)) {
93 // ok, we can save the file
94 bSuccess = FreeImage_Save(fif, dib, lpszPathName, flag);
95 // unless an abnormal bug, we are done !
96 }
97 }
98 }
99 return (bSuccess == TRUE) ? true : false;
100 }
101
102 // ----------------------------------------------------------
103
104 /**
105 FreeImage error handler
106 @param fif Format / Plugin responsible for the error
107 @param message Error message
108 */
109 void FreeImageErrorHandler(FREE_IMAGE_FORMAT fif, const char *message) {
110 printf("\n*** ");
111 if(fif != FIF_UNKNOWN) {
112 printf("%s Format\n", FreeImage_GetFormatFromFIF(fif));
113 }
114 printf(message);
115 printf(" ***\n");
116 }
117
118 // ----------------------------------------------------------
119
120 #ifndef MAX_PATH
121 #define MAX_PATH 260
122 #endif
123
124 int
125 main(int argc, char *argv[]) {
126
127 const char *input_dir = "d:\\images\\";
128 FIBITMAP *dib = NULL;
129 int id = 1;
130
131 // call this ONLY when linking with FreeImage as a static library
132 #ifdef FREEIMAGE_LIB
133 FreeImage_Initialise();
134 #endif // FREEIMAGE_LIB
135
136 // initialize your own FreeImage error handler
137
138 FreeImage_SetOutputMessage(FreeImageErrorHandler);
139
140 // print version & copyright infos
141
142 printf(FreeImage_GetVersion());
143 printf("\n");
144 printf(FreeImage_GetCopyrightMessage());
145 printf("\n");
146
147 // open the log file
148
149 FILE *log_file = fopen("log_file.txt", "w");
150
151 // batch convert all supported bitmaps
152
153 _finddata_t finddata;
154 long handle;
155 char image_path[MAX_PATH];
156
157 // scan all files
158 strcpy(image_path, input_dir);
159 strcat(image_path, "*.*");
160
161 if ((handle = _findfirst(image_path, &finddata)) != -1) {
162 do {
163 // make a path to a directory
164
165 char *directory = new char[MAX_PATH];
166 strcpy(directory, input_dir);
167 strcat(directory, finddata.name);
168
169 // make a unique filename
170
171 char *unique = new char[128];
172 itoa(id, unique, 10);
173 strcat(unique, ".png");
174
175 // open and load the file using the default load option
176 dib = GenericLoader(directory, 0);
177
178 if (dib != NULL) {
179 // save the file as PNG
180 bool bSuccess = GenericWriter(dib, unique, PNG_DEFAULT);
181
182 // free the dib
183 FreeImage_Unload(dib);
184
185 if(bSuccess) {
186 fwrite(unique, strlen(unique), 1, log_file);
187 } else {
188 strcpy(unique, "FAILED");
189 fwrite(unique, strlen(unique), 1, log_file);
190 }
191 fwrite(" >> ", 4, 1, log_file);
192 fwrite(directory, strlen(directory), 1, log_file);
193 fwrite("\n", 1, 1, log_file);
194
195 id++;
196 }
197
198 delete [] unique;
199 delete [] directory;
200
201 } while (_findnext(handle, &finddata) == 0);
202
203 _findclose(handle);
204 }
205
206 fclose(log_file);
207
208 // call this ONLY when linking with FreeImage as a static library
209 #ifdef FREEIMAGE_LIB
210 FreeImage_DeInitialise();
211 #endif // FREEIMAGE_LIB
212
213 return 0;
214 }

  ViewVC Help
Powered by ViewVC 1.1.26