summaryrefslogtreecommitdiff
path: root/src/Xw/Xw_get_pixel.cxx
blob: 1027bbe1e3b70a90dfd19ec416423922cc6cbf0e (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
108
109
110
111
112
113
114
115

#include <Xw_Extension.h>

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

/*
   STATUS Xw_get_pixel (aimage,x,y,index,npixel):
   XW_EXT_IMAGEDATA 		*aimage
   int x,y			Pixel position
   int *index			return Pixel color index 
   int *npixel			return Pixel number filled with 
				       the same color index

	Extract pixels index from an existing IMAGE created by Xw_get_image
		or fill with Xw_put_pixel

	returns ERROR if No image is defined
			 or No pixel color is defined
			 or Pixel position is wrong (Outside of image)
	returns SUCCESS if successful

*/

#ifdef XW_PROTOTYPE
XW_STATUS Xw_get_pixel (void* aimage,int x,int y,int* index,int* npixel)
#else
XW_STATUS Xw_get_pixel (aimage,x,y,index,npixel)
XW_EXT_IMAGEDATA *aimage ;
int x,y ;
int *index,*npixel ;
#endif /*XW_PROTOTYPE*/
{
XW_EXT_IMAGEDATA *pimage = (XW_EXT_IMAGEDATA*) aimage ;
register union {
    char  *data ;
    unsigned char *cdata ;
    unsigned short *sdata ;
    unsigned long  *ldata ;
} data ;
int i=0,fpixel,simage ;
unsigned long lpixel=0 ;
XImage *pximage;

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

        pximage = (_ZIMAGE) ? _ZIMAGE : _IIMAGE;
	fpixel = x*pximage->width + y ;
	simage = pximage->height*pximage->width ;

	if( x < 0 || y < 0 || (fpixel >= simage) ) {
	    /*ERROR*Bad PIXEL position*/
	    Xw_set_error(47,"Xw_get_pixel",&simage) ;
	    return (XW_ERROR) ;
	}

	switch (pximage->bitmap_pad) {
	    case 8 :
		{ register unsigned char cpixel ; 
		data.data = pximage->data + (fpixel) ; simage -= fpixel ;
		cpixel = *data.cdata ;
		for( i=1 ; i<simage ; i++ ) {
		    data.cdata++ ; 
		    if( *data.cdata != cpixel ) break ;
		}
		}
		break ;
	    case 16 :
		{ register unsigned short spixel ; 
		data.data = pximage->data + (fpixel<<1) ; simage -= fpixel ;
		spixel = *data.sdata ;
		for( i=1 ; i<simage ; i++ ) {
		    data.sdata++ ; 
		    if( *data.sdata != spixel ) break ;
		}
		}
		break ;
	    case 32 :
		data.data = pximage->data + (fpixel<<2) ; simage -= fpixel;
		lpixel = *data.ldata ;
		for( i=1 ; i<simage ; i++ ) {
		    data.ldata++ ; 
		    if( *data.ldata != lpixel ) break ;
		}
	}

	*npixel = i ;
	*index = lpixel ;
	for( i=0 ; i<_ICOLORMAP->maxcolor ; i++ ) {
	    if( _ICOLORMAP->define[i] && 
		    (lpixel == _ICOLORMAP->pixels[i]) ) break ;
	}
	if( i < _ICOLORMAP->maxcolor ) {
	    *index = i ;
	} else {
	    /*ERROR*Bad Defined Color*/
	    Xw_set_error(41,"Xw_get_pixel",&index) ;
	    return (XW_ERROR) ;
	}

#ifdef  TRACE_GET_PIXEL
if( Xw_get_trace() > 2 ) {
    printf (" Xw_get_pixel(%lx,%d,%d,%ld,%ld)\n",
					(long ) pimage,x,y,(long ) index,(long ) npixel) ;
}
#endif

	return (XW_SUCCESS);
}