-
Notifications
You must be signed in to change notification settings - Fork 9
/
ImageFile.cpp
174 lines (147 loc) · 3.9 KB
/
ImageFile.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
#include "StdAfx.h"
#include "imagefile.h"
struct t_fmt_chunk_wave
{
DWORD riff_id;
DWORD riff_size;
DWORD riff_form_type;
DWORD fmt_id;
DWORD fmt_size;
WORD formatType;
WORD channel;
DWORD samplesPerSec;
DWORD bytesPersec;
WORD blockSize;
WORD bitsPerSample;
DWORD data_id;
DWORD data_size;
};
CImageFile::CImageFile(void)
{
m_hFile = INVALID_HANDLE_VALUE;
m_hSubFile = INVALID_HANDLE_VALUE;
m_Type = FILE_DATA;
m_ImgPos = 0;
m_SubPos = 0;
}
CImageFile::~CImageFile(void)
{
Close();
}
void CImageFile::OpenSub(LPCSTR FileName)
{
m_hSubFile = CreateFile(FileName, GENERIC_READ | GENERIC_WRITE, 0, nullptr, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL,
nullptr);
}
bool CImageFile::Open(LPCSTR FileName, int Type)
{
Close();
m_Type = Type;
m_hFile = CreateFile(FileName, GENERIC_READ | GENERIC_WRITE, 0, nullptr, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL,
nullptr);
if (m_hFile == INVALID_HANDLE_VALUE)
{
return false;
}
if (Type == FILE_AUDIO)
{
SetFilePointer(m_hFile, 44, nullptr, FILE_BEGIN);
}
return true;
}
void CImageFile::Close(void)
{
if (m_hSubFile != INVALID_HANDLE_VALUE)
{
CloseHandle(m_hSubFile);
m_hSubFile = INVALID_HANDLE_VALUE;
}
if (m_hFile == INVALID_HANDLE_VALUE)
{
return;
}
if (m_Type == FILE_AUDIO)
{
DWORD FilePos;
struct t_fmt_chunk_wave header;
FilePos = SetFilePointer(m_hFile, 0, nullptr, FILE_CURRENT) - 44;
header.riff_id = 'FFIR';
header.riff_size = FilePos + 36;
header.riff_form_type = 'EVAW';
header.fmt_id = ' tmf';
header.fmt_size = 16;
header.formatType = 1;
header.channel = 2;
header.samplesPerSec = 44100;
header.bitsPerSample = 16;
header.blockSize = header.bitsPerSample * header.channel / 8;
header.bytesPersec = header.blockSize * header.samplesPerSec;
header.data_id = 'atad';
header.data_size = FilePos;
SetFilePointer(m_hFile, 0, nullptr, FILE_BEGIN);
WriteFile(m_hFile, &header, 44, &FilePos, nullptr);
}
if (m_hFile != INVALID_HANDLE_VALUE)
{
CloseHandle(m_hFile);
m_hFile = INVALID_HANDLE_VALUE;
}
}
bool CImageFile::AcceptSubCode(void)
{
if (m_hSubFile != INVALID_HANDLE_VALUE)
{
return true;
}
return false;
}
void CImageFile::Write(BYTE* Buffer)
{
if (m_hFile == INVALID_HANDLE_VALUE)
{
return;
}
DWORD wrote;
wrote = 0;
WriteFile(m_hFile, Buffer, 2352, &wrote, nullptr);
}
void CImageFile::WriteSub(BYTE* Buffer)
{
if (m_hSubFile != INVALID_HANDLE_VALUE)
{
DWORD wrote;
wrote = 0;
WriteFile(m_hSubFile, Buffer, 96, &wrote, nullptr);
}
}
void CImageFile::SaveFilePointer(void)
{
m_ImgPos = SetFilePointer(m_hFile, 0, nullptr, FILE_CURRENT);
m_SubPos = SetFilePointer(m_hSubFile, 0, nullptr, FILE_CURRENT);
}
void CImageFile::LoadFilePointer(void)
{
SetFilePointer(m_hFile, m_ImgPos, nullptr, FILE_BEGIN);
SetFilePointer(m_hSubFile, m_SubPos, nullptr, FILE_BEGIN);
}
void CImageFile::SeekFromCurrentPosition(int SeekSectors)
{
DWORD ImgPos, SubPos;
ImgPos = SetFilePointer(m_hFile, 0, nullptr, FILE_CURRENT);
SubPos = SetFilePointer(m_hSubFile, 0, nullptr, FILE_CURRENT);
ImgPos = ImgPos + SeekSectors * 2352;
SubPos = SubPos + SeekSectors * 96;
SetFilePointer(m_hFile, ImgPos, nullptr, FILE_BEGIN);
SetFilePointer(m_hSubFile, SubPos, nullptr, FILE_BEGIN);
}
BOOL CImageFile::Read(BYTE* Buffer)
{
DWORD read;
if (m_hFile != INVALID_HANDLE_VALUE && m_hSubFile != INVALID_HANDLE_VALUE)
{
ReadFile(m_hFile, Buffer, 2352, &read, nullptr);
ReadFile(m_hSubFile, Buffer + 2352, 96, &read, nullptr);
return TRUE;
}
return FALSE;
}