1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
|
/*****************************************************************************/
// Copyright 2006 Adobe Systems Incorporated
// All Rights Reserved.
//
// NOTICE: Adobe permits you to use, modify, and distribute this file in
// accordance with the terms of the Adobe license agreement accompanying it.
/*****************************************************************************/
/* $Id: //mondo/dng_sdk_1_4/dng_sdk/source/dng_resample.cpp#1 $ */
/* $DateTime: 2012/05/30 13:28:51 $ */
/* $Change: 832332 $ */
/* $Author: tknoll $ */
/*****************************************************************************/
#include "dng_resample.h"
#include "dng_assertions.h"
#include "dng_bottlenecks.h"
#include "dng_filter_task.h"
#include "dng_host.h"
#include "dng_image.h"
#include "dng_memory.h"
#include "dng_pixel_buffer.h"
#include "dng_tag_types.h"
#include "dng_utils.h"
/******************************************************************************/
real64 dng_resample_bicubic::Extent () const
{
return 2.0;
}
/******************************************************************************/
real64 dng_resample_bicubic::Evaluate (real64 x) const
{
const real64 A = -0.75;
x = Abs_real64 (x);
if (x >= 2.0)
return 0.0;
else if (x >= 1.0)
return (((A * x - 5.0 * A) * x + 8.0 * A) * x - 4.0 * A);
else
return (((A + 2.0) * x - (A + 3.0)) * x * x + 1.0);
}
/******************************************************************************/
const dng_resample_function & dng_resample_bicubic::Get ()
{
static dng_resample_bicubic static_dng_resample_bicubic;
return static_dng_resample_bicubic;
}
/*****************************************************************************/
dng_resample_coords::dng_resample_coords ()
: fOrigin (0)
, fCoords ()
{
}
/*****************************************************************************/
dng_resample_coords::~dng_resample_coords ()
{
}
/*****************************************************************************/
void dng_resample_coords::Initialize (int32 srcOrigin,
int32 dstOrigin,
uint32 srcCount,
uint32 dstCount,
dng_memory_allocator &allocator)
{
fOrigin = dstOrigin;
uint32 dstEntries = RoundUp8 (dstCount);
fCoords.Reset (allocator.Allocate (dstEntries * (uint32) sizeof (int32)));
int32 *coords = fCoords->Buffer_int32 ();
real64 invScale = (real64) srcCount /
(real64) dstCount;
for (uint32 j = 0; j < dstCount; j++)
{
real64 x = (real64) j + 0.5;
real64 y = x * invScale - 0.5 + (real64) srcOrigin;
coords [j] = Round_int32 (y * (real64) kResampleSubsampleCount);
}
// Pad out table by replicating last entry.
for (uint32 k = dstCount; k < dstEntries; k++)
{
coords [k] = coords [dstCount - 1];
}
}
/*****************************************************************************/
dng_resample_weights::dng_resample_weights ()
: fRadius (0)
, fWeightStep (0)
, fWeights32 ()
, fWeights16 ()
{
}
/*****************************************************************************/
dng_resample_weights::~dng_resample_weights ()
{
}
/*****************************************************************************/
void dng_resample_weights::Initialize (real64 scale,
const dng_resample_function &kernel,
dng_memory_allocator &allocator)
{
uint32 j;
// We only adjust the kernel size for scale factors less than 1.0.
scale = Min_real64 (scale, 1.0);
// Find radius of this kernel.
fRadius = (uint32) (kernel.Extent () / scale + 0.9999);
// Width is twice the radius.
uint32 width = fRadius * 2;
// Round to each set to weights to a multiple of 8 entries.
fWeightStep = RoundUp8 (width);
// Allocate and zero weight tables.
fWeights32.Reset (allocator.Allocate (fWeightStep *
kResampleSubsampleCount *
(uint32) sizeof (real32)));
DoZeroBytes (fWeights32->Buffer (),
fWeights32->LogicalSize ());
fWeights16.Reset (allocator.Allocate (fWeightStep *
kResampleSubsampleCount *
(uint32) sizeof (int16)));
DoZeroBytes (fWeights16->Buffer (),
fWeights16->LogicalSize ());
// Compute kernel for each subsample values.
for (uint32 sample = 0; sample < kResampleSubsampleCount; sample++)
{
real64 fract = sample * (1.0 / (real64) kResampleSubsampleCount);
real32 *w32 = fWeights32->Buffer_real32 () + fWeightStep * sample;
// Evaluate kernel function for 32 bit weights.
{
real64 t32 = 0.0;
for (j = 0; j < width; j++)
{
int32 k = j - fRadius + 1;
real64 x = (k - fract) * scale;
w32 [j] = (real32) kernel.Evaluate (x);
t32 += w32 [j];
}
// Scale 32 bit weights so total of weights is 1.0.
real32 s32 = (real32) (1.0 / t32);
for (j = 0; j < width; j++)
{
w32 [j] *= s32;
}
}
// Round off 32 bit weights to 16 bit weights.
{
int16 *w16 = fWeights16->Buffer_int16 () + fWeightStep * sample;
int32 t16 = 0;
for (j = 0; j < width; j++)
{
w16 [j] = (int16) Round_int32 (w32 [j] * 16384.0);
t16 += w16 [j];
}
// Adjust center entry for any round off error so total is
// exactly 16384.
w16 [fRadius - (fract >= 0.5 ? 0 : 1)] += (int16) (16384 - t16);
}
}
}
/*****************************************************************************/
dng_resample_weights_2d::dng_resample_weights_2d ()
: fRadius (0)
, fRowStep (0)
, fColStep (0)
, fWeights32 ()
, fWeights16 ()
{
}
/*****************************************************************************/
dng_resample_weights_2d::~dng_resample_weights_2d ()
{
}
/*****************************************************************************/
void dng_resample_weights_2d::Initialize (const dng_resample_function &kernel,
dng_memory_allocator &allocator)
{
// Find radius of this kernel. Unlike with 1d resample weights (see
// dng_resample_weights), we never scale up the kernel size.
fRadius = (uint32) (kernel.Extent () + 0.9999);
// Width is twice the radius.
const uint32 width = fRadius * 2;
const uint32 widthSqr = width * width;
const uint32 step = RoundUp8 (width * width);
fRowStep = step * kResampleSubsampleCount2D;
fColStep = step;
// Allocate and zero weight tables.
fWeights32.Reset (allocator.Allocate (step *
kResampleSubsampleCount2D *
kResampleSubsampleCount2D *
(uint32) sizeof (real32)));
DoZeroBytes (fWeights32->Buffer (),
fWeights32->LogicalSize ());
fWeights16.Reset (allocator.Allocate (step *
kResampleSubsampleCount2D *
kResampleSubsampleCount2D *
(uint32) sizeof (int16)));
DoZeroBytes (fWeights16->Buffer (),
fWeights16->LogicalSize ());
// Compute kernel for each subsample values.
for (uint32 y = 0; y < kResampleSubsampleCount2D; y++)
{
real64 yFract = y * (1.0 / (real64) kResampleSubsampleCount2D);
for (uint32 x = 0; x < kResampleSubsampleCount2D; x++)
{
real64 xFract = x * (1.0 / (real64) kResampleSubsampleCount2D);
real32 *w32 = (real32 *) Weights32 (dng_point ((int32) y,
(int32) x));
// Evaluate kernel function for 32 bit weights.
{
real64 t32 = 0.0;
uint32 index = 0;
for (uint32 i = 0; i < width; i++)
{
int32 yInt = ((int32) i) - fRadius + 1;
real64 yPos = yInt - yFract;
for (uint32 j = 0; j < width; j++)
{
int32 xInt = ((int32) j) - fRadius + 1;
real64 xPos = xInt - xFract;
#if 0
// Radial.
real64 dy2 = yPos * yPos;
real64 dx2 = xPos * xPos;
real64 r = sqrt (dx2 + dy2);
w32 [index] = (real32) kernel.Evaluate (r);
#else
// Separable.
w32 [index] = (real32) kernel.Evaluate (xPos) *
(real32) kernel.Evaluate (yPos);
#endif
t32 += w32 [index];
index++;
}
}
// Scale 32 bit weights so total of weights is 1.0.
const real32 s32 = (real32) (1.0 / t32);
for (uint32 i = 0; i < widthSqr; i++)
{
w32 [i] *= s32;
}
}
// Round off 32 bit weights to 16 bit weights.
{
int16 *w16 = (int16 *) Weights16 (dng_point ((int32) y,
(int32) x));
int32 t16 = 0;
for (uint32 j = 0; j < widthSqr; j++)
{
w16 [j] = (int16) Round_int32 (w32 [j] * 16384.0);
t16 += w16 [j];
}
// Adjust one of the center entries for any round off error so total
// is exactly 16384.
const uint32 xOffset = fRadius - ((xFract >= 0.5) ? 0 : 1);
const uint32 yOffset = fRadius - ((yFract >= 0.5) ? 0 : 1);
const uint32 centerOffset = width * yOffset + xOffset;
w16 [centerOffset] += (int16) (16384 - t16);
}
}
}
}
/*****************************************************************************/
class dng_resample_task: public dng_filter_task
{
protected:
dng_rect fSrcBounds;
dng_rect fDstBounds;
const dng_resample_function &fKernel;
real64 fRowScale;
real64 fColScale;
dng_resample_coords fRowCoords;
dng_resample_coords fColCoords;
dng_resample_weights fWeightsV;
dng_resample_weights fWeightsH;
dng_point fSrcTileSize;
AutoPtr<dng_memory_block> fTempBuffer [kMaxMPThreads];
public:
dng_resample_task (const dng_image &srcImage,
dng_image &dstImage,
const dng_rect &srcBounds,
const dng_rect &dstBounds,
const dng_resample_function &kernel);
virtual dng_rect SrcArea (const dng_rect &dstArea);
virtual dng_point SrcTileSize (const dng_point &dstTileSize);
virtual void Start (uint32 threadCount,
const dng_point &tileSize,
dng_memory_allocator *allocator,
dng_abort_sniffer *sniffer);
virtual void ProcessArea (uint32 threadIndex,
dng_pixel_buffer &srcBuffer,
dng_pixel_buffer &dstBuffer);
};
/*****************************************************************************/
dng_resample_task::dng_resample_task (const dng_image &srcImage,
dng_image &dstImage,
const dng_rect &srcBounds,
const dng_rect &dstBounds,
const dng_resample_function &kernel)
: dng_filter_task (srcImage,
dstImage)
, fSrcBounds (srcBounds)
, fDstBounds (dstBounds)
, fKernel (kernel)
, fRowScale (dstBounds.H () / (real64) srcBounds.H ())
, fColScale (dstBounds.W () / (real64) srcBounds.W ())
, fRowCoords ()
, fColCoords ()
, fWeightsV ()
, fWeightsH ()
, fSrcTileSize ()
{
if (srcImage.PixelSize () <= 2 &&
dstImage.PixelSize () <= 2 &&
srcImage.PixelRange () == dstImage.PixelRange ())
{
fSrcPixelType = ttShort;
fDstPixelType = ttShort;
}
else
{
fSrcPixelType = ttFloat;
fDstPixelType = ttFloat;
}
fUnitCell = dng_point (8, 8);
fMaxTileSize.v = Pin_int32 (fUnitCell.v,
Round_int32 (fMaxTileSize.v * fRowScale),
fMaxTileSize.v);
fMaxTileSize.h = Pin_int32 (fUnitCell.h,
Round_int32 (fMaxTileSize.h * fColScale),
fMaxTileSize.h);
}
/*****************************************************************************/
dng_rect dng_resample_task::SrcArea (const dng_rect &dstArea)
{
int32 offsetV = fWeightsV.Offset ();
int32 offsetH = fWeightsH.Offset ();
uint32 widthV = fWeightsV.Width ();
uint32 widthH = fWeightsH.Width ();
dng_rect srcArea;
srcArea.t = fRowCoords.Pixel (dstArea.t) + offsetV;
srcArea.l = fColCoords.Pixel (dstArea.l) + offsetH;
srcArea.b = fRowCoords.Pixel (dstArea.b - 1) + offsetV + widthV;
srcArea.r = fColCoords.Pixel (dstArea.r - 1) + offsetH + widthH;
return srcArea;
}
/*****************************************************************************/
dng_point dng_resample_task::SrcTileSize (const dng_point & /* dstTileSize */)
{
return fSrcTileSize;
}
/*****************************************************************************/
void dng_resample_task::Start (uint32 threadCount,
const dng_point &tileSize,
dng_memory_allocator *allocator,
dng_abort_sniffer *sniffer)
{
// Compute sub-pixel resolution coordinates in the source image for
// each row and column of the destination area.
fRowCoords.Initialize (fSrcBounds.t,
fDstBounds.t,
fSrcBounds.H (),
fDstBounds.H (),
*allocator);
fColCoords.Initialize (fSrcBounds.l,
fDstBounds.l,
fSrcBounds.W (),
fDstBounds.W (),
*allocator);
// Compute resampling kernels.
fWeightsV.Initialize (fRowScale,
fKernel,
*allocator);
fWeightsH.Initialize (fColScale,
fKernel,
*allocator);
// Find upper bound on source source tile.
fSrcTileSize.v = Round_int32 (tileSize.v / fRowScale) + fWeightsV.Width () + 2;
fSrcTileSize.h = Round_int32 (tileSize.h / fColScale) + fWeightsH.Width () + 2;
// Allocate temp buffers.
uint32 tempBufferSize = RoundUp8 (fSrcTileSize.h) * (uint32) sizeof (real32);
for (uint32 threadIndex = 0; threadIndex < threadCount; threadIndex++)
{
fTempBuffer [threadIndex] . Reset (allocator->Allocate (tempBufferSize));
}
// Allocate the pixel buffers.
dng_filter_task::Start (threadCount,
tileSize,
allocator,
sniffer);
}
/*****************************************************************************/
void dng_resample_task::ProcessArea (uint32 threadIndex,
dng_pixel_buffer &srcBuffer,
dng_pixel_buffer &dstBuffer)
{
dng_rect srcArea = srcBuffer.fArea;
dng_rect dstArea = dstBuffer.fArea;
uint32 srcCols = srcArea.W ();
uint32 dstCols = dstArea.W ();
uint32 widthV = fWeightsV.Width ();
uint32 widthH = fWeightsH.Width ();
int32 offsetV = fWeightsV.Offset ();
int32 offsetH = fWeightsH.Offset ();
uint32 stepH = fWeightsH.Step ();
const int32 *rowCoords = fRowCoords.Coords (0 );
const int32 *colCoords = fColCoords.Coords (dstArea.l);
if (fSrcPixelType == ttFloat)
{
const real32 *weightsH = fWeightsH.Weights32 (0);
real32 *tPtr = fTempBuffer [threadIndex]->Buffer_real32 ();
real32 *ttPtr = tPtr + offsetH - srcArea.l;
for (int32 dstRow = dstArea.t; dstRow < dstArea.b; dstRow++)
{
int32 rowCoord = rowCoords [dstRow];
int32 rowFract = rowCoord & kResampleSubsampleMask;
const real32 *weightsV = fWeightsV.Weights32 (rowFract);
int32 srcRow = (rowCoord >> kResampleSubsampleBits) + offsetV;
for (uint32 plane = 0; plane < dstBuffer.fPlanes; plane++)
{
const real32 *sPtr = srcBuffer.ConstPixel_real32 (srcRow,
srcArea.l,
plane);
DoResampleDown32 (sPtr,
tPtr,
srcCols,
srcBuffer.fRowStep,
weightsV,
widthV);
real32 *dPtr = dstBuffer.DirtyPixel_real32 (dstRow,
dstArea.l,
plane);
DoResampleAcross32 (ttPtr,
dPtr,
dstCols,
colCoords,
weightsH,
widthH,
stepH);
}
}
}
else
{
const int16 *weightsH = fWeightsH.Weights16 (0);
uint16 *tPtr = fTempBuffer [threadIndex]->Buffer_uint16 ();
uint16 *ttPtr = tPtr + offsetH - srcArea.l;
uint32 pixelRange = fDstImage.PixelRange ();
for (int32 dstRow = dstArea.t; dstRow < dstArea.b; dstRow++)
{
int32 rowCoord = rowCoords [dstRow];
int32 rowFract = rowCoord & kResampleSubsampleMask;
const int16 *weightsV = fWeightsV.Weights16 (rowFract);
int32 srcRow = (rowCoord >> kResampleSubsampleBits) + offsetV;
for (uint32 plane = 0; plane < dstBuffer.fPlanes; plane++)
{
const uint16 *sPtr = srcBuffer.ConstPixel_uint16 (srcRow,
srcArea.l,
plane);
DoResampleDown16 (sPtr,
tPtr,
srcCols,
srcBuffer.fRowStep,
weightsV,
widthV,
pixelRange);
uint16 *dPtr = dstBuffer.DirtyPixel_uint16 (dstRow,
dstArea.l,
plane);
DoResampleAcross16 (ttPtr,
dPtr,
dstCols,
colCoords,
weightsH,
widthH,
stepH,
pixelRange);
}
}
}
}
/*****************************************************************************/
void ResampleImage (dng_host &host,
const dng_image &srcImage,
dng_image &dstImage,
const dng_rect &srcBounds,
const dng_rect &dstBounds,
const dng_resample_function &kernel)
{
dng_resample_task task (srcImage,
dstImage,
srcBounds,
dstBounds,
kernel);
host.PerformAreaTask (task,
dstBounds);
}
/*****************************************************************************/
|