1 /*****************************************************************************/
2 // Copyright 2006-2008 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 /* $Id: //mondo/dng_sdk_1_2/dng_sdk/source/dng_host.h#1 $ */
10 /* $DateTime: 2008/03/09 14:29:54 $ */
11 /* $Change: 431850 $ */
12 /* $Author: tknoll $ */
13 
14 /** \file
15  * Class definition for dng_host, initial point of contact and control between
16  * host application and DNG SDK.
17  */
18 
19 /*****************************************************************************/
20 
21 #ifndef __dng_host__
22 #define __dng_host__
23 
24 /*****************************************************************************/
25 
26 #include "dng_classes.h"
27 #include "dng_errors.h"
28 #include "dng_types.h"
29 
30 /*****************************************************************************/
31 
32 /// \brief The main class for communication between the application and the
33 /// DNG SDK. Used to customize memory allocation and other behaviors.
34 ///
35 /// dng_host allows setting parameters for the DNG conversion, mediates callback
36 /// style interactions between the host application and the DNG SDK, and allows
37 /// controlling certain internal behavior of the SDK such as memory allocation.
38 /// Many applications will be able to use the default implementation of dng_host
39 /// by just setting the dng_memory_allocator and dng_abort_sniffer in the
40 /// constructor. More complex interactions will require deriving a class from
41 /// dng_host.
42 ///
43 /// Multiple dng_host objects can be allocated in a single process. This may
44 /// be useful for DNG processing on separate threads. (Distinct dng_host objects
45 /// are completely threadsafe for read/write. The application is responsible for
46 /// establishing mutual exclusion for read/write access to a single dng_host
47 /// object if it is used in multiple threads.)
48 
49 class dng_host
50 	{
51 
52 	private:
53 
54 		dng_memory_allocator *fAllocator;
55 
56 		dng_abort_sniffer *fSniffer;
57 
58 		// Does the host require all the image metadata (vs. just checking
59 		// to see if the file is readable)?
60 
61 		bool fNeedsMeta;
62 
63 		// Does the host require actual image data (vs. just getting metadata
64 		// or just checking to see if the file is readable)?
65 
66 		bool fNeedsImage;
67 
68 		// If we need the image data, can it be read at preview quality?
69 
70 		bool fForPreview;
71 
72 		// If non-zero, the minimum size (longer of the two pixel dimensions)
73 		// image to read.  If zero, or if the full size image is smaller than
74 		// this, read the full size image.
75 
76 		uint32 fMinimumSize;
77 
78 		// What is the preferred size for a preview image?  This can
79 		// be slightly larger than the minimum size.  Zero if we want
80 		// the full resolution image.
81 
82 		uint32 fPreferredSize;
83 
84 		// What is the maximum size for a preview image?  Zero if there
85 		// is no maximum size limit.
86 
87 		uint32 fMaximumSize;
88 
89 		// The fraction of the image kept after a crop.  This is used to
90 		// adjust the sizes to take into account the cropping that
91 		// will be peformed.
92 
93 		real64 fCropFactor;
94 
95 		// Keep the stage 1 (original raw) image?
96 
97 		bool fKeepStage1;
98 
99 		// Keep the stage 2 (linearized) image?
100 
101 		bool fKeepStage2;
102 
103 		// Keep the DNG private data block?
104 
105 		bool fKeepDNGPrivate;
106 
107 		// Keep the original raw file data block?
108 
109 		bool fKeepOriginalFile;
110 
111 	public:
112 
113 		/// Allocate a dng_host object, possiblly with custom allocator and sniffer.
114 		/// \param allocator Allows controlling all memory allocation done via this
115 		/// dng_host. Defaults to singleton global dng_memory_allocator, which calls
116 		/// new/delete dng_malloc_block for appropriate size.
117 		/// \param sniffer Used to periodically check if pending DNG conversions
118 		/// should be aborted and to communicate progress updates. Defaults to singleton
119 		/// global dng_abort_sniffer, which never aborts and ignores progress updated.
120 
121 		dng_host (dng_memory_allocator *allocator = NULL,
122 				  dng_abort_sniffer *sniffer = NULL);
123 
124 		/// Clean up direct memory for dng_host. Memory allocator and abort sniffer
125 		/// are not deleted. Objects such as dng_image and others returned from
126 		/// host can still be used after host is deleted.
127 
128 		virtual ~dng_host ();
129 
130 		/// Getter for host's memory allocator.
131 
132 		dng_memory_allocator & Allocator ();
133 
134 		/// Alocate a new dng_memory_block using the host's memory allocator.
135 		/// Uses the Allocator() property of host to allocate a new block of memory.
136 		/// Will call ThrowMemoryFull if block cannot be allocated.
137 		/// \param logicalSize Number of usable bytes returned dng_memory_block
138 		/// must contain.
139 
140 		virtual dng_memory_block * Allocate (uint32 logicalSize);
141 
142 		/// Setter for host's abort sniffer.
143 
SetSniffer(dng_abort_sniffer * sniffer)144 		void SetSniffer (dng_abort_sniffer *sniffer)
145 			{
146 			fSniffer = sniffer;
147 			}
148 
149 		/// Getter for host's abort sniffer.
150 
Sniffer()151 		dng_abort_sniffer * Sniffer ()
152 			{
153 			return fSniffer;
154 			}
155 
156 		/// Check for pending abort. Should call ThrowUserCanceled if an abort
157 		/// is pending.
158 
159 		virtual void SniffForAbort ();
160 
161 		/// Setter for flag determining whether all XMP metadata should be parsed.
162 		/// Defaults to true. One might not want metadata when doing a quick check
163 		/// to see if a file is readable.
164 		/// \param needs If true, metadata is needed.
165 
SetNeedsMeta(bool needs)166 		void SetNeedsMeta (bool needs)
167 			{
168 			fNeedsMeta = needs;
169 			}
170 
171 		/// Getter for flag determining whether all XMP metadata should be parsed.
172 
NeedsMeta()173 		bool NeedsMeta () const
174 			{
175 			return fNeedsMeta;
176 			}
177 
178 		/// Setter for flag determining whether DNG image data is needed. Defaults
179 		/// to true. Image data might not be needed for applications which only
180 		/// manipulate metadata.
181 		/// \param needs If true, image data is needed.
182 
SetNeedsImage(bool needs)183 		void SetNeedsImage (bool needs)
184 			{
185 			fNeedsImage = needs;
186 			}
187 
188 		/// Setter for flag determining whether DNG image data is needed.
189 
NeedsImage()190 		bool NeedsImage () const
191 			{
192 			return fNeedsImage;
193 			}
194 
195 		/// Setter for flag determining whether	image should be preview quality,
196 		/// or full quality.
197 		/// \param preview If true, rendered images are for preview.
198 
SetForPreview(bool preview)199 		void SetForPreview (bool preview)
200 			{
201 			fForPreview = preview;
202 			}
203 
204 		/// Getter for flag determining whether image should be preview quality.
205 		/// Preview quality images may be rendered more quickly. Current DNG SDK
206 		/// does not change rendering behavior based on this flag, but derived
207 		/// versions may use this getter to choose between a slower more accurate path
208 		/// and a faster "good enough for preview" one. Data produce with ForPreview set
209 		/// to true should not be written back to a DNG file, except as a preview image.
210 
ForPreview()211 		bool ForPreview () const
212 			{
213 			return fForPreview;
214 			}
215 
216 		/// Setter for the minimum preview size.
217 		/// \param size Minimum pixel size (long side of image).
218 
SetMinimumSize(uint32 size)219 		void SetMinimumSize (uint32 size)
220 			{
221 			fMinimumSize = size;
222 			}
223 
224 		/// Getter for the minimum preview size.
225 
MinimumSize()226 		uint32 MinimumSize () const
227 			{
228 			return fMinimumSize;
229 			}
230 
231 		/// Setter for the preferred preview size.
232 		/// \param size Preferred pixel size (long side of image).
233 
SetPreferredSize(uint32 size)234 		void SetPreferredSize (uint32 size)
235 			{
236 			fPreferredSize = size;
237 			}
238 
239 		/// Getter for the preferred preview size.
240 
PreferredSize()241 		uint32 PreferredSize () const
242 			{
243 			return fPreferredSize;
244 			}
245 
246 		/// Setter for the maximum preview size.
247 		/// \param size Maximum pixel size (long side of image).
248 
SetMaximumSize(uint32 size)249 		void SetMaximumSize (uint32 size)
250 			{
251 			fMaximumSize = size;
252 			}
253 
254 		/// Getter for the maximum preview size.
255 
MaximumSize()256 		uint32 MaximumSize () const
257 			{
258 			return fMaximumSize;
259 			}
260 
261 		/// Setter for the cropping factor.
262 		/// \param cropFactor Fraction of image to be used after crop.
263 
SetCropFactor(real64 cropFactor)264 		void SetCropFactor (real64 cropFactor)
265 			{
266 			fCropFactor = cropFactor;
267 			}
268 
269 		/// Getter for the cropping factor.
270 
CropFactor()271 		real64 CropFactor () const
272 			{
273 			return fCropFactor;
274 			}
275 
276 		/// Makes sures minimum, preferred, and maximum sizes are reasonable.
277 
278 		void ValidateSizes ();
279 
280 		/// Setter for flag determining whether	to keep stage 1, unprocessed raw
281 		/// data, after processing all stages.
282 		/// \param keep If true, stage 1 data will be kept.
283 
SetKeepStage1(bool keep)284 		void SetKeepStage1 (bool keep)
285 			{
286 			fKeepStage1 = keep;
287 			}
288 
289 		/// Getter for flag determining whether	to keep stage 1, unprocessed raw
290 		/// data, after processing all stages.
291 
KeepStage1()292 		bool KeepStage1 () const
293 			{
294 			return fKeepStage1;
295 			}
296 
297 		/// Setter for flag determining whether	to keep stage 2, linearized,
298 		/// tone curve processed data, after processing all stages.
299 		/// \param keep If true, stage 2 data will be kept.
300 
SetKeepStage2(bool keep)301 		void SetKeepStage2 (bool keep)
302 			{
303 			fKeepStage2 = keep;
304 			}
305 
306 		/// Getter for flag determining whether	to keep stage 2, linearized,
307 		/// tone curve processed data, after processing all stages.
308 
KeepStage2()309 		bool KeepStage2 () const
310 			{
311 			return fKeepStage2;
312 			}
313 
314 		/// Setter for flag determining whether DNG private data will be kept.
315 		/// \param keep If true, DNG private data will be kept.
316 
SetKeepDNGPrivate(bool keep)317 		void SetKeepDNGPrivate (bool keep)
318 			{
319 			fKeepDNGPrivate = keep;
320 			}
321 
322 		/// Getter for flag determining whether all	DNG private data will be kept.
323 
KeepDNGPrivate()324 		bool KeepDNGPrivate ()
325 			{
326 			return fKeepDNGPrivate;
327 			}
328 
329 		/// Setter for flag determining whether to keep original RAW file data.
330 		/// \param keep If true, origianl RAW data will be kept.
331 
SetKeepOriginalFile(bool keep)332 		void SetKeepOriginalFile (bool keep)
333 			{
334 			fKeepOriginalFile = keep;
335 			}
336 
337 		/// Getter for flag determining whether to keep original RAW file data.
338 
KeepOriginalFile()339 		bool KeepOriginalFile ()
340 			{
341 			return fKeepOriginalFile;
342 			}
343 
344 		/// Determine if an error is the result of a temporary, but planned-for
345 		/// occurence such as user cancellation or memory exhaustion. This method is
346 		/// sometimes used to determine whether to try and continue processing a DNG
347 		/// file despite errors in the file format, etc. In such cases, processing will
348 		/// be continued if IsTransientError returns false. This is so that user cancellation
349 		/// and memory exhaustion always terminate processing.
350 		/// \param code Error to test for transience.
351 
352 		virtual bool IsTransientError (dng_error_code code);
353 
354 		/// General top-level botttleneck for image processing tasks.
355 		/// Default implementation calls dng_area_task::PerformAreaTask method on
356 		/// task. Can be overridden in derived classes to support multiprocessing,
357 		/// for example.
358 		/// \param task Image processing task to perform on area.
359 		/// \param area Rectangle over which to perform image processing task.
360 
361 		virtual void PerformAreaTask (dng_area_task &task,
362 									  const dng_rect &area);
363 
364 		/// Factory method for dng_exif class. Can be used to customize allocation or
365 		/// to ensure a derived class is used instead of dng_exif.
366 
367 		virtual dng_exif * Make_dng_exif ();
368 
369 		/// Factory method for dng_shared class. Can be used to customize allocation
370 		/// or to ensure a derived class is used instead of dng_shared.
371 
372 		virtual dng_shared * Make_dng_shared ();
373 
374 		/// Factory method for dng_ifd class. Can be used to customize allocation or
375 		/// to ensure a derived class is used instead of dng_ifd.
376 
377 		virtual dng_ifd * Make_dng_ifd ();
378 
379 		/// Factory method for dng_negative class. Can be used to customize allocation
380 		/// or to ensure a derived class is used instead of dng_negative.
381 
382 		virtual dng_negative * Make_dng_negative ();
383 
384 		/// Factor method for dng_image class. Can be used to customize allocation
385 		/// or to ensure a derived class is used instead of dng_simple_image.
386 
387 		virtual dng_image * Make_dng_image (const dng_rect &bounds,
388 											uint32 planes,
389 											uint32 pixelType);
390 
391 	private:
392 
393 		// Hidden copy constructor and assignment operator.
394 
395 		dng_host (const dng_host &host);
396 
397 		dng_host & operator= (const dng_host &host);
398 
399 	};
400 
401 /*****************************************************************************/
402 
403 #endif
404 
405 /*****************************************************************************/
406