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

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  *  Support for working with image data in DNG SDK.
0011  */
0012 
0013 /*****************************************************************************/
0014 
0015 #ifndef __dng_image__
0016 #define __dng_image__
0017 
0018 /*****************************************************************************/
0019 
0020 #include "dng_assertions.h"
0021 #include "dng_classes.h"
0022 #include "dng_pixel_buffer.h"
0023 #include "dng_point.h"
0024 #include "dng_rect.h"
0025 #include "dng_tag_types.h"
0026 #include "dng_types.h"
0027 #include "dng_uncopyable.h"
0028 
0029 /*****************************************************************************/
0030 
0031 /// \brief Class to get resource acquisition is instantiation behavior for tile
0032 /// buffers. Can be dirty or constant tile access.
0033 
0034 class dng_tile_buffer: public dng_pixel_buffer,
0035                        private dng_uncopyable
0036     {
0037 
0038     protected:
0039 
0040         const dng_image &fImage;
0041 
0042         void *fRefData;
0043 
0044     protected:
0045 
0046         /// Obtain a tile from an image.
0047         /// \param image Image tile will come from.
0048         /// \param tile Rectangle denoting extent of tile.
0049         /// \param dirty Flag indicating whether this is read-only or read-write acesss.
0050 
0051         dng_tile_buffer (const dng_image &image,
0052                          const dng_rect &tile,
0053                          bool dirty);
0054 
0055         virtual ~dng_tile_buffer ();
0056 
0057     public:
0058 
0059         void SetRefData (void *refData)
0060             {
0061             fRefData = refData;
0062             }
0063 
0064         void * GetRefData () const
0065             {
0066             return fRefData;
0067             }
0068 
0069     };
0070 
0071 /*****************************************************************************/
0072 
0073 /// \brief Class to get resource acquisition is instantiation behavior for
0074 /// constant (read-only) tile buffers.
0075 
0076 class dng_const_tile_buffer: public dng_tile_buffer
0077     {
0078 
0079     public:
0080 
0081         /// Obtain a read-only tile from an image.
0082         /// \param image Image tile will come from.
0083         /// \param tile Rectangle denoting extent of tile.
0084 
0085         dng_const_tile_buffer (const dng_image &image,
0086                                const dng_rect &tile);
0087 
0088         virtual ~dng_const_tile_buffer ();
0089 
0090     };
0091 
0092 /*****************************************************************************/
0093 
0094 /// \brief Class to get resource acquisition is instantiation behavior for
0095 /// dirty (writable) tile buffers.
0096 
0097 class dng_dirty_tile_buffer: public dng_tile_buffer
0098     {
0099 
0100     public:
0101 
0102         /// Obtain a writable tile from an image.
0103         /// \param image Image tile will come from.
0104         /// \param tile Rectangle denoting extent of tile.
0105 
0106         dng_dirty_tile_buffer (dng_image &image,
0107                                const dng_rect &tile);
0108 
0109         virtual ~dng_dirty_tile_buffer ();
0110 
0111     };
0112 
0113 /*****************************************************************************/
0114 
0115 /// \brief Base class for holding image data in DNG SDK. See dng_simple_image
0116 /// for derived class most often used in DNG SDK.
0117 
0118 class dng_image
0119     {
0120 
0121     friend class dng_tile_buffer;
0122 
0123     protected:
0124 
0125         // Bounds for this image.
0126 
0127         dng_rect fBounds;
0128 
0129         // Number of image planes.
0130 
0131         uint32 fPlanes;
0132 
0133         // Basic pixel type (TIFF tag type code).
0134 
0135         uint32 fPixelType;
0136 
0137     public:
0138 
0139         /// How to handle requests to get image areas outside the image bounds.
0140 
0141         enum edge_option
0142             {
0143 
0144             /// Leave edge pixels unchanged.
0145 
0146             edge_none,
0147 
0148             /// Pad with zeros.
0149 
0150             edge_zero,
0151 
0152             /// Repeat edge pixels.
0153 
0154             edge_repeat,
0155 
0156             /// Repeat edge pixels, except for last plane which is zero padded.
0157 
0158             edge_repeat_zero_last
0159 
0160             };
0161 
0162     protected:
0163 
0164         dng_image (const dng_rect &bounds,
0165                    uint32 planes,
0166                    uint32 pixelType);
0167 
0168     public:
0169 
0170         virtual ~dng_image ();
0171 
0172         virtual dng_image * Clone () const;
0173 
0174         /// Getter method for bounds of an image.
0175 
0176         const dng_rect & Bounds () const
0177             {
0178             return fBounds;
0179             }
0180 
0181         /// Getter method for size of an image.
0182 
0183         dng_point Size () const
0184             {
0185             return Bounds ().Size ();
0186             }
0187 
0188         /// Getter method for width of an image.
0189 
0190         uint32 Width () const
0191             {
0192             return Bounds ().W ();
0193             }
0194 
0195         /// Getter method for height of an image.
0196 
0197         uint32 Height () const
0198             {
0199             return Bounds ().H ();
0200             }
0201 
0202         /// Getter method for number of planes in an image.
0203 
0204         uint32 Planes () const
0205             {
0206             return fPlanes;
0207             }
0208 
0209         /// Getter for pixel type.
0210         /// \retval See dng_tagtypes.h . Valid values are ttByte, ttShort, ttSShort,
0211         /// ttLong, ttFloat .
0212 
0213         uint32 PixelType () const
0214             {
0215             return fPixelType;
0216             }
0217 
0218         /// Setter for pixel type.
0219         /// \param pixelType The new pixel type .
0220 
0221         virtual void SetPixelType (uint32 pixelType);
0222 
0223         /// Getter for pixel size.
0224         /// \retval Size, in bytes, of pixel type for this image .
0225 
0226         uint32 PixelSize () const;
0227 
0228         /// Getter for pixel range.
0229         /// For unsigned types, range is 0 to return value.
0230         /// For signed types, range is return value - 0x8000U.
0231         /// For ttFloat type, pixel range is 0.0 to 1.0 and this routine returns 1.
0232 
0233         uint32 PixelRange () const;
0234 
0235         /// Getter for best "tile stride" for accessing image.
0236 
0237         virtual dng_rect RepeatingTile () const;
0238 
0239         /// Get a pixel buffer of data on image with proper edge padding.
0240         /// \param buffer Receives resulting pixel buffer.
0241         /// \param edgeOption edge_option describing how to pad edges.
0242         /// \param repeatV Amount of repeated padding needed in vertical for
0243         /// edge_repeat and edge_repeat_zero_last edgeOption cases.
0244         /// \param repeatH Amount of repeated padding needed in horizontal for
0245         /// edge_repeat and edge_repeat_zero_last edgeOption cases.
0246 
0247         void Get (dng_pixel_buffer &buffer,
0248                   edge_option edgeOption = edge_none,
0249                   uint32 repeatV = 1,
0250                   uint32 repeatH = 1) const;
0251 
0252         /// Put a pixel buffer into image.
0253         /// \param buffer Pixel buffer to copy from.
0254 
0255         void Put (const dng_pixel_buffer &buffer);
0256 
0257         /// Shrink bounds of image to given rectangle.
0258         /// \param r Rectangle to crop to.
0259 
0260         virtual void Trim (const dng_rect &r);
0261 
0262         /// Rotate image to reflect given orientation change.
0263         /// \param orientation Directive to rotate image in a certain way.
0264 
0265         virtual void Rotate (const dng_orientation &orientation);
0266 
0267         /// Copy image data from an area of one image to same area of another.
0268         /// \param src Image to copy from.
0269         /// \param area Rectangle of images to copy.
0270         /// \param srcPlane Plane to start copying in src.
0271         /// \param dstPlane Plane to start copying in this.
0272         /// \param planes Number of planes to copy.
0273 
0274         void CopyArea (const dng_image &src,
0275                        const dng_rect &area,
0276                        uint32 srcPlane,
0277                        uint32 dstPlane,
0278                        uint32 planes)
0279             {
0280 
0281             DoCopyArea (src, area, srcPlane, dstPlane, planes);
0282 
0283             }
0284 
0285         /// Copy image data from an area of one image to same area of another.
0286         /// \param src Image to copy from.
0287         /// \param area Rectangle of images to copy.
0288         /// \param plane Plane to start copying in src and this.
0289         /// \param planes Number of planes to copy.
0290 
0291         void CopyArea (const dng_image &src,
0292                        const dng_rect &area,
0293                        uint32 plane,
0294                        uint32 planes)
0295             {
0296 
0297             DoCopyArea (src, area, plane, plane, planes);
0298 
0299             }
0300 
0301         /// Return true if the contents of an area of the image are the same as those of another.
0302         /// \param rhs Image to compare against.
0303         /// \param area Rectangle of image to test.
0304         /// \param plane Plane to start comparing.
0305         /// \param planes Number of planes to compare.
0306 
0307         virtual bool EqualArea (const dng_image &rhs,
0308                                 const dng_rect &area,
0309                                 uint32 plane,
0310                                 uint32 planes) const;
0311 
0312         // Routines to set the entire image to a constant value.
0313 
0314         void SetConstant_uint8 (uint8 value,
0315                                 const dng_rect &area)
0316             {
0317 
0318             DNG_ASSERT (fPixelType == ttByte, "Mismatched pixel type");
0319 
0320             SetConstant ((uint32) value, area);
0321 
0322             }
0323 
0324         void SetConstant_uint8 (uint8 value)
0325             {
0326             SetConstant (value, Bounds ());
0327             }
0328 
0329         void SetConstant_uint16 (uint16 value,
0330                                  const dng_rect &area)
0331             {
0332 
0333             DNG_ASSERT (fPixelType == ttShort, "Mismatched pixel type");
0334 
0335             SetConstant ((uint32) value, area);
0336 
0337             }
0338 
0339         void SetConstant_uint16 (uint16 value)
0340             {
0341             SetConstant_uint16 (value, Bounds ());
0342             }
0343 
0344         void SetConstant_int16 (int16 value,
0345                                 const dng_rect &area)
0346             {
0347 
0348             DNG_ASSERT (fPixelType == ttSShort, "Mismatched pixel type");
0349 
0350             SetConstant ((uint32) (uint16) value, area);
0351 
0352             }
0353 
0354         void SetConstant_int16 (int16 value)
0355             {
0356             SetConstant_int16 (value, Bounds ());
0357             }
0358 
0359         void SetConstant_uint32 (uint32 value,
0360                                  const dng_rect &area)
0361             {
0362 
0363             DNG_ASSERT (fPixelType == ttLong, "Mismatched pixel type");
0364 
0365             SetConstant (value, area);
0366 
0367             }
0368 
0369         void SetConstant_uint32 (uint32 value)
0370             {
0371             SetConstant_uint32 (value, Bounds ());
0372             }
0373 
0374         void SetConstant_real32 (real32 value,
0375                                  const dng_rect &area)
0376             {
0377 
0378             DNG_ASSERT (fPixelType == ttFloat, "Mismatched pixel type");
0379 
0380             union
0381                 {
0382                 uint32 i;
0383                 real32 f;
0384                 } x;
0385 
0386             x.f = value;
0387 
0388             SetConstant (x.i, area);
0389 
0390             }
0391 
0392         void SetConstant_real32 (real32 value)
0393             {
0394             SetConstant_real32 (value, Bounds ());
0395             }
0396 
0397         virtual void GetRepeat (dng_pixel_buffer &buffer,
0398                                 const dng_rect &srcArea,
0399                                 const dng_rect &dstArea) const;
0400 
0401     protected:
0402 
0403         virtual void AcquireTileBuffer (dng_tile_buffer &buffer,
0404                                         const dng_rect &area,
0405                                         bool dirty) const;
0406 
0407         virtual void ReleaseTileBuffer (dng_tile_buffer &buffer) const;
0408 
0409         virtual void DoGet (dng_pixel_buffer &buffer) const;
0410 
0411         virtual void DoPut (const dng_pixel_buffer &buffer);
0412 
0413         virtual void DoCopyArea (const dng_image &src,
0414                                  const dng_rect &area,
0415                                  uint32 srcPlane,
0416                                  uint32 dstPlane,
0417                                  uint32 planes);
0418 
0419         void GetEdge (dng_pixel_buffer &buffer,
0420                       edge_option edgeOption,
0421                       const dng_rect &srcArea,
0422                       const dng_rect &dstArea) const;
0423 
0424         virtual void SetConstant (uint32 value,
0425                                   const dng_rect &area);
0426 
0427     };
0428 
0429 /*****************************************************************************/
0430 
0431 #endif
0432 
0433 /*****************************************************************************/