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.
18 // JR 01.02.100 : convert of float to int : (int )
20 #include <Xw_Extension.h>
22 /* ifdef then trace on */
24 # define TRACE_GAMMA_IMAGE
28 XW_STATUS Xw_gamma_image (aimage,gamma):
29 XW_EXT_IMAGEDATA *aimage Image where apply the gamma correction
30 float gamma gamma value to apply (>0.)
32 Apply a GAMMA correction to an image
34 returns ERROR if bad gamma value
35 returns SUCCESS if successfull
40 XW_STATUS Xw_gamma_image (void* aimage,float gamma)
42 XW_STATUS Xw_gamma_image (aimage,gamma)
45 #endif /*XW_PROTOTYPE*/
47 XW_EXT_IMAGEDATA *pimage = (XW_EXT_IMAGEDATA*)aimage ;
49 int wp,hp,xp,yp,ired,igreen,iblue;
50 unsigned long cmask,npixel,gpixel = 0,opixel = 0;
51 int sred,sgreen,sblue ;
52 float red,green,blue ;
54 if( !Xw_isdefine_image(pimage) ) {
55 /*ERROR*Bad EXT_IMAGE Address*/
56 Xw_set_error(25,"Xw_gamma_image",pimage) ;
61 /*ERROR*Bad Image GAMMA value*/
65 if( gamma == 1.0 ) return XW_SUCCESS;
67 pximage = pimage->pximage;
69 if( !pximage->red_mask || !pximage->green_mask || !pximage->blue_mask ) {
70 printf(" *** Xw_gamma_image.Cann't apply the gamma correction to this image\n");
76 wp = pximage->width; hp = pximage->height;
78 sred = sgreen = sblue = 0 ;
79 cmask = pximage->red_mask ;
80 while ( !(cmask & 1) ) { cmask >>= 1 ; sred++ ; }
81 cmask = pximage->green_mask ;
82 while ( !(cmask & 1) ) { cmask >>= 1 ; sgreen++ ; }
83 cmask = pximage->blue_mask ;
84 while ( !(cmask & 1) ) { cmask >>= 1 ; sblue++ ; }
86 for( yp=0 ; yp<hp ; yp++ ) {
87 for( xp=0 ; xp<wp ; xp++ ) {
88 npixel = XGetPixel(pximage,xp,yp) ;
89 if( npixel != opixel ) {
91 ired = (npixel >> sred) & cmask;
92 igreen = (npixel >> sgreen) & cmask;
93 iblue = (npixel >> sblue) & cmask;
95 red = (float)(ired)/(float)cmask;
96 green = (float)(igreen)/(float)cmask;
97 blue = (float)(iblue)/(float)cmask;
99 red = min(1.,pow(double(red),double(gamma)));
100 green = min(1.,pow(double(green),double(gamma)));
101 blue = min(1.,pow(double(blue),double(gamma)));
103 ired = (int )( red * cmask);
104 igreen = (int )( green * cmask);
105 iblue = (int )( blue * cmask);
107 gpixel = (ired << sred) | (igreen << sgreen) | (iblue << sblue);
109 printf(" npixel %ld gpixel %ld cmask %ld sred %d sgreen %d sblue %d\n",
110 npixel,gpixel,cmask,sred,sgreen,sblue);
113 XPutPixel(pximage,xp,yp,gpixel) ;
117 #ifdef TRACE_GAMMA_IMAGE
118 if( Xw_get_trace() ) {
119 printf (" Xw_gamma_image(%lx,%f)\n",(long ) pimage,gamma);