Debian Codegraph @

core/libs/dngwriter/extra/dng_sdk/dng_stream.h

raw
1 /*****************************************************************************/
2 // Copyright 2006-2019 Adobe Systems Incorporated
3 // All Rights Reserved.
4 //
5 // NOTICE: Adobe permits you to use, modify, and distribute this file in
6 // accordance with the terms of the Adobe license agreement accompanying it.
7 /*****************************************************************************/
8
9 /** Data stream abstraction for serializing and deserializing sequences of
10 * basic types and RAW image data.
11 */
12
13 /*****************************************************************************/
14
15 #ifndef __dng_stream__
16 #define __dng_stream__
17
18 /*****************************************************************************/
19
20 #include "dng_auto_ptr.h"
21 #include "dng_classes.h"
22 #include "dng_types.h"
23 #include "dng_memory.h"
24 #include "dng_rational.h"
25 #include "dng_uncopyable.h"
26 #include "dng_utils.h"
27
28 /*****************************************************************************/
29
30 // Constants for invalid offset in streams.
31
32 const uint64 kDNGStreamInvalidOffset = (uint64) (int64) -1;
33
34 /*****************************************************************************/
35
36 /// Base stream abstraction. Has support for going between stream and pointer
37 /// abstraction.
38 ///
39 /// Note that it is the caller's responsibility to call the Flush method to
40 /// ensure that data is fully written to the underlying storage. The class
41 /// destructor does not automatically call Flush, because Flush may throw
42 /// exceptions (e.g., write permissions, disk full) and it is up to the caller
43 /// to handle these appropriately.
44
45 class dng_stream: private dng_uncopyable
46 {
47
48 public:
49
50 enum
51 {
52
53 kSmallBufferSize = 8 * 1024,
54 kBigBufferSize = 64 * 1024,
55
56 kDefaultBufferSize = kSmallBufferSize
57
58 };
59
60 private:
61
62 bool fSwapBytes;
63
64 bool fHaveLength;
65
66 uint64 fLength;
67
68 const uint64 fOffsetInOriginalFile;
69
70 uint64 fPosition;
71
72 AutoPtr<dng_memory_block> fMemBlock;
73
74 uint8 *fBuffer;
75
76 uint32 fBufferSize;
77
78 uint64 fBufferStart;
79 uint64 fBufferEnd;
80 uint64 fBufferLimit;
81
82 bool fBufferDirty;
83
84 dng_abort_sniffer *fSniffer;
85
86 protected:
87
88 dng_stream (dng_abort_sniffer *sniffer = NULL,
89 uint32 bufferSize = kDefaultBufferSize,
90 uint64 offsetInOriginalFile = kDNGStreamInvalidOffset);
91
92 virtual uint64 DoGetLength ();
93
94 virtual void DoRead (void *data,
95 uint32 count,
96 uint64 offset);
97
98 virtual void DoSetLength (uint64 length);
99
100 virtual void DoWrite (const void *data,
101 uint32 count,
102 uint64 offset);
103
104 public:
105
106 /// Construct a stream with initial data.
107 /// \param data Pointer to initial contents of stream.
108 /// \param count Number of bytes data is valid for.
109 /// \param offsetInOriginalFile If data came from a file originally,
110 /// offset can be saved here for later use.
111
112 dng_stream (const void *data,
113 uint32 count,
114 uint64 offsetInOriginalFile = kDNGStreamInvalidOffset);
115
116 virtual ~dng_stream ();
117
118 /// Getter for whether stream is swapping byte order on input/output.
119 /// \retval If true, data will be swapped on input/output.
120
121 bool SwapBytes () const
122 {
123 return fSwapBytes;
124 }
125
126 /// Setter for whether stream is swapping byte order on input/output.
127 /// \param swapBytes If true, stream will swap byte order on input or
128 /// output for future reads/writes.
129
130 void SetSwapBytes (bool swapBytes)
131 {
132 fSwapBytes = swapBytes;
133 }
134
135 /// Getter for whether data in stream is big endian.
136 /// \retval If true, data in stream is big endian.
137
138 bool BigEndian () const;
139
140 /// Setter for whether data in stream is big endian.
141 /// \param bigEndian If true, data in stream is big endian.
142
143 void SetBigEndian (bool bigEndian = true);
144
145 /// Getter for whether data in stream is big endian.
146 /// \retval If true, data in stream is big endian.
147
148 bool LittleEndian () const
149 {
150 return !BigEndian ();
151 }
152
153 /// Setter for whether data in stream is big endian.
154 /// \param littleEndian If true, data in stream is big endian.
155
156 void SetLittleEndian (bool littleEndian = true)
157 {
158 SetBigEndian (!littleEndian);
159 }
160
161 /// Returns the size of the buffer used by the stream.
162
163 uint32 BufferSize () const
164 {
165 return fBufferSize;
166 }
167
168 /// Change the buffer size on the stream, if possible.
169
170 void SetBufferSize (dng_memory_allocator &allocator,
171 uint32 newBufferSize);
172
173 /// Getter for length of data in stream.
174 /// \retval Length of readable data in stream.
175
176 uint64 Length ()
177 {
178
179 if (!fHaveLength)
180 {
181
182 fLength = DoGetLength ();
183
184 fHaveLength = true;
185
186 }
187
188 return fLength;
189
190 }
191
192 /// Getter for current offset in stream.
193 /// \retval current offset from start of stream.
194
195 uint64 Position () const
196 {
197 return fPosition;
198 }
199
200 /// Getter for current position in original file, taking into account
201 /// OffsetInOriginalFile stream data was taken from.
202 /// \retval kInvalidOffset if no offset in original file is set, sum
203 /// of offset in original file and current position otherwise.
204
205 uint64 PositionInOriginalFile () const;
206
207 /// Getter for offset in original file.
208 /// \retval kInvalidOffset if no offset in original file is set,
209 /// offset in original file otherwise.
210
211 uint64 OffsetInOriginalFile () const;
212
213 /// Return pointer to stream contents if the stream is entirely
214 /// available as a single memory block, NULL otherwise.
215
216 const void * Data () const;
217
218 /// Return the entire stream as a single memory block.
219 /// This works for all streams, but requires copying the data to a new buffer.
220 /// \param allocator Allocator used to allocate memory.
221
222 dng_memory_block * AsMemoryBlock (dng_memory_allocator &allocator,
223 uint32 numLeadingZeroBytes = 0);
224
225 /// Seek to a new position in stream for reading.
226
227 void SetReadPosition (uint64 offset);
228
229 /// Skip forward in stream.
230 /// \param delta Number of bytes to skip forward.
231
232 void Skip (uint64 delta)
233 {
234 SetReadPosition (Position () + delta);
235 }
236
237 /// Quick check to see if data range in completely buffered.
238
239 bool DataInBuffer (uint64 count,
240 uint64 offset)
241 {
242 return (offset >= fBufferStart &&
243 offset + count <= fBufferEnd);
244 }
245
246 /// Get data from stream. Exception is thrown and no data is read if
247 /// insufficient data available in stream.
248 /// \param data Buffer to put data into. Must be valid for count bytes.
249 /// \param count Bytes of data to read.
250 /// \exception dng_exception with fErrorCode equal to dng_error_end_of_file
251 /// if not enough data in stream.
252
253 void Get (void *data, uint32 count, uint32 maxOverRead=0);
254
255 /// Seek to a new position in stream for writing.
256
257 void SetWritePosition (uint64 offset);
258
259 /// Force any stored data in stream to be written to underlying storage.
260
261 void Flush ();
262
263 /// Set length of available data.
264 /// \param length Number of bytes of avialble data in stream.
265
266 void SetLength (uint64 length);
267
268 /// Write data to stream.
269 /// \param data Buffer of data to write to stream.
270 /// \param count Bytes of in data.
271
272 void Put (const void *data, uint32 count);
273
274 /// Get an unsigned 8-bit integer from stream and advance read position.
275 /// \retval One unsigned 8-bit integer.
276 /// \exception dng_exception with fErrorCode equal to dng_error_end_of_file
277 /// if not enough data in stream.
278
279 uint8 Get_uint8 ()
280 {
281
282 // Fast check to see if in buffer
283
284 if (fPosition >= fBufferStart && fPosition < fBufferEnd)
285 {
286
287 return fBuffer [fPosition++ - fBufferStart];
288
289 }
290
291 // Not in buffer, let main routine do the work.
292
293 uint8 x;
294
295 Get (&x, 1);
296
297 return x;
298
299 }
300
301 /// Put an unsigned 8-bit integer to stream and advance write position.
302 /// \param x One unsigned 8-bit integer.
303
304 void Put_uint8 (uint8 x)
305 {
306
307 if (fBufferDirty &&
308 fPosition >= fBufferStart &&
309 fPosition <= fBufferEnd &&
310 fPosition < fBufferLimit)
311 {
312
313 fBuffer [fPosition - fBufferStart] = x;
314
315 fPosition++;
316
317 if (fBufferEnd < fPosition)
318 fBufferEnd = fPosition;
319
320 fLength = Max_uint64 (Length (), fPosition);
321
322 }
323
324 else
325 {
326
327 Put (&x, 1);
328
329 }
330
331 }
332
333 /// Get an unsigned 16-bit integer from stream and advance read position.
334 /// Byte swap if byte swapping is turned on.
335 /// \retval One unsigned 16-bit integer.
336 /// \exception dng_exception with fErrorCode equal to dng_error_end_of_file
337 /// if not enough data in stream.
338
339 uint16 Get_uint16 ();
340
341 /// Put an unsigned 16-bit integer to stream and advance write position.
342 /// Byte swap if byte swapping is turned on.
343 /// \param x One unsigned 16-bit integer.
344
345 void Put_uint16 (uint16 x);
346
347 /// Get an unsigned 32-bit integer from stream and advance read position.
348 /// Byte swap if byte swapping is turned on.
349 /// \retval One unsigned 32-bit integer.
350 /// \exception dng_exception with fErrorCode equal to dng_error_end_of_file
351 /// if not enough data in stream.
352
353 uint32 Get_uint32();
354
355 #if !qDNGBigEndian
356 inline // ep, enable compiler inlining
357 uint32 Get_uint32_LE ()
358 {
359
360 uint32 x;
361
362 Get (&x, 4, 3); // Allow 3-byte overread (undefined data returned but not used)
363
364 // No check for fSwapBytes
365
366 return x;
367
368 }
369 #endif
370
371 /// Put an unsigned 32-bit integer to stream and advance write position.
372 /// Byte swap if byte swapping is turned on.
373 /// \param x One unsigned 32-bit integer.
374
375 void Put_uint32 (uint32 x);
376
377 /// Get an unsigned 64-bit integer from stream and advance read position.
378 /// Byte swap if byte swapping is turned on.
379 /// \retval One unsigned 64-bit integer.
380 /// \exception dng_exception with fErrorCode equal to dng_error_end_of_file
381 /// if not enough data in stream.
382
383 uint64 Get_uint64 ();
384
385 /// Put an unsigned 64-bit integer to stream and advance write position.
386 /// Byte swap if byte swapping is turned on.
387 /// \param x One unsigned 64-bit integer.
388
389 void Put_uint64 (uint64 x);
390
391 /// Get one 8-bit integer from stream and advance read position.
392 /// \retval One 8-bit integer.
393 /// \exception dng_exception with fErrorCode equal to dng_error_end_of_file
394 /// if not enough data in stream.
395
396 int8 Get_int8 ()
397 {
398 return (int8) Get_uint8 ();
399 }
400
401 /// Put one 8-bit integer to stream and advance write position.
402 /// \param x One 8-bit integer.
403
404 void Put_int8 (int8 x)
405 {
406 Put_uint8 ((uint8) x);
407 }
408
409 /// Get one 16-bit integer from stream and advance read position.
410 /// Byte swap if byte swapping is turned on.
411 /// \retval One 16-bit integer.
412 /// \exception dng_exception with fErrorCode equal to dng_error_end_of_file
413 /// if not enough data in stream.
414
415 int16 Get_int16 ()
416 {
417 return (int16) Get_uint16 ();
418 }
419
420 /// Put one 16-bit integer to stream and advance write position.
421 /// Byte swap if byte swapping is turned on.
422 /// \param x One 16-bit integer.
423
424 void Put_int16 (int16 x)
425 {
426 Put_uint16 ((uint16) x);
427 }
428
429 /// Get one 32-bit integer from stream and advance read position.
430 /// Byte swap if byte swapping is turned on.
431 /// \retval One 32-bit integer.
432 /// \exception dng_exception with fErrorCode equal to dng_error_end_of_file
433 /// if not enough data in stream.
434
435 int32 Get_int32 ()
436 {
437 return (int32) Get_uint32 ();
438 }
439
440 /// Put one 32-bit integer to stream and advance write position.
441 /// Byte swap if byte swapping is turned on.
442 /// \param x One 32-bit integer.
443
444 void Put_int32 (int32 x)
445 {
446 Put_uint32 ((uint32) x);
447 }
448
449 /// Get one 64-bit integer from stream and advance read position.
450 /// Byte swap if byte swapping is turned on.
451 /// \retval One 64-bit integer.
452 /// \exception dng_exception with fErrorCode equal to dng_error_end_of_file
453 /// if not enough data in stream.
454
455 int64 Get_int64 ()
456 {
457 return (int64) Get_uint64 ();
458 }
459
460 /// Put one 64-bit integer to stream and advance write position.
461 /// Byte swap if byte swapping is turned on.
462 /// \param x One 64-bit integer.
463
464 void Put_int64 (int64 x)
465 {
466 Put_uint64 ((uint64) x);
467 }
468
469 /// Get one 32-bit IEEE floating-point number from stream and advance
470 /// read position. Byte swap if byte swapping is turned on.
471 /// \retval One 32-bit IEEE floating-point number.
472 /// \exception dng_exception with fErrorCode equal to dng_error_end_of_file
473 /// if not enough data in stream.
474
475 real32 Get_real32 ();
476
477 /// Put one 32-bit IEEE floating-point number to stream and advance write
478 /// position. Byte swap if byte swapping is turned on.
479 /// \param x One 32-bit IEEE floating-point number.
480
481 void Put_real32 (real32 x);
482
483 /// Get one 64-bit IEEE floating-point number from stream and advance
484 /// read position. Byte swap if byte swapping is turned on.
485 /// \retval One 64-bit IEEE floating-point number .
486 /// \exception dng_exception with fErrorCode equal to dng_error_end_of_file
487 /// if not enough data in stream.
488
489 real64 Get_real64 ();
490
491 /// Put one 64-bit IEEE floating-point number to stream and advance write
492 /// position. Byte swap if byte swapping is turned on.
493 /// \param x One64-bit IEEE floating-point number.
494
495 void Put_real64 (real64 x);
496
497 /// Get an 8-bit character string from stream and advance read position.
498 /// Routine always reads until a NUL character (8-bits of zero) is read.
499 /// (That is, only maxLength bytes will be returned in buffer, but the
500 /// stream is always advanced until a NUL is read or EOF is reached.)
501 /// \param data Buffer in which string is returned.
502 /// \param maxLength Maximum number of bytes to place in buffer.
503 /// \exception dng_exception with fErrorCode equal to dng_error_end_of_file
504 /// if stream runs out before NUL is seen.
505
506 void Get_CString (char *data,
507 uint32 maxLength);
508
509 /// Puts an 8-bit character string from stream, including trailing NUL.
510 /// \param data Buffer pointing to null terminated string.
511
512 void Put_CString (const char *data);
513
514 /// Get a 16-bit character string from stream and advance read position.
515 /// 16-bit characters are truncated to 8-bits.
516 /// Routine always reads until a NUL character (16-bits of zero) is read.
517 /// (That is, only maxLength bytes will be returned in buffer, but the
518 /// stream is always advanced until a NUL is read or EOF is reached.)
519 /// \param data Buffer to place string in.
520 /// \param maxLength Maximum number of bytes to place in buffer.
521 /// \exception dng_exception with fErrorCode equal to dng_error_end_of_file
522 /// if stream runs out before NUL is seen.
523
524 void Get_UString (char *data,
525 uint32 maxLength);
526
527 /// Writes the specified number of zero bytes to stream.
528 /// \param count Number of zero bytes to write.
529
530 void PutZeros (uint64 count);
531
532 /// Writes zeros to align the stream position to a multiple of 2.
533
534 void PadAlign2 ();
535
536 /// Writes zeros to align the stream position to a multiple of 4.
537
538 void PadAlign4 ();
539
540 /// Get a value of size indicated by tag type from stream and advance
541 /// read position. Byte swap if byte swapping is turned on and tag type
542 /// is larger than a byte. Value is returned as an unsigned 32-bit integer.
543 /// \param tagType Tag type of data stored in stream.
544 /// \retval One unsigned 32-bit integer.
545 /// \exception dng_exception with fErrorCode equal to dng_error_end_of_file
546 /// if not enough data in stream.
547
548 uint32 TagValue_uint32 (uint32 tagType);
549
550 /// Get a value of size indicated by tag type from stream and advance
551 /// read position. Byte swap if byte swapping is turned on and tag type
552 /// is larger than a byte. Value is returned as an unsigned 64-bit integer.
553 /// \param tagType Tag type of data stored in stream.
554 /// \retval One unsigned 64-bit integer.
555 /// \exception dng_exception with fErrorCode equal to dng_error_end_of_file
556 /// if not enough data in stream.
557
558 uint64 TagValue_uint64 (uint32 tagType);
559
560 /// Get a value of size indicated by tag type from stream and advance read
561 /// position. Byte swap if byte swapping is turned on and tag type is larger
562 /// than a byte. Value is returned as a 32-bit integer.
563 /// \param tagType Tag type of data stored in stream.
564 /// \retval One 32-bit integer.
565 /// \exception dng_exception with fErrorCode equal to dng_error_end_of_file
566 /// if not enough data in stream.
567
568 int32 TagValue_int32 (uint32 tagType);
569
570 /// Get a value of size indicated by tag type from stream and advance read
571 /// position. Byte swap if byte swapping is turned on and tag type is larger
572 /// than a byte. Value is returned as a 64-bit integer.
573 /// \param tagType Tag type of data stored in stream.
574 /// \retval One 64-bit integer.
575 /// \exception dng_exception with fErrorCode equal to dng_error_end_of_file
576 /// if not enough data in stream.
577
578 int64 TagValue_int64 (uint32 tagType);
579
580 /// Get a value of size indicated by tag type from stream and advance read
581 /// position. Byte swap if byte swapping is turned on and tag type is larger
582 /// than a byte. Value is returned as a dng_urational.
583 /// \param tagType Tag type of data stored in stream.
584 /// \retval One dng_urational.
585 /// \exception dng_exception with fErrorCode equal to dng_error_end_of_file
586 /// if not enough data in stream.
587
588 dng_urational TagValue_urational (uint32 tagType);
589
590 /// Get a value of size indicated by tag type from stream and advance read
591 /// position. Byte swap if byte swapping is turned on and tag type is larger
592 /// than a byte. Value is returned as a dng_srational.
593 /// \param tagType Tag type of data stored in stream.
594 /// \retval One dng_srational.
595 /// \exception dng_exception with fErrorCode equal to dng_error_end_of_file
596 /// if not enough data in stream.
597
598 dng_srational TagValue_srational (uint32 tagType);
599
600 /// Get a value of size indicated by tag type from stream and advance read
601 /// position. Byte swap if byte swapping is turned on and tag type is larger
602 /// than a byte. Value is returned as a 64-bit IEEE floating-point number.
603 /// \param tagType Tag type of data stored in stream.
604 /// \retval One 64-bit IEEE floating-point number.
605 /// \exception dng_exception with fErrorCode equal to dng_error_end_of_file
606 /// if not enough data in stream.
607
608 real64 TagValue_real64 (uint32 tagType);
609
610 /// Getter for sniffer associated with stream.
611 /// \retval The sniffer for this stream.
612
613 dng_abort_sniffer * Sniffer () const
614 {
615 return fSniffer;
616 }
617
618 /// Putter for sniffer associated with stream.
619 /// \param sniffer The new sniffer to use (or NULL for none).
620
621 void SetSniffer (dng_abort_sniffer *sniffer)
622 {
623 fSniffer = sniffer;
624 }
625
626 /// Copy a specified number of bytes to a target stream.
627 /// \param dstStream The target stream.
628 /// \param count The number of bytes to copy.
629
630 virtual void CopyToStream (dng_stream &dstStream,
631 uint64 count);
632
633 /// Makes the target stream a copy of this stream.
634 /// \param dstStream The target stream.
635
636 void DuplicateStream (dng_stream &dstStream);
637
638 };
639
640 /*****************************************************************************/
641
642 class dng_stream_double_buffered : public dng_stream
643 {
644
645 private:
646
647 dng_stream &fStream;
648
649 public:
650
651 dng_stream_double_buffered (dng_stream &stream,
652 uint32 bufferSize = kDefaultBufferSize)
653
654 : dng_stream ((dng_abort_sniffer *) NULL,
655 bufferSize,
656 stream.OffsetInOriginalFile ())
657
658 , fStream (stream)
659
660 {
661 SetBigEndian (fStream.BigEndian ());
662 }
663
664 protected:
665
666 virtual uint64 DoGetLength ()
667 {
668 return fStream.Length ();
669 }
670
671 virtual void DoRead (void *data,
672 uint32 count,
673 uint64 offset)
674 {
675 fStream.SetReadPosition (offset);
676 fStream.Get (data, count);
677 }
678
679 };
680
681 /*****************************************************************************/
682
683 class dng_stream_contiguous_read_hint
684 {
685
686 private:
687
688 dng_stream &fStream;
689
690 dng_memory_allocator &fAllocator;
691
692 uint32 fOldBufferSize;
693
694 public:
695
696 dng_stream_contiguous_read_hint (dng_stream &stream,
697 dng_memory_allocator &allocator,
698 uint64 offset,
699 uint64 count);
700
701 ~dng_stream_contiguous_read_hint ();
702
703 };
704
705 /*****************************************************************************/
706
707 class TempBigEndian
708 {
709
710 private:
711
712 dng_stream & fStream;
713
714 bool fOldSwap;
715
716 public:
717
718 TempBigEndian (dng_stream &stream,
719 bool bigEndian = true);
720
721 virtual ~TempBigEndian ();
722
723 };
724
725 /*****************************************************************************/
726
727 class TempLittleEndian: public TempBigEndian
728 {
729
730 public:
731
732 TempLittleEndian (dng_stream &stream,
733 bool littleEndian = true)
734
735 : TempBigEndian (stream, !littleEndian)
736
737 {
738 }
739
740 virtual ~TempLittleEndian ()
741 {
742 }
743
744 };
745
746 /*****************************************************************************/
747
748 class TempStreamSniffer: private dng_uncopyable
749 {
750
751 private:
752
753 dng_stream & fStream;
754
755 dng_abort_sniffer *fOldSniffer;
756
757 public:
758
759 TempStreamSniffer (dng_stream &stream,
760 dng_abort_sniffer *sniffer);
761
762 ~TempStreamSniffer ();
763
764 };
765
766 /*****************************************************************************/
767
768 class PreserveStreamReadPosition: private dng_uncopyable
769 {
770
771 private:
772
773 dng_stream & fStream;
774
775 uint64 fPosition;
776
777 public:
778
779 PreserveStreamReadPosition (dng_stream &stream)
780
781 : fStream (stream)
782 , fPosition (stream.Position ())
783
784 {
785 }
786
787 ~PreserveStreamReadPosition ()
788 {
789 fStream.SetReadPosition (fPosition);
790 }
791
792 };
793
794 /*****************************************************************************/
795
796 #endif
797
798 /*****************************************************************************/