-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathj2p.cpp
More file actions
277 lines (237 loc) · 6.76 KB
/
Copy pathj2p.cpp
File metadata and controls
277 lines (237 loc) · 6.76 KB
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
#include "j2p.h"
#include <fstream>
using namespace BJPEG;
using namespace std;
J2PPartPtr J2PPart::createByMarkerId(uint32_t markerId)
{
switch (markerId)
{
case J2PFileType::MARKER_ID:
return shared_ptr<J2PFileType>(new J2PFileType());
case J2PHeader::MARKER_ID:
return shared_ptr<J2PHeader>(new J2PHeader());
case J2PImageHeader::MARKER_ID:
return shared_ptr<J2PImageHeader>(new J2PImageHeader());
case J2PBitsPerComponent::MARKER_ID:
return shared_ptr<J2PBitsPerComponent>(new J2PBitsPerComponent());
case J2PColourSpecification::MARKER_ID:
return shared_ptr<J2PColourSpecification>(new J2PColourSpecification());
case J2PFile::MARKER_ID:
return shared_ptr<J2PFile>(new J2PFile());
case J2PResolution::MARKER_ID:
return shared_ptr<J2PResolution>(new J2PResolution());
}
return nullptr;
}
ErrorCode J2PFile::load(const uint8_t* buffer, int offset)
{
if (!JpegAccess::VerifyReadUint32(buffer, offset, MARKER0) ||
!JpegAccess::VerifyReadUint32(buffer, offset + 4, MARKER1) ||
!JpegAccess::VerifyReadUint32(buffer, offset + 8, MARKER2))
{
return J2P_FILE_MAGIC_STRING_DOESNT_MATCH;
}
ErrorCode result;
result = fileType.load(buffer, offset + 12);
if (result != SUCCESS)
{
return result;
}
offset += 12 + fileType.size();
result = header.load(buffer, offset);
if (result != SUCCESS)
{
return result;
}
offset += header.size();
codestream.load(buffer, offset);
offset += codestream.size();
return SUCCESS;
}
void J2PFile::save(std::ostream& stream) const
{
}
ErrorCode J2PFileType::load(const uint8_t* buffer, int offset)
{
readLength = JpegAccess::ReadUint32(buffer, offset);
if (!JpegAccess::VerifyReadUint32(buffer, offset + 4, MARKER_ID))
{
return J2P_FILE_TYPE_DESCRIPTOR_DOESNT_MATCH;
}
BR = string(buffer + offset + 8, buffer + offset + 12);
if (BR != "jp2 ")
{
return J2P_UNKNOWN_FILE_TYPE;
}
MinV = JpegAccess::ReadUint32(buffer, offset + 12);
CL = string(buffer + offset + 16, buffer + offset + 20);
return SUCCESS;
}
void J2PFileType::save(std::ostream& stream) const
{
}
ErrorCode J2PHeader::load(const uint8_t* buffer, int offset)
{
readLength = JpegAccess::ReadUint32(buffer, offset);
if (!JpegAccess::VerifyReadUint32(buffer, offset + 4, MARKER_ID))
{
return J2P_HEADER_DESCRIPTOR_DOESNT_MATCH;
}
int endOffset = offset + readLength;
offset += 8;
boxes.clear();
while(true)
{
uint32_t markerId = JpegAccess::ReadUint32(buffer, offset + 4);
J2PPartPtr part = J2PPart::createByMarkerId(markerId);
if (part == nullptr)
{
return J2P_UNKOWN_BOX;
}
else
{
part.get()->load(buffer, offset);
boxes.push_back(part);
offset += part.get()->size();
if (offset == endOffset)
{
return SUCCESS;
}
}
}
}
void J2PHeader::save(std::ostream& stream) const
{
}
ErrorCode J2PImageHeader::load(const uint8_t* buffer, int offset)
{
readLength = 22;
if (!JpegAccess::VerifyReadUint32(buffer, offset, readLength))
{
return J2P_IMAGE_HEADER_INVALID_SIZE;
}
if (!JpegAccess::VerifyReadUint32(buffer, offset + 4, MARKER_ID))
{
return J2P_IMAGE_HEADER_DESCRIPTOR_DOESNT_MATCH;
}
Height = JpegAccess::ReadUint32(buffer, offset + 8);
Width = JpegAccess::ReadUint32(buffer, offset + 12);
NumberOfComponents = JpegAccess::ReadUint16(buffer, offset + 16);
BitsPerComponent = JpegAccess::ReadUint8(buffer, offset + 18);
CompressionType = JpegAccess::ReadUint8(buffer, offset + 19);
ColourspaceUnkown = JpegAccess::ReadUint8(buffer, offset + 20);
IntellectualProperty = JpegAccess::ReadUint8(buffer, offset + 21);
return SUCCESS;
}
void J2PImageHeader::save(std::ostream& stream) const
{
}
ErrorCode J2PBitsPerComponent::load(const uint8_t* buffer, int offset)
{
return SUCCESS;
}
void J2PBitsPerComponent::save(std::ostream& stream) const
{
}
ErrorCode J2PColourSpecification::load(const uint8_t* buffer, int offset)
{
readLength = JpegAccess::ReadUint32(buffer, offset);
if (!JpegAccess::VerifyReadUint32(buffer, offset + 4, MARKER_ID))
{
return J2P_COLOUR_SPECIFICATION_DOESNT_MATCH;
}
SpecificationMethod = JpegAccess::ReadUint8(buffer, offset + 8);
Precedence = JpegAccess::ReadUint8(buffer, offset + 9);
ColourspaceApproximation = JpegAccess::ReadUint8(buffer, offset + 10);
offset += 11;
if (SpecificationMethod == 1)
{
EnumeratedColourspace = JpegAccess::ReadUint32(buffer, offset);
offset += 4;
}
if (SpecificationMethod == 2)
{
// TODO - load PROFILE
}
return SUCCESS;
}
void J2PColourSpecification::save(std::ostream& stream) const
{
}
ErrorCode J2PResolution::load(const uint8_t* buffer, int offset)
{
readLength = JpegAccess::ReadUint32(buffer, offset);
if (!JpegAccess::VerifyReadUint32(buffer, offset + 4, MARKER_ID))
{
return J2P_RESOLUTION_DESCRIPTOR_DOESNT_MATCH;
}
offset += 8;
ErrorCode result = captureResolution.load(buffer, offset);
if (result != SUCCESS)
{
return result;
}
offset += captureResolution.size();
result = defaultDisplayResolution.load(buffer, offset);
return result;
}
void J2PResolution::save(std::ostream& stream) const
{
}
ErrorCode J2PCaptureResolution::load(const uint8_t* buffer, int offset)
{
readLength = JpegAccess::ReadUint32(buffer, offset);
if (!JpegAccess::VerifyReadUint32(buffer, offset + 4, MARKER_ID))
{
return J2P_CAPTURE_RESOLUTION_DESCRIPTOR_DOESNT_MATCH;
}
VRcN = JpegAccess::ReadUint16(buffer, offset + 8);
VRcD = JpegAccess::ReadUint16(buffer, offset + 10);
HRcN = JpegAccess::ReadUint16(buffer, offset + 12);
HRcD = JpegAccess::ReadUint16(buffer, offset + 14);
VRcE = JpegAccess::ReadUint8(buffer, offset + 16);
HRcE = JpegAccess::ReadUint8(buffer, offset + 17);
return SUCCESS;
}
void J2PCaptureResolution::save(std::ostream& stream) const
{
}
ErrorCode J2PDefaultDisplayResolution::load(const uint8_t* buffer, int offset)
{
readLength = JpegAccess::ReadUint32(buffer, offset);
if (readLength == 0)
{
return SUCCESS;
}
if (!JpegAccess::VerifyReadUint32(buffer, offset + 4, MARKER_ID))
{
return J2P_DEFAULT_DISPLAY_RESOLUTION_DESCRIPTOR_DOESNT_MATCH;
}
VRcN = JpegAccess::ReadUint16(buffer, offset + 8);
VRcD = JpegAccess::ReadUint16(buffer, offset + 10);
HRcN = JpegAccess::ReadUint16(buffer, offset + 12);
HRcD = JpegAccess::ReadUint16(buffer, offset + 14);
VRcE = JpegAccess::ReadUint8(buffer, offset + 16);
HRcE = JpegAccess::ReadUint8(buffer, offset + 17);
return SUCCESS;
}
void J2PDefaultDisplayResolution::save(std::ostream& stream) const
{
}
ErrorCode J2PContiguousCodestream::load(const uint8_t* buffer, int offset)
{
readLength = JpegAccess::ReadUint32(buffer, offset);
if (!JpegAccess::VerifyReadUint32(buffer, offset + 4, MARKER_ID))
{
return J2P_CONTIGUOUS_CODESTREAM_DOESNT_MATCH;
}
ErrorCode result = file.load(buffer, offset + 8);
return result;
}
void J2PContiguousCodestream::save(std::ostream& stream) const
{
}
uint32_t J2PContiguousCodestream::size() const
{
return 8 + file.size();
}