ac0e8ecbe262e3619db1954e92bdf0532aadb1eb
[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 = 0;
68 char *cdata = NULL;
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         Xw_free(cdata);
105         return (NULL) ;
106     }
107
108     pimage->pimageinfo = aimageinfo ;
109     pimage->pcolormap = pcolormap = _COLORMAP ;
110     _IIMAGE = XCreateImage(_CDISPLAY,_CVISUAL,_DEPTH,
111                         ZPixmap,0,cdata,width,height,pad*8,width*pad) ;
112     if( !_IIMAGE ) {
113         /*ERROR*XImage Creation failed*/
114         Xw_set_error(62,"Xw_open_image",0) ;
115         Xw_del_imagedata_structure(pimage) ;
116     }
117
118 #ifdef  TRACE_OPEN_IMAGE
119 if( Xw_get_trace() ) {
120     printf (" %lx = Xw_open_image(%lx,%lx,%d,%d)\n",(long ) pimage,(long ) pwindow,(long ) aimageinfo,
121                                                         width,height);
122 }
123 #endif
124
125     return (pimage);
126 }
127
128 static XW_EXT_IMAGEDATA *PimageList =NULL ;
129
130 #ifdef XW_PROTOTYPE
131 XW_EXT_IMAGEDATA* Xw_add_imagedata_structure(int size)
132 #else
133 XW_EXT_IMAGEDATA* Xw_add_imagedata_structure(size)
134 int size ;
135 #endif /*XW_PROTOTYPE*/
136 /*
137         Create and Insert one Extended image structure in the
138         EXtended image List
139
140         returns Extended image address if successful
141                 or NULL if Bad Allocation
142 */
143 {
144 XW_EXT_IMAGEDATA *pimage ;
145 //int i ;
146
147         pimage = (XW_EXT_IMAGEDATA*) Xw_malloc(size) ;
148
149         if( pimage ) {
150             pimage->type = IMAGE_TYPE ;
151             pimage->link = PimageList ;
152             PimageList = pimage ;
153             pimage->pcolormap = NULL ;
154             pimage->zoom = 1.;
155             pimage->pximage = NULL ;
156             pimage->zximage = NULL ;
157             pimage->maxwindow = 0 ;
158         } else {
159             /*ERROR*EXT_IMAGE allocation failed*/
160             Xw_set_error(27,"Xw_open_image",0) ;
161         }
162
163         return (pimage) ;
164 }
165
166 #ifdef XW_PROTOTYPE
167 XW_STATUS Xw_close_image (void* aimage)
168 #else
169 XW_STATUS Xw_close_image (aimage)
170 void *aimage;
171 #endif /*XW_PROTOTYPE*/
172 {
173 XW_EXT_IMAGEDATA *pimage = (XW_EXT_IMAGEDATA*) aimage ;
174
175         Xw_del_imagedata_structure(pimage) ;
176
177 #ifdef  TRACE_CLOSE_IMAGE
178 if( Xw_get_trace() ) {
179         printf (" Xw_close_image(%x)\n",pimage);
180 }
181 #endif
182
183         return (XW_SUCCESS);
184 }
185
186 #ifdef XW_PROTOTYPE
187 void* Xw_get_image_handle (void* awindow,void *aimageinfo)
188 #else
189 void* Xw_get_image_handle (awindow,aimageinfo)
190 void *awindow ;
191 void *aimageinfo ;
192 #endif /*XW_PROTOTYPE*/
193 {
194 #ifdef  TRACE_OPEN_IMAGE
195 XW_EXT_WINDOW *pwindow = (XW_EXT_WINDOW*)awindow ;
196 #endif
197 XW_EXT_IMAGEDATA *pimage ;
198
199         for( pimage = PimageList ; pimage ; 
200                         pimage = (XW_EXT_IMAGEDATA*) pimage->link ) {
201             if( pimage->pimageinfo == aimageinfo ) break ; 
202         }
203
204 #ifdef  TRACE_OPEN_IMAGE
205 if( Xw_get_trace() ) {
206     printf (" %lx = Xw_get_image_handle(%lx,%lx)\n",(long ) pimage,(long ) pwindow,(long ) aimageinfo);
207 }
208 #endif
209
210         return (pimage) ;
211 }
212
213 #ifdef XW_PROTOTYPE
214 XW_STATUS Xw_del_imagedata_structure(XW_EXT_IMAGEDATA *aimage)
215 #else
216 XW_STATUS Xw_del_imagedata_structure(aimage)
217 XW_EXT_IMAGEDATA *aimage;
218 #endif /*XW_PROTOTYPE*/
219 /*
220         Remove the Extended image address from the Extended List and
221         Free the Extended Image
222
223         returns ERROR if the image address is not Found in the list
224         returns XW_SUCCESS if successful
225 */
226 {
227 XW_EXT_IMAGEDATA *pimage = aimage ;
228 //int i ;
229
230     if( !pimage ) return (XW_ERROR) ;
231
232     if( pimage->maxwindow ) --pimage->maxwindow ;
233
234     if( pimage->maxwindow ) {
235         return (XW_ERROR) ;
236     } else {
237         if( _IIMAGE ) XDestroyImage(_IIMAGE) ;
238         if( _ZIMAGE ) XDestroyImage(_ZIMAGE) ;
239         if( pimage == PimageList ) {
240             PimageList = (XW_EXT_IMAGEDATA*) pimage->link ;
241         } else {
242             for( pimage = PimageList ; pimage ; 
243                         pimage = (XW_EXT_IMAGEDATA*) pimage->link ) {
244                 if( pimage->link == aimage ) {
245                     pimage->link = aimage->link ;
246                     break ;
247                 }
248             }
249         }
250         Xw_free(aimage) ;
251     }
252     return (XW_SUCCESS) ;
253 }