Debian Codegraph @

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

raw
1 /*****************************************************************************/
2 // Copyright 2008-2019 Adobe Systems Incorporated
3 // All Rights Reserved.
4 //
5 // NOTICE: Adobe permits you to use, modify, and distribute this file in
6 // accordance with the terms of the Adobe license agreement accompanying it.
7 /*****************************************************************************/
8
9 #include "dng_misc_opcodes.h"
10
11 #include "dng_bottlenecks.h"
12 #include "dng_exceptions.h"
13 #include "dng_globals.h"
14 #include "dng_host.h"
15 #include "dng_image.h"
16 #include "dng_negative.h"
17 #include "dng_rect.h"
18 #include "dng_safe_arithmetic.h"
19 #include "dng_stream.h"
20 #include "dng_tag_values.h"
21
22 /*****************************************************************************/
23
24 dng_opcode_TrimBounds::dng_opcode_TrimBounds (const dng_rect &bounds)
25
26 : dng_opcode (dngOpcode_TrimBounds,
27 dngVersion_1_3_0_0,
28 kFlag_None)
29
30 , fBounds (bounds)
31
32 {
33
34 }
35
36 /*****************************************************************************/
37
38 dng_opcode_TrimBounds::dng_opcode_TrimBounds (dng_stream &stream)
39
40 : dng_opcode (dngOpcode_TrimBounds,
41 stream,
42 "TrimBounds")
43
44 , fBounds ()
45
46 {
47
48 if (stream.Get_uint32 () != 16)
49 {
50 ThrowBadFormat ();
51 }
52
53 fBounds.t = stream.Get_int32 ();
54 fBounds.l = stream.Get_int32 ();
55 fBounds.b = stream.Get_int32 ();
56 fBounds.r = stream.Get_int32 ();
57
58 if (fBounds.IsEmpty ())
59 {
60 ThrowBadFormat ();
61 }
62
63 #if qDNGValidate
64
65 if (gVerbose)
66 {
67
68 printf ("Bounds: t=%d, l=%d, b=%d, r=%d\n",
69 (int) fBounds.t,
70 (int) fBounds.l,
71 (int) fBounds.b,
72 (int) fBounds.r);
73
74 }
75
76 #endif
77
78 }
79
80 /*****************************************************************************/
81
82 void dng_opcode_TrimBounds::PutData (dng_stream &stream) const
83 {
84
85 stream.Put_uint32 (16);
86
87 stream.Put_int32 (fBounds.t);
88 stream.Put_int32 (fBounds.l);
89 stream.Put_int32 (fBounds.b);
90 stream.Put_int32 (fBounds.r);
91
92 }
93
94 /*****************************************************************************/
95
96 void dng_opcode_TrimBounds::Apply (dng_host & /* host */,
97 dng_negative & /* negative */,
98 AutoPtr<dng_image> &image)
99 {
100
101 if (fBounds.IsEmpty () || (fBounds & image->Bounds ()) != fBounds)
102 {
103 ThrowBadFormat ();
104 }
105
106 image->Trim (fBounds);
107
108 }
109
110 /*****************************************************************************/
111
112 void dng_area_spec::GetData (dng_stream &stream)
113 {
114
115 fArea.t = stream.Get_int32 ();
116 fArea.l = stream.Get_int32 ();
117 fArea.b = stream.Get_int32 ();
118 fArea.r = stream.Get_int32 ();
119
120 fPlane = stream.Get_uint32 ();
121 fPlanes = stream.Get_uint32 ();
122
123 fRowPitch = stream.Get_uint32 ();
124 fColPitch = stream.Get_uint32 ();
125
126 if (fPlanes < 1)
127 {
128 ThrowBadFormat ();
129 }
130
131 if (fRowPitch < 1 || fColPitch < 1)
132 {
133 ThrowBadFormat ();
134 }
135
136 if (fRowPitch >= fArea.H () || fColPitch >= fArea.W ())
137 {
138
139 DNG_REPORT ("Bad rowPitch or colPitch in dng_area_spec::GetData");
140
141 fRowPitch = Min_uint32 (fRowPitch, fArea.H ());
142 fColPitch = Min_uint32 (fColPitch, fArea.W ());
143
144 }
145
146 if (fArea.IsEmpty ())
147 {
148
149 if (fRowPitch != 1 || fColPitch != 1)
150 {
151 ThrowBadFormat ();
152 }
153
154 }
155
156 else
157 {
158
159 int32 width = 0;
160 int32 height = 0;
161
162 if (!SafeInt32Sub (fArea.b, fArea.t, &height) ||
163 !SafeInt32Sub (fArea.r, fArea.l, &width) ||
164 fRowPitch > static_cast<uint32> (height) ||
165 fColPitch > static_cast<uint32> (width))
166 {
167 ThrowBadFormat ();
168 }
169
170 }
171
172 #if qDNGValidate
173
174 if (gVerbose)
175 {
176
177 printf ("AreaSpec: t=%d, l=%d, b=%d, r=%d, p=%u:%u, rp=%u, cp=%u\n",
178 (int) fArea.t,
179 (int) fArea.l,
180 (int) fArea.b,
181 (int) fArea.r,
182 (unsigned) fPlane,
183 (unsigned) fPlanes,
184 (unsigned) fRowPitch,
185 (unsigned) fColPitch);
186
187 }
188
189 #endif
190
191 }
192
193 /*****************************************************************************/
194
195 void dng_area_spec::PutData (dng_stream &stream) const
196 {
197
198 stream.Put_int32 (fArea.t);
199 stream.Put_int32 (fArea.l);
200 stream.Put_int32 (fArea.b);
201 stream.Put_int32 (fArea.r);
202
203 stream.Put_uint32 (fPlane);
204 stream.Put_uint32 (fPlanes);
205
206 stream.Put_uint32 (fRowPitch);
207 stream.Put_uint32 (fColPitch);
208
209 }
210
211 /*****************************************************************************/
212
213 dng_rect dng_area_spec::Overlap (const dng_rect &tile) const
214 {
215
216 // Special case - if the fArea is empty, then dng_area_spec covers
217 // the entire image, no matter how large it is.
218
219 if (fArea.IsEmpty ())
220 {
221 return tile;
222 }
223
224 dng_rect overlap = fArea & tile;
225
226 if (overlap.NotEmpty ())
227 {
228
229 overlap.t = fArea.t +
230 ConvertUint32ToInt32 (RoundUpUint32ToMultiple
231 (static_cast<uint32> (overlap.t - fArea.t),
232 fRowPitch));
233
234 overlap.l = fArea.l +
235 ConvertUint32ToInt32 (RoundUpUint32ToMultiple
236 (static_cast<uint32> (overlap.l - fArea.l),
237 fColPitch));
238
239 if (overlap.NotEmpty ())
240 {
241
242 overlap.b = overlap.t + ((overlap.H () - 1) / fRowPitch) * fRowPitch + 1;
243 overlap.r = overlap.l + ((overlap.W () - 1) / fColPitch) * fColPitch + 1;
244
245 return overlap;
246
247 }
248
249 }
250
251 return dng_rect ();
252
253 }
254
255 /*****************************************************************************/
256
257 dng_rect dng_area_spec::ScaledOverlap (const dng_rect &tile) const
258 {
259
260 // Special case - if the fArea is empty, then dng_area_spec covers
261 // the entire image, no matter how large it is.
262
263 if (fArea.IsEmpty ())
264 {
265 return tile;
266 }
267
268 dng_rect area = ScaledArea (); // Use ScaledArea instead of fArea
269
270 dng_rect overlap = area & tile;
271
272 if (overlap.NotEmpty ())
273 {
274
275 overlap.t = area.t + ((overlap.t - area.t + fRowPitch - 1) / fRowPitch) * fRowPitch;
276 overlap.l = area.l + ((overlap.l - area.l + fColPitch - 1) / fColPitch) * fColPitch;
277
278 if (overlap.NotEmpty ())
279 {
280
281 overlap.b = overlap.t + ((overlap.H () - 1) / fRowPitch) * fRowPitch + 1;
282 overlap.r = overlap.l + ((overlap.W () - 1) / fColPitch) * fColPitch + 1;
283
284 return overlap;
285
286 }
287
288 }
289
290 return dng_rect ();
291
292 }
293
294 /*****************************************************************************/
295
296 dng_rect dng_area_spec::ScaledArea () const
297 {
298
299 if (fAreaScale.n == fAreaScale.d)
300 {
301 return fArea;
302 }
303
304 dng_rect area = fArea;
305
306 real64 x = fAreaScale.As_real64 ();
307
308 area.t = Round_int32 (x * area.t);
309 area.l = Round_int32 (x * area.l);
310 area.b = Round_int32 (x * area.b);
311 area.r = Round_int32 (x * area.r);
312
313 return area;
314
315 }
316
317 /*****************************************************************************/
318
319 void dng_area_spec::ApplyAreaScale (const dng_urational &scale)
320 {
321
322 DNG_REQUIRE (scale.n > 0 &&
323 scale.d > 0,
324 "invalid scale in dng_area_spec::Scale");
325
326 fAreaScale = scale;
327
328 }
329
330 /*****************************************************************************/
331
332 dng_opcode_MapTable::dng_opcode_MapTable (dng_host &host,
333 const dng_area_spec &areaSpec,
334 const uint16 *table,
335 uint32 count)
336
337 : dng_inplace_opcode (dngOpcode_MapTable,
338 dngVersion_1_3_0_0,
339 kFlag_None)
340
341 , fAreaSpec (areaSpec)
342 , fTable ()
343 , fCount (count)
344
345 , fBlackAdjustedTable ()
346
347 {
348
349 if (count == 0 || count > 0x10000)
350 {
351 ThrowProgramError ();
352 }
353
354 fTable.Reset (host.Allocate (0x10000 * sizeof (uint16)));
355
356 DoCopyBytes (table,
357 fTable->Buffer (),
358 count * (uint32) sizeof (uint16));
359
360 ReplicateLastEntry ();
361
362 }
363
364 /*****************************************************************************/
365
366 dng_opcode_MapTable::dng_opcode_MapTable (dng_host &host,
367 dng_stream &stream)
368
369 : dng_inplace_opcode (dngOpcode_MapTable,
370 stream,
371 "MapTable")
372
373 , fAreaSpec ()
374 , fTable ()
375 , fCount (0)
376
377 , fBlackAdjustedTable ()
378
379 {
380
381 uint32 dataSize = stream.Get_uint32 ();
382
383 fAreaSpec.GetData (stream);
384
385 fCount = stream.Get_uint32 ();
386
387 uint32 requiredSize = SafeUint32Mult (fCount, 2);
388
389 requiredSize = SafeUint32Add (requiredSize, dng_area_spec::kDataSize);
390 requiredSize = SafeUint32Add (requiredSize, 4);
391
392 if (dataSize != requiredSize)
393 {
394 ThrowBadFormat ();
395 }
396
397 if (fCount == 0 || fCount > 0x10000)
398 {
399 ThrowBadFormat ();
400 }
401
402 fTable.Reset (host.Allocate (0x10000 * sizeof (uint16)));
403
404 uint16 *table = fTable->Buffer_uint16 ();
405
406 for (uint32 index = 0; index < fCount; index++)
407 {
408 table [index] = stream.Get_uint16 ();
409 }
410
411 ReplicateLastEntry ();
412
413 #if qDNGValidate
414
415 if (gVerbose)
416 {
417
418 printf ("Count: %u\n", (unsigned) fCount);
419
420 for (uint32 j = 0; j < fCount && j < gDumpLineLimit; j++)
421 {
422 printf ("\tTable [%5u] = %5u\n", (unsigned) j, (unsigned) table [j]);
423 }
424
425 if (fCount > gDumpLineLimit)
426 {
427 printf ("\t ... %u table entries skipped\n", (unsigned) (fCount - gDumpLineLimit));
428 }
429
430 }
431
432 #endif
433
434 }
435
436 /*****************************************************************************/
437
438 void dng_opcode_MapTable::ReplicateLastEntry ()
439 {
440
441 uint16 *table = fTable->Buffer_uint16 ();
442
443 uint16 lastEntry = table [fCount];
444
445 for (uint32 index = fCount; index < 0x10000; index++)
446 {
447 table [index] = lastEntry;
448 }
449
450 }
451
452 /*****************************************************************************/
453
454 void dng_opcode_MapTable::PutData (dng_stream &stream) const
455 {
456
457 stream.Put_uint32 (dng_area_spec::kDataSize + 4 + fCount * 2);
458
459 fAreaSpec.PutData (stream);
460
461 stream.Put_uint32 (fCount);
462
463 uint16 *table = fTable->Buffer_uint16 ();
464
465 for (uint32 index = 0; index < fCount; index++)
466 {
467 stream.Put_uint16 (table [index]);
468 }
469
470 }
471
472 /*****************************************************************************/
473
474 uint32 dng_opcode_MapTable::BufferPixelType (uint32 /* imagePixelType */)
475 {
476
477 return ttShort;
478
479 }
480
481 /*****************************************************************************/
482
483 dng_rect dng_opcode_MapTable::ModifiedBounds (const dng_rect &imageBounds)
484 {
485
486 return fAreaSpec.ScaledOverlap (imageBounds);
487
488 }
489
490 /*****************************************************************************/
491
492 void dng_opcode_MapTable::Prepare (dng_negative &negative,
493 uint32 /* threadCount */,
494 const dng_point & /* tileSize */,
495 const dng_rect & /* imageBounds */,
496 uint32 /* imagePlanes */,
497 uint32 /* bufferPixelType */,
498 dng_memory_allocator &allocator)
499 {
500
501 fBlackAdjustedTable.Reset ();
502
503 int32 blackLevel = negative.Stage3BlackLevel ();
504
505 if (Stage () >= 2 && blackLevel != 0)
506 {
507
508 fBlackAdjustedTable.Reset (allocator.Allocate (0x10000 * sizeof (uint16)));
509
510 const uint16 *srcTable = fTable->Buffer_uint16 ();
511
512 uint16 *dstTable = fBlackAdjustedTable->Buffer_uint16 ();
513
514 real64 srcScale = 65535.0 / (65535.0 - blackLevel);
515
516 real64 dstScale = (65535.0 - blackLevel) / 65535.0;
517
518 for (int32 index = 0; index < 0x10000; index++)
519 {
520
521 real64 x = (index - blackLevel) * srcScale;
522
523 real64 y;
524
525 if (x < 0.0)
526 {
527
528 y = srcTable [0] * 2.0 - (real64) srcTable [Round_uint32 (-x)];
529
530 }
531
532 else
533 {
534
535 y = srcTable [Round_uint32 (x)];
536
537 }
538
539 dstTable [index] = Pin_uint16 (Round_int32 (y * dstScale) + blackLevel);
540
541 }
542
543 }
544
545 }
546
547 /*****************************************************************************/
548
549 void dng_opcode_MapTable::ProcessArea (dng_negative & /* negative */,
550 uint32 /* threadIndex */,
551 dng_pixel_buffer &buffer,
552 const dng_rect &dstArea,
553 const dng_rect & /* imageBounds */)
554 {
555
556 dng_rect overlap = fAreaSpec.ScaledOverlap (dstArea);
557
558 if (overlap.NotEmpty ())
559 {
560
561 const uint16 *table = fBlackAdjustedTable.Get () ? fBlackAdjustedTable->Buffer_uint16 ()
562 : fTable ->Buffer_uint16 ();
563
564 for (uint32 plane = fAreaSpec.Plane ();
565 plane < fAreaSpec.Plane () + fAreaSpec.Planes () &&
566 plane < buffer.Planes ();
567 plane++)
568 {
569
570 DoMapArea16 (buffer.DirtyPixel_uint16 (overlap.t, overlap.l, plane),
571 1,
572 (overlap.H () + fAreaSpec.RowPitch () - 1) / fAreaSpec.RowPitch (),
573 (overlap.W () + fAreaSpec.ColPitch () - 1) / fAreaSpec.ColPitch (),
574 0,
575 fAreaSpec.RowPitch () * buffer.RowStep (),
576 fAreaSpec.ColPitch (),
577 table);
578
579 }
580
581 }
582
583 }
584
585 /*****************************************************************************/
586
587 dng_opcode_MapPolynomial::dng_opcode_MapPolynomial (const dng_area_spec &areaSpec,
588 uint32 degree,
589 const real64 *coefficient)
590
591 : dng_inplace_opcode (dngOpcode_MapPolynomial,
592 dngVersion_1_3_0_0,
593 kFlag_None)
594
595 , fAreaSpec (areaSpec)
596 , fDegree (degree)
597
598 {
599
600 for (uint32 j = 0; j <= kMaxDegree; j++)
601 {
602
603 if (j <= fDegree)
604 {
605 fCoefficient [j] = coefficient [j];
606 }
607
608 else
609 {
610 fCoefficient [j] = 0.0;
611 }
612
613 }
614
615 // Reduce degree if possible.
616
617 while (fDegree > 0 && fCoefficient [fDegree] == 0.0)
618 {
619 fDegree--;
620 }
621
622 }
623
624 /*****************************************************************************/
625
626 dng_opcode_MapPolynomial::dng_opcode_MapPolynomial (dng_stream &stream)
627
628 : dng_inplace_opcode (dngOpcode_MapPolynomial,
629 stream,
630 "MapPolynomial")
631
632 , fAreaSpec ()
633 , fDegree (0)
634
635 {
636
637 uint32 dataSize = stream.Get_uint32 ();
638
639 fAreaSpec.GetData (stream);
640
641 fDegree = stream.Get_uint32 ();
642
643 if (dataSize != dng_area_spec::kDataSize + 4 + (fDegree + 1) * 8)
644 {
645 ThrowBadFormat ();
646 }
647
648 if (fDegree > kMaxDegree)
649 {
650 ThrowBadFormat ();
651 }
652
653 for (uint32 j = 0; j <= kMaxDegree; j++)
654 {
655
656 if (j <= fDegree)
657 {
658 fCoefficient [j] = stream.Get_real64 ();
659 }
660 else
661 {
662 fCoefficient [j] = 0.0;
663 }
664
665 }
666
667 #if qDNGValidate
668
669 if (gVerbose)
670 {
671
672 for (uint32 k = 0; k <= fDegree; k++)
673 {
674 printf ("\tCoefficient [%u] = %f\n", (unsigned) k, fCoefficient [k]);
675 }
676
677 }
678
679 #endif
680
681 }
682
683 /*****************************************************************************/
684
685 void dng_opcode_MapPolynomial::PutData (dng_stream &stream) const
686 {
687
688 stream.Put_uint32 (dng_area_spec::kDataSize + 4 + (fDegree + 1) * 8);
689
690 fAreaSpec.PutData (stream);
691
692 stream.Put_uint32 (fDegree);
693
694 for (uint32 j = 0; j <= fDegree; j++)
695 {
696 stream.Put_real64 (fCoefficient [j]);
697 }
698
699 }
700
701 /*****************************************************************************/
702
703 uint32 dng_opcode_MapPolynomial::BufferPixelType (uint32 imagePixelType)
704 {
705
706 // If we are operating on the stage 1 image, then we need
707 // to adjust the coefficients to convert from the image
708 // values to the 32-bit floating point values that this
709 // opcode operates on.
710
711 // If we are operating on the stage 2 or 3 image, the logical
712 // range of the image is already 0.0 to 1.0, so we don't
713 // need to adjust the values.
714
715 real64 scale32 = 1.0;
716
717 if (Stage () == 1)
718 {
719
720 switch (imagePixelType)
721 {
722
723 case ttFloat:
724 break;
725
726 case ttShort:
727 {
728 scale32 = (real64) 0xFFFF;
729 break;
730 }
731
732 case ttLong:
733 {
734 scale32 = (real64) 0xFFFFFFFF;
735 break;
736 }
737
738 default:
739 ThrowBadFormat ();
740
741 }
742
743 }
744
745 real64 factor32 = 1.0 / scale32;
746
747 for (uint32 j = 0; j <= kMaxDegree; j++)
748 {
749
750 fCoefficient32 [j] = (real32) (fCoefficient [j] * factor32);
751
752 factor32 *= scale32;
753
754 }
755
756 return ttFloat;
757
758 }
759
760 /*****************************************************************************/
761
762 dng_rect dng_opcode_MapPolynomial::ModifiedBounds (const dng_rect &imageBounds)
763 {
764
765 return fAreaSpec.ScaledOverlap (imageBounds);
766
767 }
768
769 /*****************************************************************************/
770
771 void dng_opcode_MapPolynomial::ProcessArea (dng_negative &negative,
772 uint32 /* threadIndex */,
773 dng_pixel_buffer &buffer,
774 const dng_rect &dstArea,
775 const dng_rect & /* imageBounds */)
776 {
777
778 dng_rect overlap = fAreaSpec.ScaledOverlap (dstArea);
779
780 if (overlap.NotEmpty ())
781 {
782
783 uint16 blackLevel = Stage () >= 2 ? negative.Stage3BlackLevel () : 0;
784
785 uint32 rowPitch = fAreaSpec.RowPitch ();
786 uint32 colPitch = fAreaSpec.ColPitch ();
787
788 for (uint32 plane = fAreaSpec.Plane ();
789 plane < fAreaSpec.Plane () + fAreaSpec.Planes () &&
790 plane < buffer.Planes ();
791 plane++)
792 {
793
794 DoProcess (buffer,
795 overlap,
796 plane,
797 rowPitch,
798 colPitch,
799 fCoefficient32,
800 fDegree,
801 blackLevel);
802
803 }
804
805 }
806
807 }
808
809 /*****************************************************************************/
810
811 void dng_opcode_MapPolynomial::DoProcess (dng_pixel_buffer &buffer,
812 const dng_rect &area,
813 const uint32 plane,
814 const uint32 rowPitch,
815 const uint32 colPitch,
816 const real32 *coefficients,
817 const uint32 degree,
818 uint16 blackLevel) const
819 {
820
821 DoBaselineMapPoly32 (buffer.DirtyPixel_real32 (area.t,
822 area.l,
823 plane),
824 buffer.RowStep () * (int32) rowPitch,
825 area.H (),
826 area.W (),
827 rowPitch,
828 colPitch,
829 coefficients,
830 degree,
831 blackLevel);
832
833 }
834
835 /*****************************************************************************/
836
837 dng_opcode_DeltaPerRow::dng_opcode_DeltaPerRow (const dng_area_spec &areaSpec,
838 AutoPtr<dng_memory_block> &table)
839
840 : dng_inplace_opcode (dngOpcode_DeltaPerRow,
841 dngVersion_1_3_0_0,
842 kFlag_None)
843
844 , fAreaSpec (areaSpec)
845 , fTable ()
846 , fScale (1.0f)
847
848 {
849
850 fTable.Reset (table.Release ());
851
852 }
853
854 /*****************************************************************************/
855
856 dng_opcode_DeltaPerRow::dng_opcode_DeltaPerRow (dng_host &host,
857 dng_stream &stream)
858
859 : dng_inplace_opcode (dngOpcode_DeltaPerRow,
860 stream,
861 "DeltaPerRow")
862
863 , fAreaSpec ()
864 , fTable ()
865 , fScale (1.0f)
866
867 {
868
869 uint32 dataSize = stream.Get_uint32 ();
870
871 fAreaSpec.GetData (stream);
872
873 uint32 deltas = SafeUint32DivideUp (fAreaSpec.Area ().H (),
874 fAreaSpec.RowPitch ());
875
876 if (deltas != stream.Get_uint32 ())
877 {
878 ThrowBadFormat ();
879 }
880
881 if (dataSize != dng_area_spec::kDataSize + 4 + deltas * 4)
882 {
883 ThrowBadFormat ();
884 }
885
886 fTable.Reset (host.Allocate
887 (SafeUint32Mult (deltas,
888 static_cast<uint32> (sizeof (real32)))));
889
890 real32 *table = fTable->Buffer_real32 ();
891
892 for (uint32 j = 0; j < deltas; j++)
893 {
894 table [j] = stream.Get_real32 ();
895 }
896
897 #if qDNGValidate
898
899 if (gVerbose)
900 {
901
902 printf ("Count: %u\n", (unsigned) deltas);
903
904 for (uint32 k = 0; k < deltas && k < gDumpLineLimit; k++)
905 {
906 printf ("\tDelta [%u] = %f\n", (unsigned) k, table [k]);
907 }
908
909 if (deltas > gDumpLineLimit)
910 {
911 printf ("\t ... %u deltas skipped\n", (unsigned) (deltas - gDumpLineLimit));
912 }
913
914 }
915
916 #endif
917
918 }
919
920 /*****************************************************************************/
921
922 void dng_opcode_DeltaPerRow::PutData (dng_stream &stream) const
923 {
924
925 uint32 deltas = SafeUint32DivideUp (fAreaSpec.Area ().H (),
926 fAreaSpec.RowPitch ());
927
928 stream.Put_uint32 (dng_area_spec::kDataSize + 4 + deltas * 4);
929
930 fAreaSpec.PutData (stream);
931
932 stream.Put_uint32 (deltas);
933
934 real32 *table = fTable->Buffer_real32 ();
935
936 for (uint32 j = 0; j < deltas; j++)
937 {
938 stream.Put_real32 (table [j]);
939 }
940
941 }
942
943 /*****************************************************************************/
944
945 uint32 dng_opcode_DeltaPerRow::BufferPixelType (uint32 imagePixelType)
946 {
947
948 real64 scale32 = 1.0;
949
950 switch (imagePixelType)
951 {
952
953 case ttFloat:
954 break;
955
956 case ttShort:
957 {
958 scale32 = (real64) 0xFFFF;
959 break;
960 }
961
962 case ttLong:
963 {
964 scale32 = (real64) 0xFFFFFFFF;
965 break;
966 }
967
968 default:
969 ThrowBadFormat ();
970
971 }
972
973 fScale = (real32) (1.0 / scale32);
974
975 return ttFloat;
976
977 }
978
979 /*****************************************************************************/
980
981 dng_rect dng_opcode_DeltaPerRow::ModifiedBounds (const dng_rect &imageBounds)
982 {
983
984 return fAreaSpec.Overlap (imageBounds);
985
986 }
987
988 /*****************************************************************************/
989
990 void dng_opcode_DeltaPerRow::ProcessArea (dng_negative &negative,
991 uint32 /* threadIndex */,
992 dng_pixel_buffer &buffer,
993 const dng_rect &dstArea,
994 const dng_rect & /* imageBounds */)
995 {
996
997 dng_rect overlap = fAreaSpec.Overlap (dstArea);
998
999 if (overlap.NotEmpty ())
1000 {
1001
1002 uint32 cols = overlap.W ();
1003
1004 uint32 colPitch = fAreaSpec.ColPitch ();
1005
1006 real32 scale = fScale;
1007
1008 if (Stage () >= 2 && negative.Stage3BlackLevel () != 0)
1009 {
1010 scale *= (real32) (1.0 - negative.Stage3BlackLevelNormalized ());
1011 }
1012
1013 for (uint32 plane = fAreaSpec.Plane ();
1014 plane < fAreaSpec.Plane () + fAreaSpec.Planes () &&
1015 plane < buffer.Planes ();
1016 plane++)
1017 {
1018
1019 const real32 *table = fTable->Buffer_real32 () +
1020 ((overlap.t - fAreaSpec.Area ().t) /
1021 fAreaSpec.RowPitch ());
1022
1023 for (int32 row = overlap.t; row < overlap.b; row += fAreaSpec.RowPitch ())
1024 {
1025
1026 real32 rowDelta = *(table++) * scale;
1027
1028 real32 *dPtr = buffer.DirtyPixel_real32 (row, overlap.l, plane);
1029
1030 for (uint32 col = 0; col < cols; col += colPitch)
1031 {
1032
1033 real32 x = dPtr [col];
1034
1035 real32 y = x + rowDelta;
1036
1037 dPtr [col] = Pin_real32 (-1.0f, y, 1.0f);
1038
1039 }
1040
1041 }
1042
1043 }
1044
1045 }
1046
1047 }
1048
1049 /*****************************************************************************/
1050
1051 dng_opcode_DeltaPerColumn::dng_opcode_DeltaPerColumn (const dng_area_spec &areaSpec,
1052 AutoPtr<dng_memory_block> &table)
1053
1054 : dng_inplace_opcode (dngOpcode_DeltaPerColumn,
1055 dngVersion_1_3_0_0,
1056 kFlag_None)
1057
1058 , fAreaSpec (areaSpec)
1059 , fTable ()
1060 , fScale (1.0f)
1061
1062 {
1063
1064 fTable.Reset (table.Release ());
1065
1066 }
1067
1068 /*****************************************************************************/
1069
1070 dng_opcode_DeltaPerColumn::dng_opcode_DeltaPerColumn (dng_host &host,
1071 dng_stream &stream)
1072
1073 : dng_inplace_opcode (dngOpcode_DeltaPerColumn,
1074 stream,
1075 "DeltaPerColumn")
1076
1077 , fAreaSpec ()
1078 , fTable ()
1079 , fScale (1.0f)
1080
1081 {
1082
1083 uint32 dataSize = stream.Get_uint32 ();
1084
1085 fAreaSpec.GetData (stream);
1086
1087 uint32 deltas = SafeUint32DivideUp (fAreaSpec.Area ().W (),
1088 fAreaSpec.ColPitch ());
1089
1090 if (deltas != stream.Get_uint32 ())
1091 {
1092 ThrowBadFormat ();
1093 }
1094
1095 if (dataSize != dng_area_spec::kDataSize + 4 + deltas * 4)
1096 {
1097 ThrowBadFormat ();
1098 }
1099
1100 fTable.Reset (host.Allocate
1101 (SafeUint32Mult (deltas,
1102 static_cast<uint32> (sizeof (real32)))));
1103
1104 real32 *table = fTable->Buffer_real32 ();
1105
1106 for (uint32 j = 0; j < deltas; j++)
1107 {
1108 table [j] = stream.Get_real32 ();
1109 }
1110
1111 #if qDNGValidate
1112
1113 if (gVerbose)
1114 {
1115
1116 printf ("Count: %u\n", (unsigned) deltas);
1117
1118 for (uint32 k = 0; k < deltas && k < gDumpLineLimit; k++)
1119 {
1120 printf ("\tDelta [%u] = %f\n", (unsigned) k, table [k]);
1121 }
1122
1123 if (deltas > gDumpLineLimit)
1124 {
1125 printf ("\t ... %u deltas skipped\n", (unsigned) (deltas - gDumpLineLimit));
1126 }
1127
1128 }
1129
1130 #endif
1131
1132 }
1133
1134 /*****************************************************************************/
1135
1136 void dng_opcode_DeltaPerColumn::PutData (dng_stream &stream) const
1137 {
1138
1139 uint32 deltas = SafeUint32DivideUp (fAreaSpec.Area ().W (),
1140 fAreaSpec.ColPitch ());
1141
1142 stream.Put_uint32 (dng_area_spec::kDataSize + 4 + deltas * 4);
1143
1144 fAreaSpec.PutData (stream);
1145
1146 stream.Put_uint32 (deltas);
1147
1148 real32 *table = fTable->Buffer_real32 ();
1149
1150 for (uint32 j = 0; j < deltas; j++)
1151 {
1152 stream.Put_real32 (table [j]);
1153 }
1154
1155 }
1156
1157 /*****************************************************************************/
1158
1159 uint32 dng_opcode_DeltaPerColumn::BufferPixelType (uint32 imagePixelType)
1160 {
1161
1162 real64 scale32 = 1.0;
1163
1164 switch (imagePixelType)
1165 {
1166
1167 case ttFloat:
1168 break;
1169
1170 case ttShort:
1171 {
1172 scale32 = (real64) 0xFFFF;
1173 break;
1174 }
1175
1176 case ttLong:
1177 {
1178 scale32 = (real64) 0xFFFFFFFF;
1179 break;
1180 }
1181
1182 default:
1183 ThrowBadFormat ();
1184
1185 }
1186
1187 fScale = (real32) (1.0 / scale32);
1188
1189 return ttFloat;
1190
1191 }
1192
1193 /*****************************************************************************/
1194
1195 dng_rect dng_opcode_DeltaPerColumn::ModifiedBounds (const dng_rect &imageBounds)
1196 {
1197
1198 return fAreaSpec.Overlap (imageBounds);
1199
1200 }
1201
1202 /*****************************************************************************/
1203
1204 void dng_opcode_DeltaPerColumn::ProcessArea (dng_negative &negative,
1205 uint32 /* threadIndex */,
1206 dng_pixel_buffer &buffer,
1207 const dng_rect &dstArea,
1208 const dng_rect & /* imageBounds */)
1209 {
1210
1211 dng_rect overlap = fAreaSpec.Overlap (dstArea);
1212
1213 if (overlap.NotEmpty ())
1214 {
1215
1216 uint32 rows = (overlap.H () + fAreaSpec.RowPitch () - 1) /
1217 fAreaSpec.RowPitch ();
1218
1219 int32 rowStep = buffer.RowStep () * fAreaSpec.RowPitch ();
1220
1221 real32 scale = fScale;
1222
1223 if (Stage () >= 2 && negative.Stage3BlackLevel () != 0)
1224 {
1225 scale *= (real32) (1.0 - negative.Stage3BlackLevelNormalized ());
1226 }
1227
1228 for (uint32 plane = fAreaSpec.Plane ();
1229 plane < fAreaSpec.Plane () + fAreaSpec.Planes () &&
1230 plane < buffer.Planes ();
1231 plane++)
1232 {
1233
1234 const real32 *table = fTable->Buffer_real32 () +
1235 ((overlap.l - fAreaSpec.Area ().l) /
1236 fAreaSpec.ColPitch ());
1237
1238 for (int32 col = overlap.l; col < overlap.r; col += fAreaSpec.ColPitch ())
1239 {
1240
1241 real32 colDelta = *(table++) * scale;
1242
1243 real32 *dPtr = buffer.DirtyPixel_real32 (overlap.t, col, plane);
1244
1245 for (uint32 row = 0; row < rows; row++)
1246 {
1247
1248 real32 x = dPtr [0];
1249
1250 real32 y = x + colDelta;
1251
1252 dPtr [0] = Pin_real32 (-1.0f, y, 1.0f);
1253
1254 dPtr += rowStep;
1255
1256 }
1257
1258 }
1259
1260 }
1261
1262 }
1263
1264 }
1265
1266 /*****************************************************************************/
1267
1268 dng_opcode_ScalePerRow::dng_opcode_ScalePerRow (const dng_area_spec &areaSpec,
1269 AutoPtr<dng_memory_block> &table)
1270
1271 : dng_inplace_opcode (dngOpcode_ScalePerRow,
1272 dngVersion_1_3_0_0,
1273 kFlag_None)
1274
1275 , fAreaSpec (areaSpec)
1276 , fTable ()
1277
1278 {
1279
1280 fTable.Reset (table.Release ());
1281
1282 }
1283
1284 /*****************************************************************************/
1285
1286 dng_opcode_ScalePerRow::dng_opcode_ScalePerRow (dng_host &host,
1287 dng_stream &stream)
1288
1289 : dng_inplace_opcode (dngOpcode_ScalePerRow,
1290 stream,
1291 "ScalePerRow")
1292
1293 , fAreaSpec ()
1294 , fTable ()
1295
1296 {
1297
1298 uint32 dataSize = stream.Get_uint32 ();
1299
1300 fAreaSpec.GetData (stream);
1301
1302 uint32 scales = SafeUint32DivideUp (fAreaSpec.Area ().H (),
1303 fAreaSpec.RowPitch ());
1304
1305 if (scales != stream.Get_uint32 ())
1306 {
1307 ThrowBadFormat ();
1308 }
1309
1310 if (dataSize != dng_area_spec::kDataSize + 4 + scales * 4)
1311 {
1312 ThrowBadFormat ();
1313 }
1314
1315 fTable.Reset (host.Allocate
1316 (SafeUint32Mult (scales,
1317 static_cast<uint32> (sizeof (real32)))));
1318
1319 real32 *table = fTable->Buffer_real32 ();
1320
1321 for (uint32 j = 0; j < scales; j++)
1322 {
1323 table [j] = stream.Get_real32 ();
1324 }
1325
1326 #if qDNGValidate
1327
1328 if (gVerbose)
1329 {
1330
1331 printf ("Count: %u\n", (unsigned) scales);
1332
1333 for (uint32 k = 0; k < scales && k < gDumpLineLimit; k++)
1334 {
1335 printf ("\tScale [%u] = %f\n", (unsigned) k, table [k]);
1336 }
1337
1338 if (scales > gDumpLineLimit)
1339 {
1340 printf ("\t ... %u scales skipped\n", (unsigned) (scales - gDumpLineLimit));
1341 }
1342
1343 }
1344
1345 #endif
1346
1347 }
1348
1349 /*****************************************************************************/
1350
1351 void dng_opcode_ScalePerRow::PutData (dng_stream &stream) const
1352 {
1353
1354 uint32 scales = SafeUint32DivideUp (fAreaSpec.Area ().H (),
1355 fAreaSpec.RowPitch ());
1356
1357 stream.Put_uint32 (dng_area_spec::kDataSize + 4 + scales * 4);
1358
1359 fAreaSpec.PutData (stream);
1360
1361 stream.Put_uint32 (scales);
1362
1363 real32 *table = fTable->Buffer_real32 ();
1364
1365 for (uint32 j = 0; j < scales; j++)
1366 {
1367 stream.Put_real32 (table [j]);
1368 }
1369
1370 }
1371
1372 /*****************************************************************************/
1373
1374 uint32 dng_opcode_ScalePerRow::BufferPixelType (uint32 /* imagePixelType */)
1375 {
1376
1377 return ttFloat;
1378
1379 }
1380
1381 /*****************************************************************************/
1382
1383 dng_rect dng_opcode_ScalePerRow::ModifiedBounds (const dng_rect &imageBounds)
1384 {
1385
1386 return fAreaSpec.Overlap (imageBounds);
1387
1388 }
1389
1390 /*****************************************************************************/
1391
1392 void dng_opcode_ScalePerRow::ProcessArea (dng_negative &negative,
1393 uint32 /* threadIndex */,
1394 dng_pixel_buffer &buffer,
1395 const dng_rect &dstArea,
1396 const dng_rect & /* imageBounds */)
1397 {
1398
1399 dng_rect overlap = fAreaSpec.Overlap (dstArea);
1400
1401 if (overlap.NotEmpty ())
1402 {
1403
1404 uint32 cols = overlap.W ();
1405
1406 uint32 colPitch = fAreaSpec.ColPitch ();
1407
1408 real32 blackOffset = 0.0f;
1409
1410 if (Stage () >= 2 && negative.Stage3BlackLevel () != 0)
1411 {
1412 blackOffset = (real32) negative.Stage3BlackLevelNormalized ();
1413 }
1414
1415 for (uint32 plane = fAreaSpec.Plane ();
1416 plane < fAreaSpec.Plane () + fAreaSpec.Planes () &&
1417 plane < buffer.Planes ();
1418 plane++)
1419 {
1420
1421 const real32 *table = fTable->Buffer_real32 () +
1422 ((overlap.t - fAreaSpec.Area ().t) /
1423 fAreaSpec.RowPitch ());
1424
1425 for (int32 row = overlap.t; row < overlap.b; row += fAreaSpec.RowPitch ())
1426 {
1427
1428 real32 rowScale = *(table++);
1429
1430 real32 *dPtr = buffer.DirtyPixel_real32 (row, overlap.l, plane);
1431
1432 for (uint32 col = 0; col < cols; col += colPitch)
1433 {
1434
1435 real32 x = dPtr [col];
1436
1437 real32 y = (x - blackOffset) * rowScale + blackOffset;
1438
1439 dPtr [col] = Pin_real32 (-1.0f, y, 1.0f);
1440
1441 }
1442
1443 }
1444
1445 }
1446
1447 }
1448
1449 }
1450
1451 /*****************************************************************************/
1452
1453 dng_opcode_ScalePerColumn::dng_opcode_ScalePerColumn (const dng_area_spec &areaSpec,
1454 AutoPtr<dng_memory_block> &table)
1455
1456 : dng_inplace_opcode (dngOpcode_ScalePerColumn,
1457 dngVersion_1_3_0_0,
1458 kFlag_None)
1459
1460 , fAreaSpec (areaSpec)
1461 , fTable ()
1462
1463 {
1464
1465 fTable.Reset (table.Release ());
1466
1467 }
1468
1469 /*****************************************************************************/
1470
1471 dng_opcode_ScalePerColumn::dng_opcode_ScalePerColumn (dng_host &host,
1472 dng_stream &stream)
1473
1474 : dng_inplace_opcode (dngOpcode_ScalePerColumn,
1475 stream,
1476 "ScalePerColumn")
1477
1478 , fAreaSpec ()
1479 , fTable ()
1480
1481 {
1482
1483 uint32 dataSize = stream.Get_uint32 ();
1484
1485 fAreaSpec.GetData (stream);
1486
1487 uint32 scales = SafeUint32DivideUp (fAreaSpec.Area ().W (),
1488 fAreaSpec.ColPitch());
1489
1490 if (scales != stream.Get_uint32 ())
1491 {
1492 ThrowBadFormat ();
1493 }
1494
1495 if (dataSize != dng_area_spec::kDataSize + 4 + scales * 4)
1496 {
1497 ThrowBadFormat ();
1498 }
1499
1500 fTable.Reset (host.Allocate
1501 (SafeUint32Mult (scales,
1502 static_cast<uint32> (sizeof (real32)))));
1503
1504 real32 *table = fTable->Buffer_real32 ();
1505
1506 for (uint32 j = 0; j < scales; j++)
1507 {
1508 table [j] = stream.Get_real32 ();
1509 }
1510
1511 #if qDNGValidate
1512
1513 if (gVerbose)
1514 {
1515
1516 printf ("Count: %u\n", (unsigned) scales);
1517
1518 for (uint32 k = 0; k < scales && k < gDumpLineLimit; k++)
1519 {
1520 printf ("\tScale [%u] = %f\n", (unsigned) k, table [k]);
1521 }
1522
1523 if (scales > gDumpLineLimit)
1524 {
1525 printf ("\t ... %u deltas skipped\n", (unsigned) (scales - gDumpLineLimit));
1526 }
1527
1528 }
1529
1530 #endif
1531
1532 }
1533
1534 /*****************************************************************************/
1535
1536 void dng_opcode_ScalePerColumn::PutData (dng_stream &stream) const
1537 {
1538
1539 uint32 scales = SafeUint32DivideUp (fAreaSpec.Area ().W (),
1540 fAreaSpec.ColPitch ());
1541
1542 stream.Put_uint32 (dng_area_spec::kDataSize + 4 + scales * 4);
1543
1544 fAreaSpec.PutData (stream);
1545
1546 stream.Put_uint32 (scales);
1547
1548 real32 *table = fTable->Buffer_real32 ();
1549
1550 for (uint32 j = 0; j < scales; j++)
1551 {
1552 stream.Put_real32 (table [j]);
1553 }
1554
1555 }
1556
1557 /*****************************************************************************/
1558
1559 uint32 dng_opcode_ScalePerColumn::BufferPixelType (uint32 /* imagePixelType */)
1560 {
1561
1562 return ttFloat;
1563
1564 }
1565
1566 /*****************************************************************************/
1567
1568 dng_rect dng_opcode_ScalePerColumn::ModifiedBounds (const dng_rect &imageBounds)
1569 {
1570
1571 return fAreaSpec.Overlap (imageBounds);
1572
1573 }
1574
1575 /*****************************************************************************/
1576
1577 void dng_opcode_ScalePerColumn::ProcessArea (dng_negative &negative,
1578 uint32 /* threadIndex */,
1579 dng_pixel_buffer &buffer,
1580 const dng_rect &dstArea,
1581 const dng_rect & /* imageBounds */)
1582 {
1583
1584 dng_rect overlap = fAreaSpec.Overlap (dstArea);
1585
1586 if (overlap.NotEmpty ())
1587 {
1588
1589 uint32 rows = (overlap.H () + fAreaSpec.RowPitch () - 1) /
1590 fAreaSpec.RowPitch ();
1591
1592 int32 rowStep = buffer.RowStep () * fAreaSpec.RowPitch ();
1593
1594 real32 blackOffset = 0.0f;
1595
1596 if (Stage () >= 2 && negative.Stage3BlackLevel () != 0)
1597 {
1598 blackOffset = (real32) negative.Stage3BlackLevelNormalized ();
1599 }
1600
1601 for (uint32 plane = fAreaSpec.Plane ();
1602 plane < fAreaSpec.Plane () + fAreaSpec.Planes () &&
1603 plane < buffer.Planes ();
1604 plane++)
1605 {
1606
1607 const real32 *table = fTable->Buffer_real32 () +
1608 ((overlap.l - fAreaSpec.Area ().l) /
1609 fAreaSpec.ColPitch ());
1610
1611 for (int32 col = overlap.l; col < overlap.r; col += fAreaSpec.ColPitch ())
1612 {
1613
1614 real32 colScale = *(table++);
1615
1616 real32 *dPtr = buffer.DirtyPixel_real32 (overlap.t, col, plane);
1617
1618 for (uint32 row = 0; row < rows; row++)
1619 {
1620
1621 real32 x = dPtr [0];
1622
1623 real32 y = (x - blackOffset) * colScale + blackOffset;
1624
1625 dPtr [0] = Pin_real32 (-1.0f, y, 1.0f);
1626
1627 dPtr += rowStep;
1628
1629 }
1630
1631 }
1632
1633 }
1634
1635 }
1636
1637 }
1638
1639 /*****************************************************************************/