/src/dng_sdk/source/dng_stream.cpp
Line | Count | Source |
1 | | /*****************************************************************************/ |
2 | | // Copyright 2006-2007 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 | | /* $Id: //mondo/dng_sdk_1_4/dng_sdk/source/dng_stream.cpp#2 $ */ |
10 | | /* $DateTime: 2012/06/01 07:28:57 $ */ |
11 | | /* $Change: 832715 $ */ |
12 | | /* $Author: tknoll $ */ |
13 | | |
14 | | /*****************************************************************************/ |
15 | | |
16 | | #include "dng_stream.h" |
17 | | |
18 | | #include "dng_abort_sniffer.h" |
19 | | #include "dng_auto_ptr.h" |
20 | | #include "dng_bottlenecks.h" |
21 | | #include "dng_exceptions.h" |
22 | | #include "dng_flags.h" |
23 | | #include "dng_memory.h" |
24 | | #include "dng_tag_types.h" |
25 | | |
26 | | /*****************************************************************************/ |
27 | | |
28 | | dng_stream::dng_stream (dng_abort_sniffer *sniffer, |
29 | | uint32 bufferSize, |
30 | | uint64 offsetInOriginalFile) |
31 | | |
32 | 300k | : fSwapBytes (false) |
33 | 300k | , fHaveLength (false) |
34 | 300k | , fLength (0) |
35 | 300k | , fOffsetInOriginalFile (offsetInOriginalFile) |
36 | 300k | , fPosition (0) |
37 | 300k | , fMemBlock (bufferSize) |
38 | 300k | , fBuffer (fMemBlock.Buffer_uint8 ()) |
39 | 300k | , fBufferSize (bufferSize) |
40 | 300k | , fBufferStart (0) |
41 | 300k | , fBufferEnd (0) |
42 | 300k | , fBufferLimit (bufferSize) |
43 | 300k | , fBufferDirty (false) |
44 | 300k | , fSniffer (sniffer) |
45 | | |
46 | 300k | { |
47 | | |
48 | 300k | } |
49 | | |
50 | | /*****************************************************************************/ |
51 | | |
52 | | dng_stream::dng_stream (const void *data, |
53 | | uint32 count, |
54 | | uint64 offsetInOriginalFile) |
55 | | |
56 | 18.4k | : fSwapBytes (false) |
57 | 18.4k | , fHaveLength (true) |
58 | 18.4k | , fLength (count) |
59 | 18.4k | , fOffsetInOriginalFile (offsetInOriginalFile) |
60 | 18.4k | , fPosition (0) |
61 | 18.4k | , fMemBlock () |
62 | 18.4k | , fBuffer ((uint8 *) data) |
63 | 18.4k | , fBufferSize (count) |
64 | 18.4k | , fBufferStart (0) |
65 | 18.4k | , fBufferEnd (count) |
66 | 18.4k | , fBufferLimit (count) |
67 | 18.4k | , fBufferDirty (false) |
68 | 18.4k | , fSniffer (NULL) |
69 | | |
70 | 18.4k | { |
71 | | |
72 | 18.4k | } |
73 | | |
74 | | /*****************************************************************************/ |
75 | | |
76 | | dng_stream::~dng_stream () |
77 | 319k | { |
78 | | |
79 | 319k | } |
80 | | |
81 | | /*****************************************************************************/ |
82 | | |
83 | | uint64 dng_stream::DoGetLength () |
84 | 0 | { |
85 | | |
86 | 0 | ThrowProgramError (); |
87 | |
|
88 | 0 | return 0; |
89 | | |
90 | 0 | } |
91 | | |
92 | | /*****************************************************************************/ |
93 | | |
94 | | void dng_stream::DoRead (void * /* data */, |
95 | | uint32 /* count */, |
96 | | uint64 /* offset */) |
97 | 0 | { |
98 | | |
99 | 0 | ThrowProgramError (); |
100 | |
|
101 | 0 | } |
102 | | |
103 | | /*****************************************************************************/ |
104 | | |
105 | | void dng_stream::DoSetLength (uint64 /* length */) |
106 | 0 | { |
107 | | |
108 | 0 | ThrowProgramError (); |
109 | |
|
110 | 0 | } |
111 | | |
112 | | /*****************************************************************************/ |
113 | | |
114 | | void dng_stream::DoWrite (const void * /* data */, |
115 | | uint32 /* count */, |
116 | | uint64 /* offset */) |
117 | 0 | { |
118 | | |
119 | 0 | ThrowProgramError (); |
120 | |
|
121 | 0 | } |
122 | | |
123 | | /*****************************************************************************/ |
124 | | |
125 | | bool dng_stream::BigEndian () const |
126 | 10.6M | { |
127 | | |
128 | 10.6M | return fSwapBytes != (!!qDNGBigEndian); |
129 | | |
130 | 10.6M | } |
131 | | |
132 | | /*****************************************************************************/ |
133 | | |
134 | | void dng_stream::SetBigEndian (bool bigEndian) |
135 | 4.11M | { |
136 | | |
137 | 4.11M | fSwapBytes = (bigEndian != (!!qDNGBigEndian)); |
138 | | |
139 | 4.11M | } |
140 | | |
141 | | /*****************************************************************************/ |
142 | | |
143 | | const void * dng_stream::Data () const |
144 | 0 | { |
145 | | |
146 | 0 | if (fBufferStart == 0 && fHaveLength && fBufferEnd == fLength) |
147 | 0 | { |
148 | | |
149 | 0 | return fBuffer; |
150 | | |
151 | 0 | } |
152 | | |
153 | 0 | return NULL; |
154 | | |
155 | 0 | } |
156 | | |
157 | | /*****************************************************************************/ |
158 | | |
159 | | dng_memory_block * dng_stream::AsMemoryBlock (dng_memory_allocator &allocator) |
160 | 76.9k | { |
161 | | |
162 | 76.9k | Flush (); |
163 | | |
164 | 76.9k | uint64 len64 = Length (); |
165 | | |
166 | 76.9k | if (len64 > 0xFFFFFFFF) |
167 | 0 | { |
168 | 0 | ThrowProgramError (); |
169 | 0 | } |
170 | | |
171 | 76.9k | uint32 len = (uint32) len64; |
172 | | |
173 | 76.9k | AutoPtr<dng_memory_block> block (allocator.Allocate (len)); |
174 | | |
175 | 76.9k | if (len) |
176 | 24.2k | { |
177 | | |
178 | 24.2k | SetReadPosition (0); |
179 | | |
180 | 24.2k | Get (block->Buffer (), len); |
181 | | |
182 | 24.2k | } |
183 | | |
184 | 76.9k | return block.Release (); |
185 | | |
186 | 76.9k | } |
187 | | |
188 | | /*****************************************************************************/ |
189 | | |
190 | | void dng_stream::SetReadPosition (uint64 offset) |
191 | 240M | { |
192 | | |
193 | 240M | fPosition = offset; |
194 | | |
195 | 240M | if (fPosition > Length ()) |
196 | 190k | { |
197 | | |
198 | 190k | ThrowEndOfFile (); |
199 | | |
200 | 190k | } |
201 | | |
202 | 240M | } |
203 | | |
204 | | /*****************************************************************************/ |
205 | | |
206 | | uint64 dng_stream::OffsetInOriginalFile () const |
207 | 0 | { |
208 | | |
209 | 0 | return fOffsetInOriginalFile; |
210 | | |
211 | 0 | } |
212 | | |
213 | | /*****************************************************************************/ |
214 | | |
215 | | uint64 dng_stream::PositionInOriginalFile () const |
216 | 202k | { |
217 | | |
218 | 202k | if (fOffsetInOriginalFile == kDNGStreamInvalidOffset) |
219 | 15.7k | return kDNGStreamInvalidOffset; |
220 | | |
221 | 186k | return fOffsetInOriginalFile + Position (); |
222 | | |
223 | 202k | } |
224 | | |
225 | | /*****************************************************************************/ |
226 | | |
227 | | void dng_stream::Get (void *data, uint32 count) |
228 | 317M | { |
229 | | |
230 | 331M | while (count) |
231 | 330M | { |
232 | | |
233 | | // See if the request is totally inside buffer. |
234 | | |
235 | 330M | if (fPosition >= fBufferStart && fPosition + count <= fBufferEnd) |
236 | 315M | { |
237 | | |
238 | 315M | DoCopyBytes (fBuffer + (uint32) (fPosition - fBufferStart), |
239 | 315M | data, |
240 | 315M | count); |
241 | | |
242 | 315M | fPosition += count; |
243 | | |
244 | 315M | return; |
245 | | |
246 | 315M | } |
247 | | |
248 | | // See if first part of request is inside buffer. |
249 | | |
250 | 15.8M | if (fPosition >= fBufferStart && fPosition < fBufferEnd) |
251 | 224k | { |
252 | | |
253 | 224k | uint32 block = (uint32) (fBufferEnd - fPosition); |
254 | | |
255 | 224k | DoCopyBytes (fBuffer + (fPosition - fBufferStart), |
256 | 224k | data, |
257 | 224k | block); |
258 | | |
259 | 224k | count -= block; |
260 | | |
261 | 224k | data = (void *) (((char *) data) + block); |
262 | | |
263 | 224k | fPosition += block; |
264 | | |
265 | 224k | } |
266 | | |
267 | | // Flush buffer if dirty. |
268 | | |
269 | 15.8M | Flush (); |
270 | | |
271 | | // Do large reads unbuffered. |
272 | | |
273 | 15.8M | if (count > fBufferSize) |
274 | 1.98M | { |
275 | | |
276 | 1.98M | if (fPosition + count > Length ()) |
277 | 18.6k | { |
278 | | |
279 | 18.6k | ThrowEndOfFile (); |
280 | | |
281 | 18.6k | } |
282 | | |
283 | 1.98M | DoRead (data, |
284 | 1.98M | count, |
285 | 1.98M | fPosition); |
286 | | |
287 | 1.98M | fPosition += count; |
288 | | |
289 | 1.98M | return; |
290 | | |
291 | 1.98M | } |
292 | | |
293 | | // Figure out new buffer range. |
294 | | |
295 | 13.8M | fBufferStart = fPosition; |
296 | | |
297 | 13.8M | if (fBufferSize >= 4096) |
298 | 13.8M | { |
299 | | |
300 | | // Align to a 4K file block. |
301 | | |
302 | 13.8M | fBufferStart &= (uint64) ~((int64) 4095); |
303 | | |
304 | 13.8M | } |
305 | | |
306 | 13.8M | fBufferEnd = Min_uint64 (fBufferStart + fBufferSize, Length ()); |
307 | | |
308 | 13.8M | if (fBufferEnd <= fPosition) |
309 | 87.1k | { |
310 | | |
311 | 87.1k | ThrowEndOfFile (); |
312 | | |
313 | 87.1k | } |
314 | | |
315 | | // Read data into buffer. |
316 | | |
317 | 13.8M | dng_abort_sniffer::SniffForAbort (fSniffer); |
318 | | |
319 | 13.8M | DoRead (fBuffer, |
320 | 13.8M | (uint32) (fBufferEnd - fBufferStart), |
321 | 13.8M | fBufferStart); |
322 | | |
323 | 13.8M | } |
324 | | |
325 | 317M | } |
326 | | |
327 | | /*****************************************************************************/ |
328 | | |
329 | | void dng_stream::SetWritePosition (uint64 offset) |
330 | 126k | { |
331 | | |
332 | 126k | fPosition = offset; |
333 | | |
334 | 126k | } |
335 | | |
336 | | /*****************************************************************************/ |
337 | | |
338 | | void dng_stream::Flush () |
339 | 16.9M | { |
340 | | |
341 | 16.9M | if (fBufferDirty) |
342 | 628k | { |
343 | | |
344 | 628k | dng_abort_sniffer::SniffForAbort (fSniffer); |
345 | | |
346 | 628k | DoWrite (fBuffer, |
347 | 628k | (uint32) (fBufferEnd - fBufferStart), |
348 | 628k | fBufferStart); |
349 | | |
350 | 628k | fBufferStart = 0; |
351 | 628k | fBufferEnd = 0; |
352 | 628k | fBufferLimit = fBufferSize; |
353 | | |
354 | 628k | fBufferDirty = false; |
355 | | |
356 | 628k | } |
357 | | |
358 | 16.9M | } |
359 | | |
360 | | /*****************************************************************************/ |
361 | | |
362 | | void dng_stream::SetLength (uint64 length) |
363 | 57.0k | { |
364 | | |
365 | 57.0k | Flush (); |
366 | | |
367 | 57.0k | if (Length () != length) |
368 | 0 | { |
369 | | |
370 | 0 | DoSetLength (length); |
371 | | |
372 | 0 | fLength = length; |
373 | | |
374 | 0 | } |
375 | | |
376 | 57.0k | } |
377 | | |
378 | | /*****************************************************************************/ |
379 | | |
380 | | void dng_stream::Put (const void *data, |
381 | | uint32 count) |
382 | 200M | { |
383 | | |
384 | | // See if we can replace or append to the existing buffer. |
385 | | |
386 | 200M | uint64 endPosition = fPosition + count; |
387 | | |
388 | 200M | if (fBufferDirty && |
389 | 199M | fPosition >= fBufferStart && |
390 | 199M | fPosition <= fBufferEnd && |
391 | 199M | endPosition <= fBufferLimit) |
392 | 199M | { |
393 | | |
394 | 199M | DoCopyBytes (data, |
395 | 199M | fBuffer + (uint32) (fPosition - fBufferStart), |
396 | 199M | count); |
397 | | |
398 | 199M | if (fBufferEnd < endPosition) |
399 | 199M | fBufferEnd = endPosition; |
400 | | |
401 | 199M | } |
402 | | |
403 | | // Else we need to write to the file. |
404 | | |
405 | 833k | else |
406 | 833k | { |
407 | | |
408 | | // Write existing buffer. |
409 | | |
410 | 833k | Flush (); |
411 | | |
412 | | // Write large blocks unbuffered. |
413 | | |
414 | 833k | if (count >= fBufferSize) |
415 | 204k | { |
416 | | |
417 | 204k | dng_abort_sniffer::SniffForAbort (fSniffer); |
418 | | |
419 | 204k | DoWrite (data, count, fPosition); |
420 | | |
421 | 204k | } |
422 | | |
423 | | // Start a new buffer with small blocks. |
424 | | |
425 | 628k | else |
426 | 628k | { |
427 | | |
428 | 628k | fBufferDirty = true; |
429 | | |
430 | 628k | fBufferStart = fPosition; |
431 | 628k | fBufferEnd = endPosition; |
432 | 628k | fBufferLimit = fBufferStart + fBufferSize; |
433 | | |
434 | 628k | DoCopyBytes (data, |
435 | 628k | fBuffer, |
436 | 628k | count); |
437 | | |
438 | 628k | } |
439 | | |
440 | 833k | } |
441 | | |
442 | 200M | fPosition = endPosition; |
443 | | |
444 | 200M | fLength = Max_uint64 (Length (), fPosition); |
445 | | |
446 | 200M | } |
447 | | |
448 | | /*****************************************************************************/ |
449 | | |
450 | | uint16 dng_stream::Get_uint16 () |
451 | 176M | { |
452 | | |
453 | 176M | uint16 x; |
454 | | |
455 | 176M | Get (&x, 2); |
456 | | |
457 | 176M | if (fSwapBytes) |
458 | 21.1M | { |
459 | | |
460 | 21.1M | x = SwapBytes16 (x); |
461 | | |
462 | 21.1M | } |
463 | | |
464 | 176M | return x; |
465 | | |
466 | 176M | } |
467 | | |
468 | | /*****************************************************************************/ |
469 | | |
470 | | void dng_stream::Put_uint16 (uint16 x) |
471 | 3.39M | { |
472 | | |
473 | 3.39M | if (fSwapBytes) |
474 | 18 | { |
475 | | |
476 | 18 | x = SwapBytes16 (x); |
477 | | |
478 | 18 | } |
479 | | |
480 | 3.39M | Put (&x, 2); |
481 | | |
482 | 3.39M | } |
483 | | |
484 | | /*****************************************************************************/ |
485 | | |
486 | | uint32 dng_stream::Get_uint32 () |
487 | 133M | { |
488 | | |
489 | 133M | uint32 x; |
490 | | |
491 | 133M | Get (&x, 4); |
492 | | |
493 | 133M | if (fSwapBytes) |
494 | 38.1M | { |
495 | | |
496 | 38.1M | x = SwapBytes32 (x); |
497 | | |
498 | 38.1M | } |
499 | | |
500 | 133M | return x; |
501 | | |
502 | 133M | } |
503 | | |
504 | | /*****************************************************************************/ |
505 | | |
506 | | void dng_stream::Put_uint32 (uint32 x) |
507 | 2.85M | { |
508 | | |
509 | 2.85M | if (fSwapBytes) |
510 | 132k | { |
511 | | |
512 | 132k | x = SwapBytes32 (x); |
513 | | |
514 | 132k | } |
515 | | |
516 | 2.85M | Put (&x, 4); |
517 | | |
518 | 2.85M | } |
519 | | |
520 | | /*****************************************************************************/ |
521 | | |
522 | | uint64 dng_stream::Get_uint64 () |
523 | 0 | { |
524 | | |
525 | 0 | if (fSwapBytes) |
526 | 0 | { |
527 | | |
528 | 0 | union |
529 | 0 | { |
530 | 0 | uint32 u32 [2]; |
531 | 0 | uint64 u64; |
532 | 0 | } u; |
533 | | |
534 | 0 | u.u32 [1] = Get_uint32 (); |
535 | 0 | u.u32 [0] = Get_uint32 (); |
536 | | |
537 | 0 | return u.u64; |
538 | | |
539 | 0 | } |
540 | | |
541 | 0 | uint64 x; |
542 | | |
543 | 0 | Get (&x, 8); |
544 | | |
545 | 0 | return x; |
546 | | |
547 | 0 | } |
548 | | |
549 | | /*****************************************************************************/ |
550 | | |
551 | | void dng_stream::Put_uint64 (uint64 x) |
552 | 0 | { |
553 | | |
554 | 0 | if (fSwapBytes) |
555 | 0 | { |
556 | | |
557 | 0 | union |
558 | 0 | { |
559 | 0 | uint32 u32 [2]; |
560 | 0 | uint64 u64; |
561 | 0 | } u; |
562 | | |
563 | 0 | u.u64 = x; |
564 | | |
565 | 0 | Put_uint32 (u.u32 [1]); |
566 | 0 | Put_uint32 (u.u32 [0]); |
567 | | |
568 | 0 | } |
569 | | |
570 | 0 | else |
571 | 0 | { |
572 | | |
573 | 0 | Put (&x, 8); |
574 | | |
575 | 0 | } |
576 | | |
577 | 0 | } |
578 | | |
579 | | /*****************************************************************************/ |
580 | | |
581 | | real32 dng_stream::Get_real32 () |
582 | 15.8M | { |
583 | | |
584 | 15.8M | union |
585 | 15.8M | { |
586 | 15.8M | uint32 i; |
587 | 15.8M | real32 r; |
588 | 15.8M | } u; |
589 | | |
590 | 15.8M | u.i = Get_uint32 (); |
591 | | |
592 | 15.8M | return u.r; |
593 | | |
594 | 15.8M | } |
595 | | |
596 | | /*****************************************************************************/ |
597 | | |
598 | | void dng_stream::Put_real32 (real32 x) |
599 | 191M | { |
600 | | |
601 | 191M | if (fSwapBytes) |
602 | 0 | { |
603 | | |
604 | 0 | union |
605 | 0 | { |
606 | 0 | uint32 i; |
607 | 0 | real32 r; |
608 | 0 | } u; |
609 | | |
610 | 0 | u.r = x; |
611 | | |
612 | 0 | Put_uint32 (u.i); |
613 | | |
614 | 0 | } |
615 | | |
616 | 191M | else |
617 | 191M | { |
618 | | |
619 | 191M | Put (&x, 4); |
620 | | |
621 | 191M | } |
622 | | |
623 | 191M | } |
624 | | |
625 | | /*****************************************************************************/ |
626 | | |
627 | | real64 dng_stream::Get_real64 () |
628 | 84.6k | { |
629 | | |
630 | 84.6k | if (fSwapBytes) |
631 | 20.4k | { |
632 | | |
633 | 20.4k | union |
634 | 20.4k | { |
635 | 20.4k | uint32 i [2]; |
636 | 20.4k | real64 r; |
637 | 20.4k | } u; |
638 | | |
639 | 20.4k | u.i [1] = Get_uint32 (); |
640 | 20.4k | u.i [0] = Get_uint32 (); |
641 | | |
642 | 20.4k | return u.r; |
643 | | |
644 | 20.4k | } |
645 | | |
646 | 64.1k | real64 x; |
647 | | |
648 | 64.1k | Get (&x, 8); |
649 | | |
650 | 64.1k | return x; |
651 | | |
652 | 84.6k | } |
653 | | |
654 | | /*****************************************************************************/ |
655 | | |
656 | | void dng_stream::Put_real64 (real64 x) |
657 | 55.5k | { |
658 | | |
659 | 55.5k | if (fSwapBytes) |
660 | 23.6k | { |
661 | | |
662 | 23.6k | union |
663 | 23.6k | { |
664 | 23.6k | uint32 i [2]; |
665 | 23.6k | real64 r; |
666 | 23.6k | } u; |
667 | | |
668 | 23.6k | u.r = x; |
669 | | |
670 | 23.6k | Put_uint32 (u.i [1]); |
671 | 23.6k | Put_uint32 (u.i [0]); |
672 | | |
673 | 23.6k | } |
674 | | |
675 | 31.9k | else |
676 | 31.9k | { |
677 | | |
678 | 31.9k | Put (&x, 8); |
679 | | |
680 | 31.9k | } |
681 | | |
682 | 55.5k | } |
683 | | |
684 | | /*****************************************************************************/ |
685 | | |
686 | | void dng_stream::Get_CString (char *data, uint32 maxLength) |
687 | 0 | { |
688 | |
|
689 | 0 | memset (data, 0, maxLength); |
690 | | |
691 | 0 | uint32 index = 0; |
692 | | |
693 | 0 | while (true) |
694 | 0 | { |
695 | | |
696 | 0 | char c = (char) Get_uint8 (); |
697 | | |
698 | 0 | if (index + 1 < maxLength) |
699 | 0 | data [index++] = c; |
700 | | |
701 | 0 | if (c == 0) |
702 | 0 | break; |
703 | | |
704 | 0 | } |
705 | | |
706 | 0 | } |
707 | | |
708 | | /*****************************************************************************/ |
709 | | |
710 | | void dng_stream::Get_UString (char *data, uint32 maxLength) |
711 | 0 | { |
712 | | |
713 | 0 | memset (data, 0, maxLength); |
714 | | |
715 | 0 | uint32 index = 0; |
716 | | |
717 | 0 | while (true) |
718 | 0 | { |
719 | | |
720 | 0 | char c = (char) Get_uint16 (); |
721 | | |
722 | 0 | if (index + 1 < maxLength) |
723 | 0 | data [index++] = (char) c; |
724 | | |
725 | 0 | if (c == 0) |
726 | 0 | break; |
727 | | |
728 | 0 | } |
729 | | |
730 | 0 | } |
731 | | |
732 | | /*****************************************************************************/ |
733 | | |
734 | | void dng_stream::PutZeros (uint64 count) |
735 | 0 | { |
736 | | |
737 | 0 | const uint32 kZeroBufferSize = 4096; |
738 | | |
739 | 0 | if (count >= kZeroBufferSize) |
740 | 0 | { |
741 | | |
742 | 0 | dng_memory_data zeroBuffer (kZeroBufferSize); |
743 | | |
744 | 0 | DoZeroBytes (zeroBuffer.Buffer (), |
745 | 0 | kZeroBufferSize); |
746 | | |
747 | 0 | while (count) |
748 | 0 | { |
749 | | |
750 | 0 | uint64 blockSize = Min_uint64 (count, kZeroBufferSize); |
751 | | |
752 | 0 | Put (zeroBuffer.Buffer (), (uint32) blockSize); |
753 | | |
754 | 0 | count -= blockSize; |
755 | | |
756 | 0 | } |
757 | | |
758 | 0 | } |
759 | | |
760 | 0 | else |
761 | 0 | { |
762 | | |
763 | 0 | uint32 count32 = (uint32) count; |
764 | | |
765 | 0 | for (uint32 j = 0; j < count32; j++) |
766 | 0 | { |
767 | | |
768 | 0 | Put_uint8 (0); |
769 | | |
770 | 0 | } |
771 | | |
772 | 0 | } |
773 | | |
774 | 0 | } |
775 | | |
776 | | /*****************************************************************************/ |
777 | | |
778 | | void dng_stream::PadAlign2 () |
779 | 0 | { |
780 | | |
781 | 0 | PutZeros (Position () & 1); |
782 | | |
783 | 0 | } |
784 | | |
785 | | /*****************************************************************************/ |
786 | | |
787 | | void dng_stream::PadAlign4 () |
788 | 0 | { |
789 | | |
790 | 0 | PutZeros ((4 - (Position () & 3)) & 3); |
791 | | |
792 | 0 | } |
793 | | |
794 | | /*****************************************************************************/ |
795 | | |
796 | | uint32 dng_stream::TagValue_uint32 (uint32 tagType) |
797 | 288M | { |
798 | | |
799 | 288M | switch (tagType) |
800 | 288M | { |
801 | | |
802 | 7.84M | case ttByte: |
803 | 7.84M | return (uint32) Get_uint8 (); |
804 | | |
805 | 1.41M | case ttShort: |
806 | 1.41M | return (uint32) Get_uint16 (); |
807 | | |
808 | 1.08M | case ttLong: |
809 | 1.33M | case ttIFD: |
810 | 1.33M | return Get_uint32 (); |
811 | | |
812 | 288M | } |
813 | | |
814 | 277M | real64 x = TagValue_real64 (tagType); |
815 | | |
816 | 277M | if (x < 0.0) |
817 | 432k | x = 0.0; |
818 | | |
819 | 277M | if (x > (real64) 0xFFFFFFFF) |
820 | 1.53k | x = (real64) 0xFFFFFFFF; |
821 | | |
822 | 277M | return ConvertDoubleToUint32(x + 0.5); |
823 | | |
824 | 288M | } |
825 | | |
826 | | /*****************************************************************************/ |
827 | | |
828 | | int32 dng_stream::TagValue_int32 (uint32 tagType) |
829 | 2.95M | { |
830 | | |
831 | 2.95M | switch (tagType) |
832 | 2.95M | { |
833 | | |
834 | 1.68M | case ttSByte: |
835 | 1.68M | return (int32) Get_int8 (); |
836 | | |
837 | 1.10M | case ttSShort: |
838 | 1.10M | return (int32) Get_int16 (); |
839 | | |
840 | 36.1k | case ttSLong: |
841 | 36.1k | return Get_int32 (); |
842 | | |
843 | 2.95M | } |
844 | | |
845 | 131k | real64 x = TagValue_real64 (tagType); |
846 | | |
847 | 131k | if (x < 0.0) |
848 | 12.2k | { |
849 | | |
850 | 12.2k | if (x < -2147483648.0) |
851 | 1.11k | x = -2147483648.0; |
852 | | |
853 | 12.2k | return ConvertDoubleToInt32(x - 0.5); |
854 | | |
855 | 12.2k | } |
856 | | |
857 | 119k | else |
858 | 119k | { |
859 | | |
860 | 119k | if (x > 2147483647.0) |
861 | 8.03k | x = 2147483647.0; |
862 | | |
863 | 119k | return ConvertDoubleToInt32(x + 0.5); |
864 | | |
865 | 119k | } |
866 | | |
867 | 131k | } |
868 | | |
869 | | /*****************************************************************************/ |
870 | | |
871 | | dng_urational dng_stream::TagValue_urational (uint32 tagType) |
872 | 344k | { |
873 | | |
874 | 344k | dng_urational result; |
875 | | |
876 | 344k | result.n = 0; |
877 | 344k | result.d = 1; |
878 | | |
879 | 344k | switch (tagType) |
880 | 344k | { |
881 | | |
882 | 233k | case ttRational: |
883 | 233k | { |
884 | | |
885 | 233k | result.n = Get_uint32 (); |
886 | 233k | result.d = Get_uint32 (); |
887 | | |
888 | 233k | break; |
889 | | |
890 | 0 | } |
891 | | |
892 | 35.7k | case ttSRational: |
893 | 35.7k | { |
894 | | |
895 | 35.7k | int32 n = Get_int32 (); |
896 | 35.7k | int32 d = Get_int32 (); |
897 | | |
898 | 35.7k | if ((n < 0) == (d < 0)) |
899 | 25.5k | { |
900 | | |
901 | 25.5k | if (d < 0) |
902 | 3.52k | { |
903 | 3.52k | result.n = (uint32) ((int64) n * -1); |
904 | 3.52k | result.d = (uint32) ((int64) d * -1); |
905 | 3.52k | } |
906 | 22.0k | else |
907 | 22.0k | { |
908 | 22.0k | result.n = (uint32) n; |
909 | 22.0k | result.d = (uint32) d; |
910 | 22.0k | } |
911 | | |
912 | 25.5k | } |
913 | | |
914 | 35.7k | break; |
915 | | |
916 | 0 | } |
917 | | |
918 | 11.8k | case ttByte: |
919 | 17.1k | case ttShort: |
920 | 34.1k | case ttLong: |
921 | 36.6k | case ttIFD: |
922 | 36.6k | { |
923 | | |
924 | 36.6k | result.n = TagValue_uint32 (tagType); |
925 | | |
926 | 36.6k | break; |
927 | | |
928 | 34.1k | } |
929 | | |
930 | 7.45k | case ttSByte: |
931 | 21.0k | case ttSShort: |
932 | 24.5k | case ttSLong: |
933 | 24.5k | { |
934 | | |
935 | 24.5k | int32 n = TagValue_int32 (tagType); |
936 | | |
937 | 24.5k | if (n > 0) |
938 | 11.3k | { |
939 | 11.3k | result.n = (uint32) n; |
940 | 11.3k | } |
941 | | |
942 | 24.5k | break; |
943 | | |
944 | 21.0k | } |
945 | | |
946 | 13.5k | default: |
947 | 13.5k | { |
948 | | |
949 | 13.5k | real64 x = TagValue_real64 (tagType); |
950 | | |
951 | 13.5k | if (x > 0.0) |
952 | 4.66k | { |
953 | | |
954 | 20.6k | while (result.d < 10000 && x < 1000000) |
955 | 15.9k | { |
956 | | |
957 | 15.9k | result.d *= 10; |
958 | | |
959 | 15.9k | x *= 10.0; |
960 | | |
961 | 15.9k | } |
962 | | |
963 | 4.66k | result.n = ConvertDoubleToUint32(x + 0.5); |
964 | | |
965 | 4.66k | } |
966 | | |
967 | 13.5k | } |
968 | | |
969 | 344k | } |
970 | | |
971 | 344k | return result; |
972 | | |
973 | 344k | } |
974 | | |
975 | | /*****************************************************************************/ |
976 | | |
977 | | dng_srational dng_stream::TagValue_srational (uint32 tagType) |
978 | 80.7k | { |
979 | | |
980 | 80.7k | dng_srational result; |
981 | | |
982 | 80.7k | result.n = 0; |
983 | 80.7k | result.d = 1; |
984 | | |
985 | 80.7k | switch (tagType) |
986 | 80.7k | { |
987 | | |
988 | 44.8k | case ttSRational: |
989 | 44.8k | { |
990 | | |
991 | 44.8k | result.n = Get_int32 (); |
992 | 44.8k | result.d = Get_int32 (); |
993 | | |
994 | 44.8k | break; |
995 | | |
996 | 0 | } |
997 | | |
998 | 35.8k | default: |
999 | 35.8k | { |
1000 | | |
1001 | 35.8k | real64 x = TagValue_real64 (tagType); |
1002 | | |
1003 | 35.8k | if (x > 0.0) |
1004 | 20.1k | { |
1005 | | |
1006 | 55.6k | while (result.d < 10000 && x < 1000000.0) |
1007 | 35.5k | { |
1008 | | |
1009 | 35.5k | result.d *= 10; |
1010 | | |
1011 | 35.5k | x *= 10.0; |
1012 | | |
1013 | 35.5k | } |
1014 | | |
1015 | 20.1k | result.n = ConvertDoubleToInt32(x + 0.5); |
1016 | | |
1017 | 20.1k | } |
1018 | | |
1019 | 15.6k | else |
1020 | 15.6k | { |
1021 | | |
1022 | 67.3k | while (result.d < 10000 && x > -1000000.0) |
1023 | 51.7k | { |
1024 | | |
1025 | 51.7k | result.d *= 10; |
1026 | | |
1027 | 51.7k | x *= 10.0; |
1028 | | |
1029 | 51.7k | } |
1030 | | |
1031 | 15.6k | result.n = ConvertDoubleToInt32(x - 0.5); |
1032 | | |
1033 | 15.6k | } |
1034 | | |
1035 | 35.8k | } |
1036 | | |
1037 | 80.7k | } |
1038 | | |
1039 | 77.3k | return result; |
1040 | | |
1041 | 80.7k | } |
1042 | | |
1043 | | /*****************************************************************************/ |
1044 | | |
1045 | | real64 dng_stream::TagValue_real64 (uint32 tagType) |
1046 | 281M | { |
1047 | | |
1048 | 281M | switch (tagType) |
1049 | 281M | { |
1050 | | |
1051 | 116k | case ttByte: |
1052 | 142k | case ttShort: |
1053 | 310k | case ttLong: |
1054 | 354k | case ttIFD: |
1055 | 354k | return (real64) TagValue_uint32 (tagType); |
1056 | | |
1057 | 1.66M | case ttSByte: |
1058 | 2.75M | case ttSShort: |
1059 | 2.78M | case ttSLong: |
1060 | 2.78M | return (real64) TagValue_int32 (tagType); |
1061 | | |
1062 | 415k | case ttRational: |
1063 | 415k | { |
1064 | | |
1065 | 415k | uint32 n = Get_uint32 (); |
1066 | 415k | uint32 d = Get_uint32 (); |
1067 | | |
1068 | 415k | if (d == 0) |
1069 | 14.1k | return 0.0; |
1070 | 401k | else |
1071 | 401k | return (real64) n / (real64) d; |
1072 | | |
1073 | 415k | } |
1074 | | |
1075 | 2.21M | case ttSRational: |
1076 | 2.21M | { |
1077 | | |
1078 | 2.21M | int32 n = Get_int32 (); |
1079 | 2.21M | int32 d = Get_int32 (); |
1080 | | |
1081 | 2.21M | if (d == 0) |
1082 | 32.7k | return 0.0; |
1083 | 2.18M | else |
1084 | 2.18M | return (real64) n / (real64) d; |
1085 | | |
1086 | 2.21M | } |
1087 | | |
1088 | 31.0k | case ttFloat: |
1089 | 31.0k | return (real64) Get_real32 (); |
1090 | | |
1091 | 67.0k | case ttDouble: |
1092 | 67.0k | return Get_real64 (); |
1093 | | |
1094 | 281M | } |
1095 | | |
1096 | 275M | return 0.0; |
1097 | | |
1098 | 281M | } |
1099 | | |
1100 | | /*****************************************************************************/ |
1101 | | |
1102 | | void dng_stream::CopyToStream (dng_stream &dstStream, |
1103 | | uint64 count) |
1104 | 0 | { |
1105 | | |
1106 | 0 | uint8 smallBuffer [1024]; |
1107 | | |
1108 | 0 | if (count <= sizeof (smallBuffer)) |
1109 | 0 | { |
1110 | | |
1111 | 0 | Get (smallBuffer, (uint32) count); |
1112 | | |
1113 | 0 | dstStream.Put (smallBuffer, (uint32) count); |
1114 | | |
1115 | 0 | } |
1116 | | |
1117 | 0 | else |
1118 | 0 | { |
1119 | | |
1120 | 0 | const uint32 bigBufferSize = (uint32) Min_uint64 (kBigBufferSize, |
1121 | 0 | count); |
1122 | | |
1123 | 0 | dng_memory_data bigBuffer (bigBufferSize); |
1124 | | |
1125 | 0 | while (count) |
1126 | 0 | { |
1127 | | |
1128 | 0 | uint32 blockCount = (uint32) Min_uint64 (bigBufferSize, |
1129 | 0 | count); |
1130 | | |
1131 | 0 | Get (bigBuffer.Buffer (), |
1132 | 0 | blockCount); |
1133 | | |
1134 | 0 | dstStream.Put (bigBuffer.Buffer (), |
1135 | 0 | blockCount); |
1136 | | |
1137 | 0 | count -= blockCount; |
1138 | | |
1139 | 0 | } |
1140 | | |
1141 | 0 | } |
1142 | |
|
1143 | 0 | } |
1144 | | |
1145 | | /*****************************************************************************/ |
1146 | | |
1147 | | void dng_stream::DuplicateStream (dng_stream &dstStream) |
1148 | 0 | { |
1149 | | |
1150 | | // Turn off sniffers for this operation. |
1151 | | |
1152 | 0 | TempStreamSniffer noSniffer1 (*this , NULL); |
1153 | 0 | TempStreamSniffer noSniffer2 (dstStream, NULL); |
1154 | | |
1155 | | // First grow the destination stream if required, in an attempt to |
1156 | | // reserve any needed space before overwriting the existing data. |
1157 | | |
1158 | 0 | if (dstStream.Length () < Length ()) |
1159 | 0 | { |
1160 | 0 | dstStream.SetLength (Length ()); |
1161 | 0 | } |
1162 | | |
1163 | 0 | SetReadPosition (0); |
1164 | | |
1165 | 0 | dstStream.SetWritePosition (0); |
1166 | | |
1167 | 0 | CopyToStream (dstStream, Length ()); |
1168 | | |
1169 | 0 | dstStream.Flush (); |
1170 | | |
1171 | 0 | dstStream.SetLength (Length ()); |
1172 | |
|
1173 | 0 | } |
1174 | | |
1175 | | /*****************************************************************************/ |
1176 | | |
1177 | | TempBigEndian::TempBigEndian (dng_stream &stream, |
1178 | | bool bigEndian) |
1179 | | |
1180 | 3.95M | : fStream (stream) |
1181 | 3.95M | , fOldSwap (stream.SwapBytes ()) |
1182 | | |
1183 | 3.95M | { |
1184 | | |
1185 | 3.95M | fStream.SetBigEndian (bigEndian); |
1186 | | |
1187 | 3.95M | } |
1188 | | |
1189 | | /*****************************************************************************/ |
1190 | | |
1191 | | TempBigEndian::~TempBigEndian () |
1192 | 3.95M | { |
1193 | | |
1194 | 3.95M | fStream.SetSwapBytes (fOldSwap); |
1195 | | |
1196 | 3.95M | } |
1197 | | |
1198 | | /*****************************************************************************/ |
1199 | | |
1200 | | TempStreamSniffer::TempStreamSniffer (dng_stream &stream, |
1201 | | dng_abort_sniffer *sniffer) |
1202 | | |
1203 | 0 | : fStream (stream) |
1204 | 0 | , fOldSniffer (stream.Sniffer ()) |
1205 | | |
1206 | 0 | { |
1207 | | |
1208 | 0 | fStream.SetSniffer (sniffer); |
1209 | | |
1210 | 0 | } |
1211 | | |
1212 | | /*****************************************************************************/ |
1213 | | |
1214 | | TempStreamSniffer::~TempStreamSniffer () |
1215 | 0 | { |
1216 | | |
1217 | 0 | fStream.SetSniffer (fOldSniffer); |
1218 | | |
1219 | 0 | } |
1220 | | |
1221 | | /*****************************************************************************/ |