File indexing completed on 2025-01-19 03:55:15

0001 /*****************************************************************************/
0002 // Copyright 2006-2019 Adobe Systems Incorporated
0003 // All Rights Reserved.
0004 //
0005 // NOTICE:  Adobe permits you to use, modify, and distribute this file in
0006 // accordance with the terms of the Adobe license agreement accompanying it.
0007 /*****************************************************************************/
0008 
0009 #ifndef __dng_resample__
0010 #define __dng_resample__
0011 
0012 /*****************************************************************************/
0013 
0014 #include "dng_assertions.h"
0015 #include "dng_auto_ptr.h"
0016 #include "dng_classes.h"
0017 #include "dng_memory.h"
0018 #include "dng_point.h"
0019 #include "dng_types.h"
0020 
0021 /*****************************************************************************/
0022 
0023 class dng_resample_function
0024     {
0025 
0026     public:
0027 
0028         dng_resample_function ()
0029             {
0030             }
0031 
0032         virtual ~dng_resample_function ()
0033             {
0034             }
0035 
0036         virtual real64 Extent () const = 0;
0037 
0038         virtual real64 Evaluate (real64 x) const = 0;
0039 
0040     };
0041 
0042 /*****************************************************************************/
0043 
0044 class dng_resample_bicubic: public dng_resample_function
0045     {
0046 
0047     public:
0048 
0049         virtual real64 Extent () const;
0050 
0051         virtual real64 Evaluate (real64 x) const;
0052 
0053         static const dng_resample_function & Get ();
0054 
0055     };
0056 
0057 /******************************************************************************/
0058 
0059 const uint32 kResampleSubsampleBits  = 7;
0060 const uint32 kResampleSubsampleCount = 1 << kResampleSubsampleBits;
0061 const uint32 kResampleSubsampleMask  = kResampleSubsampleCount - 1;
0062 
0063 /*****************************************************************************/
0064 
0065 class dng_resample_coords
0066     {
0067 
0068     protected:
0069 
0070         int32 fOrigin;
0071 
0072         AutoPtr<dng_memory_block> fCoords;
0073 
0074     public:
0075 
0076         dng_resample_coords ();
0077 
0078         virtual ~dng_resample_coords ();
0079 
0080         void Initialize (int32 srcOrigin,
0081                          int32 dstOrigin,
0082                          uint32 srcCount,
0083                          uint32 dstCount,
0084                          dng_memory_allocator &allocator);
0085 
0086         const int32 * Coords (int32 index) const
0087             {
0088             return fCoords->Buffer_int32 () + (index - fOrigin);
0089             }
0090 
0091         int32 Pixel (int32 index) const
0092             {
0093             return Coords (index) [0] >> kResampleSubsampleBits;
0094             }
0095 
0096     };
0097 
0098 /*****************************************************************************/
0099 
0100 class dng_resample_weights
0101     {
0102 
0103     protected:
0104 
0105         uint32 fRadius;
0106 
0107         uint32 fWeightStep;
0108 
0109         AutoPtr<dng_memory_block> fWeights32;
0110         AutoPtr<dng_memory_block> fWeights16;
0111 
0112     public:
0113 
0114         dng_resample_weights ();
0115 
0116         virtual ~dng_resample_weights ();
0117 
0118         void Initialize (real64 scale,
0119                          const dng_resample_function &kernel,
0120                          dng_memory_allocator &allocator);
0121 
0122         uint32 Radius () const
0123             {
0124             return fRadius;
0125             }
0126 
0127         uint32 Width () const
0128             {
0129             return fRadius * 2;
0130             }
0131 
0132         int32 Offset () const
0133             {
0134             return 1 - (int32) fRadius;
0135             }
0136 
0137         uint32 Step () const
0138             {
0139             return fWeightStep;
0140             }
0141 
0142         const real32 *Weights32 (uint32 fract) const
0143             {
0144 
0145             DNG_ASSERT (fWeights32->Buffer (), "Weights32 is NULL");
0146 
0147             if (fract >= kResampleSubsampleCount)
0148                 {
0149 
0150                 ThrowBadFormat ();
0151 
0152                 }
0153 
0154             return fWeights32->Buffer_real32 () + fract * fWeightStep;
0155 
0156             }
0157 
0158         const int16 *Weights16 (uint32 fract) const
0159             {
0160 
0161             DNG_ASSERT (fWeights16->Buffer (), "Weights16 is NULL");
0162 
0163             if (fract >= kResampleSubsampleCount)
0164                 {
0165 
0166                 ThrowBadFormat ();
0167 
0168                 }
0169 
0170             return fWeights16->Buffer_int16 () + fract * fWeightStep;
0171 
0172             }
0173 
0174     };
0175 
0176 /*****************************************************************************/
0177 
0178 const uint32 kResampleSubsampleBits2D  = 5;
0179 const uint32 kResampleSubsampleCount2D = 1 << kResampleSubsampleBits2D;
0180 const uint32 kResampleSubsampleMask2D  = kResampleSubsampleCount2D - 1;
0181 
0182 /*****************************************************************************/
0183 
0184 class dng_resample_weights_2d
0185     {
0186 
0187     protected:
0188 
0189         uint32 fRadius;
0190 
0191         uint32 fRowStep;
0192         uint32 fColStep;
0193 
0194         AutoPtr<dng_memory_block> fWeights32;
0195         AutoPtr<dng_memory_block> fWeights16;
0196 
0197     public:
0198 
0199         dng_resample_weights_2d ();
0200 
0201         virtual ~dng_resample_weights_2d ();
0202 
0203         void Initialize (const dng_resample_function &kernel,
0204                          dng_memory_allocator &allocator);
0205 
0206         uint32 Radius () const
0207             {
0208             return fRadius;
0209             }
0210 
0211         uint32 Width () const
0212             {
0213             return fRadius * 2;
0214             }
0215 
0216         int32 Offset () const
0217             {
0218             return 1 - (int32) fRadius;
0219             }
0220 
0221         uint32 RowStep () const
0222             {
0223             return fRowStep;
0224             }
0225 
0226         uint32 ColStep () const
0227             {
0228             return fColStep;
0229             }
0230 
0231         const real32 *Weights32 (dng_point fract) const
0232             {
0233 
0234             DNG_ASSERT (fWeights32->Buffer (), "Weights32 is NULL");
0235 
0236             const uint32 offset = fract.v * fRowStep + fract.h * fColStep;
0237 
0238             return fWeights32->Buffer_real32 () + offset;
0239 
0240             }
0241 
0242         const int16 *Weights16 (dng_point fract) const
0243             {
0244 
0245             DNG_ASSERT (fWeights16->Buffer (), "Weights16 is NULL");
0246 
0247             const uint32 offset = fract.v * fRowStep + fract.h * fColStep;
0248 
0249             return fWeights16->Buffer_int16 () + offset;
0250 
0251             }
0252 
0253     };
0254 
0255 /*****************************************************************************/
0256 
0257 void ResampleImage (dng_host &host,
0258                     const dng_image &srcImage,
0259                     dng_image &dstImage,
0260                     const dng_rect &srcBounds,
0261                     const dng_rect &dstBounds,
0262                     const dng_resample_function &kernel);
0263 
0264 /*****************************************************************************/
0265 
0266 #endif
0267 
0268 /*****************************************************************************/