summaryrefslogtreecommitdiff
path: root/src/Xw/Xw_gamma_image.cxx
blob: 9eaab4afc9d3f90249f0dcc2d5f125b0acc7d1ad (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
// JR 01.02.100 : convert of float to int : (int )

#include <Xw_Extension.h>

/* ifdef then trace on */
#ifdef TRACE
# define TRACE_GAMMA_IMAGE
#endif

/*
   XW_STATUS Xw_gamma_image (aimage,gamma):
   XW_EXT_IMAGEDATA *aimage     Image where apply the gamma correction
   float gamma                   gamma value to apply (>0.)

        Apply a GAMMA correction to an image

        returns  ERROR if bad gamma value
        returns  SUCCESS if successfull

*/

#ifdef XW_PROTOTYPE
XW_STATUS Xw_gamma_image (void* aimage,float gamma)
#else
XW_STATUS Xw_gamma_image (aimage,gamma)
void *aimage;
float gamma ;
#endif /*XW_PROTOTYPE*/
{
XW_EXT_IMAGEDATA *pimage = (XW_EXT_IMAGEDATA*)aimage ;
XImage *pximage;
int wp,hp,xp,yp,ired,igreen,iblue;
unsigned long cmask,npixel,gpixel = 0,opixel = 0;
int sred,sgreen,sblue ;
float red,green,blue ;

    if( !Xw_isdefine_image(pimage) ) {
        /*ERROR*Bad EXT_IMAGE Address*/
        Xw_set_error(25,"Xw_gamma_image",pimage) ;
        return (XW_ERROR) ;
    }

    if( gamma <= 0. ) {
        /*ERROR*Bad Image GAMMA value*/
        return (XW_ERROR) ;
    }

    if( gamma == 1.0 ) return XW_SUCCESS;

    pximage = pimage->pximage;

    if( !pximage->red_mask || !pximage->green_mask || !pximage->blue_mask ) {
        printf(" *** Xw_gamma_image.Cann't apply the gamma correction to this image\n");
	return (XW_ERROR) ;
    }

    gamma = 1./gamma;

    wp = pximage->width; hp = pximage->height;

    sred = sgreen = sblue = 0 ;
    cmask = pximage->red_mask ;
    while ( !(cmask & 1) ) { cmask >>= 1 ; sred++ ; }
    cmask = pximage->green_mask ;
    while ( !(cmask & 1) ) { cmask >>= 1 ; sgreen++ ; }
    cmask = pximage->blue_mask ;
    while ( !(cmask & 1) ) { cmask >>= 1 ; sblue++ ; }

    for( yp=0 ; yp<hp ; yp++ ) {
      for( xp=0 ; xp<wp ; xp++ ) {
        npixel = XGetPixel(pximage,xp,yp) ;
	if( npixel != opixel ) {
	  opixel = npixel;
	  ired = (npixel >> sred) & cmask;
	  igreen = (npixel >> sgreen) & cmask;
	  iblue = (npixel >> sblue) & cmask;

	  red = (float)(ired)/(float)cmask;
	  green = (float)(igreen)/(float)cmask;
	  blue = (float)(iblue)/(float)cmask;

	  red = min(1.,pow(double(red),double(gamma)));
	  green = min(1.,pow(double(green),double(gamma)));
	  blue = min(1.,pow(double(blue),double(gamma)));

	  ired = (int )( red * cmask);
	  igreen = (int )( green * cmask);
	  iblue = (int )( blue * cmask);

	  gpixel = (ired << sred) | (igreen << sgreen) | (iblue << sblue);
/*
printf(" npixel %ld gpixel %ld cmask %ld sred %d sgreen %d sblue %d\n",
npixel,gpixel,cmask,sred,sgreen,sblue);
*/
	}
        XPutPixel(pximage,xp,yp,gpixel) ;
      }
    }

#ifdef  TRACE_GAMMA_IMAGE
if( Xw_get_trace() ) {
    printf (" Xw_gamma_image(%lx,%f)\n",(long ) pimage,gamma);
}
#endif

    return (XW_SUCCESS);
}