summaryrefslogtreecommitdiff
path: root/src/Xw/Xw_put_rgbpixel.cxx
blob: 627b34c8b6b598872cb0fd53d9011b3a3b5242be (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

#include <Xw_Extension.h>

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

/*
   XW_STATUS Xw_put_rgbpixel (aimage,x,y,r,g,b,npixel):
   XW_EXT_IMAGEDATA image		*aimage
   int x,y			Pixel position
   float r,g,b			Pixel color index 
   int npixel			Pixel number to fill with the same color index

	Fill an existing IMAGE created by Xw_open_image with R,G,B values

	returns XW_ERROR if Extended Image is not defined
			 or VISUAL Image class is not TRUECOLOR
			 or Pixel position is wrong (Outside of image)
	returns XW_SUCCESS if successful

*/
#ifdef XW_PROTOTYPE
XW_STATUS Xw_put_rgbpixel (void *aimage,int x,int y,float r,float g,float b,int npixel)
#else
XW_STATUS Xw_put_rgbpixel (aimage,x,y,r,g,b,npixel)
void *aimage ;
int x,y,npixel ;
float r,g,b ;
#endif /*XW_PROTOTYPE*/
{
XW_EXT_IMAGEDATA *pimage = (XW_EXT_IMAGEDATA*)aimage;
XW_EXT_COLORMAP *pcolormap = pimage->pcolormap;
union {
    char  *data ;
    unsigned char *cdata ;
    unsigned short *sdata ;
    unsigned long  *ldata ;
} data ;
int fpixel,lpixel,simage,index,isapproximate ;
register int np ;
unsigned long pixel ;
XImage *pximage;

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

	switch ( _CCLASS ) {
	    case TrueColor :
		Xw_get_color_pixel(_ICOLORMAP,r,g,b,&pixel,&isapproximate) ;
		break ;

	    case PseudoColor :
		Xw_get_color_index(_ICOLORMAP,r,g,b,&index) ;
		pixel = _ICOLORMAP->pixels[index] ;
		break ;
	    
	    default :
	    	/*ERROR*Unimplemented Visual class*/
	    	Xw_set_error(5,"Xw_put_rgbpixel",&_CCLASS) ;
	    	return (XW_ERROR) ;
	}

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

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

	switch (pximage->bitmap_pad) {
	    case 8 :
		data.data = pximage->data + (fpixel) ;
		for( np=npixel ; np ; --np )
				*data.cdata++ = (unsigned char) pixel ;
		break ;
	    case 16 :
		data.data = pximage->data + (fpixel<<1) ;
		for( np=npixel ; np ; --np )
				*data.sdata++ = (unsigned short) pixel ;
		break ;
	    case 32 :
		data.data = pximage->data + (fpixel<<2) ;
		for( np=npixel ; np ; --np )
				*data.ldata++ = (unsigned long) pixel ;
	}

#ifdef  TRACE_PUT_RGBPIXEL
	printf (" Xw_put_rgbpixel(%x,%d,%d,%f,%f,%f,%d)\n",
					pimage,x,y,r,g,b,npixel) ;
#endif

	return (XW_SUCCESS);
}