0026936: Drawbacks of inlining in new type system in OCCT 7.0 -- automatic
[occt.git] / src / Image / Image_PixMapData.hxx
1 // Created on: 2012-07-18
2 // Created by: Kirill GAVRILOV
3 // Copyright (c) 2012-2014 OPEN CASCADE SAS
4 //
5 // This file is part of Open CASCADE Technology software library.
6 //
7 // This library is free software; you can redistribute it and/or modify it under
8 // the terms of the GNU Lesser General Public License version 2.1 as published
9 // by the Free Software Foundation, with special exception defined in the file
10 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
11 // distribution for complete text of the license and disclaimer of any warranty.
12 //
13 // Alternatively, this file may be used under the terms of Open CASCADE
14 // commercial license or contractual agreement.
15
16 #ifndef _Image_PixMapData_H__
17 #define _Image_PixMapData_H__
18
19 #include <Image_Color.hxx>
20 #include <NCollection_Buffer.hxx>
21
22 //! Structure to manage image buffer.
23 class Image_PixMapData : public NCollection_Buffer
24 {
25 public:
26
27   //! Empty constructor.
28   Image_PixMapData()
29   : NCollection_Buffer (Handle(NCollection_BaseAllocator)()),
30     myTopRowPtr  (NULL),
31     SizeBPP      (0),
32     SizeX        (0),
33     SizeY        (0),
34     SizeRowBytes (0),
35     TopToDown    (Standard_Size(-1))
36   {
37     //
38   }
39
40   //! Initializer.
41   void Init (const Handle(NCollection_BaseAllocator)& theAlloc,
42              const Standard_Size                      theSizeBPP,
43              const Standard_Size                      theSizeX,
44              const Standard_Size                      theSizeY,
45              const Standard_Size                      theSizeRowBytes,
46              Standard_Byte*                           theDataPtr)
47   {
48     SetAllocator (theAlloc); // will free old data as well
49
50     myData       = theDataPtr;
51     myTopRowPtr  = NULL;
52     SizeBPP      = theSizeBPP;
53     SizeX        = theSizeX;
54     SizeY        = theSizeY;
55     SizeRowBytes = theSizeRowBytes != 0 ? theSizeRowBytes : (theSizeX * theSizeBPP);
56     mySize       = SizeRowBytes * SizeY;
57     if (myData == NULL)
58     {
59       Allocate (mySize);
60     }
61     SetTopDown (TopToDown == 1);
62   }
63
64   //! @return data pointer to requested row (first column).
65   inline const Standard_Byte* Row (const Standard_Size theRow) const
66   {
67     return myTopRowPtr + SizeRowBytes * theRow * TopToDown;
68   }
69
70   //! @return data pointer to requested row (first column).
71   inline Standard_Byte* ChangeRow (const Standard_Size theRow)
72   {
73     return myTopRowPtr + SizeRowBytes * theRow * TopToDown;
74   }
75
76   //! @return data pointer to requested position.
77   inline const Standard_Byte* Value (const Standard_Size theRow,
78                                      const Standard_Size theCol) const
79   {
80     return myTopRowPtr + SizeRowBytes * theRow * TopToDown + SizeBPP * theCol;
81   }
82
83   //! @return data pointer to requested position.
84   inline Standard_Byte* ChangeValue (const Standard_Size theRow,
85                                      const Standard_Size theCol)
86   {
87     return myTopRowPtr + SizeRowBytes * theRow * TopToDown + SizeBPP * theCol;
88   }
89
90   //! Compute the maximal row alignment for current row size.
91   //! @return maximal row alignment in bytes (up to 16 bytes).
92   inline Standard_Size MaxRowAligmentBytes() const
93   {
94     Standard_Size anAlignment = 2;
95     for (; anAlignment <= 16; anAlignment <<= 1)
96     {
97       if ((SizeRowBytes % anAlignment) != 0 || (Standard_Size(myData) % anAlignment) != 0)
98       {
99         return (anAlignment >> 1);
100       }
101     }
102     return anAlignment;
103   }
104
105   //! Setup scanlines order in memory - top-down or bottom-up.
106   //! Drawers should explicitly specify this value if current state IsTopDown() was ignored!
107   //! @param theIsTopDown top-down flag
108   inline void SetTopDown (const bool theIsTopDown)
109   {
110     TopToDown   = (theIsTopDown ? 1 : Standard_Size(-1));
111     myTopRowPtr = ((TopToDown == 1 || myData == NULL)
112                 ? myData : (myData + SizeRowBytes * (SizeY - 1)));
113   }
114
115 protected:
116
117   Standard_Byte* myTopRowPtr;  //!< pointer to the topmost row (depending on scanlines order in memory)
118
119 public:
120
121   Standard_Size  SizeBPP;      //!< bytes per pixel
122   Standard_Size  SizeX;        //!< width  in pixels
123   Standard_Size  SizeY;        //!< height in pixels
124   Standard_Size  SizeRowBytes; //!< number of bytes per line (in most cases equal to 3 * sizeX)
125   Standard_Size  TopToDown;    //!< image scanlines direction in memory from Top to the Down
126
127
128 public:
129
130   DEFINE_STANDARD_RTTI_INLINE(Image_PixMapData,NCollection_Buffer) // Type definition
131
132 };
133
134 DEFINE_STANDARD_HANDLE(Image_PixMapData, NCollection_Buffer)
135
136 #endif // _Image_PixMapData_H__