File: appPutImageMotif.c

package info (click to toggle)
ted 2.11-1
  • links: PTS
  • area: main
  • in suites: woody
  • size: 11,064 kB
  • ctags: 13,935
  • sloc: ansic: 120,446; makefile: 7,469; sh: 253
file content (264 lines) | stat: -rw-r--r-- 6,412 bytes parent folder | download
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
/************************************************************************/
/*									*/
/*  Make XImages and/or Pixmaps from a bitmap.				*/
/*									*/
/*  MOTIF/X11 specific code.						*/
/*									*/
/************************************************************************/

#   include	"appFrameConfig.h"

#   include	<stddef.h>
#   include	<stdio.h>
#   include	<stdlib.h>

#   include	"appImage.h"

#   include	<appDebugon.h>

#   ifdef USE_MOTIF

extern void _XInitImageFuncPtrs( XImage * xim );

/************************************************************************/
/*									*/
/*  Make a pixmap from a picture.					*/
/*									*/
/*  2)  Make sure that at least the 222 colors are available.		*/
/*  2a) Allocate an array of XColors and initialise it.			*/
/*  3)  Fill dither tables.						*/
/*  4)  Fill an XImage, the way depends on the bitmap.			*/
/*  5)  Store it in a Pixmap.						*/
/*									*/
/************************************************************************/

int appImgMakeImage(	AppDrawingData *		add,
			APP_IMAGE **			pPimage,
			int				toWide,
			int				toHigh,
			AppColors *			ac,
			AppBitmapImage *		abi )
    {
    Display *			display= add->addDisplay;
    int				screen= add->addScreen;
    int				depth= DefaultDepth( display, screen );

    const BitmapDescription *	bdIn= &(abi->abiBitmap);
    const unsigned char *	bufferIn= abi->abiBuffer;
    BitmapDescription		bdOut;

    APP_IMAGE *			xim;

    Visual *			vis;

    int				pad= 8;
    int				col;

    unsigned int		one= 1;

    int				bitmapUnit= 0;
    int				swapBitmapBytes= 0;
    int				swapBitmapBits= 0;

    vis= DefaultVisual( display, screen );

    bmInitDescription ( &bdOut );

    bdOut.bdPixelsWide= toWide;
    bdOut.bdPixelsHigh= toHigh;
    bdOut.bdHasAlpha= 0;
    bdOut.bdXResolution= 1;
    bdOut.bdYResolution= 1;
    bdOut.bdUnit= BMunPIXEL;

    if  ( ac->acVisualClass ==	TrueColor	||
	  ac->acVisualClass ==	DirectColor	)
	{
	bdOut.bdBytesPerRow= toWide;
	if  ( depth > 8 )
	    { bdOut.bdBytesPerRow *= 2;	}
	if  ( depth > 16 )
	    { bdOut.bdBytesPerRow *= 2;	}

	xim= XCreateImage( display, vis, depth, ZPixmap, 0, (char *)0,
						    toWide, toHigh, pad, 0 );

	if  ( ! xim )
	    { LDEB(xim); return -1;	}

	bdOut.bdBytesPerRow= xim->bytes_per_line;
	bdOut.bdBufferLength= toHigh* bdOut.bdBytesPerRow;
	bdOut.bdBitsPerSample= depth/ 3;
	bdOut.bdSamplesPerPixel= 3;
	bdOut.bdBitsPerPixel= xim->bits_per_pixel;
	bdOut.bdColorEncoding= BMcoRGB;
	bdOut.bdColorCount= 0;
	}
    else{
	switch( depth )
	    {
	    case 1:
		xim= XCreateImage( display, vis, depth, XYPixmap, 0,
					(char *)0, toWide, toHigh, pad, 0 );

		if  ( ! xim )
		    { LDEB(xim); return -1;	}

		if  ( xim->bitmap_unit > 32 )
		    { LDEB(xim->bitmap_unit); return -1;	}

		bitmapUnit= xim->bitmap_unit;
		if  ( xim->byte_order == MSBFirst )
		    {
		    if  ( *((unsigned char *)&one)	)
			{ swapBitmapBytes= 1;	}
		    else{ swapBitmapBytes= 0;	}
		    }
		else{
		    if  ( *((unsigned char *)&one)	)
			{ swapBitmapBytes= 0;	}
		    else{ swapBitmapBytes= 1;	}
		    }

		/*  ?  */
		if  ( xim->bitmap_bit_order == MSBFirst )
		    { swapBitmapBits= 0;	}
		else{ swapBitmapBits= 1;	}

		bdOut.bdBytesPerRow= xim->bytes_per_line;
		bdOut.bdBufferLength= toHigh* bdOut.bdBytesPerRow;
		bdOut.bdBitsPerSample= 1;
		bdOut.bdSamplesPerPixel= 1;
		bdOut.bdBitsPerPixel= 1;
		bdOut.bdColorEncoding= BMcoBLACKWHITE;
		bdOut.bdColorCount= 0;

		break;

	    case 8:
		xim= XCreateImage( display, vis, depth, ZPixmap, 0,
						    (char *)0,
						    toWide, toHigh, pad, 0 );

		if  ( ! xim )
		    { LDEB(xim); return -1;	}

		bdOut.bdBytesPerRow= xim->bytes_per_line;
		bdOut.bdBufferLength= toHigh* bdOut.bdBytesPerRow;
		bdOut.bdBitsPerSample= 8;
		bdOut.bdSamplesPerPixel= 3;
		bdOut.bdBitsPerPixel= 8;
		bdOut.bdColorEncoding= BMcoRGB8PALETTE;
		bdOut.bdColorCount= 0;

		break;
	    case 16:
		bdOut.bdBytesPerRow= 4* ( ( 2* toWide+ 3 )/ 4 );
		bdOut.bdBufferLength= toHigh* bdOut.bdBytesPerRow;

		xim= XCreateImage( display, vis, depth, ZPixmap, 0,
						    (char *)0,
						    toWide, toHigh, pad, 0 );

		if  ( ! xim )
		    { LDEB(xim); return -1;	}

		bdOut.bdBytesPerRow= xim->bytes_per_line;
		bdOut.bdBufferLength= toHigh* bdOut.bdBytesPerRow;
		bdOut.bdBitsPerSample= 8;
		bdOut.bdSamplesPerPixel= 3;
		bdOut.bdBitsPerPixel= 16;
		bdOut.bdColorEncoding= BMcoRGB8PALETTE;
		bdOut.bdColorCount= 0;

		break;

	    case 32:
	    case 24:
	    default:
		LDEB(depth); return -1;
	    }
	}

    /*  2  */
    for ( col= 0; col < 64; col++ )
	{
	int		r, g, b;
	XColor		xc;

	r= ( col & 0x30 ) << 2;
	g= ( col & 0x0c ) << 4;
	b= ( col & 0x03 ) << 6;

	if  ( appColorRgb( &xc, ac, r, g, b ) )
	    { LDEB(col); return -1; }
	}

    if  ( *((unsigned char *)&one) )
	{
	if  ( xim->byte_order != LSBFirst )
	    {
	    xim->byte_order= LSBFirst;
	    _XInitImageFuncPtrs( xim );
	    }
	}
    else{
	if  ( xim->byte_order != MSBFirst )
	    {
	    xim->byte_order= MSBFirst;
	    _XInitImageFuncPtrs( xim );
	    }
	}

    xim->data= malloc( bdOut.bdBufferLength );
    if  ( ! xim->data )
	{
	LXDEB(bdOut.bdBufferLength,xim->data);
	XDestroyImage( xim ); return -1;
	}

    if  ( bmFillImage( &(ac->acAllocator),
			bitmapUnit, swapBitmapBytes, swapBitmapBits,
			(unsigned char *)xim->data, bufferIn, &bdOut, bdIn ) )
	{ LDEB(1); XDestroyImage( xim ); return -1; }

    *pPimage= xim; return 0;
    }

/************************************************************************/
/*									*/
/*  Convert a BitmapDescription to an APP_IMAGE.			*/
/*									*/
/************************************************************************/

int appImgMakePixmap(	AppDrawingData *		add,
			Pixmap *			pPixmap,
			int				toWide,
			int				toHigh,
			AppColors *			ac,
			AppBitmapImage *		abi )
    {
    APP_IMAGE *		xim;

    Display *		display= add->addDisplay;
    Window		win= add->addDrawable;
    int			screen= DefaultScreen( display );
    int			depth= DefaultDepth( display, screen );

    if  ( appImgMakeImage( add, &xim, toWide, toHigh, ac, abi ) )
	{ LLDEB(toWide,toHigh); return -1;	}

    /*  5  */
    *pPixmap= XCreatePixmap( display, win, toWide, toHigh, depth );

    if  ( ! *pPixmap )
	{ XDEB(*pPixmap); return -1;	}

    XPutImage( display, *pPixmap, add->addGc, xim, 0, 0, 0, 0, toWide, toHigh );

    XDestroyImage( xim );

    return 0;
    }

#   endif /* USE_MOTIF */