b311480e |
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 | |
7fd59977 |
18 | |
19 | #include <Xw_Extension.h> |
20 | |
b311480e |
21 | /* ifdef then trace on */ |
7fd59977 |
22 | #ifdef TRACE |
23 | #define TRACE_GET_PIXEL |
24 | #endif |
25 | |
26 | /* |
27 | STATUS Xw_get_pixel (aimage,x,y,index,npixel): |
28 | XW_EXT_IMAGEDATA *aimage |
29 | int x,y Pixel position |
30 | int *index return Pixel color index |
31 | int *npixel return Pixel number filled with |
32 | the same color index |
33 | |
34 | Extract pixels index from an existing IMAGE created by Xw_get_image |
35 | or fill with Xw_put_pixel |
36 | |
37 | returns ERROR if No image is defined |
38 | or No pixel color is defined |
39 | or Pixel position is wrong (Outside of image) |
40 | returns SUCCESS if successful |
41 | |
42 | */ |
43 | |
44 | #ifdef XW_PROTOTYPE |
45 | XW_STATUS Xw_get_pixel (void* aimage,int x,int y,int* index,int* npixel) |
46 | #else |
47 | XW_STATUS Xw_get_pixel (aimage,x,y,index,npixel) |
48 | XW_EXT_IMAGEDATA *aimage ; |
49 | int x,y ; |
50 | int *index,*npixel ; |
51 | #endif /*XW_PROTOTYPE*/ |
52 | { |
53 | XW_EXT_IMAGEDATA *pimage = (XW_EXT_IMAGEDATA*) aimage ; |
54 | register union { |
55 | char *data ; |
56 | unsigned char *cdata ; |
57 | unsigned short *sdata ; |
58 | unsigned long *ldata ; |
59 | } data ; |
60 | int i=0,fpixel,simage ; |
61 | unsigned long lpixel=0 ; |
62 | XImage *pximage; |
63 | |
64 | if( !Xw_isdefine_image(pimage) ) { |
65 | /*ERROR*Bad EXT_IMAGEDATA Address*/ |
66 | Xw_set_error(25,"Xw_get_pixel",pimage) ; |
67 | return (XW_ERROR) ; |
68 | } |
69 | |
70 | pximage = (_ZIMAGE) ? _ZIMAGE : _IIMAGE; |
71 | fpixel = x*pximage->width + y ; |
72 | simage = pximage->height*pximage->width ; |
73 | |
74 | if( x < 0 || y < 0 || (fpixel >= simage) ) { |
75 | /*ERROR*Bad PIXEL position*/ |
76 | Xw_set_error(47,"Xw_get_pixel",&simage) ; |
77 | return (XW_ERROR) ; |
78 | } |
79 | |
80 | switch (pximage->bitmap_pad) { |
81 | case 8 : |
82 | { register unsigned char cpixel ; |
83 | data.data = pximage->data + (fpixel) ; simage -= fpixel ; |
84 | cpixel = *data.cdata ; |
85 | for( i=1 ; i<simage ; i++ ) { |
86 | data.cdata++ ; |
87 | if( *data.cdata != cpixel ) break ; |
88 | } |
89 | } |
90 | break ; |
91 | case 16 : |
92 | { register unsigned short spixel ; |
93 | data.data = pximage->data + (fpixel<<1) ; simage -= fpixel ; |
94 | spixel = *data.sdata ; |
95 | for( i=1 ; i<simage ; i++ ) { |
96 | data.sdata++ ; |
97 | if( *data.sdata != spixel ) break ; |
98 | } |
99 | } |
100 | break ; |
101 | case 32 : |
102 | data.data = pximage->data + (fpixel<<2) ; simage -= fpixel; |
103 | lpixel = *data.ldata ; |
104 | for( i=1 ; i<simage ; i++ ) { |
105 | data.ldata++ ; |
106 | if( *data.ldata != lpixel ) break ; |
107 | } |
108 | } |
109 | |
110 | *npixel = i ; |
111 | *index = lpixel ; |
112 | for( i=0 ; i<_ICOLORMAP->maxcolor ; i++ ) { |
113 | if( _ICOLORMAP->define[i] && |
114 | (lpixel == _ICOLORMAP->pixels[i]) ) break ; |
115 | } |
116 | if( i < _ICOLORMAP->maxcolor ) { |
117 | *index = i ; |
118 | } else { |
119 | /*ERROR*Bad Defined Color*/ |
120 | Xw_set_error(41,"Xw_get_pixel",&index) ; |
121 | return (XW_ERROR) ; |
122 | } |
123 | |
124 | #ifdef TRACE_GET_PIXEL |
125 | if( Xw_get_trace() > 2 ) { |
126 | printf (" Xw_get_pixel(%lx,%d,%d,%ld,%ld)\n", |
127 | (long ) pimage,x,y,(long ) index,(long ) npixel) ; |
128 | } |
129 | #endif |
130 | |
131 | return (XW_SUCCESS); |
132 | } |