1 // Copyright (c) 1999-2012 OPEN CASCADE SAS
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.
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.
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.
20 C***********************************************************************
26 C$ HISTORIQUE DES MODIFICATIONS :
27 C --------------------------------
28 C 21-02-97 : GG ; G1334-PERF
29 C Ameliorer les performances de traduction
31 C >> Rempacer XGetPixel et XputPixel par des
32 C directes dans le buffer image.
33 C 07-05-97 : GG ; PRO7909
34 C Eviter de detruire l'image convertie lorsqu'il n'y a pas
35 C de conversion a faire !
40 #include <Xw_Extension.h>
42 /* ifdef then trace on */
44 #define TRACE_CONVERT_IMAGE
48 XW_STATUS Xw_convert_image (awindow,aimage,acolors,ncolors):
49 XW_EXT_WINDOW *awindow
50 XW_EXT_IMAGEDATA *aimage
51 XW_EXT_COLOR *acolors (can be NULL)
54 Convert an image depending of the window class
56 returns ERROR if image can't be converted.
57 returns SUCCESS if successfull
61 #define CREATE_IMAGE(_fun,_out,_nbp) \
62 /* Allocate the new pixel buffer */ \
63 if( !(_out = (unsigned char*) Xw_calloc(piimage->width * piimage->height,_nbp)) ) {\
64 /*ERROR*Bad Image allocation*/ \
65 Xw_set_error(60,_fun,0) ; \
68 /* Create the new Image structure */ \
69 if( !(poimage = XCreateImage(_DISPLAY,_VISUAL,_DEPTH,ZPixmap,\
70 0,(char*)_out,piimage->width,piimage->height,_nbp*8,0)) ) {\
71 /* ERROR*XImage Creation failed */ \
72 Xw_set_error(62,_fun,0) ; \
73 Xw_free((char*)_out) ; \
77 #define COPY_PSEUDO_TO_PSEUDO(_fun,_in,_out) \
78 for( y=0 ; y<piimage->height ; y++ ) { \
79 for( x=0 ; x<piimage->width ; x++ ) { \
81 if( ipixel != lipixel ) { \
83 if( ipixel >= MAXCOLOR ) ipixel = 0 ; \
84 opixel = mapping[ipixel] ; \
86 for( i=0,j=ipixel ; i<ncolors ; i++,j++) { \
87 if( j >= ncolors ) j = 0 ; \
88 if( ipixel == pcolors[j].pixel ) break; \
90 red = (float)pcolors[j].red/0xFFFF ; \
91 green = (float)pcolors[j].green/0xFFFF ; \
92 blue = (float)pcolors[j].blue/0xFFFF ; \
93 if( !Xw_get_color_pixel(_COLORMAP,red,green,blue,&opixel,&isapproximate)) {\
94 /*ERROR*XImage Creation failed*/ \
95 Xw_set_error(63,_fun,&_COLORMAP->info.colormap) ;\
97 mapping[ipixel] = opixel ; \
102 if( ipad > 0 ) pidata.cdata += ipad; \
105 #define COPY_PSEUDO_TO_TRUE(_fun,_in,_out) \
106 for( y=0 ; y<piimage->height ; y++ ) { \
107 for( x=0 ; x<piimage->width ; x++ ) { \
109 if( ipixel != lipixel ) { \
111 if( ipixel >= MAXCOLOR ) ipixel = 0 ; \
112 opixel = mapping[ipixel] ; \
114 for( i=0,j=ipixel ; i<ncolors ; i++,j++) { \
115 if( j >= ncolors ) j = 0 ; \
116 if( ipixel == pcolors[j].pixel ) break; \
118 red = (float)pcolors[j].red/0xFFFF ; \
119 green = (float)pcolors[j].green/0xFFFF ; \
120 blue = (float)pcolors[j].blue/0xFFFF ; \
121 if( !Xw_get_color_pixel(_COLORMAP,red,green,blue,&opixel,&isapproximate)) {\
122 /*ERROR*XImage Creation failed*/ \
123 Xw_set_error(63,_fun,&_COLORMAP->info.colormap) ;\
125 mapping[ipixel] = opixel ; \
130 if( ipad > 0 ) pidata.cdata += ipad; \
133 #define COPY_TRUE_TO_PSEUDO(_fun,_in,_out) \
134 for( y=0 ; y<piimage->height ; y++ ) { \
135 for( x=0 ; x<piimage->width ; x++ ) { \
137 if( ipixel != lipixel ) { \
139 red = (float)((ipixel >> sred) & cmask)/(float)cmask; \
140 green = (float)((ipixel >> sgreen) & cmask)/(float)cmask; \
141 blue = (float)((ipixel >> sblue) & cmask)/(float)cmask; \
142 if( !Xw_get_color_pixel(_COLORMAP,red,green,blue,&opixel,&isapproximate)) {\
143 /*ERROR*XImage Creation failed*/ \
144 Xw_set_error(63,_fun,&_COLORMAP->info.colormap) ; \
149 if( ipad > 0 ) pidata.cdata += ipad; \
152 #define COPY_TRUE_TO_TRUE(_fun,_in,_out) \
153 for( y=0 ; y<piimage->height ; y++ ) { \
154 for( x=0 ; x<piimage->width ; x++ ) { \
156 if( ipixel != lipixel ) { \
158 red = (float)((ipixel >> sred) & cmask)/(float)cmask; \
159 green = (float)((ipixel >> sgreen) & cmask)/(float)cmask; \
160 blue = (float)((ipixel >> sblue) & cmask)/(float)cmask; \
161 if( !Xw_get_color_pixel(_COLORMAP,red,green,blue,&opixel,&isapproximate)) {\
162 /*ERROR*XImage Creation failed*/ \
163 Xw_set_error(63,_fun,&_COLORMAP->info.colormap) ; \
168 if( ipad > 0 ) pidata.cdata += ipad; \
173 static XImage* ConvertPseudoToPseudo(XW_EXT_WINDOW *pwindow,XImage *piimage,XColor *pcolors,int ncolors) ;
174 static XImage* ConvertPseudoToTrue(XW_EXT_WINDOW *pwindow,XImage *piimage,XColor *pcolors,int ncolors) ;
175 static XImage* ConvertTrueToPseudo(XW_EXT_WINDOW *pwindow,XImage *piimage) ;
176 static XImage* ConvertTrueToTrue(XW_EXT_WINDOW *pwindow,XImage *piimage) ;
178 static XImage* ConvertPseudoToPseudo() ;
179 static XImage* ConvertPseudoToTrue() ;
180 static XImage* ConvertTrueToPseudo() ;
181 static XImage* ConvertTrueToTrue() ;
182 #endif /*XW_PROTOTYPE*/
185 XW_STATUS Xw_convert_image (void *awindow,void *aimage,void *acolors,int ncolors)
187 XW_STATUS Xw_convert_image (awindow,aimage,acolors,ncolors)
192 #endif /*XW_PROTOTYPE*/
194 XW_EXT_WINDOW *pwindow = (XW_EXT_WINDOW*)awindow;
195 XW_EXT_IMAGEDATA *pimage = (XW_EXT_IMAGEDATA*)aimage;
196 XColor *pcolors = (XColor*)acolors;
197 XImage *piimage,*poimage = NULL;
198 XW_STATUS status = XW_ERROR;
201 if( !Xw_isdefine_window(pwindow) ) {
202 /*ERROR*Bad EXT_WINDOW Address*/
203 Xw_set_error(24,"Xw_convert_image",pwindow) ;
207 if( !Xw_isdefine_image(pimage) ) {
208 /*ERROR*Bad EXT_IMAGEDATA Address*/
209 Xw_set_error(25,"Xw_convert_image",pimage) ;
213 piimage = (_ZIMAGE) ? _ZIMAGE : _IIMAGE;
215 if( !pcolors && piimage->red_mask &&
216 piimage->green_mask && piimage->blue_mask ) iclass = TrueColor;
217 else iclass = PseudoColor;
223 if( !_ICOLORMAP ) _ICOLORMAP = _COLORMAP;
224 ncolors = min(_IVISUAL->map_entries,MAXCOLOR);
225 pcolors = (XColor*) Xw_calloc(sizeof(XColor),ncolors);
226 for( i=0 ; i<ncolors ; i++ ) pcolors[i].pixel = i;
227 XQueryColors(_DISPLAY,_ICOLORMAP->info.colormap,pcolors,ncolors) ;
230 if( pcolors ) switch (_CLASS ) {
231 case PseudoColor : /* Convert Pseudo to PseudoColor */
232 if( acolors || (_ICOLORMAP != _COLORMAP) ) {
233 poimage = ConvertPseudoToPseudo(pwindow,piimage,pcolors,ncolors) ;
238 case TrueColor : /* Convert Pseudo to TrueColor */
239 poimage = ConvertPseudoToTrue(pwindow,piimage,pcolors,ncolors) ;
242 /*ERROR*Unimplemented Visual class*/
243 Xw_set_error(5,"Xw_convert_image",&_CLASS) ;
245 #ifdef BUG /* Cette memoire est liberee par Xw_load_image !! */
246 if( pcolors && !acolors ) Xw_free(pcolors);
252 case PseudoColor : /* Convert True to PseudoColor */
253 poimage = ConvertTrueToPseudo(pwindow,piimage) ;
255 case TrueColor : /* Convert True to TrueColor */
256 poimage = ConvertTrueToTrue(pwindow,piimage) ;
259 /*ERROR*Unimplemented Visual class*/
260 Xw_set_error(5,"Xw_convert_image",&_CLASS) ;
265 /*ERROR*Unimplemented Visual class*/
266 Xw_set_error(5,"Xw_convert_image",&iclass) ;
269 _ICOLORMAP = _COLORMAP;
272 /* Destroy the old image */
273 if( _ZIMAGE && (_ZIMAGE != _IIMAGE) ) {
274 XDestroyImage(_ZIMAGE);
277 /* Initialize the output image */
279 if( _IIMAGE != poimage ) {
280 XDestroyImage(_IIMAGE);
284 XDestroyImage(_IIMAGE);
290 #ifdef TRACE_CONVERT_IMAGE
291 if( Xw_get_trace() ) {
292 printf (" %d = Xw_convert_image(%lx,%lx,%lx,%d)\n",
293 status,(long ) pwindow,(long ) pimage,(long ) pcolors,ncolors);
301 static XImage* ConvertTrueToPseudo(XW_EXT_WINDOW *pwindow,XImage *piimage)
303 static XImage* ConvertTrueToPseudo(pwindow,piimage)
304 XW_EXT_WINDOW *pwindow ;
306 #endif /*XW_PROTOTYPE*/
308 Convert TrueColor to PseudoColor Image
311 unsigned long ipixel,opixel,cmask ;
312 unsigned long lipixel = 0xFFFFFFFF ;
313 //int i,j,ipad,isapproximate ;
314 int ipad,isapproximate ;
315 int sred,sgreen,sblue ;
316 float red,green,blue ;
318 unsigned char *cdata ;
319 unsigned short *sdata ;
320 unsigned int *idata ;
323 XImage *poimage = NULL ;
325 #ifdef TRACE_CONVERT_IMAGE
326 if( Xw_get_trace() ) {
327 printf(" Xw_convert_image.TrueToPseudo()\n") ;
331 ipad = piimage->bytes_per_line -
332 piimage->width * piimage->bits_per_pixel/8 ;
333 pidata.cdata = (unsigned char*)piimage->data + piimage->xoffset;
335 sred = sgreen = sblue = 0 ;
336 cmask = piimage->red_mask ;
337 while ( !(cmask & 1) ) { cmask >>= 1 ; sred++ ; }
338 cmask = piimage->green_mask ;
339 while ( !(cmask & 1) ) { cmask >>= 1 ; sgreen++ ; }
340 cmask = piimage->blue_mask ;
341 while ( !(cmask & 1) ) { cmask >>= 1 ; sblue++ ; }
343 CREATE_IMAGE("ConvertTrueToPseudo",podata.cdata,1)
344 switch (piimage->bits_per_pixel) {
346 COPY_TRUE_TO_PSEUDO("ConvertTrueToPseudo",pidata.cdata,podata.cdata);
349 COPY_TRUE_TO_PSEUDO("ConvertTrueToPseudo",pidata.sdata,podata.cdata);
352 COPY_TRUE_TO_PSEUDO("ConvertTrueToPseudo",pidata.idata,podata.cdata);
355 /*ERROR*Unimplemented Image Visual depth*/
356 Xw_set_error(64,"ConvertTrueToPseudo",&piimage->bits_per_pixel);
363 static XImage* ConvertPseudoToTrue(XW_EXT_WINDOW *pwindow,XImage *piimage,XColor *pcolors,int ncolors)
365 static XImage* ConvertPseudoToTrue(pwindow,piimage,pcolors,ncolors)
366 XW_EXT_WINDOW *pwindow ;
370 #endif /*XW_PROTOTYPE*/
372 Convert PseudoColor to TrueColor Image
375 unsigned long ipixel,opixel ;
376 unsigned long lipixel = 0xFFFFFFFF ;
377 unsigned long mapping[MAXCOLOR];
378 int i,j,ipad,isapproximate ;
379 float red,green,blue ;
381 unsigned char *cdata ;
382 unsigned short *sdata ;
383 unsigned int *idata ;
386 XImage *poimage = NULL ;
388 #ifdef TRACE_CONVERT_IMAGE
389 if( Xw_get_trace() ) {
390 printf(" Xw_convert_image.PseudoToTrue()\n") ;
394 for( i=0 ; i<MAXCOLOR ; i++ ) mapping[i] = 0;
395 ipad = piimage->bytes_per_line -
396 piimage->width * piimage->bits_per_pixel/8 ;
397 pidata.cdata = (unsigned char*)piimage->data + piimage->xoffset;
402 CREATE_IMAGE("ConvertPseudoToTrue",podata.cdata,1)
403 COPY_PSEUDO_TO_TRUE("ConvertPseudoToTrue",pidata.cdata,podata.cdata);
406 CREATE_IMAGE("ConvertPseudoToTrue",podata.cdata,2)
407 COPY_PSEUDO_TO_TRUE("ConvertPseudoToTrue",pidata.cdata,podata.sdata);
410 CREATE_IMAGE("ConvertPseudoToTrue",podata.cdata,4)
411 COPY_PSEUDO_TO_TRUE("ConvertPseudoToTrue",pidata.cdata,podata.idata);
414 /*ERROR*Unimplemented Image Visual depth*/
415 Xw_set_error(64,"ConvertPseudoToTrue",&_DEPTH);
423 static XImage* ConvertPseudoToPseudo(XW_EXT_WINDOW *pwindow,XImage *piimage,XColor *pcolors,int ncolors)
425 static XImage* ConvertPseudoToPseudo(pwindow,piimage,pcolors,ncolors)
426 XW_EXT_WINDOW *pwindow ;
430 #endif /*XW_PROTOTYPE*/
432 Convert PseudoColor to PseudoColor Image
435 unsigned long ipixel,opixel ;
436 unsigned long lipixel = 0xFFFFFFFF ;
437 unsigned long mapping[MAXCOLOR];
438 int i,j,ipad,isapproximate ;
439 float red,green,blue ;
441 unsigned char *cdata ;
442 unsigned short *sdata ;
443 unsigned int *idata ;
446 XImage *poimage = NULL ;
448 #ifdef TRACE_CONVERT_IMAGE
449 if( Xw_get_trace() ) {
450 printf(" Xw_convert_image.PseudoToPseudo()\n") ;
454 for( i=0 ; i<MAXCOLOR ; i++ ) mapping[i] = 0;
455 ipad = piimage->bytes_per_line -
456 piimage->width * piimage->bits_per_pixel/8 ;
457 pidata.cdata = (unsigned char*)piimage->data + piimage->xoffset;
462 CREATE_IMAGE("ConvertPseudoToPseudo",podata.cdata,1)
463 COPY_PSEUDO_TO_PSEUDO("ConvertPseudoToPseudo",pidata.cdata,podata.cdata);
466 /*ERROR*Unimplemented Image Visual depth*/
467 Xw_set_error(64,"ConvertPseudoToPseudo",&_DEPTH);
475 static XImage* ConvertTrueToTrue(XW_EXT_WINDOW *pwindow,XImage *piimage)
477 static XImage* ConvertTrueToTrue(pwindow,piimage)
478 XW_EXT_WINDOW *pwindow ;
480 #endif /*XW_PROTOTYPE*/
482 Convert TrueColor to TrueColor Image
485 unsigned long ipixel,opixel,cmask ;
486 unsigned long lipixel = 0xFFFFFFFF ;
487 //int i,j,ipad,isapproximate ;
488 int ipad,isapproximate ;
489 int sred,sgreen,sblue ;
490 float red,green,blue ;
492 unsigned char *cdata ;
493 unsigned short *sdata ;
494 unsigned int *idata ;
497 XImage *poimage = NULL ;
499 #ifdef TRACE_CONVERT_IMAGE
500 if( Xw_get_trace() ) {
501 printf(" Xw_convert_image.TrueToTrue()\n") ;
505 if( (_DEPTH == piimage->depth) &&
506 (_VISUAL->red_mask == piimage->red_mask) &&
507 (_VISUAL->green_mask == piimage->green_mask) &&
508 (_VISUAL->blue_mask == piimage->blue_mask) ) return piimage;
510 ipad = piimage->bytes_per_line -
511 piimage->width * piimage->bits_per_pixel/8 ;
512 pidata.cdata = (unsigned char*)piimage->data + piimage->xoffset;
514 sred = sgreen = sblue = 0 ;
515 cmask = piimage->red_mask ;
516 while ( !(cmask & 1) ) { cmask >>= 1 ; sred++ ; }
517 cmask = piimage->green_mask ;
518 while ( !(cmask & 1) ) { cmask >>= 1 ; sgreen++ ; }
519 cmask = piimage->blue_mask ;
520 while ( !(cmask & 1) ) { cmask >>= 1 ; sblue++ ; }
525 CREATE_IMAGE("ConvertTrueToTrue",podata.cdata,1)
526 switch (piimage->bits_per_pixel) {
528 COPY_TRUE_TO_TRUE("ConvertTrueToTrue",pidata.cdata,podata.cdata);
531 COPY_TRUE_TO_TRUE("ConvertTrueToTrue",pidata.sdata,podata.cdata);
534 COPY_TRUE_TO_TRUE("ConvertTrueToTrue",pidata.idata,podata.cdata);
537 /*ERROR*Unimplemented Image Visual depth*/
538 Xw_set_error(64,"ConvertTrueToTrue",&piimage->bits_per_pixel);
542 CREATE_IMAGE("ConvertTrueToTrue",podata.cdata,2)
543 switch (piimage->bits_per_pixel) {
545 COPY_TRUE_TO_TRUE("ConvertTrueToTrue",pidata.cdata,podata.sdata);
548 COPY_TRUE_TO_TRUE("ConvertTrueToTrue",pidata.sdata,podata.sdata);
551 COPY_TRUE_TO_TRUE("ConvertTrueToTrue",pidata.idata,podata.sdata);
554 /*ERROR*Unimplemented Image Visual depth*/
555 Xw_set_error(64,"ConvertTrueToTrue",&piimage->bits_per_pixel);
559 CREATE_IMAGE("ConvertTrueToTrue",podata.cdata,4)
560 switch (piimage->bits_per_pixel) {
562 COPY_TRUE_TO_TRUE("ConvertTrueToTrue",pidata.cdata,podata.idata);
565 COPY_TRUE_TO_TRUE("ConvertTrueToTrue",pidata.sdata,podata.idata);
568 COPY_TRUE_TO_TRUE("ConvertTrueToTrue",pidata.idata,podata.idata);
571 /*ERROR*Unimplemented Image Visual depth*/
572 Xw_set_error(64,"ConvertTrueToTrue",&piimage->bits_per_pixel);
576 /*ERROR*Unimplemented Image Visual depth*/
577 Xw_set_error(64,"ConvertTrueToTrue",&_DEPTH);