Debian Codegraph @

core/libs/dngwriter/extra/dng_sdk/dng_read_image.cpp

raw
1 /*****************************************************************************/
2 // Copyright 2006-2022 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 #include "dng_read_image.h"
10
11 #include "dng_abort_sniffer.h"
12 #include "dng_area_task.h"
13 #include "dng_bottlenecks.h"
14 #include "dng_exceptions.h"
15 #include "dng_flags.h"
16 #include "dng_globals.h"
17 #include "dng_host.h"
18 #include "dng_image.h"
19 #include "dng_ifd.h"
20 #include "dng_jpeg_image.h"
21 #include "dng_jxl.h"
22 #include "dng_lossless_jpeg.h"
23 #include "dng_mutex.h"
24 #include "dng_memory.h"
25 #include "dng_pixel_buffer.h"
26 #include "dng_safe_arithmetic.h"
27 #include "dng_tag_types.h"
28 #include "dng_tag_values.h"
29 #include "dng_uncopyable.h"
30 #include "dng_utils.h"
31
32 #include "zlib.h"
33
34 #if qDNGUseLibJPEG
35 #include "dng_jpeg_memory_source.h"
36 #include "dng_jpeglib.h"
37 #endif
38
39 #include <limits>
40
41 /******************************************************************************/
42
43 static void DecodeDelta8 (uint8 *dPtr,
44 uint32 rows,
45 uint32 cols,
46 uint32 channels)
47 {
48
49 const uint32 dRowStep = cols * channels;
50
51 for (uint32 row = 0; row < rows; row++)
52 {
53
54 for (uint32 col = 1; col < cols; col++)
55 {
56
57 for (uint32 channel = 0; channel < channels; channel++)
58 {
59
60 dPtr [col * channels + channel] += dPtr [(col - 1) * channels + channel];
61
62 }
63
64 }
65
66 dPtr += dRowStep;
67
68 }
69
70 }
71
72 /******************************************************************************/
73
74 static void DecodeDelta16 (uint16 *dPtr,
75 uint32 rows,
76 uint32 cols,
77 uint32 channels)
78 {
79
80 const uint32 dRowStep = cols * channels;
81
82 for (uint32 row = 0; row < rows; row++)
83 {
84
85 for (uint32 col = 1; col < cols; col++)
86 {
87
88 for (uint32 channel = 0; channel < channels; channel++)
89 {
90
91 dPtr [col * channels + channel] += dPtr [(col - 1) * channels + channel];
92
93 }
94
95 }
96
97 dPtr += dRowStep;
98
99 }
100
101 }
102
103 /******************************************************************************/
104
105 static void DecodeDelta32 (uint32 *dPtr,
106 uint32 rows,
107 uint32 cols,
108 uint32 channels)
109 {
110
111 const uint32 dRowStep = cols * channels;
112
113 for (uint32 row = 0; row < rows; row++)
114 {
115
116 for (uint32 col = 1; col < cols; col++)
117 {
118
119 for (uint32 channel = 0; channel < channels; channel++)
120 {
121
122 dPtr [col * channels + channel] += dPtr [(col - 1) * channels + channel];
123
124 }
125
126 }
127
128 dPtr += dRowStep;
129
130 }
131
132 }
133
134 /*****************************************************************************/
135
136 inline void DecodeDeltaBytes (uint8 *bytePtr, int32 cols, int32 channels)
137 {
138
139 if (channels == 1)
140 {
141
142 uint8 b0 = bytePtr [0];
143
144 bytePtr += 1;
145
146 for (int32 col = 1; col < cols; ++col)
147 {
148
149 b0 += bytePtr [0];
150
151 bytePtr [0] = b0;
152
153 bytePtr += 1;
154
155 }
156
157 }
158
159 else if (channels == 3)
160 {
161
162 uint8 b0 = bytePtr [0];
163 uint8 b1 = bytePtr [1];
164 uint8 b2 = bytePtr [2];
165
166 bytePtr += 3;
167
168 for (int32 col = 1; col < cols; ++col)
169 {
170
171 b0 += bytePtr [0];
172 b1 += bytePtr [1];
173 b2 += bytePtr [2];
174
175 bytePtr [0] = b0;
176 bytePtr [1] = b1;
177 bytePtr [2] = b2;
178
179 bytePtr += 3;
180
181 }
182
183 }
184
185 else if (channels == 4)
186 {
187
188 uint8 b0 = bytePtr [0];
189 uint8 b1 = bytePtr [1];
190 uint8 b2 = bytePtr [2];
191 uint8 b3 = bytePtr [3];
192
193 bytePtr += 4;
194
195 for (int32 col = 1; col < cols; ++col)
196 {
197
198 b0 += bytePtr [0];
199 b1 += bytePtr [1];
200 b2 += bytePtr [2];
201 b3 += bytePtr [3];
202
203 bytePtr [0] = b0;
204 bytePtr [1] = b1;
205 bytePtr [2] = b2;
206 bytePtr [3] = b3;
207
208 bytePtr += 4;
209
210 }
211
212 }
213
214 else
215 {
216
217 for (int32 col = 1; col < cols; ++col)
218 {
219
220 for (int32 chan = 0; chan < channels; ++chan)
221 {
222
223 bytePtr [chan + channels] += bytePtr [chan];
224
225 }
226
227 bytePtr += channels;
228
229 }
230
231 }
232
233 }
234
235 /*****************************************************************************/
236
237 static void DecodeFPDelta (uint8 *input,
238 uint8 *output,
239 int32 cols,
240 int32 channels,
241 int32 bytesPerSample)
242 {
243
244 DecodeDeltaBytes (input, cols * bytesPerSample, channels);
245
246 int32 rowIncrement = cols * channels;
247
248 if (bytesPerSample == 2)
249 {
250
251 #if qDNGBigEndian
252 const uint8 *input0 = input;
253 const uint8 *input1 = input + rowIncrement;
254 #else
255 const uint8 *input1 = input;
256 const uint8 *input0 = input + rowIncrement;
257 #endif
258
259 for (int32 col = 0; col < rowIncrement; ++col)
260 {
261
262 output [0] = input0 [col];
263 output [1] = input1 [col];
264
265 output += 2;
266
267 }
268
269 }
270
271 else if (bytesPerSample == 3)
272 {
273
274 const uint8 *input0 = input;
275 const uint8 *input1 = input + rowIncrement;
276 const uint8 *input2 = input + rowIncrement * 2;
277
278 for (int32 col = 0; col < rowIncrement; ++col)
279 {
280
281 output [0] = input0 [col];
282 output [1] = input1 [col];
283 output [2] = input2 [col];
284
285 output += 3;
286
287 }
288
289 }
290
291 else
292 {
293
294 #if qDNGBigEndian
295 const uint8 *input0 = input;
296 const uint8 *input1 = input + rowIncrement;
297 const uint8 *input2 = input + rowIncrement * 2;
298 const uint8 *input3 = input + rowIncrement * 3;
299 #else
300 const uint8 *input3 = input;
301 const uint8 *input2 = input + rowIncrement;
302 const uint8 *input1 = input + rowIncrement * 2;
303 const uint8 *input0 = input + rowIncrement * 3;
304 #endif
305
306 for (int32 col = 0; col < rowIncrement; ++col)
307 {
308
309 output [0] = input0 [col];
310 output [1] = input1 [col];
311 output [2] = input2 [col];
312 output [3] = input3 [col];
313
314 output += 4;
315
316 }
317
318 }
319
320 }
321
322 /*****************************************************************************/
323
324 bool DecodePackBits (dng_stream &stream,
325 uint8 *dPtr,
326 int32 dstCount)
327 {
328
329 while (dstCount > 0)
330 {
331
332 int32 runCount = (int8) stream.Get_uint8 ();
333
334 if (runCount >= 0)
335 {
336
337 ++runCount;
338
339 dstCount -= runCount;
340
341 if (dstCount < 0)
342 return false;
343
344 stream.Get (dPtr, runCount);
345
346 dPtr += runCount;
347
348 }
349
350 else
351 {
352
353 runCount = -runCount + 1;
354
355 dstCount -= runCount;
356
357 if (dstCount < 0)
358 return false;
359
360 uint8 x = stream.Get_uint8 ();
361
362 while (runCount--)
363 {
364
365 *(dPtr++) = x;
366
367 }
368
369 }
370
371 }
372
373 return true;
374
375 }
376
377 /******************************************************************************/
378
379 class dng_lzw_expander: private dng_uncopyable
380 {
381
382 private:
383
384 enum
385 {
386 kResetCode = 256,
387 kEndCode = 257,
388 kTableSize = 4096
389 };
390
391 struct LZWExpanderNode
392 {
393 int16 prefix;
394 int16 final;
395 int16 depth;
396 int16 fake_for_padding;
397 };
398
399 dng_memory_data fBuffer;
400
401 LZWExpanderNode *fTable;
402
403 const uint8 *fSrcPtr;
404
405 int32 fSrcCount;
406
407 int32 fByteOffset;
408
409 uint32 fBitBuffer;
410 int32 fBitBufferCount;
411
412 int32 fNextCode;
413
414 int32 fCodeSize;
415
416 public:
417
418 dng_lzw_expander ();
419
420 bool Expand (const uint8 *sPtr,
421 uint8 *dPtr,
422 int32 sCount,
423 int32 dCount);
424
425 private:
426
427 void InitTable ();
428
429 void AddTable (int32 w, int32 k);
430
431 bool GetCodeWord (int32 &code);
432
433 };
434
435 /******************************************************************************/
436
437 dng_lzw_expander::dng_lzw_expander ()
438
439 : fBuffer ()
440 , fTable (NULL)
441 , fSrcPtr (NULL)
442 , fSrcCount (0)
443 , fByteOffset (0)
444 , fBitBuffer (0)
445 , fBitBufferCount (0)
446 , fNextCode (0)
447 , fCodeSize (0)
448
449 {
450
451 fBuffer.Allocate ((kTableSize + 1) * sizeof (LZWExpanderNode));
452
453 fTable = (LZWExpanderNode *) fBuffer.Buffer ();
454
455 }
456
457 /******************************************************************************/
458
459 void dng_lzw_expander::InitTable ()
460 {
461
462 fCodeSize = 9;
463
464 fNextCode = 258;
465
466 LZWExpanderNode *node = &fTable [0];
467
468 for (int32 code = 0; code <= kTableSize; code++)
469 {
470
471 node->prefix = -1;
472 node->final = (int16) code;
473 node->depth = 1;
474
475 node++;
476
477 }
478
479 }
480
481 /******************************************************************************/
482
483 void dng_lzw_expander::AddTable (int32 w, int32 k)
484 {
485
486 DNG_ASSERT ((w >= 0) && (w <= kTableSize),
487 "bad w value in dng_lzw_expander::AddTable");
488
489 LZWExpanderNode *parentNode = &fTable [w];
490
491 int32 nextCode = fNextCode;
492
493 fNextCode++;
494
495 DNG_ASSERT ((nextCode >= 0) && (nextCode <= kTableSize),
496 "bad fNextCode value in dng_lzw_expander::AddTable");
497
498 LZWExpanderNode *node = &fTable [nextCode];
499
500 node->prefix = (int16) w;
501 node->final = (int16) k;
502 node->depth = 1 + parentNode->depth;
503
504 if (nextCode + 1 == (1 << fCodeSize) - 1)
505 {
506 if (fCodeSize != 12)
507 fCodeSize++;
508 }
509
510 }
511
512 /******************************************************************************/
513
514 bool dng_lzw_expander::GetCodeWord (int32 &code)
515 {
516
517 // The bit buffer has the current code in the most significant bits,
518 // so shift off the low orders.
519
520 int32 codeSize = fCodeSize;
521
522 code = fBitBuffer >> (32 - codeSize);
523
524 if (fBitBufferCount >= codeSize)
525 {
526
527 // Typical case; get the code from the bit buffer.
528
529 fBitBuffer <<= codeSize;
530 fBitBufferCount -= codeSize;
531
532 }
533
534 else
535 {
536
537 // The buffer needs to be refreshed.
538
539 const int32 bitsSoFar = fBitBufferCount;
540
541 if (fByteOffset >= fSrcCount)
542 return false;
543
544 // Buffer a long word
545
546 const uint8 *ptr = fSrcPtr + fByteOffset;
547
548 #if qDNGBigEndian
549
550 fBitBuffer = *((const uint32 *) ptr);
551
552 #else
553
554 {
555
556 uint32 b0 = ptr [0];
557 uint32 b1 = ptr [1];
558 uint32 b2 = ptr [2];
559 uint32 b3 = ptr [3];
560
561 fBitBuffer = (((((b0 << 8) | b1) << 8) | b2) << 8) | b3;
562
563 }
564
565 #endif
566
567 fBitBufferCount = 32;
568
569 fByteOffset += 4;
570
571 // Number of additional bits we need
572
573 const int32 bitsUsed = codeSize - bitsSoFar;
574
575 // Number of low order bits in the current buffer we don't care about
576
577 const int32 bitsNotUsed = 32 - bitsUsed;
578
579 code |= fBitBuffer >> bitsNotUsed;
580
581 fBitBuffer <<= bitsUsed;
582 fBitBufferCount -= bitsUsed;
583
584 }
585
586 return true;
587
588 }
589
590 /******************************************************************************/
591
592 bool dng_lzw_expander::Expand (const uint8 *sPtr,
593 uint8 *dPtr,
594 int32 sCount,
595 int32 dCount)
596 {
597
598 if (sCount < 0 || dCount < 0)
599 {
600 return false;
601 }
602
603 void *dStartPtr = dPtr;
604
605 fSrcPtr = sPtr;
606
607 fSrcCount = sCount;
608
609 fByteOffset = 0;
610
611 /* the master decode loop */
612
613 while (true)
614 {
615
616 InitTable ();
617
618 int32 code;
619
620 do
621 {
622
623 if (!GetCodeWord (code))
624 return false;
625
626 DNG_ASSERT (code <= fNextCode,
627 "Unexpected LZW code in dng_lzw_expander::Expand");
628
629 }
630 while (code == kResetCode);
631
632 if (code == kEndCode)
633 return true;
634
635 if (code > kEndCode)
636 return false;
637
638 int32 oldCode = code;
639 int32 inChar = code;
640
641 (void) inChar;
642
643 *(dPtr++) = (uint8) code;
644
645 if (--dCount == 0)
646 return true;
647
648 while (true)
649 {
650
651 if (!GetCodeWord (code))
652 return false;
653
654 if (code == kResetCode)
655 break;
656
657 if (code == kEndCode)
658 return true;
659
660 const int32 inCode = code;
661
662 bool repeatLastPixel = false;
663
664 if (code >= fNextCode)
665 {
666
667 // This is either a bad file or our code table is not big enough; we
668 // are going to repeat the last code seen and attempt to muddle thru.
669
670 code = oldCode;
671
672 repeatLastPixel = true;
673
674 }
675
676 // this can only happen if we hit 2 bad codes in a row
677
678 if (code > fNextCode)
679 return false;
680
681 const int32 depth = fTable [code].depth;
682
683 if (depth < dCount)
684 {
685
686 dCount -= depth;
687
688 dPtr += depth;
689
690 uint8 *ptr = dPtr;
691
692 // give the compiler an extra hint to optimize these as registers
693
694 const LZWExpanderNode *localTable = fTable;
695
696 int32 localCode = code;
697
698 // this is usually the hottest loop in LZW expansion
699
700 while (localCode >= kResetCode)
701 {
702
703 if (ptr <= dStartPtr)
704 return false; // about to trash memory
705
706 const LZWExpanderNode &node = localTable [localCode];
707
708 uint8 tempFinal = (uint8) node.final;
709
710 localCode = node.prefix;
711
712 // Check for bogus table entry
713
714 if (localCode < 0 || localCode > kTableSize)
715 return false;
716
717 *(--ptr) = tempFinal;
718
719 }
720
721 code = localCode;
722
723 inChar = localCode;
724
725 if (ptr <= dStartPtr)
726 return false; // about to trash memory
727
728 *(--ptr) = (uint8) inChar;
729
730 }
731
732 else
733 {
734
735 // There might not be enough room for the full code
736 // so skip the end of it.
737
738 const int32 skip = depth - dCount;
739
740 for (int32 i = 0; i < skip ; i++)
741 {
742 const LZWExpanderNode &node = fTable [code];
743 code = node.prefix;
744 }
745
746 int32 depthUsed = depth - skip;
747
748 dCount -= depthUsed;
749
750 (void) dCount;
751
752 dPtr += depthUsed;
753
754 uint8 *ptr = dPtr;
755
756 while (code >= 0)
757 {
758
759 if (ptr <= dStartPtr)
760 return false; // about to trash memory
761
762 const LZWExpanderNode &node = fTable [code];
763
764 *(--ptr) = (uint8) node.final;
765
766 code = node.prefix;
767
768 // Check for bogus table entry
769
770 if (code > kTableSize)
771 return false;
772
773 }
774
775 return true;
776
777 }
778
779 if (repeatLastPixel)
780 {
781
782 *(dPtr++) = (uint8) inChar;
783
784 if (--dCount == 0)
785 return true;
786
787 }
788
789 if (fNextCode < kTableSize)
790 {
791
792 AddTable (oldCode, code);
793
794 }
795
796 oldCode = inCode;
797
798 }
799
800 }
801
802 return false;
803
804 }
805
806 /*****************************************************************************/
807
808 static void ReorderSubTileBlocks (dng_host &host,
809 const dng_ifd &ifd,
810 dng_pixel_buffer &buffer,
811 AutoPtr<dng_memory_block> &tempBuffer)
812 {
813
814 uint32 tempBufferSize = ComputeBufferSize (buffer.fPixelType,
815 buffer.fArea.Size (),
816 buffer.fPlanes,
817 padNone);
818
819 if (!tempBuffer.Get () || tempBuffer->LogicalSize () < tempBufferSize)
820 {
821
822 tempBuffer.Reset (host.Allocate (tempBufferSize));
823
824 }
825
826 uint32 blockRows = ifd.fSubTileBlockRows;
827 uint32 blockCols = ifd.fSubTileBlockCols;
828
829 uint32 rowBlocks = buffer.fArea.H () / blockRows;
830 uint32 colBlocks = buffer.fArea.W () / blockCols;
831
832 int32 rowStep = buffer.fRowStep * buffer.fPixelSize;
833 int32 colStep = buffer.fColStep * buffer.fPixelSize;
834
835 int32 rowBlockStep = rowStep * blockRows;
836 int32 colBlockStep = colStep * blockCols;
837
838 uint32 blockColBytes = blockCols * buffer.fPlanes * buffer.fPixelSize;
839
840 const uint8 *s0 = (const uint8 *) buffer.fData;
841 uint8 *d0 = tempBuffer->Buffer_uint8 ();
842
843 for (uint32 rowBlock = 0; rowBlock < rowBlocks; rowBlock++)
844 {
845
846 uint8 *d1 = d0;
847
848 for (uint32 colBlock = 0; colBlock < colBlocks; colBlock++)
849 {
850
851 uint8 *d2 = d1;
852
853 for (uint32 blockRow = 0; blockRow < blockRows; blockRow++)
854 {
855
856 for (uint32 j = 0; j < blockColBytes; j++)
857 {
858
859 d2 [j] = s0 [j];
860
861 }
862
863 s0 += blockColBytes;
864
865 d2 += rowStep;
866
867 }
868
869 d1 += colBlockStep;
870
871 }
872
873 d0 += rowBlockStep;
874
875 }
876
877 // Copy back reordered pixels.
878
879 DoCopyBytes (tempBuffer->Buffer (),
880 buffer.fData,
881 tempBufferSize);
882
883 }
884
885 /*****************************************************************************/
886
887 class dng_image_spooler: public dng_spooler,
888 private dng_uncopyable
889 {
890
891 private:
892
893 dng_host &fHost;
894
895 const dng_ifd &fIFD;
896
897 dng_image &fImage;
898
899 dng_rect fTileArea;
900
901 uint32 fPlane;
902 uint32 fPlanes;
903
904 dng_memory_block &fBlock;
905
906 AutoPtr<dng_memory_block> &fSubTileBuffer;
907
908 dng_rect fTileStrip;
909
910 uint8 *fBuffer;
911
912 uint32 fBufferCount;
913 uint32 fBufferSize;
914
915 public:
916
917 dng_image_spooler (dng_host &host,
918 const dng_ifd &ifd,
919 dng_image &image,
920 const dng_rect &tileArea,
921 uint32 plane,
922 uint32 planes,
923 dng_memory_block &block,
924 AutoPtr<dng_memory_block> &subTileBuffer);
925
926 virtual ~dng_image_spooler ();
927
928 virtual void Spool (const void *data,
929 uint32 count);
930
931 };
932
933 /*****************************************************************************/
934
935 dng_image_spooler::dng_image_spooler (dng_host &host,
936 const dng_ifd &ifd,
937 dng_image &image,
938 const dng_rect &tileArea,
939 uint32 plane,
940 uint32 planes,
941 dng_memory_block &block,
942 AutoPtr<dng_memory_block> &subTileBuffer)
943
944 : fHost (host)
945 , fIFD (ifd)
946 , fImage (image)
947 , fTileArea (tileArea)
948 , fPlane (plane)
949 , fPlanes (planes)
950 , fBlock (block)
951 , fSubTileBuffer (subTileBuffer)
952
953 , fTileStrip ()
954 , fBuffer (NULL)
955 , fBufferCount (0)
956 , fBufferSize (0)
957
958 {
959
960 uint32 bytesPerRow = fTileArea.W () * fPlanes * (uint32) sizeof (uint16);
961
962 DNG_REQUIRE (bytesPerRow > 0, "Bad bytesPerRow in dng_image_spooler");
963
964 uint32 stripLength = Pin_uint32 (ifd.fSubTileBlockRows,
965 fBlock.LogicalSize () / bytesPerRow,
966 fTileArea.H ());
967
968 stripLength = stripLength / ifd.fSubTileBlockRows
969 * ifd.fSubTileBlockRows;
970
971 fTileStrip = fTileArea;
972 fTileStrip.b = fTileArea.t + stripLength;
973
974 fBuffer = (uint8 *) fBlock.Buffer ();
975
976 fBufferCount = 0;
977 fBufferSize = bytesPerRow * stripLength;
978
979 }
980
981 /*****************************************************************************/
982
983 dng_image_spooler::~dng_image_spooler ()
984 {
985
986 }
987
988 /*****************************************************************************/
989
990 void dng_image_spooler::Spool (const void *data,
991 uint32 count)
992 {
993
994 while (count)
995 {
996
997 uint32 block = Min_uint32 (count, fBufferSize - fBufferCount);
998
999 if (block == 0)
1000 {
1001 return;
1002 }
1003
1004 DoCopyBytes (data,
1005 fBuffer + fBufferCount,
1006 block);
1007
1008 data = ((const uint8 *) data) + block;
1009
1010 count -= block;
1011
1012 fBufferCount += block;
1013
1014 if (fBufferCount == fBufferSize)
1015 {
1016
1017 fHost.SniffForAbort ();
1018
1019 dng_pixel_buffer buffer (fTileStrip,
1020 fPlane,
1021 fPlanes,
1022 ttShort,
1023 pcInterleaved,
1024 fBuffer);
1025
1026 if (fIFD.fSubTileBlockRows > 1)
1027 {
1028
1029 ReorderSubTileBlocks (fHost,
1030 fIFD,
1031 buffer,
1032 fSubTileBuffer);
1033
1034 }
1035
1036 fImage.Put (buffer);
1037
1038 uint32 stripLength = fTileStrip.H ();
1039
1040 fTileStrip.t = fTileStrip.b;
1041
1042 fTileStrip.b = Min_int32 (fTileStrip.t + stripLength,
1043 fTileArea.b);
1044
1045 fBufferCount = 0;
1046
1047 fBufferSize = fTileStrip.W () *
1048 fTileStrip.H () *
1049 fPlanes * (uint32) sizeof (uint16);
1050
1051 }
1052
1053 }
1054
1055 }
1056
1057 /*****************************************************************************/
1058
1059 dng_read_image::dng_read_image ()
1060
1061 : fJPEGTables ()
1062
1063 {
1064
1065 }
1066
1067 /*****************************************************************************/
1068
1069 dng_read_image::~dng_read_image ()
1070 {
1071
1072 }
1073
1074 /*****************************************************************************/
1075
1076 bool dng_read_image::ReadUncompressed (dng_host &host,
1077 const dng_ifd &ifd,
1078 dng_stream &stream,
1079 dng_image &image,
1080 const dng_rect &tileArea,
1081 uint32 plane,
1082 uint32 planes,
1083 AutoPtr<dng_memory_block> &uncompressedBuffer,
1084 AutoPtr<dng_memory_block> &subTileBlockBuffer)
1085 {
1086
1087 uint32 rows = tileArea.H ();
1088 uint32 samplesPerRow = tileArea.W ();
1089
1090 if (ifd.fPlanarConfiguration == pcRowInterleaved)
1091 {
1092 rows = SafeUint32Mult (rows, planes);
1093 }
1094 else
1095 {
1096 samplesPerRow = SafeUint32Mult (samplesPerRow, planes);
1097 }
1098
1099 uint32 samplesPerTile = SafeUint32Mult (samplesPerRow, rows);
1100
1101 if (uncompressedBuffer.Get () == NULL)
1102 {
1103
1104 #if qDNGValidate
1105
1106 ReportError ("Fuzz: Missing uncompressed buffer");
1107
1108 #endif
1109
1110 ThrowBadFormat ();
1111
1112 }
1113
1114 uint32 bitDepth = ifd.fBitsPerSample [plane];
1115
1116 uint32 pixelType = ttUndefined;
1117
1118 if (bitDepth == 8)
1119 {
1120
1121 pixelType = ttByte;
1122
1123 stream.Get (uncompressedBuffer->Buffer (), samplesPerTile);
1124
1125 }
1126
1127 else if (bitDepth == 16 && ifd.fSampleFormat [0] == sfFloatingPoint)
1128 {
1129
1130 pixelType = ttFloat;
1131
1132 uint32 *p_uint32 = (uint32 *) uncompressedBuffer->Buffer ();
1133
1134 for (uint32 j = 0; j < samplesPerTile; j++)
1135 {
1136
1137 p_uint32 [j] = DNG_HalfToFloat (stream.Get_uint16 ());
1138
1139 }
1140
1141 }
1142
1143 else if (bitDepth == 24 && ifd.fSampleFormat [0] == sfFloatingPoint)
1144 {
1145
1146 pixelType = ttFloat;
1147
1148 uint32 *p_uint32 = (uint32 *) uncompressedBuffer->Buffer ();
1149
1150 for (uint32 j = 0; j < samplesPerTile; j++)
1151 {
1152
1153 uint8 input [3];
1154
1155 if (stream.LittleEndian ())
1156 {
1157 input [2] = stream.Get_uint8 ();
1158 input [1] = stream.Get_uint8 ();
1159 input [0] = stream.Get_uint8 ();
1160 }
1161
1162 else
1163 {
1164 input [0] = stream.Get_uint8 ();
1165 input [1] = stream.Get_uint8 ();
1166 input [2] = stream.Get_uint8 ();
1167 }
1168
1169 p_uint32 [j] = DNG_FP24ToFloat (input);
1170
1171 }
1172
1173 }
1174
1175 else if (bitDepth == 16)
1176 {
1177
1178 pixelType = ttShort;
1179
1180 stream.Get (uncompressedBuffer->Buffer (), samplesPerTile * 2);
1181
1182 if (stream.SwapBytes ())
1183 {
1184
1185 DoSwapBytes16 ((uint16 *) uncompressedBuffer->Buffer (),
1186 samplesPerTile);
1187
1188 }
1189
1190 }
1191
1192 else if (bitDepth == 32)
1193 {
1194
1195 pixelType = image.PixelType ();
1196
1197 stream.Get (uncompressedBuffer->Buffer (), samplesPerTile * 4);
1198
1199 if (stream.SwapBytes ())
1200 {
1201
1202 DoSwapBytes32 ((uint32 *) uncompressedBuffer->Buffer (),
1203 samplesPerTile);
1204
1205 }
1206
1207 }
1208
1209 else if (bitDepth == 12)
1210 {
1211
1212 pixelType = ttShort;
1213
1214 uint16 *p = (uint16 *) uncompressedBuffer->Buffer ();
1215
1216 uint32 evenSamples = samplesPerRow >> 1;
1217
1218 for (uint32 row = 0; row < rows; row++)
1219 {
1220
1221 for (uint32 j = 0; j < evenSamples; j++)
1222 {
1223
1224 uint32 b0 = stream.Get_uint8 ();
1225 uint32 b1 = stream.Get_uint8 ();
1226 uint32 b2 = stream.Get_uint8 ();
1227
1228 p [0] = (uint16) ((b0 << 4) | (b1 >> 4));
1229 p [1] = (uint16) (((b1 << 8) | b2) & 0x0FFF);
1230
1231 p += 2;
1232
1233 }
1234
1235 if (samplesPerRow & 1)
1236 {
1237
1238 uint32 b0 = stream.Get_uint8 ();
1239 uint32 b1 = stream.Get_uint8 ();
1240
1241 p [0] = (uint16) ((b0 << 4) | (b1 >> 4));
1242
1243 p += 1;
1244
1245 }
1246
1247 }
1248
1249 }
1250
1251 else if (bitDepth > 8 && bitDepth < 16)
1252 {
1253
1254 pixelType = ttShort;
1255
1256 uint16 *p = (uint16 *) uncompressedBuffer->Buffer ();
1257
1258 uint32 bitMask = (1 << bitDepth) - 1;
1259
1260 for (uint32 row = 0; row < rows; row++)
1261 {
1262
1263 uint32 bitBuffer = 0;
1264 uint32 bufferBits = 0;
1265
1266 for (uint32 j = 0; j < samplesPerRow; j++)
1267 {
1268
1269 while (bufferBits < bitDepth)
1270 {
1271
1272 bitBuffer = (bitBuffer << 8) | stream.Get_uint8 ();
1273
1274 bufferBits += 8;
1275
1276 }
1277
1278 p [j] = (uint16) ((bitBuffer >> (bufferBits - bitDepth)) & bitMask);
1279
1280 bufferBits -= bitDepth;
1281
1282 }
1283
1284 p += samplesPerRow;
1285
1286 }
1287
1288 }
1289
1290 else if (bitDepth > 16 && bitDepth < 32)
1291 {
1292
1293 pixelType = ttLong;
1294
1295 uint32 *p = (uint32 *) uncompressedBuffer->Buffer ();
1296
1297 uint32 bitMask = ((uint32) 1 << bitDepth) - 1;
1298
1299 for (uint32 row = 0; row < rows; row++)
1300 {
1301
1302 uint64 bitBuffer = 0;
1303 uint32 bufferBits = 0;
1304
1305 for (uint32 j = 0; j < samplesPerRow; j++)
1306 {
1307
1308 while (bufferBits < bitDepth)
1309 {
1310
1311 bitBuffer = (bitBuffer << 8) | stream.Get_uint8 ();
1312
1313 bufferBits += 8;
1314
1315 }
1316
1317 p [j] = ((uint32) (bitBuffer >> (bufferBits - bitDepth))) & bitMask;
1318
1319 bufferBits -= bitDepth;
1320
1321 }
1322
1323 p += samplesPerRow;
1324
1325 }
1326
1327 }
1328
1329 else
1330 {
1331
1332 return false;
1333
1334 }
1335
1336 dng_pixel_buffer buffer (tileArea,
1337 plane,
1338 planes,
1339 pixelType,
1340 ifd.fPlanarConfiguration,
1341 uncompressedBuffer->Buffer ());
1342
1343 if (ifd.fSampleBitShift)
1344 {
1345
1346 buffer.ShiftRight (ifd.fSampleBitShift);
1347
1348 }
1349
1350 if (ifd.fSubTileBlockRows > 1)
1351 {
1352
1353 ReorderSubTileBlocks (host,
1354 ifd,
1355 buffer,
1356 subTileBlockBuffer);
1357
1358 }
1359
1360 image.Put (buffer);
1361
1362 return true;
1363
1364 }
1365
1366 /*****************************************************************************/
1367
1368 #if qDNGUseLibJPEG
1369
1370 /*****************************************************************************/
1371
1372 static void dng_error_exit (j_common_ptr cinfo)
1373 {
1374
1375 // Output message.
1376
1377 (*cinfo->err->output_message) (cinfo);
1378
1379 // Convert to a dng_exception.
1380
1381 switch (cinfo->err->msg_code)
1382 {
1383
1384 case JERR_OUT_OF_MEMORY:
1385 {
1386 ThrowMemoryFull ();
1387 break;
1388 }
1389
1390 default:
1391 {
1392 ThrowBadFormat ();
1393 }
1394
1395 }
1396
1397 }
1398
1399 /*****************************************************************************/
1400
1401 static void dng_output_message (j_common_ptr cinfo)
1402 {
1403
1404 // Format message to string.
1405
1406 char buffer [JMSG_LENGTH_MAX];
1407
1408 (*cinfo->err->format_message) (cinfo, buffer);
1409
1410 // Report the libjpeg message as a warning.
1411
1412 ReportWarning ("libjpeg", buffer);
1413
1414 }
1415
1416 /*****************************************************************************/
1417
1418 #endif
1419
1420 /*****************************************************************************/
1421
1422 void dng_read_image::DecodeLossyJPEG (dng_host &host,
1423 dng_image &image,
1424 const dng_rect &tileArea,
1425 uint32 plane,
1426 uint32 planes,
1427 uint32 /* photometricInterpretation */,
1428 uint32 jpegDataSize,
1429 uint8 *jpegDataInMemory,
1430 bool /* usingMultipleThreads */)
1431 {
1432
1433 #if qDNGUseLibJPEG
1434
1435 struct jpeg_decompress_struct cinfo;
1436
1437 // Setup the error manager.
1438
1439 struct jpeg_error_mgr jerr;
1440
1441 cinfo.err = jpeg_std_error (&jerr);
1442
1443 jerr.error_exit = dng_error_exit;
1444 jerr.output_message = dng_output_message;
1445
1446 try
1447 {
1448
1449 // Create the decompression context.
1450
1451 jpeg_create_decompress (&cinfo);
1452
1453 // Set up the memory data source manager.
1454
1455 size_t jpegDataSizeAsSizet = 0;
1456
1457 ConvertUnsigned (jpegDataSize, &jpegDataSizeAsSizet);
1458
1459 jpeg_source_mgr memorySource =
1460 CreateJpegMemorySource (jpegDataInMemory,
1461 jpegDataSizeAsSizet);
1462
1463 cinfo.src = &memorySource;
1464
1465 // Read the JPEG header.
1466
1467 jpeg_read_header (&cinfo, TRUE);
1468
1469 // Check header.
1470
1471 {
1472
1473 // Number of components may not be negative.
1474
1475 if (cinfo.num_components < 0)
1476 {
1477 ThrowBadFormat ("invalid cinfo.num_components");
1478 }
1479
1480 // Convert relevant values from header to uint32.
1481
1482 uint32 imageWidthAsUint32 = 0;
1483 uint32 imageHeightAsUint32 = 0;
1484 uint32 numComponentsAsUint32 = 0;
1485
1486 ConvertUnsigned (cinfo.image_width, &imageWidthAsUint32);
1487 ConvertUnsigned (cinfo.image_height, &imageHeightAsUint32);
1488
1489 // num_components is an int. Casting to unsigned is safe because
1490 // the test above guarantees num_components is not negative.
1491
1492 ConvertUnsigned (static_cast<unsigned> (cinfo.num_components),
1493 &numComponentsAsUint32);
1494
1495 // Check that dimensions of JPEG correspond to dimensions of tile.
1496
1497 if (imageWidthAsUint32 != tileArea.W () ||
1498 imageHeightAsUint32 != tileArea.H () ||
1499 numComponentsAsUint32 != planes)
1500 {
1501 ThrowBadFormat ("JPEG dimensions do not match tile");
1502 }
1503
1504 }
1505
1506 // Start the compression.
1507
1508 jpeg_start_decompress (&cinfo);
1509
1510 // Setup a one-scanline size buffer.
1511
1512 dng_pixel_buffer buffer (tileArea,
1513 plane,
1514 planes,
1515 ttByte,
1516 pcInterleaved,
1517 NULL);
1518
1519 buffer.fArea.b = tileArea.t + 1;
1520
1521 buffer.fDirty = true;
1522
1523 AutoPtr<dng_memory_block> bufferData (host.Allocate (buffer.fRowStep));
1524
1525 buffer.fData = bufferData->Buffer ();
1526
1527 uint8 *sampArray [1];
1528
1529 sampArray [0] = bufferData->Buffer_uint8 ();
1530
1531 // Read each scanline and save to image.
1532
1533 while (buffer.fArea.t < tileArea.b)
1534 {
1535
1536 jpeg_read_scanlines (&cinfo, sampArray, 1);
1537
1538 image.Put (buffer);
1539
1540 buffer.fArea.t = buffer.fArea.b;
1541 buffer.fArea.b = buffer.fArea.t + 1;
1542
1543 }
1544
1545 // Cleanup.
1546
1547 jpeg_finish_decompress (&cinfo);
1548
1549 jpeg_destroy_decompress (&cinfo);
1550
1551 }
1552
1553 catch (...)
1554 {
1555
1556 jpeg_destroy_decompress (&cinfo);
1557
1558 throw;
1559
1560 }
1561
1562 #else
1563
1564 // The dng_sdk does not include a lossy JPEG decoder. Override this
1565 // this method to add lossy JPEG support.
1566
1567 (void) host;
1568 (void) image;
1569 (void) tileArea;
1570 (void) plane;
1571 (void) planes;
1572 (void) jpegDataSize;
1573 (void) jpegDataInMemory;
1574
1575 ThrowProgramError ("Missing lossy JPEG decoder");
1576
1577 #endif
1578
1579 }
1580
1581 /*****************************************************************************/
1582
1583 static dng_memory_block * ReadJPEGDataToBlock (dng_host &host,
1584 dng_stream &stream,
1585 dng_memory_block *tablesBlock,
1586 uint64 tileOffset,
1587 uint32 tileByteCount,
1588 bool patchFirstByte)
1589 {
1590
1591 if (tileByteCount <= 2)
1592 {
1593 ThrowEndOfFile ();
1594 }
1595
1596 uint32 tablesByteCount = tablesBlock ? tablesBlock->LogicalSize () : 0;
1597
1598 if (tablesByteCount && tablesByteCount < 4)
1599 {
1600 ThrowEndOfFile ();
1601 }
1602
1603 // The JPEG tables start with a two byte SOI marker, and
1604 // and end with a two byte EOI marker. The JPEG tile
1605 // data also starts with a two byte SOI marker. We can
1606 // convert this combination a normal JPEG stream removing
1607 // the last two bytes of the JPEG tables and the first two
1608 // bytes of the tile data, and then concatenating them.
1609
1610 if (tablesByteCount)
1611 {
1612
1613 // Ensure the "tileOffset += 2" operation below will not wrap around.
1614
1615 if (tileOffset > std::numeric_limits<uint64>::max () - 2)
1616 {
1617 ThrowEndOfFile ();
1618 }
1619
1620 tablesByteCount -= 2;
1621
1622 tileOffset += 2;
1623 tileByteCount -= 2;
1624
1625 }
1626
1627 // Allocate buffer.
1628
1629 AutoPtr<dng_memory_block> buffer
1630 (host.Allocate (SafeUint32Add (tablesByteCount, tileByteCount)));
1631
1632 // Read in table.
1633
1634 if (tablesByteCount)
1635 {
1636
1637 DoCopyBytes (tablesBlock->Buffer (),
1638 buffer->Buffer (),
1639 tablesByteCount);
1640
1641 }
1642
1643 // Read in tile data.
1644
1645 stream.SetReadPosition (tileOffset);
1646
1647 stream.Get (buffer->Buffer_uint8 () + tablesByteCount, tileByteCount);
1648
1649 // Patch first byte, if required.
1650
1651 if (patchFirstByte)
1652 {
1653
1654 buffer->Buffer_uint8 () [0] = 0xFF;
1655
1656 }
1657
1658 // Return buffer.
1659
1660 return buffer.Release ();
1661
1662 }
1663
1664 /*****************************************************************************/
1665
1666 bool dng_read_image::ReadBaselineJPEG (dng_host &host,
1667 const dng_ifd &ifd,
1668 dng_stream &stream,
1669 dng_image &image,
1670 const dng_rect &tileArea,
1671 uint32 plane,
1672 uint32 planes,
1673 uint32 tileByteCount,
1674 uint8 *jpegDataInMemory,
1675 bool usingMultipleThreads)
1676 {
1677
1678 // Setup the data source.
1679
1680 if (fJPEGTables.Get () || !jpegDataInMemory)
1681 {
1682
1683 AutoPtr<dng_memory_block> jpegDataBlock;
1684
1685 jpegDataBlock.Reset (ReadJPEGDataToBlock (host,
1686 stream,
1687 fJPEGTables.Get (),
1688 stream.Position (),
1689 tileByteCount,
1690 ifd.fPatchFirstJPEGByte));
1691
1692 DecodeLossyJPEG (host,
1693 image,
1694 tileArea,
1695 plane,
1696 planes,
1697 ifd.fPhotometricInterpretation,
1698 jpegDataBlock->LogicalSize (),
1699 jpegDataBlock->Buffer_uint8 (),
1700 usingMultipleThreads);
1701
1702 }
1703
1704 else
1705 {
1706
1707 if (ifd.fPatchFirstJPEGByte && tileByteCount)
1708 {
1709 jpegDataInMemory [0] = 0xFF;
1710 }
1711
1712 DecodeLossyJPEG (host,
1713 image,
1714 tileArea,
1715 plane,
1716 planes,
1717 ifd.fPhotometricInterpretation,
1718 tileByteCount,
1719 jpegDataInMemory,
1720 usingMultipleThreads);
1721
1722 }
1723
1724 return true;
1725
1726 }
1727
1728 /*****************************************************************************/
1729
1730 bool dng_read_image::ReadLosslessJPEG (dng_host &host,
1731 const dng_ifd &ifd,
1732 dng_stream &stream,
1733 dng_image &image,
1734 const dng_rect &tileArea,
1735 uint32 plane,
1736 uint32 planes,
1737 uint32 tileByteCount,
1738 AutoPtr<dng_memory_block> &uncompressedBuffer,
1739 AutoPtr<dng_memory_block> &subTileBlockBuffer)
1740 {
1741
1742 // If the tile area is empty, there's nothing to read.
1743
1744 if (tileArea.IsEmpty ())
1745 {
1746 return true;
1747 }
1748
1749 dng_safe_uint32 bytesPerRow =
1750 (dng_safe_uint32 (tileArea.W ()) * planes *
1751 static_cast<uint32> (sizeof (uint16)));
1752
1753 uint32 rowsPerStrip = Pin_uint32 (ifd.fSubTileBlockRows,
1754 kImageBufferSize / bytesPerRow.Get (),
1755 tileArea.H ());
1756
1757 rowsPerStrip = rowsPerStrip / ifd.fSubTileBlockRows
1758 * ifd.fSubTileBlockRows;
1759
1760 dng_safe_uint32 bufferSize = bytesPerRow * rowsPerStrip;
1761
1762 if (uncompressedBuffer.Get () &&
1763 uncompressedBuffer->LogicalSize () < bufferSize.Get ())
1764 {
1765
1766 uncompressedBuffer.Reset ();
1767
1768 }
1769
1770 if (uncompressedBuffer.Get () == NULL)
1771 {
1772
1773 uncompressedBuffer.Reset (host.Allocate (bufferSize.Get ()));
1774
1775 }
1776
1777 dng_image_spooler spooler (host,
1778 ifd,
1779 image,
1780 tileArea,
1781 plane,
1782 planes,
1783 *uncompressedBuffer.Get (),
1784 subTileBlockBuffer);
1785
1786
1787
1788 dng_safe_uint32 decodedSize = (dng_safe_uint32 (tileArea.W ()) *
1789 tileArea.H () *
1790 planes *
1791 (uint32) sizeof (uint16));
1792
1793 bool bug16 = ifd.fLosslessJPEGBug16;
1794
1795 uint64 tileOffset = stream.Position ();
1796
1797 DoDecodeLosslessJPEG (stream,
1798 spooler,
1799 decodedSize.Get (),
1800 decodedSize.Get (),
1801 bug16,
1802 tileOffset + tileByteCount);
1803
1804 return true;
1805
1806 }
1807
1808 /*****************************************************************************/
1809
1810 bool dng_read_image::ReadJXL (dng_host &host,
1811 const dng_ifd &ifd,
1812 dng_stream &stream,
1813 dng_image &image,
1814 const dng_rect &tileArea,
1815 uint32 tileByteCount,
1816 uint8 *jxlCompressedRawBitStream,
1817 bool usingMultipleThreads)
1818 {
1819
1820 (void) ifd;
1821
1822 dng_jxl_decoder decoder;
1823
1824 decoder.fNeedBoxMeta = false;
1825 decoder.fNeedImage = true;
1826 decoder.fUsePixelBuffer = true;
1827 decoder.fUseSingleThread = usingMultipleThreads;
1828
1829 if (!jxlCompressedRawBitStream)
1830 {
1831
1832 decoder.Decode (host,
1833 stream);
1834
1835 }
1836
1837 else
1838 {
1839
1840 dng_stream tempStream (jxlCompressedRawBitStream,
1841 tileByteCount);
1842
1843 decoder.Decode (host,
1844 tempStream);
1845
1846 }
1847
1848 // Check there is a result pixel buffer.
1849
1850 DNG_REQUIRE (decoder.fMainPixelBuffer.Get (),
1851 "missing fMainPixelBuffer in ReadJXL");
1852
1853 auto &buffer = *decoder.fMainPixelBuffer;
1854
1855 // Check that it has the same dimensions as the requested tile area.
1856
1857 DNG_REQUIRE (buffer.fArea.Size () == tileArea.Size (),
1858 "mismatch tile size in ReadJXL");
1859
1860 // Check matching pixel type.
1861
1862 DNG_REQUIRE (buffer.fPixelType == image.PixelType (),
1863 "mismatch pixel type in ReadJXL");
1864
1865 // Check matching plane count.
1866
1867 DNG_REQUIRE (buffer.fPlanes == image.Planes (),
1868 "mismatch plane count in ReadJXL");
1869
1870 // Store in image.
1871
1872 buffer.fArea = tileArea;
1873
1874 image.Put (buffer);
1875
1876 return true;
1877
1878 }
1879
1880 /*****************************************************************************/
1881
1882 bool dng_read_image::CanReadTile (const dng_ifd &ifd)
1883 {
1884
1885 if (ifd.fSampleFormat [0] != sfUnsignedInteger &&
1886 ifd.fSampleFormat [0] != sfFloatingPoint)
1887 {
1888 return false;
1889 }
1890
1891 switch (ifd.fCompression)
1892 {
1893
1894 case ccUncompressed:
1895 {
1896
1897 if (ifd.fSampleFormat [0] == sfFloatingPoint)
1898 {
1899
1900 return (ifd.fBitsPerSample [0] == 16 ||
1901 ifd.fBitsPerSample [0] == 24 ||
1902 ifd.fBitsPerSample [0] == 32);
1903
1904 }
1905
1906 return ifd.fBitsPerSample [0] >= 8 &&
1907 ifd.fBitsPerSample [0] <= 32;
1908
1909 }
1910
1911 case ccJPEG:
1912 {
1913
1914 if (ifd.fSampleFormat [0] != sfUnsignedInteger)
1915 {
1916 return false;
1917 }
1918
1919 if (ifd.IsBaselineJPEG ())
1920 {
1921
1922 // Baseline JPEG.
1923
1924 return true;
1925
1926 }
1927
1928 else
1929 {
1930
1931 // Lossless JPEG.
1932
1933 return ifd.fBitsPerSample [0] >= 8 &&
1934 ifd.fBitsPerSample [0] <= 16;
1935
1936 }
1937
1938 break;
1939
1940 }
1941
1942 case ccJXL:
1943 {
1944
1945 if (ifd.fSamplesPerPixel != 1 &&
1946 ifd.fSamplesPerPixel != 3)
1947 {
1948 return false;
1949 }
1950
1951 if (ifd.fSampleFormat [0] == sfUnsignedInteger)
1952 {
1953
1954 return ifd.fBitsPerSample [0] >= 8 &&
1955 ifd.fBitsPerSample [0] <= 16;
1956
1957 }
1958
1959 else
1960 {
1961
1962 return ifd.fBitsPerSample [0] >= 16 &&
1963 ifd.fBitsPerSample [0] <= 32;
1964
1965 }
1966
1967 break;
1968
1969 }
1970
1971 case ccLZW:
1972 case ccDeflate:
1973 case ccOldDeflate:
1974 case ccPackBits:
1975 {
1976
1977 if (ifd.fSampleFormat [0] == sfFloatingPoint)
1978 {
1979
1980 if (ifd.fCompression == ccPackBits)
1981 {
1982 return false;
1983 }
1984
1985 if (ifd.fPredictor != cpNullPredictor &&
1986 ifd.fPredictor != cpFloatingPoint &&
1987 ifd.fPredictor != cpFloatingPointX2 &&
1988 ifd.fPredictor != cpFloatingPointX4)
1989 {
1990 return false;
1991 }
1992
1993 if (ifd.fBitsPerSample [0] != 16 &&
1994 ifd.fBitsPerSample [0] != 24 &&
1995 ifd.fBitsPerSample [0] != 32)
1996 {
1997 return false;
1998 }
1999
2000 }
2001
2002 else
2003 {
2004
2005 if (ifd.fPredictor != cpNullPredictor &&
2006 ifd.fPredictor != cpHorizontalDifference &&
2007 ifd.fPredictor != cpHorizontalDifferenceX2 &&
2008 ifd.fPredictor != cpHorizontalDifferenceX4)
2009 {
2010 return false;
2011 }
2012
2013 if (ifd.fBitsPerSample [0] != 8 &&
2014 ifd.fBitsPerSample [0] != 16 &&
2015 ifd.fBitsPerSample [0] != 32)
2016 {
2017 return false;
2018 }
2019
2020 }
2021
2022 return true;
2023
2024 }
2025
2026 default:
2027 {
2028 break;
2029 }
2030
2031 }
2032
2033 return false;
2034
2035 }
2036
2037 /*****************************************************************************/
2038
2039 bool dng_read_image::NeedsCompressedBuffer (const dng_ifd &ifd)
2040 {
2041
2042 if (ifd.fCompression == ccLZW ||
2043 ifd.fCompression == ccDeflate ||
2044 ifd.fCompression == ccOldDeflate ||
2045 ifd.fCompression == ccPackBits)
2046 {
2047 return true;
2048 }
2049
2050 return false;
2051
2052 }
2053
2054 /*****************************************************************************/
2055
2056 void dng_read_image::ByteSwapBuffer (dng_host & /* host */,
2057 dng_pixel_buffer &buffer)
2058 {
2059
2060 uint32 pixels = buffer.fRowStep * buffer.fArea.H ();
2061
2062 switch (buffer.fPixelSize)
2063 {
2064
2065 case 2:
2066 {
2067
2068 DoSwapBytes16 ((uint16 *) buffer.fData,
2069 pixels);
2070
2071 break;
2072
2073 }
2074
2075 case 4:
2076 {
2077
2078 DoSwapBytes32 ((uint32 *) buffer.fData,
2079 pixels);
2080
2081 break;
2082
2083 }
2084
2085 default:
2086 break;
2087
2088 }
2089
2090 }
2091
2092 /*****************************************************************************/
2093
2094 void dng_read_image::DecodePredictor (dng_host & /* host */,
2095 const dng_ifd &ifd,
2096 dng_pixel_buffer &buffer)
2097 {
2098
2099 switch (ifd.fPredictor)
2100 {
2101
2102 case cpNullPredictor:
2103 {
2104
2105 return;
2106
2107 }
2108
2109 case cpHorizontalDifference:
2110 case cpHorizontalDifferenceX2:
2111 case cpHorizontalDifferenceX4:
2112 {
2113
2114 int32 xFactor = 1;
2115
2116 if (ifd.fPredictor == cpHorizontalDifferenceX2)
2117 {
2118 xFactor = 2;
2119 }
2120
2121 else if (ifd.fPredictor == cpHorizontalDifferenceX4)
2122 {
2123 xFactor = 4;
2124 }
2125
2126 switch (buffer.fPixelType)
2127 {
2128
2129 case ttByte:
2130 {
2131
2132 DecodeDelta8 ((uint8 *) buffer.fData,
2133 buffer.fArea.H (),
2134 buffer.fArea.W () / xFactor,
2135 buffer.fPlanes * xFactor);
2136
2137 return;
2138
2139 }
2140
2141 case ttShort:
2142 {
2143
2144 DecodeDelta16 ((uint16 *) buffer.fData,
2145 buffer.fArea.H (),
2146 buffer.fArea.W () / xFactor,
2147 buffer.fPlanes * xFactor);
2148
2149 return;
2150
2151 }
2152
2153 case ttLong:
2154 {
2155
2156 DecodeDelta32 ((uint32 *) buffer.fData,
2157 buffer.fArea.H (),
2158 buffer.fArea.W () / xFactor,
2159 buffer.fPlanes * xFactor);
2160
2161 return;
2162
2163 }
2164
2165 default:
2166 break;
2167
2168 }
2169
2170 break;
2171
2172 }
2173
2174 default:
2175 break;
2176
2177 }
2178
2179 ThrowBadFormat ();
2180
2181 }
2182
2183 /*****************************************************************************/
2184
2185 void dng_read_image::ReadTile (dng_host &host,
2186 const dng_ifd &ifd,
2187 dng_stream &stream,
2188 dng_image &image,
2189 const dng_rect &tileArea,
2190 uint32 plane,
2191 uint32 planes,
2192 uint32 tileByteCount,
2193 std::shared_ptr<dng_memory_block> &compressedBuffer,
2194 AutoPtr<dng_memory_block> &uncompressedBuffer,
2195 AutoPtr<dng_memory_block> &subTileBlockBuffer,
2196 bool usingMultipleThreads)
2197 {
2198
2199 switch (ifd.fCompression)
2200 {
2201
2202 case ccLZW:
2203 case ccDeflate:
2204 case ccOldDeflate:
2205 case ccPackBits:
2206 {
2207
2208 // Figure out uncompressed size.
2209
2210 dng_safe_uint32 bytesPerSample = (ifd.fBitsPerSample [0] >> 3);
2211
2212 dng_safe_uint32 sampleCount = (dng_safe_uint32 (planes) *
2213 dng_safe_uint32 (tileArea.W ()) *
2214 dng_safe_uint32 (tileArea.H ()));
2215
2216 dng_safe_uint32 uncompressedSize = sampleCount * bytesPerSample;
2217
2218 // Setup pixel buffer to hold uncompressed data.
2219
2220 uint32 pixelType = ttUndefined;
2221
2222 if (ifd.fSampleFormat [0] == sfFloatingPoint)
2223 {
2224 pixelType = ttFloat;
2225 }
2226
2227 else if (ifd.fBitsPerSample [0] == 8)
2228 {
2229 pixelType = ttByte;
2230 }
2231
2232 else if (ifd.fBitsPerSample [0] == 16)
2233 {
2234 pixelType = ttShort;
2235 }
2236
2237 else if (ifd.fBitsPerSample [0] == 32)
2238 {
2239 pixelType = ttLong;
2240 }
2241
2242 else
2243 {
2244 ThrowBadFormat ();
2245 }
2246
2247 dng_pixel_buffer buffer (tileArea,
2248 plane,
2249 planes,
2250 pixelType,
2251 pcInterleaved,
2252 NULL);
2253
2254 // Specify custom pixel size (e.g., ttFloat pixel type but
2255 // pixel size may be 2 for stored 16-bit half-float).
2256
2257 buffer.fPixelSize = bytesPerSample.Get ();
2258
2259 dng_safe_uint32 bufferSize = uncompressedSize;
2260
2261 // If we are using the floating point predictor, we need an extra
2262 // buffer row.
2263
2264 if (ifd.fPredictor == cpFloatingPoint ||
2265 ifd.fPredictor == cpFloatingPointX2 ||
2266 ifd.fPredictor == cpFloatingPointX4)
2267 {
2268
2269 bufferSize += (dng_safe_uint32 (dng_safe_int32 (buffer.fRowStep)) *
2270 dng_safe_uint32 (buffer.fPixelSize));
2271
2272 }
2273
2274 // If are processing less than full size floating point data,
2275 // we need space to expand the data to full floating point size.
2276
2277 if (buffer.fPixelType == ttFloat)
2278 {
2279 bufferSize = Max_uint32 (bufferSize.Get (),
2280 (sampleCount * 4u).Get ());
2281 }
2282
2283 // Sometimes with multi-threading and planar image using strips,
2284 // we can process a small tile before a large tile on a thread.
2285 // Simple fix is to just reallocate the buffer if it is too small.
2286
2287 if (uncompressedBuffer.Get () &&
2288 uncompressedBuffer->LogicalSize () < bufferSize.Get ())
2289 {
2290
2291 uncompressedBuffer.Reset ();
2292
2293 }
2294
2295 if (uncompressedBuffer.Get () == NULL)
2296 {
2297
2298 uncompressedBuffer.Reset (host.Allocate (bufferSize.Get ()));
2299
2300 }
2301
2302 buffer.fData = uncompressedBuffer->Buffer ();
2303
2304 // If using floating point predictor, move buffer pointer to second row.
2305
2306 if (ifd.fPredictor == cpFloatingPoint ||
2307 ifd.fPredictor == cpFloatingPointX2 ||
2308 ifd.fPredictor == cpFloatingPointX4)
2309 {
2310
2311 buffer.fData = (uint8 *) buffer.fData +
2312 buffer.fRowStep * buffer.fPixelSize;
2313
2314 }
2315
2316 // Decompress the data.
2317
2318 if (ifd.fCompression == ccLZW)
2319 {
2320
2321 dng_lzw_expander expander;
2322
2323 if (!expander.Expand (compressedBuffer->Buffer_uint8 (),
2324 (uint8 *) buffer.fData,
2325 tileByteCount,
2326 uncompressedSize.Get ()))
2327 {
2328 ThrowBadFormat ();
2329 }
2330
2331 }
2332
2333 else if (ifd.fCompression == ccPackBits)
2334 {
2335
2336 dng_stream subStream (compressedBuffer->Buffer_uint8 (),
2337 tileByteCount);
2338
2339 if (!DecodePackBits (subStream,
2340 (uint8 *) buffer.fData,
2341 uncompressedSize.Get ()))
2342 {
2343 ThrowBadFormat ();
2344 }
2345
2346 }
2347
2348 else
2349 {
2350
2351 uLongf dstLen = uncompressedSize.Get ();
2352
2353 int err = uncompress ((Bytef *) buffer.fData,
2354 &dstLen,
2355 (const Bytef *) compressedBuffer->Buffer (),
2356 tileByteCount);
2357
2358 if (err != Z_OK)
2359 {
2360
2361 if (err == Z_MEM_ERROR)
2362 {
2363 ThrowMemoryFull ();
2364 }
2365
2366 else if (err == Z_DATA_ERROR)
2367 {
2368 // Most other TIFF readers do not fail for this error
2369 // so we should not either, even if it means showing
2370 // a corrupted image to the user. Watson #2530216
2371 // - tknoll 12/20/11
2372 }
2373
2374 else
2375 {
2376 ThrowBadFormat ();
2377 }
2378
2379 }
2380
2381 if (dstLen != uncompressedSize.Get ())
2382 {
2383 ThrowBadFormat ();
2384 }
2385
2386 }
2387
2388 // The floating point predictor is byte order independent.
2389
2390 if (ifd.fPredictor == cpFloatingPoint ||
2391 ifd.fPredictor == cpFloatingPointX2 ||
2392 ifd.fPredictor == cpFloatingPointX4)
2393 {
2394
2395 int32 xFactor = 1;
2396
2397 if (ifd.fPredictor == cpFloatingPointX2)
2398 {
2399 xFactor = 2;
2400 }
2401
2402 else if (ifd.fPredictor == cpFloatingPointX4)
2403 {
2404 xFactor = 4;
2405 }
2406
2407 for (int32 row = tileArea.t; row < tileArea.b; row++)
2408 {
2409
2410 uint8 *srcPtr = (uint8 *) buffer.DirtyPixel (row , tileArea.l, plane);
2411 uint8 *dstPtr = (uint8 *) buffer.DirtyPixel (row - 1, tileArea.l, plane);
2412
2413 DecodeFPDelta (srcPtr,
2414 dstPtr,
2415 tileArea.W () / xFactor,
2416 planes * xFactor,
2417 bytesPerSample.Get ());
2418
2419 }
2420
2421 buffer.fData = (uint8 *) buffer.fData -
2422 buffer.fRowStep * buffer.fPixelSize;
2423
2424 }
2425
2426 else
2427 {
2428
2429 // Both these compression algorithms are byte based.
2430
2431 if (stream.SwapBytes ())
2432 {
2433
2434 ByteSwapBuffer (host,
2435 buffer);
2436
2437 }
2438
2439 // Undo the predictor.
2440
2441 DecodePredictor (host,
2442 ifd,
2443 buffer);
2444
2445 }
2446
2447 // Expand floating point data, if needed.
2448
2449 if (buffer.fPixelType == ttFloat && buffer.fPixelSize == 2)
2450 {
2451
2452 uint16 *srcPtr = (uint16 *) buffer.fData;
2453 uint32 *dstPtr = (uint32 *) buffer.fData;
2454
2455 for (int32 index = sampleCount.Get () - 1; index >= 0; index--)
2456 {
2457
2458 dstPtr [index] = DNG_HalfToFloat (srcPtr [index]);
2459
2460 }
2461
2462 buffer.fPixelSize = 4;
2463
2464 }
2465
2466 else if (buffer.fPixelType == ttFloat && buffer.fPixelSize == 3)
2467 {
2468
2469 uint8 *srcPtr = ((uint8 *) buffer.fData) + (sampleCount.Get () - 1) * 3;
2470 uint32 *dstPtr = ((uint32 *) buffer.fData) + (sampleCount.Get () - 1);
2471
2472 if (stream.BigEndian () || ifd.fPredictor == cpFloatingPoint ||
2473 ifd.fPredictor == cpFloatingPointX2 ||
2474 ifd.fPredictor == cpFloatingPointX4)
2475 {
2476
2477 for (uint32 index = 0; index < sampleCount.Get (); index++)
2478 {
2479
2480 *(dstPtr--) = DNG_FP24ToFloat (srcPtr);
2481
2482 srcPtr -= 3;
2483
2484 }
2485
2486 }
2487
2488 else
2489 {
2490
2491 for (uint32 index = 0; index < sampleCount.Get (); index++)
2492 {
2493
2494 uint8 input [3];
2495
2496 input [2] = srcPtr [0];
2497 input [1] = srcPtr [1];
2498 input [0] = srcPtr [2];
2499
2500 *(dstPtr--) = DNG_FP24ToFloat (input);
2501
2502 srcPtr -= 3;
2503
2504 }
2505
2506 }
2507
2508 buffer.fPixelSize = 4;
2509
2510 }
2511
2512 // Save the data.
2513
2514 image.Put (buffer);
2515
2516 return;
2517
2518 }
2519
2520 case ccUncompressed:
2521 {
2522
2523 if (ReadUncompressed (host,
2524 ifd,
2525 stream,
2526 image,
2527 tileArea,
2528 plane,
2529 planes,
2530 uncompressedBuffer,
2531 subTileBlockBuffer))
2532 {
2533
2534 return;
2535
2536 }
2537
2538 break;
2539
2540 }
2541
2542 case ccJPEG:
2543 {
2544
2545 if (ifd.IsBaselineJPEG ())
2546 {
2547
2548 // Baseline JPEG.
2549
2550 if (ReadBaselineJPEG (host,
2551 ifd,
2552 stream,
2553 image,
2554 tileArea,
2555 plane,
2556 planes,
2557 tileByteCount,
2558 compressedBuffer ? compressedBuffer->Buffer_uint8 () : NULL,
2559 usingMultipleThreads))
2560 {
2561
2562 return;
2563
2564 }
2565
2566 }
2567
2568 else
2569 {
2570
2571 // Otherwise it should be lossless JPEG.
2572
2573 if (ReadLosslessJPEG (host,
2574 ifd,
2575 stream,
2576 image,
2577 tileArea,
2578 plane,
2579 planes,
2580 tileByteCount,
2581 uncompressedBuffer,
2582 subTileBlockBuffer))
2583 {
2584
2585 return;
2586
2587 }
2588
2589 }
2590
2591 break;
2592
2593 }
2594
2595 case ccLossyJPEG:
2596 {
2597
2598 if (ReadBaselineJPEG (host,
2599 ifd,
2600 stream,
2601 image,
2602 tileArea,
2603 plane,
2604 planes,
2605 tileByteCount,
2606 compressedBuffer ? compressedBuffer->Buffer_uint8 () : NULL,
2607 usingMultipleThreads))
2608 {
2609
2610 return;
2611
2612 }
2613
2614 break;
2615
2616 }
2617
2618 case ccJXL:
2619 {
2620
2621 DNG_REQUIRE (plane == 0,
2622 "Unexpected plane in ReadTile for ccJXL");
2623
2624 DNG_REQUIRE (planes == 1 ||
2625 planes == 3,
2626 "Unexpected planes in ReadTile for ccJXL");
2627
2628 if (ReadJXL (host,
2629 ifd,
2630 stream,
2631 image,
2632 tileArea,
2633 tileByteCount,
2634 compressedBuffer ? compressedBuffer->Buffer_uint8 () : NULL,
2635 usingMultipleThreads))
2636 {
2637
2638 return;
2639
2640 }
2641
2642 break;
2643
2644 }
2645
2646 default:
2647 break;
2648
2649 }
2650
2651 ThrowBadFormat ();
2652
2653 }
2654
2655 /*****************************************************************************/
2656
2657 bool dng_read_image::CanRead (const dng_ifd &ifd)
2658 {
2659
2660 if (ifd.fImageWidth < 1 ||
2661 ifd.fImageLength < 1)
2662 {
2663 return false;
2664 }
2665
2666 if (ifd.fSamplesPerPixel < 1)
2667 {
2668 return false;
2669 }
2670
2671 if (ifd.fBitsPerSample [0] < 1)
2672 {
2673 return false;
2674 }
2675
2676 for (uint32 j = 1; j < Min_uint32 (ifd.fSamplesPerPixel,
2677 kMaxSamplesPerPixel); j++)
2678 {
2679
2680 if (ifd.fBitsPerSample [j] !=
2681 ifd.fBitsPerSample [0])
2682 {
2683 return false;
2684 }
2685
2686 if (ifd.fSampleFormat [j] !=
2687 ifd.fSampleFormat [0])
2688 {
2689 return false;
2690 }
2691
2692 }
2693
2694 if ((ifd.fPlanarConfiguration != pcInterleaved ) &&
2695 (ifd.fPlanarConfiguration != pcPlanar ) &&
2696 (ifd.fPlanarConfiguration != pcRowInterleaved))
2697 {
2698 return false;
2699 }
2700
2701 if (ifd.fUsesStrips == ifd.fUsesTiles)
2702 {
2703 return false;
2704 }
2705
2706 uint32 tileCount = ifd.TilesPerImage ();
2707
2708 if (tileCount < 1)
2709 {
2710 return false;
2711 }
2712
2713 bool needTileByteCounts = (ifd.TileByteCount (ifd.TileArea (0, 0)) == 0);
2714
2715 if (tileCount == 1)
2716 {
2717
2718 if (needTileByteCounts)
2719 {
2720
2721 if (ifd.fTileByteCount [0] < 1)
2722 {
2723 return false;
2724 }
2725
2726 }
2727
2728 }
2729
2730 else
2731 {
2732
2733 if (ifd.fTileOffsetsCount != tileCount)
2734 {
2735 return false;
2736 }
2737
2738 if (needTileByteCounts)
2739 {
2740
2741 if (ifd.fTileByteCountsCount != tileCount)
2742 {
2743 return false;
2744 }
2745
2746 }
2747
2748 }
2749
2750 if (!CanReadTile (ifd))
2751 {
2752 return false;
2753 }
2754
2755 return true;
2756
2757 }
2758
2759 /*****************************************************************************/
2760
2761 dng_read_tiles_task::dng_read_tiles_task (dng_read_image &readImage,
2762 dng_host &host,
2763 const dng_ifd &ifd,
2764 dng_stream &stream,
2765 dng_image &image,
2766 dng_lossy_compressed_image *lossyImage,
2767 dng_fingerprint *lossyTileDigest,
2768 uint32 outerSamples,
2769 uint32 innerSamples,
2770 uint32 tilesDown,
2771 uint32 tilesAcross,
2772 uint64 *tileOffset,
2773 uint32 *tileByteCount,
2774 uint32 compressedSize,
2775 uint32 uncompressedSize)
2776
2777 : dng_area_task ("dng_read_tiles_task")
2778
2779 , fReadImage (readImage)
2780 , fHost (host)
2781 , fIFD (ifd)
2782 , fStream (stream)
2783 , fImage (image)
2784 , fLossyImage (lossyImage)
2785 , fLossyTileDigest (lossyTileDigest)
2786 , fOuterSamples (outerSamples)
2787 , fInnerSamples (innerSamples)
2788 , fTilesDown (tilesDown)
2789 , fTilesAcross (tilesAcross)
2790 , fTileOffset (tileOffset)
2791 , fTileByteCount (tileByteCount)
2792 , fCompressedSize (compressedSize)
2793 , fUncompressedSize (uncompressedSize)
2794 , fMutex ("dng_read_tiles_task")
2795 , fNextTileIndex (0)
2796
2797 {
2798
2799 fMinTaskArea = 16 * 16;
2800 fUnitCell = dng_point (16, 16);
2801 fMaxTileSize = dng_point (16, 16);
2802
2803 }
2804
2805 /*****************************************************************************/
2806
2807 void dng_read_tiles_task::Process (uint32 /* threadIndex */,
2808 const dng_rect & /* tile */,
2809 dng_abort_sniffer *sniffer)
2810 {
2811
2812 std::shared_ptr<dng_memory_block> compressedBuffer;
2813
2814 AutoPtr<dng_memory_block> uncompressedBuffer;
2815 AutoPtr<dng_memory_block> subTileBlockBuffer;
2816
2817 if (!fLossyImage)
2818 {
2819 compressedBuffer.reset (fHost.Allocate (fCompressedSize));
2820 }
2821
2822 if (fUncompressedSize)
2823 {
2824 uncompressedBuffer.Reset (fHost.Allocate (fUncompressedSize));
2825 }
2826
2827 while (true)
2828 {
2829
2830 uint32 tileIndex;
2831 uint32 byteCount;
2832
2833 {
2834
2835 dng_lock_mutex lock (&fMutex);
2836
2837 if (fNextTileIndex == fOuterSamples * fTilesDown * fTilesAcross)
2838 {
2839 return;
2840 }
2841
2842 tileIndex = fNextTileIndex++;
2843
2844 ReadTask (tileIndex,
2845 byteCount,
2846 compressedBuffer.get ());
2847
2848 }
2849
2850 ProcessTask (tileIndex,
2851 byteCount,
2852 sniffer,
2853 compressedBuffer,
2854 uncompressedBuffer,
2855 subTileBlockBuffer);
2856
2857 }
2858
2859 }
2860
2861 /*****************************************************************************/
2862
2863 void dng_read_tiles_task::ReadTask (uint32 tileIndex,
2864 uint32 &byteCount,
2865 dng_memory_block *compressedBuffer)
2866 {
2867
2868 TempStreamSniffer noSniffer (fStream, NULL);
2869
2870 fStream.SetReadPosition (fTileOffset [tileIndex]);
2871
2872 byteCount = fTileByteCount [tileIndex];
2873
2874 if (fLossyImage)
2875 {
2876
2877 fLossyImage->fData [tileIndex] . reset (fHost.Allocate (byteCount));
2878
2879 }
2880
2881 fStream.Get (fLossyImage ? fLossyImage->fData [tileIndex]->Buffer ()
2882 : compressedBuffer->Buffer (),
2883 byteCount);
2884
2885 }
2886
2887 /*****************************************************************************/
2888
2889 void dng_read_tiles_task::ProcessTask (uint32 tileIndex,
2890 uint32 byteCount,
2891 dng_abort_sniffer *sniffer,
2892 std::shared_ptr<dng_memory_block> &compressedBuffer,
2893 AutoPtr<dng_memory_block> &uncompressedBuffer,
2894 AutoPtr<dng_memory_block> &subTileBlockBuffer)
2895 {
2896
2897 dng_abort_sniffer::SniffForAbort (sniffer);
2898
2899 if (fLossyTileDigest)
2900 {
2901
2902 dng_md5_printer printer;
2903
2904 printer.Process (compressedBuffer->Buffer (),
2905 byteCount);
2906
2907 fLossyTileDigest [tileIndex] = printer.Result ();
2908
2909 }
2910
2911 dng_stream tileStream (fLossyImage ? fLossyImage->fData [tileIndex]->Buffer ()
2912 : compressedBuffer->Buffer (),
2913 byteCount);
2914
2915 tileStream.SetLittleEndian (fStream.LittleEndian ());
2916
2917 uint32 plane = tileIndex / (fTilesDown * fTilesAcross);
2918
2919 uint32 rowIndex = (tileIndex - plane * fTilesDown * fTilesAcross) / fTilesAcross;
2920
2921 uint32 colIndex = tileIndex - (plane * fTilesDown + rowIndex) * fTilesAcross;
2922
2923 dng_rect tileArea = fIFD.TileArea (rowIndex, colIndex);
2924
2925 dng_host host (&fHost.Allocator (),
2926 sniffer); // Cannot use sniffer attached to main host
2927
2928 fReadImage.ReadTile (host,
2929 fIFD,
2930 tileStream,
2931 fImage,
2932 tileArea,
2933 plane,
2934 fInnerSamples,
2935 byteCount,
2936 fLossyImage ? fLossyImage->fData [tileIndex]
2937 : compressedBuffer,
2938 uncompressedBuffer,
2939 subTileBlockBuffer,
2940 true);
2941
2942 }
2943
2944 /*****************************************************************************/
2945
2946 class dng_interleave_task : public dng_area_task
2947 , private dng_uncopyable
2948 {
2949
2950 public:
2951
2952 const dng_image &fSrcImage;
2953 dng_image &fDstImage;
2954
2955 int32 fRowFactor = 1;
2956 int32 fColFactor = 1;
2957
2958 bool fEncode = false;
2959
2960 private:
2961
2962 enum
2963 {
2964 kMaxThreads = 4
2965 };
2966
2967 AutoPtr<dng_memory_block> fSrcBuffer [kMaxThreads];
2968 AutoPtr<dng_memory_block> fDstBuffer [kMaxThreads];
2969
2970 public:
2971
2972 dng_interleave_task (const dng_image &srcImage,
2973 dng_image &dstImage,
2974 const int32 rowFactor,
2975 const int32 colFactor,
2976 bool encode)
2977
2978 : dng_area_task ("dng_interleave_task")
2979
2980 , fSrcImage (srcImage)
2981 , fDstImage (dstImage)
2982 , fRowFactor (rowFactor)
2983 , fColFactor (colFactor)
2984 , fEncode (encode)
2985
2986 {
2987
2988 if (fRowFactor >= (int32) fSrcImage.Bounds ().H ())
2989 {
2990 fRowFactor = 1;
2991 }
2992
2993 if (fColFactor >= (int32) fSrcImage.Bounds ().W ())
2994 {
2995 fColFactor = 1;
2996 }
2997
2998 fMaxThreads = kMaxThreads;
2999
3000 fMaxTileSize = dng_point (512, 512);
3001
3002 }
3003
3004 dng_rect RepeatingTile1 () const override
3005 {
3006 return fDstImage.RepeatingTile ();
3007 }
3008
3009 void Start (uint32 threadCount,
3010 const dng_rect &dstArea,
3011 const dng_point &tileSize,
3012 dng_memory_allocator *allocator,
3013 dng_abort_sniffer *sniffer) override;
3014
3015 void Process (uint32 threadIndex,
3016 const dng_rect &tile,
3017 dng_abort_sniffer *sniffer) override;
3018
3019 };
3020
3021 /*****************************************************************************/
3022
3023 void dng_interleave_task::Start (uint32 threadCount,
3024 const dng_rect & /* dstArea */,
3025 const dng_point &tileSize,
3026 dng_memory_allocator *allocator,
3027 dng_abort_sniffer * /* sniffer */)
3028 {
3029
3030 uint32 srcBufferSize = ((tileSize.h + fColFactor - 1) / fColFactor) *
3031 ((tileSize.v + fRowFactor - 1) / fRowFactor) *
3032 fDstImage.PixelSize () *
3033 fDstImage.Planes ();
3034
3035 uint32 dstBufferSize = tileSize.h *
3036 tileSize.v *
3037 fDstImage.PixelSize () *
3038 fDstImage.Planes ();
3039
3040 for (uint32 threadIndex = 0; threadIndex < threadCount; threadIndex++)
3041 {
3042
3043 fSrcBuffer [threadIndex].Reset (allocator->Allocate (srcBufferSize));
3044
3045 fDstBuffer [threadIndex].Reset (allocator->Allocate (dstBufferSize));
3046
3047 }
3048
3049 }
3050
3051 /*****************************************************************************/
3052
3053 void dng_interleave_task::Process (uint32 threadIndex,
3054 const dng_rect &tile,
3055 dng_abort_sniffer * /* sniffer */)
3056 {
3057
3058 dng_pixel_buffer dstBuffer;
3059
3060 dstBuffer.fArea = tile;
3061 dstBuffer.fPlane = 0;
3062 dstBuffer.fPlanes = fDstImage.Planes ();
3063 dstBuffer.fPlaneStep = 1;
3064 dstBuffer.fColStep = dstBuffer.fPlaneStep * dstBuffer.fPlanes;
3065 dstBuffer.fRowStep = dstBuffer.fColStep * dstBuffer.fArea.W ();
3066 dstBuffer.fPixelType = fDstImage.PixelType ();
3067 dstBuffer.fPixelSize = fDstImage.PixelSize ();
3068 dstBuffer.fData = fDstBuffer [threadIndex]->Buffer ();
3069 dstBuffer.fDirty = true;
3070
3071 dng_pixel_buffer srcBuffer = dstBuffer;
3072
3073 srcBuffer.fData = fSrcBuffer [threadIndex]->Buffer ();
3074
3075 if (fEncode)
3076 {
3077 fSrcImage.Get (dstBuffer);
3078 }
3079
3080 for (int32 rOffset = 0; rOffset < Min_int32 (fRowFactor, tile.H ()); rOffset++)
3081 {
3082
3083 for (int32 cOffset = 0; cOffset < Min_int32 (fColFactor, tile.W ()); cOffset++)
3084 {
3085
3086 int32 rField = (tile.t + rOffset) % fRowFactor;
3087 int32 cField = (tile.l + cOffset) % fColFactor;
3088
3089 int32 rFieldOffset = rField * (fDstImage.Height () / fRowFactor) +
3090 Min_int32 (rField, fDstImage.Height () % fRowFactor);
3091
3092 int32 cFieldOffset = cField * (fDstImage.Width () / fColFactor) +
3093 Min_int32 (cField, fDstImage.Width () % fColFactor);
3094
3095 srcBuffer.fArea.t = rFieldOffset + (tile.t + rOffset) / fRowFactor;
3096 srcBuffer.fArea.l = cFieldOffset + (tile.l + cOffset) / fColFactor;
3097
3098 srcBuffer.fArea.b = srcBuffer.fArea.t + (tile.H () - rOffset + fRowFactor - 1) / fRowFactor;
3099 srcBuffer.fArea.r = srcBuffer.fArea.l + (tile.W () - cOffset + fColFactor - 1) / fColFactor;
3100
3101 srcBuffer.fRowStep = srcBuffer.fColStep * srcBuffer.fArea.W ();
3102
3103 if (!fEncode)
3104 {
3105 fSrcImage.Get (srcBuffer);
3106 }
3107
3108 dng_pixel_buffer tmpBuffer = dstBuffer;
3109
3110 tmpBuffer.fArea = srcBuffer.fArea;
3111
3112 tmpBuffer.fData = dstBuffer.DirtyPixel (tile.t + rOffset,
3113 tile.l + cOffset);
3114
3115 tmpBuffer.fRowStep *= fRowFactor;
3116 tmpBuffer.fColStep *= fColFactor;
3117
3118 if (fEncode)
3119 {
3120 srcBuffer.CopyArea (tmpBuffer,
3121 tmpBuffer.fArea,
3122 tmpBuffer.fPlane,
3123 tmpBuffer.fPlanes);
3124 }
3125 else
3126 {
3127 tmpBuffer.CopyArea (srcBuffer,
3128 srcBuffer.fArea,
3129 srcBuffer.fPlane,
3130 srcBuffer.fPlanes);
3131 }
3132
3133 if (fEncode)
3134 {
3135 fDstImage.Put (srcBuffer);
3136 }
3137
3138 }
3139
3140 }
3141
3142 if (!fEncode)
3143 {
3144 fDstImage.Put (dstBuffer);
3145 }
3146
3147 }
3148
3149 /*****************************************************************************/
3150
3151 void Interleave2D (dng_host &host,
3152 const dng_image &srcImage,
3153 dng_image &dstImage,
3154 const int32 rowFactor,
3155 const int32 colFactor,
3156 bool encode)
3157 {
3158
3159 #if qDNGValidate
3160 dng_timer timer ("Interleave2D");
3161 #endif
3162
3163 DNG_REQUIRE (srcImage.Bounds () == dstImage.Bounds () &&
3164 srcImage.Planes () == dstImage.Planes () &&
3165 srcImage.PixelType () == dstImage.PixelType (),
3166 "Mismatched src and dst in Interleave2D");
3167
3168 dng_interleave_task task (srcImage,
3169 dstImage,
3170 rowFactor,
3171 colFactor,
3172 encode);
3173
3174 host.PerformAreaTask (task, dstImage.Bounds ());
3175
3176 }
3177
3178 /*****************************************************************************/
3179
3180 void dng_read_image::Read (dng_host &host,
3181 const dng_ifd &ifd,
3182 dng_stream &stream,
3183 dng_image &image,
3184 dng_lossy_compressed_image *lossyImage,
3185 dng_fingerprint *lossyDigest)
3186 {
3187
3188 // Reject images that are too big.
3189
3190 // Allow up to 2 * kMaxImageSide to deal with intermediate image objects
3191 // (e.g., rotated and padded).
3192
3193 static const uint32 kLimit = 2 * kMaxImageSide;
3194
3195 if (ifd.fImageWidth > kLimit ||
3196 ifd.fImageLength > kLimit)
3197 {
3198
3199 ThrowBadFormat ("dng_read_image::Read image too large");
3200
3201 }
3202
3203 uint32 tileIndex;
3204
3205 // Deal with both images that have row or column interleaving.
3206
3207 if (ifd.fRowInterleaveFactor > 1 ||
3208 ifd.fColumnInterleaveFactor > 1)
3209 {
3210
3211 // First, read into a full interleaved temporary image.
3212
3213 AutoPtr<dng_image> tempImage (host.Make_dng_image (image.Bounds (),
3214 image.Planes (),
3215 image.PixelType ()));
3216
3217 dng_ifd tempIFD (ifd);
3218
3219 tempIFD.fRowInterleaveFactor = 1;
3220 tempIFD.fColumnInterleaveFactor = 1;
3221
3222 Read (host,
3223 tempIFD,
3224 stream,
3225 *tempImage,
3226 lossyImage,
3227 lossyDigest);
3228
3229 // Interleave both rows and columns.
3230
3231 Interleave2D (host,
3232 *tempImage,
3233 image,
3234 ifd.fRowInterleaveFactor,
3235 ifd.fColumnInterleaveFactor,
3236 false);
3237
3238 return;
3239
3240 }
3241
3242 // Figure out inner and outer samples.
3243
3244 uint32 innerSamples = 1;
3245 uint32 outerSamples = 1;
3246
3247 if (ifd.fPlanarConfiguration == pcPlanar)
3248 {
3249 outerSamples = ifd.fSamplesPerPixel;
3250 }
3251 else
3252 {
3253 innerSamples = ifd.fSamplesPerPixel;
3254 }
3255
3256 // Calculate number of tiles to read.
3257
3258 uint32 tilesAcross = ifd.TilesAcross ();
3259 uint32 tilesDown = ifd.TilesDown ();
3260
3261 uint32 tileCount = SafeUint32Mult (tilesAcross, tilesDown, outerSamples);
3262
3263 // Find the tile offsets.
3264
3265 dng_memory_data tileOffsetData (tileCount, sizeof (uint64));
3266
3267 uint64 *tileOffset = tileOffsetData.Buffer_uint64 ();
3268
3269 if (tileCount <= dng_ifd::kMaxTileInfo)
3270 {
3271
3272 for (tileIndex = 0; tileIndex < tileCount; tileIndex++)
3273 {
3274
3275 tileOffset [tileIndex] = ifd.fTileOffset [tileIndex];
3276
3277 }
3278
3279 }
3280
3281 else
3282 {
3283
3284 stream.SetReadPosition (ifd.fTileOffsetsOffset);
3285
3286 for (tileIndex = 0; tileIndex < tileCount; tileIndex++)
3287 {
3288
3289 tileOffset [tileIndex] = stream.TagValue_uint64 (ifd.fTileOffsetsType);
3290
3291 }
3292
3293 }
3294
3295 // Quick validity check on tile offsets.
3296
3297 for (tileIndex = 0; tileIndex < tileCount; tileIndex++)
3298 {
3299
3300 #if qDNGValidate
3301
3302 if (tileOffset [tileIndex] < 8)
3303 {
3304
3305 ReportWarning ("Tile/Strip offset less than 8");
3306
3307 }
3308
3309 #endif
3310
3311 if (tileOffset [tileIndex] >= stream.Length ())
3312 {
3313
3314 ThrowBadFormat ();
3315
3316 }
3317
3318 }
3319
3320 // Buffer to hold the tile byte counts, if needed.
3321
3322 dng_memory_data tileByteCountData;
3323
3324 uint32 *tileByteCount = NULL;
3325
3326 // If we can compute the number of bytes needed to store the
3327 // data, we can split the read for each tile into sub-tiles.
3328
3329 uint32 uncompressedSize = 0;
3330
3331 uint32 subTileLength = ifd.fTileLength;
3332
3333 if (ifd.TileByteCount (ifd.TileArea (0, 0)) != 0)
3334 {
3335
3336 uint32 bytesPerPixel = TagTypeSize (ifd.PixelType ());
3337
3338 uint32 bytesPerRow = SafeUint32Mult (ifd.fTileWidth,
3339 innerSamples,
3340 bytesPerPixel);
3341
3342 subTileLength = Pin_uint32 (ifd.fSubTileBlockRows,
3343 kImageBufferSize / bytesPerRow,
3344 ifd.fTileLength);
3345
3346 subTileLength = subTileLength / ifd.fSubTileBlockRows
3347 * ifd.fSubTileBlockRows;
3348
3349 uncompressedSize = SafeUint32Mult (subTileLength, bytesPerRow);
3350
3351 }
3352
3353 // Else we need to know the byte counts.
3354
3355 else
3356 {
3357
3358 tileByteCountData.Allocate (tileCount, sizeof (uint32));
3359
3360 tileByteCount = tileByteCountData.Buffer_uint32 ();
3361
3362 if (tileCount <= dng_ifd::kMaxTileInfo)
3363 {
3364
3365 for (tileIndex = 0; tileIndex < tileCount; tileIndex++)
3366 {
3367
3368 if (ifd.fTileByteCount [tileIndex] > 0xFFFFFFFF)
3369 {
3370 ThrowBadFormat ();
3371 }
3372
3373 tileByteCount [tileIndex] = (uint32) ifd.fTileByteCount [tileIndex];
3374
3375 }
3376
3377 }
3378
3379 else
3380 {
3381
3382 stream.SetReadPosition (ifd.fTileByteCountsOffset);
3383
3384 for (tileIndex = 0; tileIndex < tileCount; tileIndex++)
3385 {
3386
3387 uint64 tileByteCount64 = stream.TagValue_uint64 (ifd.fTileByteCountsType);
3388
3389 if (tileByteCount64 > 0xFFFFFFFF)
3390 {
3391 ThrowBadFormat ();
3392 }
3393
3394 tileByteCount [tileIndex] = (uint32) tileByteCount64;
3395
3396 }
3397
3398 }
3399
3400 // Quick validity check on tile byte counts.
3401
3402 for (tileIndex = 0; tileIndex < tileCount; tileIndex++)
3403 {
3404
3405 if (tileByteCount [tileIndex] < 1 ||
3406 tileByteCount [tileIndex] > stream.Length ())
3407 {
3408
3409 ThrowBadFormat ();
3410
3411 }
3412
3413 }
3414
3415 }
3416
3417 // Find maximum tile size, if possible.
3418
3419 uint32 maxTileByteCount = 0;
3420
3421 if (tileByteCount)
3422 {
3423
3424 for (tileIndex = 0; tileIndex < tileCount; tileIndex++)
3425 {
3426
3427 maxTileByteCount = Max_uint32 (maxTileByteCount,
3428 tileByteCount [tileIndex]);
3429
3430 }
3431
3432 }
3433
3434 // Do we need a compressed data buffer?
3435
3436 uint32 compressedSize = 0;
3437
3438 bool needsCompressedBuffer = NeedsCompressedBuffer (ifd);
3439
3440 if (needsCompressedBuffer)
3441 {
3442
3443 if (!tileByteCount)
3444 {
3445 ThrowBadFormat ();
3446 }
3447
3448 compressedSize = maxTileByteCount;
3449
3450 }
3451
3452 // Are we keeping the lossy compressed image data?
3453
3454 if (lossyImage)
3455 {
3456
3457 if (ifd.IsBaselineJPEG () ||
3458 (ifd.fCompression == ccJXL))
3459 {
3460
3461 lossyImage->fImageSize.h = ifd.fImageWidth;
3462 lossyImage->fImageSize.v = ifd.fImageLength;
3463
3464 lossyImage->fTileSize.h = ifd.fTileWidth;
3465 lossyImage->fTileSize.v = ifd.fTileLength;
3466
3467 lossyImage->fUsesStrips = ifd.fUsesStrips;
3468
3469 lossyImage->fData.resize (tileCount);
3470
3471 }
3472
3473 else
3474 {
3475
3476 lossyImage = NULL;
3477
3478 }
3479
3480 }
3481
3482 // Do we need to read the JPEG tables?
3483
3484 if (ifd.fJPEGTablesOffset && ifd.fJPEGTablesCount)
3485 {
3486
3487 if (ifd.IsBaselineJPEG ())
3488 {
3489
3490 fJPEGTables.Reset (host.Allocate (ifd.fJPEGTablesCount));
3491
3492 stream.SetReadPosition (ifd.fJPEGTablesOffset);
3493
3494 stream.Get (fJPEGTables->Buffer (),
3495 fJPEGTables->LogicalSize ());
3496
3497 }
3498
3499 }
3500
3501 std::vector<dng_fingerprint> lossyTileDigests;
3502
3503 if (lossyDigest)
3504 lossyTileDigests.resize (tileCount);
3505
3506 // Don't read planes we are not actually saving.
3507
3508 outerSamples = Min_uint32 (image.Planes (), outerSamples);
3509
3510 // Performance optimization. We are reading in a potentially large image,
3511 // which is usually in a fairly contiguous byte range in the file. See if
3512 // it makes sense to increase the stream buffer size for this operation.
3513
3514 uint64 contiguousByteCount = 0;
3515
3516 {
3517
3518 bool tilesInOrder = true;
3519
3520 uint64 totalTileBytes = 0;
3521
3522 uint64 minFileOffset = tileOffset [0];
3523 uint64 maxFileOffset = tileOffset [0];
3524
3525 tileIndex = 0;
3526
3527 for (uint32 plane = 0; plane < outerSamples; plane++)
3528 {
3529
3530 for (uint32 rowIndex = 0; rowIndex < tilesDown; rowIndex++)
3531 {
3532
3533 for (uint32 colIndex = 0; colIndex < tilesAcross; colIndex++)
3534 {
3535
3536 uint64 thisOffset = tileOffset [tileIndex];
3537
3538 uint64 thisByteCount;
3539
3540 if (tileByteCount)
3541 {
3542
3543 thisByteCount = tileByteCount [tileIndex];
3544
3545 }
3546
3547 else
3548 {
3549
3550 thisByteCount = ifd.TileByteCount (ifd.TileArea (rowIndex, colIndex));
3551
3552 }
3553
3554 if (thisOffset < maxFileOffset)
3555 {
3556 tilesInOrder = false;
3557 }
3558
3559 totalTileBytes += thisByteCount;
3560
3561 minFileOffset = Min_uint64 (minFileOffset, thisOffset);
3562 maxFileOffset = Max_uint64 (maxFileOffset, thisOffset + thisByteCount);
3563
3564 tileIndex++;
3565
3566 }
3567
3568 }
3569
3570 }
3571
3572 // Quick check for enough data in file.
3573
3574 if (maxFileOffset > stream.Length ())
3575 {
3576
3577 ThrowBadFormat ();
3578
3579 }
3580
3581 // Are all the tiles in order?
3582
3583 if (tilesInOrder)
3584 {
3585
3586 // And are going to read at least 90% of the bytes in the range?
3587
3588 uint64 totalFileBytes = maxFileOffset - minFileOffset;
3589
3590 if (totalTileBytes >= totalFileBytes * 9 / 10)
3591 {
3592
3593 contiguousByteCount = totalFileBytes;
3594
3595 }
3596
3597 }
3598
3599 }
3600
3601 dng_stream_contiguous_read_hint readHint (stream,
3602 host.Allocator (),
3603 tileOffset [0],
3604 contiguousByteCount);
3605
3606 // See if we can do this read using multiple threads.
3607
3608 bool useMultipleThreads = (outerSamples * tilesDown * tilesAcross >= 2) &&
3609 (host.PerformAreaTaskThreads () > 1) &&
3610 (maxTileByteCount > 0 && maxTileByteCount <= 1024 * 1024) &&
3611 (subTileLength == ifd.fTileLength) &&
3612 (ifd.fCompression != ccUncompressed);
3613
3614 if (useMultipleThreads)
3615 {
3616
3617 DoReadTiles (host,
3618 ifd,
3619 stream,
3620 image,
3621 lossyImage,
3622 lossyDigest ? &lossyTileDigests [0] : nullptr,
3623 outerSamples,
3624 innerSamples,
3625 tilesDown,
3626 tilesAcross,
3627 tileOffset,
3628 tileByteCount,
3629 maxTileByteCount,
3630 uncompressedSize);
3631
3632 }
3633
3634 // Else use a single thread to read all the tiles.
3635
3636 else
3637 {
3638
3639 std::shared_ptr<dng_memory_block> compressedBuffer;
3640 AutoPtr<dng_memory_block> uncompressedBuffer;
3641 AutoPtr<dng_memory_block> subTileBlockBuffer;
3642
3643 if (uncompressedSize)
3644 {
3645 uncompressedBuffer.Reset (host.Allocate (uncompressedSize));
3646 }
3647
3648 if (compressedSize && !lossyImage)
3649 {
3650 compressedBuffer.reset (host.Allocate (compressedSize));
3651 }
3652
3653 else if (lossyDigest)
3654 {
3655 compressedBuffer.reset (host.Allocate (maxTileByteCount));
3656 }
3657
3658 tileIndex = 0;
3659
3660 for (uint32 plane = 0; plane < outerSamples; plane++)
3661 {
3662
3663 for (uint32 rowIndex = 0; rowIndex < tilesDown; rowIndex++)
3664 {
3665
3666 for (uint32 colIndex = 0; colIndex < tilesAcross; colIndex++)
3667 {
3668
3669 stream.SetReadPosition (tileOffset [tileIndex]);
3670
3671 dng_rect tileArea = ifd.TileArea (rowIndex, colIndex);
3672
3673 uint32 subTileCount = (tileArea.H () + subTileLength - 1) /
3674 subTileLength;
3675
3676 for (uint32 subIndex = 0; subIndex < subTileCount; subIndex++)
3677 {
3678
3679 host.SniffForAbort ();
3680
3681 dng_rect subArea (tileArea);
3682
3683 subArea.t = tileArea.t + subIndex * subTileLength;
3684
3685 subArea.b = Min_int32 (subArea.t + subTileLength,
3686 tileArea.b);
3687
3688 uint32 subByteCount;
3689
3690 if (tileByteCount)
3691 {
3692 subByteCount = tileByteCount [tileIndex];
3693 }
3694 else
3695 {
3696 subByteCount = ifd.TileByteCount (subArea);
3697 }
3698
3699 if (lossyImage)
3700 {
3701
3702 lossyImage->fData [tileIndex].reset (host.Allocate (subByteCount));
3703
3704 stream.Get (lossyImage->fData [tileIndex]->Buffer (), subByteCount);
3705
3706 stream.SetReadPosition (tileOffset [tileIndex]);
3707
3708 }
3709
3710 else if ((needsCompressedBuffer || lossyDigest) && subByteCount)
3711 {
3712
3713 stream.Get (compressedBuffer->Buffer (), subByteCount);
3714
3715 if (lossyDigest)
3716 {
3717
3718 dng_md5_printer printer;
3719
3720 printer.Process (compressedBuffer->Buffer (),
3721 subByteCount);
3722
3723 lossyTileDigests [tileIndex] = printer.Result ();
3724
3725 }
3726
3727 }
3728
3729 ReadTile (host,
3730 ifd,
3731 stream,
3732 image,
3733 subArea,
3734 plane,
3735 innerSamples,
3736 subByteCount,
3737 lossyImage ? lossyImage->fData [tileIndex] : compressedBuffer,
3738 uncompressedBuffer,
3739 subTileBlockBuffer,
3740 useMultipleThreads);
3741
3742 }
3743
3744 tileIndex++;
3745
3746 }
3747
3748 }
3749
3750 }
3751
3752 }
3753
3754 // Finish up JPEG digest computation, if needed.
3755
3756 if (lossyDigest)
3757 {
3758
3759 if (fJPEGTables.Get ())
3760 {
3761
3762 dng_md5_printer printer;
3763
3764 printer.Process (fJPEGTables->Buffer (),
3765 fJPEGTables->LogicalSize ());
3766
3767 lossyTileDigests.push_back (printer.Result ());
3768
3769 }
3770
3771 dng_md5_printer printer2;
3772
3773 for (const auto &digest : lossyTileDigests)
3774 printer2.Process (digest.data,
3775 uint32 (sizeof (digest.data)));
3776
3777 *lossyDigest = printer2.Result ();
3778
3779 }
3780
3781 // Keep the JPEG table in the jpeg image, if any.
3782
3783 if (lossyImage && fJPEGTables.Get ())
3784 ((dng_jpeg_image *) lossyImage)->fJPEGTables.Reset (fJPEGTables.Release ());
3785
3786 }
3787
3788 /*****************************************************************************/
3789
3790 void dng_read_image::DoReadTiles (dng_host &host,
3791 const dng_ifd &ifd,
3792 dng_stream &stream,
3793 dng_image &image,
3794 dng_lossy_compressed_image *lossyImage,
3795 dng_fingerprint *lossyTileDigest,
3796 uint32 outerSamples,
3797 uint32 innerSamples,
3798 uint32 tilesDown,
3799 uint32 tilesAcross,
3800 uint64 *tileOffset,
3801 uint32 *tileByteCount,
3802 uint32 compressedSize,
3803 uint32 uncompressedSize)
3804 {
3805
3806 uint32 threadCount = Min_uint32 (outerSamples * tilesDown * tilesAcross,
3807 host.PerformAreaTaskThreads ());
3808
3809 dng_read_tiles_task task (*this,
3810 host,
3811 ifd,
3812 stream,
3813 image,
3814 lossyImage,
3815 lossyTileDigest,
3816 outerSamples,
3817 innerSamples,
3818 tilesDown,
3819 tilesAcross,
3820 tileOffset,
3821 tileByteCount,
3822 compressedSize,
3823 uncompressedSize);
3824
3825 host.PerformAreaTask (task,
3826 dng_rect (0, 0, 16, 16 * threadCount));
3827
3828 }
3829
3830 /*****************************************************************************/