f6fa9322409b00164722d3d7b05ccf9698e041d0
[occt.git] / src / Xw / Xw_open_image.cxx
1 // Copyright (c) 1999-2012 OPEN CASCADE SAS
2 //
3 // The content of this file is subject to the Open CASCADE Technology Public
4 // License Version 6.5 (the "License"). You may not use the content of this file
5 // except in compliance with the License. Please obtain a copy of the License
6 // at http://www.opencascade.org and read it completely before using this file.
7 //
8 // The Initial Developer of the Original Code is Open CASCADE S.A.S., having its
9 // main offices at: 1, place des Freres Montgolfier, 78280 Guyancourt, France.
10 //
11 // The Original Code and all software distributed under the License is
12 // distributed on an "AS IS" basis, without warranty of any kind, and the
13 // Initial Developer hereby disclaims all such warranties, including without
14 // limitation, any warranties of merchantability, fitness for a particular
15 // purpose or non-infringement. Please see the License for the specific terms
16 // and conditions governing the rights and limitations under the License.
17
18
19 #include <Xw_Extension.h>
20
21 /* ifdef then trace on */
22 #ifdef TRACE
23 #define TRACE_OPEN_IMAGE
24 #endif
25
26 /*
27    XW_EXT_IMAGEDATA Xw_open_image (awindow,aimageinfo,width,height):
28    XW_EXT_WINDOW *awindow ;
29    XW_USERDATA *aimageinfo ;            Image identifier
30    int width,height             Image size in pixels
31
32         Open an Image in the same Window visual as defined
33         NOTE than this image can be filled with Xw_put_pixel 
34
35         Returns The image extension address if Successful      
36         returns  NULL if BadAllocation
37
38    XW_STATUS Xw_close_image (aimage):
39    XW_EXT_IMAGEDATA *aimage     Extended Image address
40
41         Close an Image
42
43         returns  SUCCESS always
44
45    XW_EXT_IMAGEDATA* Xw_get_image_handle (awindow,aimageinfo):
46    XW_EXT_WINDOW *awindow ;
47    XW_USERDATA  aimageinfo     Image identifier
48
49         returns the Image handle associated to the image ID
50                 or NULL if no image exist.
51
52 */
53
54 #ifdef XW_PROTOTYPE
55 void* Xw_open_image (void *awindow,void *aimageinfo,int width,int height)
56 #else
57 void* Xw_open_image (awindow,aimageinfo,width,height)
58 void *awindow ;
59 void *aimageinfo ;
60 int width,height ;
61 #endif /*XW_PROTOTYPE*/
62 {
63 XW_EXT_WINDOW *pwindow = (XW_EXT_WINDOW*)awindow;
64 XW_EXT_IMAGEDATA *pimage ;
65 XW_EXT_COLORMAP *pcolormap ;
66 //int i,pad ;
67 int pad ;
68 char *cdata ;
69
70     if( !Xw_isdefine_window(pwindow) ) {
71         /*ERROR*Bad EXT_WINDOW Address*/
72         Xw_set_error(24,"Xw_open_image",pwindow) ;
73         return (NULL) ;
74     }
75
76     switch (_DEPTH) {
77         case 8 :
78             pad = 1 ;
79             break ;
80         case 12 :
81         case 16 :
82             pad = 2 ;
83             break ;
84         case 24 :
85             pad = 4 ;
86             break ;
87         case 48 :
88         case 64 :
89             pad = 8 ;
90             break ;
91         default :
92             /*ERROR*Unimplemented Image Visual depth*/
93             Xw_set_error(64,"Xw_open_image",&_DEPTH) ;
94             return (NULL) ;
95     }
96
97     if( !(cdata = (char*) Xw_calloc(width*height,pad)) ) {
98         /*ERROR*XImage Allocation failed*/
99         Xw_set_error(60,"Xw_open_image",0) ;
100         return (NULL) ;
101     }
102
103     if( !(pimage = Xw_add_imagedata_structure(sizeof(XW_EXT_IMAGEDATA))) ) 
104                                                                 return (NULL) ;
105
106     pimage->pimageinfo = aimageinfo ;
107     pimage->pcolormap = pcolormap = _COLORMAP ;
108     _IIMAGE = XCreateImage(_CDISPLAY,_CVISUAL,_DEPTH,
109                         ZPixmap,0,cdata,width,height,pad*8,width*pad) ;
110     if( !_IIMAGE ) {
111         /*ERROR*XImage Creation failed*/
112         Xw_set_error(62,"Xw_open_image",0) ;
113         Xw_del_imagedata_structure(pimage) ;
114     }
115
116 #ifdef  TRACE_OPEN_IMAGE
117 if( Xw_get_trace() ) {
118     printf (" %lx = Xw_open_image(%lx,%lx,%d,%d)\n",(long ) pimage,(long ) pwindow,(long ) aimageinfo,
119                                                         width,height);
120 }
121 #endif
122
123     return (pimage);
124 }
125
126 static XW_EXT_IMAGEDATA *PimageList =NULL ;
127
128 #ifdef XW_PROTOTYPE
129 XW_EXT_IMAGEDATA* Xw_add_imagedata_structure(int size)
130 #else
131 XW_EXT_IMAGEDATA* Xw_add_imagedata_structure(size)
132 int size ;
133 #endif /*XW_PROTOTYPE*/
134 /*
135         Create and Insert one Extended image structure in the
136         EXtended image List
137
138         returns Extended image address if successful
139                 or NULL if Bad Allocation
140 */
141 {
142 XW_EXT_IMAGEDATA *pimage ;
143 //int i ;
144
145         pimage = (XW_EXT_IMAGEDATA*) Xw_malloc(size) ;
146
147         if( pimage ) {
148             pimage->type = IMAGE_TYPE ;
149             pimage->link = PimageList ;
150             PimageList = pimage ;
151             pimage->pcolormap = NULL ;
152             pimage->zoom = 1.;
153             pimage->pximage = NULL ;
154             pimage->zximage = NULL ;
155             pimage->maxwindow = 0 ;
156         } else {
157             /*ERROR*EXT_IMAGE allocation failed*/
158             Xw_set_error(27,"Xw_open_image",0) ;
159         }
160
161         return (pimage) ;
162 }
163
164 #ifdef XW_PROTOTYPE
165 XW_STATUS Xw_close_image (void* aimage)
166 #else
167 XW_STATUS Xw_close_image (aimage)
168 void *aimage;
169 #endif /*XW_PROTOTYPE*/
170 {
171 XW_EXT_IMAGEDATA *pimage = (XW_EXT_IMAGEDATA*) aimage ;
172
173         Xw_del_imagedata_structure(pimage) ;
174
175 #ifdef  TRACE_CLOSE_IMAGE
176 if( Xw_get_trace() ) {
177         printf (" Xw_close_image(%x)\n",pimage);
178 }
179 #endif
180
181         return (XW_SUCCESS);
182 }
183
184 #ifdef XW_PROTOTYPE
185 void* Xw_get_image_handle (void* awindow,void *aimageinfo)
186 #else
187 void* Xw_get_image_handle (awindow,aimageinfo)
188 void *awindow ;
189 void *aimageinfo ;
190 #endif /*XW_PROTOTYPE*/
191 {
192 #ifdef  TRACE_OPEN_IMAGE
193 XW_EXT_WINDOW *pwindow = (XW_EXT_WINDOW*)awindow ;
194 #endif
195 XW_EXT_IMAGEDATA *pimage ;
196
197         for( pimage = PimageList ; pimage ; 
198                         pimage = (XW_EXT_IMAGEDATA*) pimage->link ) {
199             if( pimage->pimageinfo == aimageinfo ) break ; 
200         }
201
202 #ifdef  TRACE_OPEN_IMAGE
203 if( Xw_get_trace() ) {
204     printf (" %lx = Xw_get_image_handle(%lx,%lx)\n",(long ) pimage,(long ) pwindow,(long ) aimageinfo);
205 }
206 #endif
207
208         return (pimage) ;
209 }
210
211 #ifdef XW_PROTOTYPE
212 XW_STATUS Xw_del_imagedata_structure(XW_EXT_IMAGEDATA *aimage)
213 #else
214 XW_STATUS Xw_del_imagedata_structure(aimage)
215 XW_EXT_IMAGEDATA *aimage;
216 #endif /*XW_PROTOTYPE*/
217 /*
218         Remove the Extended image address from the Extended List and
219         Free the Extended Image
220
221         returns ERROR if the image address is not Found in the list
222         returns XW_SUCCESS if successful
223 */
224 {
225 XW_EXT_IMAGEDATA *pimage = aimage ;
226 //int i ;
227
228     if( !pimage ) return (XW_ERROR) ;
229
230     if( pimage->maxwindow ) --pimage->maxwindow ;
231
232     if( pimage->maxwindow ) {
233         return (XW_ERROR) ;
234     } else {
235         if( _IIMAGE ) XDestroyImage(_IIMAGE) ;
236         if( _ZIMAGE ) XDestroyImage(_ZIMAGE) ;
237         if( pimage == PimageList ) {
238             PimageList = (XW_EXT_IMAGEDATA*) pimage->link ;
239         } else {
240             for( pimage = PimageList ; pimage ; 
241                         pimage = (XW_EXT_IMAGEDATA*) pimage->link ) {
242                 if( pimage->link == aimage ) {
243                     pimage->link = aimage->link ;
244                     break ;
245                 }
246             }
247         }
248         Xw_free(aimage) ;
249     }
250     return (XW_SUCCESS) ;
251 }