-
Notifications
You must be signed in to change notification settings - Fork 0
/
data_input.cpp
174 lines (138 loc) · 2.62 KB
/
data_input.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include "data_input.h"
dataInput::dataInput()
:mLabelLen(0),
mImageLen(0),
mNumLabel(0),
mNumImage(0),
mImageStartPos(16),
mLableStartPos(8)
{
//todo something
}
dataInput::~dataInput()
{
mLabelFile.close();
mImageFile.close();
}
bool dataInput::openLabelFile(const char* url)
{
mLabelFile.open(url, ios::binary | ios::in);
if (mLabelFile.is_open())
{
int num = 0;
char temp[4];
mLabelFile.read(temp, 4);
if (mLabelFile.gcount() == 4)
{
num = GRB4(temp);
if (DATA_INPUT_LABEL_FLAG == num)
{
mLabelFile.read(temp, 4);
if (mLabelFile.gcount() == 4)
{
mNumLabel = GRB4(temp);
mLabelLen = 1;
return true;
}
else
{
cout << "read label count failed" << endl;
}
}
else
{
cout << "this file isn't label file,the flag is:" << num << endl;
}
}
}
return false;
}
bool dataInput::openImageFile(const char* url)
{
mImageFile.open(url, ios::binary | ios::in);
if (mImageFile.is_open())
{
int num = 0;
char temp[4];
mImageFile.read(temp, 4);
if (mImageFile.gcount() == 4)
{
int flag = DATA_INPUT_IMAGE_FLAG;
num = GRB4(temp);
if (DATA_INPUT_IMAGE_FLAG == num)
{
mImageFile.read(temp, 4);
if (mImageFile.gcount() == 4)
{
mNumImage = GRB4(temp);
int width = 0;
int height = 0;
mImageFile.read((char*)&width, 4);
if (mImageFile.gcount() == 4)
{
width = GRB4(&width);
mImageFile.read((char*)&height, 4);
if (mImageFile.gcount() == 4)
{
height = GRB4(&height);
mImageWidth = width;
mImageHeight = height;
mImageLen = width * height;
}
}
return true;
}
else
{
cout << "read image count failed" << endl;
}
}
else
{
cout << "this file isn't image file,the flag is:" << num << endl;
}
}
}
return false;
}
bool dataInput::readIndex(int* label)
{
if (mLabelFile.is_open() && !mLabelFile.eof())
{
mLabelFile.read((char*)label, mLabelLen);
return mLabelFile.gcount() == mLabelLen;
}
return false;
}
bool dataInput::readImage(char imageBuf[])
{
if (mImageFile.is_open() && !mImageFile.eof())
{
mImageFile.read(imageBuf, mImageLen);
return mImageFile.gcount() == mImageLen;
}
return false;
}
bool dataInput::read(int* label, char imageBuf[])
{
if (readIndex(label))
{
return readImage(imageBuf);
}
return false;
}
void dataInput::reset()
{
if (mImageFile.is_open())
{
mImageFile.clear();
mImageFile.seekg(mImageStartPos);
}
if (mLabelFile.is_open())
{
mLabelFile.clear();
mLabelFile.seekg(mLableStartPos);
}
}