corrade-vassal – Blame information for rev 1
?pathlinks?
Rev | Author | Line No. | Line |
---|---|---|---|
1 | vero | 1 | /* |
2 | * cvs identifier: |
||
3 | * |
||
4 | * $Id: FileFormatReader.java,v 1.16 2002/07/25 14:04:08 grosbois Exp $ |
||
5 | * |
||
6 | * Class: FileFormatReader |
||
7 | * |
||
8 | * Description: Reads the file format |
||
9 | * |
||
10 | * |
||
11 | * |
||
12 | * COPYRIGHT: |
||
13 | * |
||
14 | * This software module was originally developed by Raphaël Grosbois and |
||
15 | * Diego Santa Cruz (Swiss Federal Institute of Technology-EPFL); Joel |
||
16 | * Askelöf (Ericsson Radio Systems AB); and Bertrand Berthelot, David |
||
17 | * Bouchard, Félix Henry, Gerard Mozelle and Patrice Onno (Canon Research |
||
18 | * Centre France S.A) in the course of development of the JPEG2000 |
||
19 | * standard as specified by ISO/IEC 15444 (JPEG 2000 Standard). This |
||
20 | * software module is an implementation of a part of the JPEG 2000 |
||
21 | * Standard. Swiss Federal Institute of Technology-EPFL, Ericsson Radio |
||
22 | * Systems AB and Canon Research Centre France S.A (collectively JJ2000 |
||
23 | * Partners) agree not to assert against ISO/IEC and users of the JPEG |
||
24 | * 2000 Standard (Users) any of their rights under the copyright, not |
||
25 | * including other intellectual property rights, for this software module |
||
26 | * with respect to the usage by ISO/IEC and Users of this software module |
||
27 | * or modifications thereof for use in hardware or software products |
||
28 | * claiming conformance to the JPEG 2000 Standard. Those intending to use |
||
29 | * this software module in hardware or software products are advised that |
||
30 | * their use may infringe existing patents. The original developers of |
||
31 | * this software module, JJ2000 Partners and ISO/IEC assume no liability |
||
32 | * for use of this software module or modifications thereof. No license |
||
33 | * or right to this software module is granted for non JPEG 2000 Standard |
||
34 | * conforming products. JJ2000 Partners have full right to use this |
||
35 | * software module for his/her own purpose, assign or donate this |
||
36 | * software module to any third party and to inhibit third parties from |
||
37 | * using this software module for non JPEG 2000 Standard conforming |
||
38 | * products. This copyright notice must be included in all copies or |
||
39 | * derivative works of this software module. |
||
40 | * |
||
41 | * Copyright (c) 1999/2000 JJ2000 Partners. |
||
42 | * */ |
||
43 | using System; |
||
44 | using CSJ2K.j2k.codestream; |
||
45 | using CSJ2K.j2k.fileformat; |
||
46 | using CSJ2K.j2k.util; |
||
47 | using CSJ2K.j2k.io; |
||
48 | namespace CSJ2K.j2k.fileformat.reader |
||
49 | { |
||
50 | |||
51 | /// <summary> This class reads the file format wrapper that may or may not exist around a |
||
52 | /// valid JPEG 2000 codestream. Since no information from the file format is |
||
53 | /// used in the actual decoding, this class simply goes through the file and |
||
54 | /// finds the first valid codestream. |
||
55 | /// |
||
56 | /// </summary> |
||
57 | /// <seealso cref="jj2000.j2k.fileformat.writer.FileFormatWriter"> |
||
58 | /// |
||
59 | /// </seealso> |
||
60 | public class FileFormatReader |
||
61 | { |
||
62 | /// <summary> This method creates and returns an array of positions to contiguous |
||
63 | /// codestreams in the file |
||
64 | /// |
||
65 | /// </summary> |
||
66 | /// <returns> The positions of the contiguous codestreams in the file |
||
67 | /// |
||
68 | /// </returns> |
||
69 | virtual public long[] CodeStreamPos |
||
70 | { |
||
71 | get |
||
72 | { |
||
73 | int size = codeStreamPos.Count; |
||
74 | long[] pos = new long[size]; |
||
75 | for (int i = 0; i < size; i++) |
||
76 | pos[i] = (long) ((System.Int32) (codeStreamPos[i])); |
||
77 | return pos; |
||
78 | } |
||
79 | |||
80 | } |
||
81 | /// <summary> This method returns the position of the first contiguous codestreams in |
||
82 | /// the file |
||
83 | /// |
||
84 | /// </summary> |
||
85 | /// <returns> The position of the first contiguous codestream in the file |
||
86 | /// |
||
87 | /// </returns> |
||
88 | virtual public int FirstCodeStreamPos |
||
89 | { |
||
90 | get |
||
91 | { |
||
92 | return ((System.Int32) (codeStreamPos[0])); |
||
93 | } |
||
94 | |||
95 | } |
||
96 | /// <summary> This method returns the length of the first contiguous codestreams in |
||
97 | /// the file |
||
98 | /// |
||
99 | /// </summary> |
||
100 | /// <returns> The length of the first contiguous codestream in the file |
||
101 | /// |
||
102 | /// </returns> |
||
103 | virtual public int FirstCodeStreamLength |
||
104 | { |
||
105 | get |
||
106 | { |
||
107 | return ((System.Int32) (codeStreamLength[0])); |
||
108 | } |
||
109 | |||
110 | } |
||
111 | |||
112 | /// <summary>The random access from which the file format boxes are read </summary> |
||
113 | private RandomAccessIO in_Renamed; |
||
114 | |||
115 | /// <summary>The positions of the codestreams in the fileformat</summary> |
||
116 | private System.Collections.ArrayList codeStreamPos; |
||
117 | |||
118 | /// <summary>The lengths of the codestreams in the fileformat</summary> |
||
119 | private System.Collections.ArrayList codeStreamLength; |
||
120 | |||
121 | /// <summary>Flag indicating whether or not the JP2 file format is used </summary> |
||
122 | public bool JP2FFUsed; |
||
123 | |||
124 | /// <summary> The constructor of the FileFormatReader |
||
125 | /// |
||
126 | /// </summary> |
||
127 | /// <param name="in">The RandomAccessIO from which to read the file format |
||
128 | /// |
||
129 | /// </param> |
||
130 | public FileFormatReader(RandomAccessIO in_Renamed) |
||
131 | { |
||
132 | this.in_Renamed = in_Renamed; |
||
133 | } |
||
134 | |||
135 | /// <summary> This method checks whether the given RandomAccessIO is a valid JP2 file |
||
136 | /// and if so finds the first codestream in the file. Currently, the |
||
137 | /// information in the codestream is not used |
||
138 | /// |
||
139 | /// </summary> |
||
140 | /// <param name="in">The RandomAccessIO from which to read the file format |
||
141 | /// |
||
142 | /// </param> |
||
143 | /// <exception cref="java.io.IOException">If an I/O error ocurred. |
||
144 | /// |
||
145 | /// </exception> |
||
146 | /// <exception cref="java.io.EOFException">If end of file is reached |
||
147 | /// |
||
148 | /// </exception> |
||
149 | public virtual void readFileFormat() |
||
150 | { |
||
151 | |||
152 | //int foundCodeStreamBoxes = 0; |
||
153 | int box; |
||
154 | int length; |
||
155 | long longLength = 0; |
||
156 | int pos; |
||
157 | short marker; |
||
158 | bool jp2HeaderBoxFound = false; |
||
159 | bool lastBoxFound = false; |
||
160 | |||
161 | try |
||
162 | { |
||
163 | |||
164 | // Go through the randomaccessio and find the first contiguous |
||
165 | // codestream box. Check also that the File Format is correct |
||
166 | |||
167 | // Make sure that the first 12 bytes is the JP2_SIGNATURE_BOX or |
||
168 | // if not that the first 2 bytes is the SOC marker |
||
169 | if (in_Renamed.readInt() != 0x0000000c || in_Renamed.readInt() != CSJ2K.j2k.fileformat.FileFormatBoxes.JP2_SIGNATURE_BOX || in_Renamed.readInt() != 0x0d0a870a) |
||
170 | { |
||
171 | // Not a JP2 file |
||
172 | in_Renamed.seek(0); |
||
173 | |||
174 | marker = (short) in_Renamed.readShort(); |
||
175 | if (marker != CSJ2K.j2k.codestream.Markers.SOC) |
||
176 | //Standard syntax marker found |
||
177 | throw new System.ApplicationException("File is neither valid JP2 file nor " + "valid JPEG 2000 codestream"); |
||
178 | JP2FFUsed = false; |
||
179 | in_Renamed.seek(0); |
||
180 | return ; |
||
181 | } |
||
182 | |||
183 | // The JP2 File format is being used |
||
184 | JP2FFUsed = true; |
||
185 | |||
186 | // Read File Type box |
||
187 | if (!readFileTypeBox()) |
||
188 | { |
||
189 | // Not a valid JP2 file or codestream |
||
190 | throw new System.ApplicationException("Invalid JP2 file: File Type box missing"); |
||
191 | } |
||
192 | |||
193 | // Read all remaining boxes |
||
194 | while (!lastBoxFound) |
||
195 | { |
||
196 | pos = in_Renamed.Pos; |
||
197 | length = in_Renamed.readInt(); |
||
198 | if ((pos + length) == in_Renamed.length()) |
||
199 | lastBoxFound = true; |
||
200 | |||
201 | box = in_Renamed.readInt(); |
||
202 | if (length == 0) |
||
203 | { |
||
204 | lastBoxFound = true; |
||
205 | length = in_Renamed.length() - in_Renamed.Pos; |
||
206 | } |
||
207 | else if (length == 1) |
||
208 | { |
||
209 | longLength = in_Renamed.readLong(); |
||
210 | throw new System.IO.IOException("File too long."); |
||
211 | } |
||
212 | else |
||
213 | longLength = (long) 0; |
||
214 | |||
215 | switch (box) |
||
216 | { |
||
217 | |||
218 | case CSJ2K.j2k.fileformat.FileFormatBoxes.CONTIGUOUS_CODESTREAM_BOX: |
||
219 | if (!jp2HeaderBoxFound) |
||
220 | { |
||
221 | throw new System.ApplicationException("Invalid JP2 file: JP2Header box not " + "found before Contiguous codestream " + "box "); |
||
222 | } |
||
223 | readContiguousCodeStreamBox(pos, length, longLength); |
||
224 | break; |
||
225 | |||
226 | case CSJ2K.j2k.fileformat.FileFormatBoxes.JP2_HEADER_BOX: |
||
227 | if (jp2HeaderBoxFound) |
||
228 | throw new System.ApplicationException("Invalid JP2 file: Multiple " + "JP2Header boxes found"); |
||
229 | readJP2HeaderBox(pos, length, longLength); |
||
230 | jp2HeaderBoxFound = true; |
||
231 | break; |
||
232 | |||
233 | case CSJ2K.j2k.fileformat.FileFormatBoxes.INTELLECTUAL_PROPERTY_BOX: |
||
234 | readIntPropertyBox(length); |
||
235 | break; |
||
236 | |||
237 | case CSJ2K.j2k.fileformat.FileFormatBoxes.XML_BOX: |
||
238 | readXMLBox(length); |
||
239 | break; |
||
240 | |||
241 | case CSJ2K.j2k.fileformat.FileFormatBoxes.UUID_BOX: |
||
242 | readUUIDBox(length); |
||
243 | break; |
||
244 | |||
245 | case CSJ2K.j2k.fileformat.FileFormatBoxes.UUID_INFO_BOX: |
||
246 | readUUIDInfoBox(length); |
||
247 | break; |
||
248 | |||
249 | case CSJ2K.j2k.fileformat.FileFormatBoxes.READER_REQUIREMENTS_BOX: |
||
250 | readReaderRequirementsBox(length); |
||
251 | break; |
||
252 | |||
253 | default: |
||
254 | FacilityManager.getMsgLogger().printmsg(CSJ2K.j2k.util.MsgLogger_Fields.WARNING, "Unknown box-type: 0x" + System.Convert.ToString(box, 16)); |
||
255 | break; |
||
256 | |||
257 | } |
||
258 | if (!lastBoxFound) |
||
259 | in_Renamed.seek(pos + length); |
||
260 | } |
||
261 | } |
||
262 | catch (System.IO.EndOfStreamException) |
||
263 | { |
||
264 | throw new System.ApplicationException("EOF reached before finding Contiguous " + "Codestream Box"); |
||
265 | } |
||
266 | |||
267 | if (codeStreamPos.Count == 0) |
||
268 | { |
||
269 | // Not a valid JP2 file or codestream |
||
270 | throw new System.ApplicationException("Invalid JP2 file: Contiguous codestream box " + "missing"); |
||
271 | } |
||
272 | |||
273 | return ; |
||
274 | } |
||
275 | |||
276 | /// <summary> This method reads the File Type box. |
||
277 | /// |
||
278 | /// </summary> |
||
279 | /// <returns> false if the File Type box was not found or invalid else true |
||
280 | /// |
||
281 | /// </returns> |
||
282 | /// <exception cref="java.io.IOException">If an I/O error ocurred. |
||
283 | /// </exception> |
||
284 | /// <exception cref="java.io.EOFException">If the end of file was reached |
||
285 | /// |
||
286 | /// </exception> |
||
287 | public virtual bool readFileTypeBox() |
||
288 | { |
||
289 | int length; |
||
290 | long longLength = 0; |
||
291 | int pos; |
||
292 | int nComp; |
||
293 | bool foundComp = false; |
||
294 | |||
295 | // Get current position in file |
||
296 | pos = in_Renamed.Pos; |
||
297 | |||
298 | // Read box length (LBox) |
||
299 | length = in_Renamed.readInt(); |
||
300 | if (length == 0) |
||
301 | { |
||
302 | // This can not be last box |
||
303 | throw new System.ApplicationException("Zero-length of Profile Box"); |
||
304 | } |
||
305 | |||
306 | // Check that this is a File Type box (TBox) |
||
307 | if (in_Renamed.readInt() != CSJ2K.j2k.fileformat.FileFormatBoxes.FILE_TYPE_BOX) |
||
308 | { |
||
309 | return false; |
||
310 | } |
||
311 | |||
312 | // Check for XLBox |
||
313 | if (length == 1) |
||
314 | { |
||
315 | // Box has 8 byte length; |
||
316 | longLength = in_Renamed.readLong(); |
||
317 | throw new System.IO.IOException("File too long."); |
||
318 | } |
||
319 | |||
320 | // Read Brand field |
||
321 | in_Renamed.readInt(); |
||
322 | |||
323 | // Read MinV field |
||
324 | in_Renamed.readInt(); |
||
325 | |||
326 | // Check that there is at least one FT_BR entry in in |
||
327 | // compatibility list |
||
328 | nComp = (length - 16) / 4; // Number of compatibilities. |
||
329 | for (int i = nComp; i > 0; i--) |
||
330 | { |
||
331 | if (in_Renamed.readInt() == CSJ2K.j2k.fileformat.FileFormatBoxes.FT_BR) |
||
332 | foundComp = true; |
||
333 | } |
||
334 | if (!foundComp) |
||
335 | { |
||
336 | return false; |
||
337 | } |
||
338 | |||
339 | return true; |
||
340 | } |
||
341 | |||
342 | /// <summary> This method reads the JP2Header box |
||
343 | /// |
||
344 | /// </summary> |
||
345 | /// <param name="pos">The position in the file |
||
346 | /// |
||
347 | /// </param> |
||
348 | /// <param name="length">The length of the JP2Header box |
||
349 | /// |
||
350 | /// </param> |
||
351 | /// <param name="long">length The length of the JP2Header box if greater than |
||
352 | /// 1<<32 |
||
353 | /// |
||
354 | /// </param> |
||
355 | /// <returns> false if the JP2Header box was not found or invalid else true |
||
356 | /// |
||
357 | /// </returns> |
||
358 | /// <exception cref="java.io.IOException">If an I/O error ocurred. |
||
359 | /// |
||
360 | /// </exception> |
||
361 | /// <exception cref="java.io.EOFException">If the end of file was reached |
||
362 | /// |
||
363 | /// </exception> |
||
364 | public virtual bool readJP2HeaderBox(long pos, int length, long longLength) |
||
365 | { |
||
366 | |||
367 | if (length == 0) |
||
368 | { |
||
369 | // This can not be last box |
||
370 | throw new System.ApplicationException("Zero-length of JP2Header Box"); |
||
371 | } |
||
372 | |||
373 | // Here the JP2Header data (DBox) would be read if we were to use it |
||
374 | |||
375 | return true; |
||
376 | } |
||
377 | |||
378 | /// <summary> This method skips the Contiguous codestream box and adds position |
||
379 | /// of contiguous codestream to a vector |
||
380 | /// |
||
381 | /// </summary> |
||
382 | /// <param name="pos">The position in the file |
||
383 | /// |
||
384 | /// </param> |
||
385 | /// <param name="length">The length of the JP2Header box |
||
386 | /// |
||
387 | /// </param> |
||
388 | /// <param name="long">length The length of the JP2Header box if greater than 1<<32 |
||
389 | /// |
||
390 | /// </param> |
||
391 | /// <returns> false if the Contiguous codestream box was not found or invalid |
||
392 | /// else true |
||
393 | /// |
||
394 | /// </returns> |
||
395 | /// <exception cref="java.io.IOException">If an I/O error ocurred. |
||
396 | /// |
||
397 | /// </exception> |
||
398 | /// <exception cref="java.io.EOFException">If the end of file was reached |
||
399 | /// |
||
400 | /// </exception> |
||
401 | public virtual bool readContiguousCodeStreamBox(long pos, int length, long longLength) |
||
402 | { |
||
403 | |||
404 | // Add new codestream position to position vector |
||
405 | int ccpos = in_Renamed.Pos; |
||
406 | |||
407 | if (codeStreamPos == null) |
||
408 | codeStreamPos = System.Collections.ArrayList.Synchronized(new System.Collections.ArrayList(10)); |
||
409 | codeStreamPos.Add((System.Int32) ccpos); |
||
410 | |||
411 | // Add new codestream length to length vector |
||
412 | if (codeStreamLength == null) |
||
413 | codeStreamLength = System.Collections.ArrayList.Synchronized(new System.Collections.ArrayList(10)); |
||
414 | codeStreamLength.Add((System.Int32) length); |
||
415 | |||
416 | return true; |
||
417 | } |
||
418 | |||
419 | /// <summary> This method reads the contents of the Intellectual property box |
||
420 | /// |
||
421 | /// </summary> |
||
422 | public virtual void readIntPropertyBox(int length) |
||
423 | { |
||
424 | } |
||
425 | |||
426 | /// <summary> This method reads the contents of the XML box |
||
427 | /// |
||
428 | /// </summary> |
||
429 | public virtual void readXMLBox(int length) |
||
430 | { |
||
431 | } |
||
432 | |||
433 | /// <summary> This method reads the contents of the Intellectual property box |
||
434 | /// |
||
435 | /// </summary> |
||
436 | public virtual void readUUIDBox(int length) |
||
437 | { |
||
438 | } |
||
439 | |||
440 | /// <summary> This method reads the contents of the Intellectual property box |
||
441 | /// |
||
442 | /// </summary> |
||
443 | public virtual void readUUIDInfoBox(int length) |
||
444 | { |
||
445 | } |
||
446 | |||
447 | /// <summary> This method reads the contents of the Reader requirements box |
||
448 | /// |
||
449 | /// </summary> |
||
450 | public virtual void readReaderRequirementsBox(int length) |
||
451 | { |
||
452 | } |
||
453 | } |
||
454 | } |