0023533: Unitialized variables used, IntTools_TopolTool.cxx
[occt.git] / src / Xw / Xw_gamma_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 // JR 01.02.100 : convert of float to int : (int )
19
20 #include <Xw_Extension.h>
21
22 /* ifdef then trace on */
23 #ifdef TRACE
24 # define TRACE_GAMMA_IMAGE
25 #endif
26
27 /*
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.)
31
32         Apply a GAMMA correction to an image
33
34         returns  ERROR if bad gamma value
35         returns  SUCCESS if successfull
36
37 */
38
39 #ifdef XW_PROTOTYPE
40 XW_STATUS Xw_gamma_image (void* aimage,float gamma)
41 #else
42 XW_STATUS Xw_gamma_image (aimage,gamma)
43 void *aimage;
44 float gamma ;
45 #endif /*XW_PROTOTYPE*/
46 {
47 XW_EXT_IMAGEDATA *pimage = (XW_EXT_IMAGEDATA*)aimage ;
48 XImage *pximage;
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 ;
53
54     if( !Xw_isdefine_image(pimage) ) {
55         /*ERROR*Bad EXT_IMAGE Address*/
56         Xw_set_error(25,"Xw_gamma_image",pimage) ;
57         return (XW_ERROR) ;
58     }
59
60     if( gamma <= 0. ) {
61         /*ERROR*Bad Image GAMMA value*/
62         return (XW_ERROR) ;
63     }
64
65     if( gamma == 1.0 ) return XW_SUCCESS;
66
67     pximage = pimage->pximage;
68
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");
71         return (XW_ERROR) ;
72     }
73
74     gamma = 1./gamma;
75
76     wp = pximage->width; hp = pximage->height;
77
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++ ; }
85
86     for( yp=0 ; yp<hp ; yp++ ) {
87       for( xp=0 ; xp<wp ; xp++ ) {
88         npixel = XGetPixel(pximage,xp,yp) ;
89         if( npixel != opixel ) {
90           opixel = npixel;
91           ired = (npixel >> sred) & cmask;
92           igreen = (npixel >> sgreen) & cmask;
93           iblue = (npixel >> sblue) & cmask;
94
95           red = (float)(ired)/(float)cmask;
96           green = (float)(igreen)/(float)cmask;
97           blue = (float)(iblue)/(float)cmask;
98
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)));
102
103           ired = (int )( red * cmask);
104           igreen = (int )( green * cmask);
105           iblue = (int )( blue * cmask);
106
107           gpixel = (ired << sred) | (igreen << sgreen) | (iblue << sblue);
108 /*
109 printf(" npixel %ld gpixel %ld cmask %ld sred %d sgreen %d sblue %d\n",
110 npixel,gpixel,cmask,sred,sgreen,sblue);
111 */
112         }
113         XPutPixel(pximage,xp,yp,gpixel) ;
114       }
115     }
116
117 #ifdef  TRACE_GAMMA_IMAGE
118 if( Xw_get_trace() ) {
119     printf (" Xw_gamma_image(%lx,%f)\n",(long ) pimage,gamma);
120 }
121 #endif
122
123     return (XW_SUCCESS);
124 }