Debian Codegraph @

third_party/etc2comp/EtcTool/EtcMemTest.cpp

raw
1 /*
2 * Copyright 2015 The Etc2Comp Authors.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include "EtcMemTest.h"
18 #include <iostream>
19 #include <fstream>
20 #include <vector>
21 #include <string>
22 #include <ctime>
23 using namespace std;
24
25 #if defined(_WIN32)
26 #include <Windows.h>
27 #include <psapi.h>
28 // Use to convert bytes to KB
29 #define DIV 1024
30
31 // Specify the width of the field in which to print the numbers.
32 // The asterisk in the format specifier "%*I64d" takes an integer
33 // argument and uses it to pad and right justify the number.
34 #define WIDTH 7
35
36 size_t GetMemoryUsageAmount()
37 {
38 //MEMORYSTATUS status; // if 64 bit version isnt working, use 32bit version
39 //GlobalMemoryStatus(&status);
40 /*MEMORYSTATUSEX status;
41 status.dwLength = sizeof(status);
42 GlobalMemoryStatusEx(&status);
43 return (size_t)(status.ullAvailPhys / DIV);*/
44
45 PROCESS_MEMORY_COUNTERS_EX pmc;
46 DWORD ret = GetProcessMemoryInfo(GetCurrentProcess(), (PROCESS_MEMORY_COUNTERS*)&pmc, sizeof(pmc));
47 if (ret == 0)
48 {
49 printf("GetProcessMemoryInfo failed with code %d\n", GetLastError());
50 }
51
52 return pmc.PrivateUsage;
53 }
54
55 void PrintWindowsMemUsage()
56 {
57 MEMORYSTATUSEX statex;
58
59 statex.dwLength = sizeof(statex);
60
61 GlobalMemoryStatusEx(&statex);
62
63 printf("There is %*ld percent of memory in use.\n",
64 WIDTH, statex.dwMemoryLoad);
65 printf("There are %*I64d total KB of physical memory.\n",
66 WIDTH, statex.ullTotalPhys / DIV);
67 printf("There are %*I64d free KB of physical memory.\n",
68 WIDTH, statex.ullAvailPhys / DIV);
69 printf("There are %*I64d total KB of paging file.\n",
70 WIDTH, statex.ullTotalPageFile / DIV);
71 printf("There are %*I64d free KB of paging file.\n",
72 WIDTH, statex.ullAvailPageFile / DIV);
73 printf("There are %*I64d total KB of virtual memory.\n",
74 WIDTH, statex.ullTotalVirtual / DIV);
75 printf("There are %*I64d free KB of virtual memory.\n",
76 WIDTH, statex.ullAvailVirtual / DIV);
77
78 // Show the amount of extended memory available.
79
80 printf("There are %*I64d free KB of extended memory.\n",
81 WIDTH, statex.ullAvailExtendedVirtual / DIV);
82 }
83
84
85
86
87 int RunMemTest(bool verboseOutput, size_t numTestIterations)
88 {
89 WIN32_FIND_DATA ffd;
90 srand(static_cast<unsigned int>(time(NULL)));
91 HANDLE hFind = INVALID_HANDLE_VALUE;
92
93
94 vector<string> allImages;
95
96 string imagesDir = "D:\\source\\etc2Sourcetree\\TestImages\\";
97 string outputDir = "C:\\Users\\BSI\\Desktop\\etc2comp\\";
98
99 hFind = FindFirstFile((imagesDir+"*").c_str(), &ffd);
100
101 if (INVALID_HANDLE_VALUE == hFind)
102 {
103 printf ("image dir doesnt exist: %s\n", imagesDir.c_str());
104 return -1;
105 }
106
107 do
108 {
109 if (ffd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
110 {
111 continue;
112 }
113 else
114 {
115 string filename = ffd.cFileName;
116 size_t found = filename.find_last_of(".");
117 string ext = filename.substr(found + 1);
118 if(ext != "png")
119 continue;
120
121 allImages.push_back(filename);
122 }
123 } while (FindNextFile(hFind, &ffd) != 0);
124 FindClose(hFind);
125
126 size_t oldMemSize = 0;
127 size_t curMemSize = 0;
128 size_t fileIndex = 0;
129 int encodingType = 2;//0 = c , 1= both c and c++ encoding, 2 = c++ encoding
130 bool encodeRandomImage = true;
131 for (size_t i = 0; i < numTestIterations; i++)
132 {
133 encodingType = rand()%3;
134
135 printf("--------------iteration %zu-----------\n",i+1);
136 int randomNumber = 1 + rand() % 11;
137 //Image::Format format = (Image::Format)randomNumber;
138 Image::Format format = Image::Format::RGBA8;
139
140 randomNumber = rand() % 4;
141 ErrorMetric e_ErrMetric = (ErrorMetric)randomNumber;
142
143 randomNumber = rand() % 100;
144 float curEffort = (float)randomNumber;
145
146 int jobs = 1 + rand() % 8;
147 long memDiff = 0;
148
149 if (verboseOutput)
150 {
151 printf(" effort = %.f\n", curEffort);
152 printf(" encoding = %s\n", Image::EncodingFormatToString(format));
153 printf(" error metric: %s\n", ErrorMetricToString(e_ErrMetric));
154 printf("jobs: %d\n", jobs);
155 }
156
157 oldMemSize = curMemSize;
158 curMemSize = GetMemoryUsageAmount();
159 memDiff = (long)curMemSize - (long)oldMemSize;
160 printf("mem diff: %ukb\n", memDiff);
161
162 string curFile = imagesDir + allImages[fileIndex];
163 string outFile = outputDir + allImages[fileIndex] +".ktx";
164 printf("file: %s\n", curFile.c_str());
165
166 if (!encodeRandomImage)
167 {
168 if (fileIndex >= allImages.size() - 1)
169 fileIndex = 0;
170 else
171 fileIndex++;
172 }
173 else
174 {
175 fileIndex = rand()%allImages.size();
176 }
177 SourceImage sourceimage(curFile.c_str(), -1, -1);
178
179 unsigned int uiSourceWidth = sourceimage.GetWidth();
180 unsigned int uiSourceHeight = sourceimage.GetHeight();
181 //char *imgonnaleak = new char[1000];
182
183 unsigned char *paucEncodingBits;
184 unsigned int uiEncodingBitsBytes;
185 unsigned int uiExtendedWidth;
186 unsigned int uiExtendedHeight;
187 int iEncodingTime_ms;
188 //////////////C INTERFACE FIRST//////////////////////
189 if (encodingType < 2)
190 {
191 if (verboseOutput)
192 {
193 printf("start C interface Encoding:\n");
194 }
195
196 Etc::Encode((float *)sourceimage.GetPixels(),
197 uiSourceWidth, uiSourceHeight,
198 format,
199 e_ErrMetric,
200 curEffort,
201 jobs,
202 MAX_JOBS,
203 &paucEncodingBits, &uiEncodingBitsBytes,
204 &uiExtendedWidth, &uiExtendedHeight,
205 &iEncodingTime_ms, verboseOutput);
206 if (verboseOutput)
207 {
208 printf(" encode time = %dms\n", iEncodingTime_ms);
209 printf("EncodedImage: %s\n", outFile.c_str());
210 }
211 Etc::File C_interfaceEtcfile(outFile.c_str(), Etc::File::Format::INFER_FROM_FILE_EXTENSION,
212 format,
213 paucEncodingBits, uiEncodingBitsBytes,
214 uiSourceWidth, uiSourceHeight,
215 uiExtendedWidth, uiExtendedHeight);
216 C_interfaceEtcfile.Write();
217
218 oldMemSize = curMemSize;
219 curMemSize = GetMemoryUsageAmount();
220 memDiff = (long)curMemSize - (long)oldMemSize;
221 printf("mem diff again: %ukb\n", memDiff);
222 }
223 //////////////C++ INTERFACE ///////////////////////////
224 if (encodingType > 0)
225 {
226 if (verboseOutput)
227 {
228 printf("start C++ Encoding:\n");
229 }
230 Etc::Image image((float *)sourceimage.GetPixels(),
231 uiSourceWidth, uiSourceHeight,
232 e_ErrMetric);
233 image.m_bVerboseOutput = verboseOutput;
234
235 Etc::Image::EncodingStatus encStatus = Etc::Image::EncodingStatus::SUCCESS;
236
237 encStatus = image.Encode(format, e_ErrMetric, curEffort, jobs, MAX_JOBS);
238 if (verboseOutput)
239 {
240 printf(" encode time = %dms\n", image.GetEncodingTimeMs());
241 printf("EncodedImage: %s\n", outFile.c_str());
242 printf("status bitfield: %u\n", encStatus);
243 }
244 Etc::File etcfile(outFile.c_str(), Etc::File::Format::INFER_FROM_FILE_EXTENSION,
245 format,
246 image.GetEncodingBits(), image.GetEncodingBitsBytes(),
247 image.GetSourceWidth(), image.GetSourceHeight(),
248 image.GetExtendedWidth(), image.GetExtendedHeight());
249
250 etcfile.Write();
251 }
252
253 }
254
255 return 0;
256 }
257
258 #endif