File indexing completed on 2025-01-19 03:54:58

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 /** \file
0010  * Class definition for dng_host, initial point of contact and control between
0011  * host application and DNG SDK.
0012  */
0013 
0014 /*****************************************************************************/
0015 
0016 #ifndef __dng_host__
0017 #define __dng_host__
0018 
0019 /*****************************************************************************/
0020 
0021 #include "dng_auto_ptr.h"
0022 #include "dng_classes.h"
0023 #include "dng_errors.h"
0024 #include "dng_types.h"
0025 #include "dng_uncopyable.h"
0026 
0027 /*****************************************************************************/
0028 
0029 /// \brief The main class for communication between the application and the
0030 /// DNG SDK. Used to customize memory allocation and other behaviors.
0031 ///
0032 /// dng_host allows setting parameters for the DNG conversion, mediates callback
0033 /// style interactions between the host application and the DNG SDK, and allows
0034 /// controlling certain internal behavior of the SDK such as memory allocation.
0035 /// Many applications will be able to use the default implementation of dng_host
0036 /// by just setting the dng_memory_allocator and dng_abort_sniffer in the
0037 /// constructor. More complex interactions will require deriving a class from
0038 /// dng_host.
0039 ///
0040 /// Multiple dng_host objects can be allocated in a single process. This may
0041 /// be useful for DNG processing on separate threads. (Distinct dng_host objects
0042 /// are completely threadsafe for read/write. The application is responsible for
0043 /// establishing mutual exclusion for read/write access to a single dng_host
0044 /// object if it is used in multiple threads.)
0045 
0046 class dng_host: private dng_uncopyable
0047     {
0048 
0049     private:
0050 
0051         dng_memory_allocator *fAllocator;
0052 
0053         dng_abort_sniffer *fSniffer;
0054 
0055         // Does the host require all the image metadata (vs. just checking
0056         // to see if the file is readable)?
0057 
0058         bool fNeedsMeta;
0059 
0060         // Does the host require actual image data (vs. just getting metadata
0061         // or just checking to see if the file is readable)?
0062 
0063         bool fNeedsImage;
0064 
0065         // If we need the image data, can it be read at preview quality?
0066 
0067         bool fForPreview;
0068 
0069         // If non-zero, the minimum size (longer of the two pixel dimensions)
0070         // image to read.  If zero, or if the full size image is smaller than
0071         // this, read the full size image.
0072 
0073         uint32 fMinimumSize;
0074 
0075         // What is the preferred size for a preview image?  This can
0076         // be slightly larger than the minimum size.  Zero if we want
0077         // the full resolution image.
0078 
0079         uint32 fPreferredSize;
0080 
0081         // What is the maximum size for a preview image?  Zero if there
0082         // is no maximum size limit.
0083 
0084         uint32 fMaximumSize;
0085 
0086         // The fraction of the image kept after a crop.  This is used to
0087         // adjust the sizes to take into account the cropping that
0088         // will be peformed.
0089 
0090         real64 fCropFactor;
0091 
0092         // What DNG version should we keep enough data to save?
0093 
0094         uint32 fSaveDNGVersion;
0095 
0096         // Do we want to force saving to a linear DNG?
0097 
0098         bool fSaveLinearDNG;
0099 
0100         // Keep the original raw file data block?
0101 
0102         bool fKeepOriginalFile;
0103 
0104         // Is this host being used to perform a negative read for fast
0105         // conversion to DNG?
0106 
0107         bool fForFastSaveToDNG;
0108 
0109         uint32 fFastSaveToDNGSize;
0110 
0111         bool fPreserveStage2;
0112 
0113     public:
0114 
0115         /// Allocate a dng_host object, possiblly with custom allocator and sniffer.
0116         /// \param allocator Allows controlling all memory allocation done via this
0117         /// dng_host. Defaults to singleton global dng_memory_allocator, which calls
0118         /// new/delete dng_malloc_block for appropriate size.
0119         /// \param sniffer Used to periodically check if pending DNG conversions
0120         /// should be aborted and to communicate progress updates. Defaults to singleton
0121         /// global dng_abort_sniffer, which never aborts and ignores progress updated.
0122 
0123         dng_host (dng_memory_allocator *allocator = NULL,
0124                   dng_abort_sniffer *sniffer = NULL);
0125 
0126         /// Clean up direct memory for dng_host. Memory allocator and abort sniffer
0127         /// are not deleted. Objects such as dng_image and others returned from
0128         /// host can still be used after host is deleted.
0129 
0130         virtual ~dng_host ();
0131 
0132         /// Getter for host's memory allocator.
0133 
0134         dng_memory_allocator & Allocator ();
0135 
0136         /// Alocate a new dng_memory_block using the host's memory allocator.
0137         /// Uses the Allocator() property of host to allocate a new block of memory.
0138         /// Will call ThrowMemoryFull if block cannot be allocated.
0139         /// \param logicalSize Number of usable bytes returned dng_memory_block
0140         /// must contain.
0141 
0142         virtual dng_memory_block * Allocate (uint32 logicalSize);
0143 
0144         /// Setter for host's abort sniffer.
0145 
0146         void SetSniffer (dng_abort_sniffer *sniffer)
0147             {
0148             fSniffer = sniffer;
0149             }
0150 
0151         /// Getter for host's abort sniffer.
0152 
0153         dng_abort_sniffer * Sniffer ()
0154             {
0155             return fSniffer;
0156             }
0157 
0158         /// Check for pending abort. Should call ThrowUserCanceled if an abort
0159         /// is pending.
0160 
0161         virtual void SniffForAbort ();
0162 
0163         /// Setter for flag determining whether all XMP metadata should be parsed.
0164         /// Defaults to true. One might not want metadata when doing a quick check
0165         /// to see if a file is readable.
0166         /// \param needs If true, metadata is needed.
0167 
0168         void SetNeedsMeta (bool needs)
0169             {
0170             fNeedsMeta = needs;
0171             }
0172 
0173         /// Getter for flag determining whether all XMP metadata should be parsed.
0174 
0175         bool NeedsMeta () const
0176             {
0177             return fNeedsMeta;
0178             }
0179 
0180         /// Setter for flag determining whether DNG image data is needed. Defaults
0181         /// to true. Image data might not be needed for applications which only
0182         /// manipulate metadata.
0183         /// \param needs If true, image data is needed.
0184 
0185         void SetNeedsImage (bool needs)
0186             {
0187             fNeedsImage = needs;
0188             }
0189 
0190         /// Setter for flag determining whether DNG image data is needed.
0191 
0192         bool NeedsImage () const
0193             {
0194             return fNeedsImage;
0195             }
0196 
0197         /// Setter for flag determining whether image should be preview quality,
0198         /// or full quality.
0199         /// \param preview If true, rendered images are for preview.
0200 
0201         void SetForPreview (bool preview)
0202             {
0203             fForPreview = preview;
0204             }
0205 
0206         /// Getter for flag determining whether image should be preview quality.
0207         /// Preview quality images may be rendered more quickly. Current DNG SDK
0208         /// does not change rendering behavior based on this flag, but derived
0209         /// versions may use this getter to choose between a slower more accurate path
0210         /// and a faster "good enough for preview" one. Data produce with ForPreview set
0211         /// to true should not be written back to a DNG file, except as a preview image.
0212 
0213         bool ForPreview () const
0214             {
0215             return fForPreview;
0216             }
0217 
0218         /// Setter for the minimum preview size.
0219         /// \param size Minimum pixel size (long side of image).
0220 
0221         void SetMinimumSize (uint32 size)
0222             {
0223             fMinimumSize = size;
0224             }
0225 
0226         /// Getter for the minimum preview size.
0227 
0228         uint32 MinimumSize () const
0229             {
0230             return fMinimumSize;
0231             }
0232 
0233         /// Setter for the preferred preview size.
0234         /// \param size Preferred pixel size (long side of image).
0235 
0236         void SetPreferredSize (uint32 size)
0237             {
0238             fPreferredSize = size;
0239             }
0240 
0241         /// Getter for the preferred preview size.
0242 
0243         uint32 PreferredSize () const
0244             {
0245             return fPreferredSize;
0246             }
0247 
0248         /// Setter for the maximum preview size.
0249         /// \param size Maximum pixel size (long side of image).
0250 
0251         void SetMaximumSize (uint32 size)
0252             {
0253             fMaximumSize = size;
0254             }
0255 
0256         /// Getter for the maximum preview size.
0257 
0258         uint32 MaximumSize () const
0259             {
0260             return fMaximumSize;
0261             }
0262 
0263         /// Setter for the perform fast save to DNG.
0264         /// \param flag True if the host is being used to perform a negative
0265         /// read for fast conversion to DNG, false otherwise.
0266 
0267         void SetForFastSaveToDNG (bool flag,
0268                                   uint32 size)
0269             {
0270             fForFastSaveToDNG = flag;
0271             fFastSaveToDNGSize = size;
0272             }
0273 
0274         /// Getter for the Boolean value that indicates whether this host is
0275         /// being used to perform a negative read for fast conversion to DNG.
0276 
0277         bool ForFastSaveToDNG () const
0278             {
0279             return fForFastSaveToDNG;
0280             }
0281 
0282         uint32 FastSaveToDNGSize () const
0283             {
0284             return fFastSaveToDNGSize;
0285             }
0286 
0287         /// Setter for the cropping factor.
0288         /// \param cropFactor Fraction of image to be used after crop.
0289 
0290         void SetCropFactor (real64 cropFactor)
0291             {
0292             fCropFactor = cropFactor;
0293             }
0294 
0295         /// Getter for the cropping factor.
0296 
0297         real64 CropFactor () const
0298             {
0299             return fCropFactor;
0300             }
0301 
0302         /// Makes sures minimum, preferred, and maximum sizes are reasonable.
0303 
0304         void ValidateSizes ();
0305 
0306         /// Setter for what version to save DNG file compatible with.
0307         /// \param version What version to save DNG file compatible with.
0308 
0309         void SetSaveDNGVersion (uint32 version)
0310             {
0311             fSaveDNGVersion = version;
0312             }
0313 
0314         /// Getter for what version to save DNG file compatible with.
0315 
0316         virtual uint32 SaveDNGVersion () const;
0317 
0318         /// Setter for flag determining whether to force saving a linear DNG file.
0319         /// \param linear If true, we should force saving a linear DNG file.
0320 
0321         void SetSaveLinearDNG (bool linear)
0322             {
0323             fSaveLinearDNG = linear;
0324             }
0325 
0326         /// Getter for flag determining whether to save a linear DNG file.
0327 
0328         virtual bool SaveLinearDNG (const dng_negative &negative) const;
0329 
0330         /// Setter for flag determining whether to keep original RAW file data.
0331         /// \param keep If true, origianl RAW data will be kept.
0332 
0333         void SetKeepOriginalFile (bool keep)
0334             {
0335             fKeepOriginalFile = keep;
0336             }
0337 
0338         /// Getter for flag determining whether to keep original RAW file data.
0339 
0340         bool KeepOriginalFile ()
0341             {
0342             return fKeepOriginalFile;
0343             }
0344 
0345         /// Determine if an error is the result of a temporary, but planned-for
0346         /// occurence such as user cancellation or memory exhaustion. This method is
0347         /// sometimes used to determine whether to try and continue processing a DNG
0348         /// file despite errors in the file format, etc. In such cases, processing will
0349         /// be continued if IsTransientError returns false. This is so that user cancellation
0350         /// and memory exhaustion always terminate processing.
0351         /// \param code Error to test for transience.
0352 
0353         virtual bool IsTransientError (dng_error_code code);
0354 
0355         /// General top-level botttleneck for image processing tasks.
0356         /// Default implementation calls dng_area_task::PerformAreaTask method on
0357         /// task. Can be overridden in derived classes to support multiprocessing,
0358         /// for example.
0359         /// \param task Image processing task to perform on area.
0360         /// \param area Rectangle over which to perform image processing task.
0361 
0362         virtual void PerformAreaTask (dng_area_task &task,
0363                                       const dng_rect &area,
0364                                       dng_area_task_progress *progress = NULL);
0365 
0366         /// How many multiprocessing threads does PerformAreaTask use?
0367         /// Default implementation always returns 1 since it is single threaded.
0368 
0369         virtual uint32 PerformAreaTaskThreads ();
0370 
0371         /// Factory method for dng_exif class. Can be used to customize allocation or
0372         /// to ensure a derived class is used instead of dng_exif.
0373 
0374         virtual dng_exif * Make_dng_exif ();
0375 
0376         /// Factory method for dng_xmp class. Can be used to customize allocation or
0377         /// to ensure a derived class is used instead of dng_xmp.
0378 
0379         virtual dng_xmp * Make_dng_xmp ();
0380 
0381         /// Factory method for dng_shared class. Can be used to customize allocation
0382         /// or to ensure a derived class is used instead of dng_shared.
0383 
0384         virtual dng_shared * Make_dng_shared ();
0385 
0386         /// Factory method for dng_ifd class. Can be used to customize allocation or
0387         /// to ensure a derived class is used instead of dng_ifd.
0388 
0389         virtual dng_ifd * Make_dng_ifd ();
0390 
0391         /// Factory method for dng_negative class. Can be used to customize allocation
0392         /// or to ensure a derived class is used instead of dng_negative.
0393 
0394         virtual dng_negative * Make_dng_negative ();
0395 
0396         /// Factory method for dng_image class. Can be used to customize allocation
0397         /// or to ensure a derived class is used instead of dng_simple_image.
0398 
0399         virtual dng_image * Make_dng_image (const dng_rect &bounds,
0400                                             uint32 planes,
0401                                             uint32 pixelType);
0402 
0403         /// Factory method for parsing dng_opcode based classs. Can be used to
0404         /// override opcode implementations.
0405 
0406         virtual dng_opcode * Make_dng_opcode (uint32 opcodeID,
0407                                               dng_stream &stream);
0408 
0409         /// Factory method to apply a dng_opcode_list. Can be used to override
0410         /// opcode list applications.
0411 
0412         virtual void ApplyOpcodeList (dng_opcode_list &list,
0413                                       dng_negative &negative,
0414                                       AutoPtr<dng_image> &image);
0415 
0416         /// Factory method to resample an image.  Can be used to override
0417         /// image method used to resample images.
0418 
0419         virtual void ResampleImage (const dng_image &srcImage,
0420                                     dng_image &dstImage);
0421 
0422         /// Getter for flag determining whether we should preserve the stage 2
0423         /// image after building the stage 3 image.
0424 
0425         bool WantsPreserveStage2 () const
0426             {
0427             return fPreserveStage2;
0428             }
0429 
0430         /// Setter for flag determining whether we should preserve the stage 2
0431         /// image after building the stage 3 image.
0432 
0433         void SetWantsPreserveStage2 (bool flag)
0434             {
0435             fPreserveStage2 = flag;
0436             }
0437 
0438     };
0439 
0440 /*****************************************************************************/
0441 
0442 #endif
0443 
0444 /*****************************************************************************/