]> Creatis software - clitk.git/blob - utilities/CxImage/ximalyr.cpp
Correct bug to compute 2NotIn1
[clitk.git] / utilities / CxImage / ximalyr.cpp
1 // xImaLyr.cpp : Layers functions\r
2 /* 21/04/2003 v1.00 - Davide Pizzolato - www.xdp.it\r
3  * CxImage version 6.0.0 02/Feb/2008\r
4  */\r
5 \r
6 #include "ximage.h"\r
7 \r
8 #if CXIMAGE_SUPPORT_LAYERS\r
9 \r
10 ////////////////////////////////////////////////////////////////////////////////\r
11 /**\r
12  * If the object is an internal layer, GetParent return its parent in the hierarchy.\r
13  */\r
14 CxImage* CxImage::GetParent() const\r
15 {\r
16         return info.pParent;\r
17 }\r
18 ////////////////////////////////////////////////////////////////////////////////\r
19 /**\r
20  * Number of layers allocated directly by the object.\r
21  */\r
22 long CxImage::GetNumLayers() const\r
23 {\r
24         return info.nNumLayers;\r
25 }\r
26 ////////////////////////////////////////////////////////////////////////////////\r
27 /**\r
28  * Creates an empty layer. If position is less than 0, the new layer will be placed in the last position\r
29  */\r
30 bool CxImage::LayerCreate(long position)\r
31 {\r
32         if ( position < 0 || position > info.nNumLayers ) position = info.nNumLayers;\r
33 \r
34         CxImage** ptmp = new CxImage*[info.nNumLayers + 1];\r
35         if (ptmp==0) return false;\r
36 \r
37         int i=0;\r
38         for (int n=0; n<info.nNumLayers; n++){\r
39                 if (position == n){\r
40                         ptmp[n] = new CxImage();\r
41                         i=1;\r
42                 }\r
43                 ptmp[n+i]=ppLayers[n];\r
44         }\r
45         if (i==0) ptmp[info.nNumLayers] = new CxImage();\r
46 \r
47         if (ptmp[position]){\r
48                 ptmp[position]->info.pParent = this;\r
49         } else {\r
50                 free(ptmp);\r
51                 return false;\r
52         }\r
53 \r
54         info.nNumLayers++;\r
55         delete [] ppLayers;\r
56         ppLayers = ptmp;\r
57         return true;\r
58 }\r
59 ////////////////////////////////////////////////////////////////////////////////\r
60 /**\r
61  * Deletes a layer. If position is less than 0, the last layer will be deleted\r
62  */\r
63 bool CxImage::LayerDelete(long position)\r
64 {\r
65         if ( position >= info.nNumLayers ) return false;\r
66         if ( position < 0) position = info.nNumLayers - 1;\r
67         if ( position < 0) return false;\r
68 \r
69         if (info.nNumLayers>1){\r
70 \r
71                 CxImage** ptmp = new CxImage*[info.nNumLayers - 1];\r
72                 if (ptmp==0) return false;\r
73 \r
74                 int i=0;\r
75                 for (int n=0; n<info.nNumLayers; n++){\r
76                         if (position == n){\r
77                                 delete ppLayers[n];\r
78                                 i=1;\r
79                         }\r
80                         ptmp[n]=ppLayers[n+i];\r
81                 }\r
82 \r
83                 info.nNumLayers--;\r
84                 delete [] ppLayers;\r
85                 ppLayers = ptmp;\r
86 \r
87         } else {\r
88                 delete ppLayers[0];\r
89                 delete [] ppLayers;\r
90                 ppLayers = 0;\r
91                 info.nNumLayers = 0;\r
92         }\r
93         return true;\r
94 }\r
95 ////////////////////////////////////////////////////////////////////////////////\r
96 void CxImage::LayerDeleteAll()\r
97 {\r
98         if (ppLayers) { \r
99                 for(long n=0; n<info.nNumLayers;n++){ delete ppLayers[n]; }\r
100                 delete [] ppLayers; ppLayers=0; info.nNumLayers = 0;\r
101         }\r
102 }\r
103 ////////////////////////////////////////////////////////////////////////////////\r
104 /**\r
105  * Returns a pointer to a layer. If position is less than 0, the last layer will be returned\r
106  */\r
107 CxImage* CxImage::GetLayer(long position)\r
108 {\r
109         if ( ppLayers == NULL) return NULL;\r
110         if ( info.nNumLayers == 0) return NULL;\r
111         if ( position >= info.nNumLayers ) return NULL;\r
112         if ( position < 0) position = info.nNumLayers - 1;\r
113         return ppLayers[position];\r
114 }\r
115 ////////////////////////////////////////////////////////////////////////////////\r
116 #endif //CXIMAGE_SUPPORT_LAYERS\r