]> Gitweb @ Texas Instruments - Open Source Git Repositories - git.TI.com/gitweb - tidl/tidl-api.git/blob - tidl_api/src/util.cpp
Merge tag 'v01.00.00.02' into develop
[tidl/tidl-api.git] / tidl_api / src / util.cpp
1 /******************************************************************************
2  * Copyright (c) 2018 Texas Instruments Incorporated - http://www.ti.com/
3  *  All rights reserved.
4  *
5  *  Redistribution and use in source and binary forms, with or without
6  *  modification, are permitted provided that the following conditions are met:
7  *      * Redistributions of source code must retain the above copyright
8  *        notice, this list of conditions and the following disclaimer.
9  *      * Redistributions in binary form must reproduce the above copyright
10  *        notice, this list of conditions and the following disclaimer in the
11  *        documentation and/or other materials provided with the distribution.
12  *      * Neither the name of Texas Instruments Incorporated nor the
13  *        names of its contributors may be used to endorse or promote products
14  *        derived from this software without specific prior written permission.
15  *
16  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17  *  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  *  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  *  ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
20  *  LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21  *  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22  *  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23  *  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24  *  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25  *  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
26  *  THE POSSIBILITY OF SUCH DAMAGE.
27  *****************************************************************************/
29 #include "util.h"
30 #include <iostream>
31 #include <fstream>
32 #include <assert.h>
34 using namespace tidl;
36 std::size_t tidl::GetBinaryFileSize(const std::string &F)
37 {
38     std::ifstream is;
39     is.open (F, std::ios::binary );
41     if (!is.good())
42     {
43         std::cout << "ERROR: File read failed for " << F << std::endl;
44         return 0;
45     }
47     is.seekg (0, std::ios::end);
48     size_t length = is.tellg();
49     is.close();
51     return length;
52 }
55 bool tidl::ReadBinary(const std::string &F, char* buffer, int size)
56 {
57     std::ifstream is;
58     is.open (F, std::ios::binary );
60     if (!is.good())
61     {
62         std::cout << "ERROR: File read failed for " << F << std::endl;
63         return false;
64     }
66     is.seekg (0, std::ios::end);
67     int length = is.tellg();
69     if (length != size)
70     {
71         std::cout << length << " != " << size << std::endl;
72         is.close();
73         return false;
74     }
76     is.seekg (0, std::ios::beg);
77     is.read (buffer, length);
78     is.close();
80     return true;
81 }
83 bool tidl::CompareFiles(const std::string &F1, const std::string &F2)
84 {
85     std::size_t s1 = GetBinaryFileSize(F1);
86     std::size_t s2 = GetBinaryFileSize(F2);
88     if (s1 != s2)
89         return false;
91     char *b1 = new char[s1];
92     char *b2 = new char[s2];
94     ReadBinary(F1, b1, s1);
95     ReadBinary(F2, b2, s2);
97     int errors = 0;
98     for (size_t i=0; i < s1; i++)
99         if (b1[i] != b2[i])
100         {
101             std::cout << "Error at " << i << " " <<
102                          (int)b1[i] << " != " << (int)b2[i];
103             std::cout << std::endl;
104             errors++;
106             if (errors > 10)
107                 break;
108         }
110     delete[] b1;
111     delete[] b2;
113     if (errors == 0) return true;
115     return false;
118 bool tidl::CompareFrames(const std::string &F1, const std::string &F2,
119                          int numFrames, int width, int height)
121     bool status = true;
123     std::size_t s1 = GetBinaryFileSize(F1);
124     std::size_t s2 = GetBinaryFileSize(F2);
126     char *b1 = new char[s1];
127     char *b2 = new char[s2];
129     ReadBinary(F1, b1, s1);
130     ReadBinary(F2, b2, s2);
132     for (int f=0; f < numFrames; f++)
133     {
134         int errors = 0;
135         int frame_offset = f*width*height;
136         std::cout << "Comparing frame: " << f << std::endl;
137         for (int h=0; h < height; h++)
138         {
139             for (int w=0; w < width; w++)
140             {
141                 size_t index = frame_offset+(h*width)+w;
142                 assert (index < s1 && index < s2);
143                 if (b1[index] != b2[index])
144                 {
145                     status = false;
146                     std::cout << "Error at " << index << " " <<
147                                  (int)b1[index] << " != " << (int)b2[index];
148                     std::cout << std::endl;
149                     errors++;
151                 }
152                 if (errors > 10) break;
153             }
154             if (errors > 10) break;
155         }
156     }
158     delete[] b1;
159     delete[] b2;
161     return status;