summaryrefslogtreecommitdiff
path: root/gpr/source/lib/dng_sdk/dng_stream.h
blob: df233ae5f6b5010a45fd54259cc20f947aec25c1 (plain)
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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
/*****************************************************************************/
// Copyright 2006-2007 Adobe Systems Incorporated
// All Rights Reserved.
//
// NOTICE:  Adobe permits you to use, modify, and distribute this file in
// accordance with the terms of the Adobe license agreement accompanying it.
/*****************************************************************************/

/* $Id: //mondo/dng_sdk_1_4/dng_sdk/source/dng_stream.h#2 $ */ 
/* $DateTime: 2012/06/01 07:28:57 $ */
/* $Change: 832715 $ */
/* $Author: tknoll $ */

/** Data stream abstraction for serializing and deserializing sequences of
 *  basic types and RAW image data.
 */

/*****************************************************************************/

#ifndef __dng_stream__
#define __dng_stream__

/*****************************************************************************/

#include "dng_classes.h"
#include "dng_types.h"
#include "dng_memory.h"
#include "dng_rational.h"
#include "dng_utils.h"

/*****************************************************************************/

// Constants for invalid offset in streams.

const uint64 kDNGStreamInvalidOffset = (uint64) (int64) -1;

/*****************************************************************************/

/// Base stream abstraction. Has support for going between stream and pointer
/// abstraction.

class dng_stream
	{
	
	public:
	
		enum
			{
			
			kSmallBufferSize =  4 * 1024,
			kBigBufferSize   = 64 * 1024,
			
			kDefaultBufferSize = kSmallBufferSize
			
			};
	
	private:
	
		bool fSwapBytes;
		
		bool fHaveLength;
		
		uint64 fLength;
		
		const uint64 fOffsetInOriginalFile;
	
		uint64 fPosition;
		
		dng_memory_data fMemBlock;
		
		uint8 *fBuffer;
		
		uint32 fBufferSize;
		
		uint64 fBufferStart;
		uint64 fBufferEnd;
		uint64 fBufferLimit;
		
		bool fBufferDirty;
		
		dng_abort_sniffer *fSniffer;
		
	protected:
	
		dng_stream (dng_abort_sniffer *sniffer = NULL,
					uint32 bufferSize = kDefaultBufferSize,
					uint64 offsetInOriginalFile = kDNGStreamInvalidOffset);
		
		virtual uint64 DoGetLength ();
	
		virtual void DoRead (void *data,
							 uint32 count,
							 uint64 offset);
							 
		virtual void DoSetLength (uint64 length);
							 
		virtual void DoWrite (const void *data,
							  uint32 count,
							  uint64 offset);
		
	public:
	
		/// Construct a stream with initial data.
		/// \param data Pointer to initial contents of stream.
		/// \param count Number of bytes data is valid for.
		/// \param offsetInOriginalFile If data came from a file originally,
		/// offset can be saved here for later use.

		dng_stream (const void *data,
					uint32 count,
					uint64 offsetInOriginalFile = kDNGStreamInvalidOffset);
		
		virtual ~dng_stream ();
		
		/// Getter for whether stream is swapping byte order on input/output.
		/// \retval If true, data will be swapped on input/output.

		bool SwapBytes () const
			{
			return fSwapBytes;
			}
		
		/// Setter for whether stream is swapping byte order on input/output.
		/// \param swapBytes If true, stream will swap byte order on input or
		/// output for future reads/writes.

		void SetSwapBytes (bool swapBytes)
			{
			fSwapBytes = swapBytes;
			}

		/// Getter for whether data in stream is big endian.
		/// \retval If true, data in stream is big endian.

		bool BigEndian () const;
		
		/// Setter for whether data in stream is big endian.
		/// \param bigEndian If true, data in stream is big endian.

		void SetBigEndian (bool bigEndian = true);
		
		/// Getter for whether data in stream is big endian.
		/// \retval If true, data in stream is big endian.

		bool LittleEndian () const
			{
			return !BigEndian ();
			}
		
		/// Setter for whether data in stream is big endian.
		/// \param littleEndian If true, data in stream is big endian.

		void SetLittleEndian (bool littleEndian = true)
			{
			SetBigEndian (!littleEndian);
			}
			
		/// Returns the size of the buffer used by the stream.
		
		uint32 BufferSize () const
			{
			return fBufferSize;
			}

		/// Getter for length of data in stream.
		/// \retval Length of readable data in stream.
			
		uint64 Length ()
			{
			
			if (!fHaveLength)
				{
				
				fLength = DoGetLength ();
				
				fHaveLength = true;
				
				}
				
			return fLength;
			
			}

		/// Getter for current offset in stream.
		/// \retval current offset from start of stream.

		uint64 Position () const
			{
			return fPosition;
			}
			
		/// Getter for current position in original file, taking into account
		/// OffsetInOriginalFile stream data was taken from.
		/// \retval kInvalidOffset if no offset in original file is set, sum 
		/// of offset in original file and current position otherwise.

		uint64 PositionInOriginalFile () const;
		
		/// Getter for offset in original file.
		/// \retval kInvalidOffset if no offset in original file is set,
		/// offset in original file otherwise.

		uint64 OffsetInOriginalFile () const;
		
		/// Return pointer to stream contents if the stream is entirely 
		/// available as a single memory block, NULL otherwise.

		const void * Data () const;
		
		/// Return the entire stream as a single memory block.
		/// This works for all streams, but requires copying the data to a new buffer.
		/// \param allocator Allocator used to allocate memory.

		dng_memory_block * AsMemoryBlock (dng_memory_allocator &allocator);

		/// Seek to a new position in stream for reading.

		void SetReadPosition (uint64 offset);
		
		/// Skip forward in stream.
		/// \param delta Number of bytes to skip forward.

		void Skip (uint64 delta)
			{
			SetReadPosition (Position () + delta);
			}

		/// Get data from stream. Exception is thrown and no data is read if 
		/// insufficient data available in stream.
		/// \param data Buffer to put data into. Must be valid for count bytes.
		/// \param count Bytes of data to read.
		/// \exception dng_exception with fErrorCode equal to dng_error_end_of_file 
		/// if not enough data in stream.
		
		void Get (void *data, uint32 count);

		/// Seek to a new position in stream for writing.
		
		void SetWritePosition (uint64 offset);
		
		/// Force any stored data in stream to be written to underlying storage.

		void Flush ();

		/// Set length of available data.
		/// \param length Number of bytes of avialble data in stream.

		void SetLength (uint64 length);

		/// Write data to stream.
		/// \param data Buffer of data to write to stream.
		/// \param count Bytes of in data.

		void Put (const void *data, uint32 count);

		/// Get an unsigned 8-bit integer from stream and advance read position.
		/// \retval One unsigned 8-bit integer.
		/// \exception dng_exception with fErrorCode equal to dng_error_end_of_file 
		/// if not enough data in stream.
		
		uint8 Get_uint8 ()
			{
			
			// Fast check to see if in buffer
			
			if (fPosition >= fBufferStart && fPosition < fBufferEnd)
				{
				
				return fBuffer [fPosition++ - fBufferStart];
				
				}
				
			// Not in buffer, let main routine do the work.
			
			uint8 x;
			
			Get (&x, 1);
			
			return x;
				
			}
		
		/// Put an unsigned 8-bit integer to stream and advance write position.
		/// \param x One unsigned 8-bit integer.
		
		void Put_uint8 (uint8 x)
			{
			
			if (fBufferDirty               &&
			    fPosition  >= fBufferStart &&
				fPosition  <= fBufferEnd   &&
				fPosition  <  fBufferLimit)
				{
				
				fBuffer [fPosition - fBufferStart] = x;
				
				fPosition++;
				
				if (fBufferEnd < fPosition)
					fBufferEnd = fPosition;
					
				fLength = Max_uint64 (Length (), fPosition);
				
				}
				
			else
				{
				
				Put (&x, 1);
				
				}
	
			}
			
		/// Get an unsigned 16-bit integer from stream and advance read position. 
		/// Byte swap if byte swapping is turned on.
		/// \retval One unsigned 16-bit integer.
		/// \exception dng_exception with fErrorCode equal to dng_error_end_of_file
		/// if not enough data in stream.
		
		uint16 Get_uint16 ();
		
		/// Put an unsigned 16-bit integer to stream and advance write position.
		/// Byte swap if byte swapping is turned on.
		/// \param x One unsigned 16-bit integer.

		void Put_uint16 (uint16 x);
		
		/// Get an unsigned 32-bit integer from stream and advance read position. 
		/// Byte swap if byte swapping is turned on.
		/// \retval One unsigned 32-bit integer.
		/// \exception dng_exception with fErrorCode equal to dng_error_end_of_file
		/// if not enough data in stream.
		
		uint32 Get_uint32 ();
		
		/// Put an unsigned 32-bit integer to stream and advance write position. 
		/// Byte swap if byte swapping is turned on.
		/// \param x One unsigned 32-bit integer.

		void Put_uint32 (uint32 x);
		
		/// Get an unsigned 64-bit integer from stream and advance read position. 
		/// Byte swap if byte swapping is turned on.
		/// \retval One unsigned 64-bit integer.
		/// \exception dng_exception with fErrorCode equal to dng_error_end_of_file
		/// if not enough data in stream.
		
		uint64 Get_uint64 ();
		
		/// Put an unsigned 64-bit integer to stream and advance write position. 
		/// Byte swap if byte swapping is turned on.
		/// \param x One unsigned 64-bit integer.

		void Put_uint64 (uint64 x);
		
		/// Get one 8-bit integer from stream and advance read position.
		/// \retval One 8-bit integer.
		/// \exception dng_exception with fErrorCode equal to dng_error_end_of_file
		/// if not enough data in stream.

		int8 Get_int8 ()
			{
			return (int8) Get_uint8 ();
			}
			
		/// Put one 8-bit integer to stream and advance write position.
		/// \param x One  8-bit integer.

		void Put_int8 (int8 x)
			{
			Put_uint8 ((uint8) x);
			}

		/// Get one 16-bit integer from stream and advance read position. 
		/// Byte swap if byte swapping is turned on.
		/// \retval One 16-bit integer.
		/// \exception dng_exception with fErrorCode equal to dng_error_end_of_file 
		/// if not enough data in stream.

		int16 Get_int16 ()
			{
			return (int16) Get_uint16 ();
			}
			
		/// Put one 16-bit integer to stream and advance write position.
		/// Byte swap if byte swapping is turned on.
		/// \param x One 16-bit integer.

		void Put_int16 (int16 x)
			{
			Put_uint16 ((uint16) x);
			}

		/// Get one 32-bit integer from stream and advance read position. 
		/// Byte swap if byte swapping is turned on.
		/// \retval One 32-bit integer.
		/// \exception dng_exception with fErrorCode equal to dng_error_end_of_file
		/// if not enough data in stream.

		int32 Get_int32 ()
			{
			return (int32) Get_uint32 ();
			}
			
		/// Put one 32-bit integer to stream and advance write position.
		/// Byte swap if byte swapping is turned on.
		/// \param x One 32-bit integer.

		void Put_int32 (int32 x)
			{
			Put_uint32 ((uint32) x);
			}
			
		/// Get one 64-bit integer from stream and advance read position. 
		/// Byte swap if byte swapping is turned on.
		/// \retval One 64-bit integer.
		/// \exception dng_exception with fErrorCode equal to dng_error_end_of_file
		/// if not enough data in stream.

		int64 Get_int64 ()
			{
			return (int64) Get_uint64 ();
			}
			
		/// Put one 64-bit integer to stream and advance write position.
		/// Byte swap if byte swapping is turned on.
		/// \param x One 64-bit integer.

		void Put_int64 (int64 x)
			{
			Put_uint64 ((uint64) x);
			}
			
		/// Get one 32-bit IEEE floating-point number from stream and advance 
		/// read position. Byte swap if byte swapping is turned on.
		/// \retval One 32-bit IEEE floating-point number.
		/// \exception dng_exception with fErrorCode equal to dng_error_end_of_file 
		/// if not enough data in stream.

		real32 Get_real32 ();
		
		/// Put one 32-bit IEEE floating-point number to stream and advance write
		/// position. Byte swap if byte swapping is turned on.
		/// \param x One 32-bit IEEE floating-point number.

		void Put_real32 (real32 x);
		
		/// Get one 64-bit IEEE floating-point number from stream and advance
		/// read position. Byte swap if byte swapping is turned on.
		/// \retval One 64-bit IEEE floating-point number .
		/// \exception dng_exception with fErrorCode equal to dng_error_end_of_file
		/// if not enough data in stream.

		real64 Get_real64 ();
		
		/// Put one 64-bit IEEE floating-point number to stream and advance write
		/// position. Byte swap if byte swapping is turned on.
		/// \param x One64-bit IEEE floating-point number.

		void Put_real64 (real64 x);
		
		/// Get an 8-bit character string from stream and advance read position.
		/// Routine always reads until a NUL character (8-bits of zero) is read.
		/// (That is, only maxLength bytes will be returned in buffer, but the
		/// stream is always advanced until a NUL is read or EOF is reached.)
		/// \param data Buffer in which string is returned.
		/// \param maxLength Maximum number of bytes to place in buffer.
		/// \exception dng_exception with fErrorCode equal to dng_error_end_of_file
		/// if stream runs out before NUL is seen.

		void Get_CString (char *data,
						  uint32 maxLength);
		
		/// Get a 16-bit character string from stream and advance read position.
		/// 16-bit characters are truncated to 8-bits.
		/// Routine always reads until a NUL character (16-bits of zero) is read.
		/// (That is, only maxLength bytes will be returned in buffer, but the 
		/// stream is always advanced until a NUL is read or EOF is reached.)
		/// \param data Buffer to place string in.
		/// \param maxLength Maximum number of bytes to place in buffer.
		/// \exception dng_exception with fErrorCode equal to dng_error_end_of_file
		/// if stream runs out before NUL is seen.

		void Get_UString (char *data,
						  uint32 maxLength);
						  
		/// Writes the specified number of zero bytes to stream.
		/// \param count Number of zero bytes to write.
		
		void PutZeros (uint64 count);
		
		/// Writes zeros to align the stream position to a multiple of 2.
		
		void PadAlign2 ();
		
		/// Writes zeros to align the stream position to a multiple of 4.
		
		void PadAlign4 ();
		
		/// Get a value of size indicated by tag type from stream and advance
		/// read position. Byte swap if byte swapping is turned on and tag type
		/// is larger than a byte. Value is returned as an unsigned 32-bit integer. 
		/// \param tagType Tag type of data stored in stream.
		/// \retval One unsigned 32-bit integer.
		/// \exception dng_exception with fErrorCode equal to dng_error_end_of_file
		/// if not enough data in stream.

		uint32 TagValue_uint32 (uint32 tagType);

		/// Get a value of size indicated by tag type from stream and advance read
		/// position. Byte swap if byte swapping is turned on and tag type is larger
		/// than a byte. Value is returned as a 32-bit integer. 
		/// \param tagType Tag type of data stored in stream.
		/// \retval One 32-bit integer.
		/// \exception dng_exception with fErrorCode equal to dng_error_end_of_file 
		/// if not enough data in stream.

		int32 TagValue_int32 (uint32 tagType);
		
		/// Get a value of size indicated by tag type from stream and advance read 
		/// position. Byte swap if byte swapping is turned on and tag type is larger
		/// than a byte. Value is returned as a dng_urational. 
		/// \param tagType Tag type of data stored in stream.
		/// \retval One dng_urational.
		/// \exception dng_exception with fErrorCode equal to dng_error_end_of_file
		/// if not enough data in stream.

		dng_urational TagValue_urational (uint32 tagType);

		/// Get a value of size indicated by tag type from stream and advance read
		/// position. Byte swap if byte swapping is turned on and tag type is larger
		/// than a byte. Value is returned as a dng_srational. 
		/// \param tagType Tag type of data stored in stream.
		/// \retval One dng_srational.
		/// \exception dng_exception with fErrorCode equal to dng_error_end_of_file
		/// if not enough data in stream.
		
		dng_srational TagValue_srational (uint32 tagType);

		/// Get a value of size indicated by tag type from stream and advance read
		/// position. Byte swap if byte swapping is turned on and tag type is larger
		/// than a byte. Value is returned as a 64-bit IEEE floating-point number. 
		/// \param tagType Tag type of data stored in stream.
		/// \retval One 64-bit IEEE floating-point number.
		/// \exception dng_exception with fErrorCode equal to dng_error_end_of_file
		/// if not enough data in stream.

		real64 TagValue_real64 (uint32 tagType);

		/// Getter for sniffer associated with stream.
		/// \retval The sniffer for this stream.
		
		dng_abort_sniffer * Sniffer () const
			{
			return fSniffer;
			}
			
		/// Putter for sniffer associated with stream.
		/// \param sniffer The new sniffer to use (or NULL for none).
		
		void SetSniffer (dng_abort_sniffer *sniffer)
			{
			fSniffer = sniffer;
			}
			
		/// Copy a specified number of bytes to a target stream.
		/// \param dstStream The target stream.
		/// \param count The number of bytes to copy.
		
		virtual void CopyToStream (dng_stream &dstStream,
								   uint64 count);
								   
		/// Makes the target stream a copy of this stream.
		/// \param dstStream The target stream.
		
		void DuplicateStream (dng_stream &dstStream);
		
	private:
	
		// Hidden copy constructor and assignment operator.
	
		dng_stream (const dng_stream &stream);
		
		dng_stream & operator= (const dng_stream &stream);
		
	};
	
/*****************************************************************************/

class TempBigEndian
	{
	
	private:
	
		dng_stream & fStream;
		
		bool fOldSwap;
		
	public:
	
		TempBigEndian (dng_stream &stream,
					   bool bigEndian = true);
						 
		virtual ~TempBigEndian ();
		
	};
			
/*****************************************************************************/

class TempLittleEndian: public TempBigEndian
	{
	
	public:
	
		TempLittleEndian (dng_stream &stream,
						  bool littleEndian = true)
			
			:	TempBigEndian (stream, !littleEndian)
			
			{
			}
	
		virtual ~TempLittleEndian ()
			{
			}

	};
				
/*****************************************************************************/

class TempStreamSniffer
	{
	
	private:
	
		dng_stream & fStream;
		
		dng_abort_sniffer *fOldSniffer;
		
	public:
	
		TempStreamSniffer (dng_stream &stream,
					       dng_abort_sniffer *sniffer);
						 
		virtual ~TempStreamSniffer ();
		
	private:
	
		// Hidden copy constructor and assignment operator.
	
		TempStreamSniffer (const TempStreamSniffer &temp);
		
		TempStreamSniffer & operator= (const TempStreamSniffer &temp);
		
	};
				
/*****************************************************************************/

class PreserveStreamReadPosition
	{
	
	private:
	
		dng_stream & fStream;
	
		uint64 fPosition;
	
	public:
	
		PreserveStreamReadPosition (dng_stream &stream)

			:	fStream	  (stream)
			,	fPosition (stream.Position ())

			{
			}
			
		~PreserveStreamReadPosition ()
			{
			fStream.SetReadPosition (fPosition);
			}
	
	private:
	
		// Hidden copy constructor and assignment operator.
	
		PreserveStreamReadPosition (const PreserveStreamReadPosition &rhs);
		
		PreserveStreamReadPosition & operator= (const PreserveStreamReadPosition &rhs);
		
	};
				
/*****************************************************************************/

#endif
				
/*****************************************************************************/