1 /************************************************************************/
2 /* */
3 /* Make XImages and/or Pixmaps from a bitmap. */
4 /* */
5 /* MOTIF/X11 specific code. */
6 /* */
7 /************************************************************************/
8
9 # include "appFrameConfig.h"
10
11 # include <stddef.h>
12 # include <stdio.h>
13 # include <stdlib.h>
14
15 # include "appGuiBase.h"
16 # include <bitmap.h>
17 # include <bmRender.h>
18 # include "drawUtilMotif.h"
19 # include <appDebugon.h>
20
21 # ifdef USE_MOTIF
22
23 extern void _XInitImageFuncPtrs( XImage * xim );
24
25 /************************************************************************/
26 /* */
27 /* Make a pixmap from a picture. */
28 /* */
29 /* 2) Make sure that at least the 222 colors are available. */
30 /* 2a) Allocate an array of XColors and initialise it. */
31 /* 3) Fill dither tables. */
32 /* 4) Fill an XImage, the way depends on the bitmap. */
33 /* 5) Store it in a Pixmap. */
34 /* */
35 /************************************************************************/
36
drawUtilMotifMakeImage(Display * display,int screen,APP_IMAGE ** pPimage,int toWide,int toHigh,AppColors * ac,const RasterImage * abi,const DocumentRectangle * drSel)37 int drawUtilMotifMakeImage(
38 Display * display,
39 int screen,
40 APP_IMAGE ** pPimage,
41 int toWide,
42 int toHigh,
43 AppColors * ac,
44 const RasterImage * abi,
45 const DocumentRectangle * drSel )
46 {
47 int depth= DefaultDepth( display, screen );
48
49 BitmapDescription bdOut;
50
51 APP_IMAGE * xim;
52
53 Visual * vis;
54
55 int pad= 8;
56 int col;
57
58 unsigned int one= 1;
59
60 int bitmapUnit= 0;
61 int swapBitmapBytes= 0;
62 int swapBitmapBits= 0;
63 const int dither= 1;
64
65 vis= DefaultVisual( display, screen );
66
67 bmInitDescription ( &bdOut );
68
69 bdOut.bdPixelsWide= toWide;
70 bdOut.bdPixelsHigh= toHigh;
71 bdOut.bdHasAlpha= 0;
72 bdOut.bdXResolution= 1;
73 bdOut.bdYResolution= 1;
74 bdOut.bdUnit= BMunPIXEL;
75
76 if ( ac->acVisualClass == TrueColor ||
77 ac->acVisualClass == DirectColor )
78 {
79 bdOut.bdBytesPerRow= toWide;
80 if ( depth > 8 )
81 { bdOut.bdBytesPerRow *= 2; }
82 if ( depth > 16 )
83 { bdOut.bdBytesPerRow *= 2; }
84
85 xim= XCreateImage( display, vis, depth, ZPixmap, 0, (char *)0,
86 toWide, toHigh, pad, 0 );
87
88 if ( ! xim )
89 { LDEB(xim); return -1; }
90
91 bdOut.bdBytesPerRow= xim->bytes_per_line;
92 bdOut.bdBufferLength= toHigh* bdOut.bdBytesPerRow;
93 bdOut.bdBitsPerSample= depth/ 3;
94 bdOut.bdSamplesPerPixel= 3;
95 bdOut.bdBitsPerPixel= xim->bits_per_pixel;
96 bdOut.bdColorEncoding= BMcoRGB;
97 bdOut.bdPalette.cpColorCount= 0;
98 }
99 else{
100 switch( depth )
101 {
102 case 1:
103 xim= XCreateImage( display, vis, depth, XYPixmap, 0,
104 (char *)0, toWide, toHigh, pad, 0 );
105
106 if ( ! xim )
107 { LDEB(xim); return -1; }
108
109 if ( xim->bitmap_unit > 32 )
110 { LDEB(xim->bitmap_unit); return -1; }
111
112 bitmapUnit= xim->bitmap_unit;
113 if ( xim->byte_order == MSBFirst )
114 {
115 if ( *((unsigned char *)&one) )
116 { swapBitmapBytes= 1; }
117 else{ swapBitmapBytes= 0; }
118 }
119 else{
120 if ( *((unsigned char *)&one) )
121 { swapBitmapBytes= 0; }
122 else{ swapBitmapBytes= 1; }
123 }
124
125 /* ? */
126 if ( xim->bitmap_bit_order == MSBFirst )
127 { swapBitmapBits= 0; }
128 else{ swapBitmapBits= 1; }
129
130 bdOut.bdBytesPerRow= xim->bytes_per_line;
131 bdOut.bdBufferLength= toHigh* bdOut.bdBytesPerRow;
132 bdOut.bdBitsPerSample= 1;
133 bdOut.bdSamplesPerPixel= 1;
134 bdOut.bdBitsPerPixel= 1;
135 bdOut.bdColorEncoding= BMcoBLACKWHITE;
136 bdOut.bdPalette.cpColorCount= 0;
137
138 break;
139
140 case 8:
141 xim= XCreateImage( display, vis, depth, ZPixmap, 0,
142 (char *)0,
143 toWide, toHigh, pad, 0 );
144
145 if ( ! xim )
146 { LDEB(xim); return -1; }
147
148 bdOut.bdBytesPerRow= xim->bytes_per_line;
149 bdOut.bdBufferLength= toHigh* bdOut.bdBytesPerRow;
150 bdOut.bdBitsPerSample= 8;
151 bdOut.bdSamplesPerPixel= 3;
152 bdOut.bdBitsPerPixel= 8;
153 bdOut.bdColorEncoding= BMcoRGB8PALETTE;
154 bdOut.bdPalette.cpColorCount= 0;
155
156 break;
157 case 16:
158 bdOut.bdBytesPerRow= 4* ( ( 2* toWide+ 3 )/ 4 );
159 bdOut.bdBufferLength= toHigh* bdOut.bdBytesPerRow;
160
161 xim= XCreateImage( display, vis, depth, ZPixmap, 0,
162 (char *)0,
163 toWide, toHigh, pad, 0 );
164
165 if ( ! xim )
166 { LDEB(xim); return -1; }
167
168 bdOut.bdBytesPerRow= xim->bytes_per_line;
169 bdOut.bdBufferLength= toHigh* bdOut.bdBytesPerRow;
170 bdOut.bdBitsPerSample= 8;
171 bdOut.bdSamplesPerPixel= 3;
172 bdOut.bdBitsPerPixel= 16;
173 bdOut.bdColorEncoding= BMcoRGB8PALETTE;
174 bdOut.bdPalette.cpColorCount= 0;
175
176 break;
177
178 case 32:
179 case 24:
180 default:
181 LDEB(depth); return -1;
182 }
183 }
184
185 /* 2 */
186 for ( col= 0; col < 64; col++ )
187 {
188 int r, g, b;
189 XColor xc;
190
191 r= ( col & 0x30 ) << 2;
192 g= ( col & 0x0c ) << 4;
193 b= ( col & 0x03 ) << 6;
194
195 if ( appColorRgb( &xc, ac, r, g, b ) )
196 { LDEB(col); return -1; }
197 }
198
199 if ( *((unsigned char *)&one) )
200 {
201 if ( xim->byte_order != LSBFirst )
202 {
203 xim->byte_order= LSBFirst;
204 _XInitImageFuncPtrs( xim );
205 }
206 }
207 else{
208 if ( xim->byte_order != MSBFirst )
209 {
210 xim->byte_order= MSBFirst;
211 _XInitImageFuncPtrs( xim );
212 }
213 }
214
215 xim->data= malloc( bdOut.bdBufferLength );
216 if ( ! xim->data )
217 {
218 LXDEB(bdOut.bdBufferLength,xim->data);
219 XDestroyImage( xim ); return -1;
220 }
221
222 if ( bmFillImage( &(ac->acAllocator),
223 bitmapUnit, swapBitmapBytes, swapBitmapBits, dither,
224 (unsigned char *)xim->data, &bdOut, abi, drSel ) )
225 {
226 LDEB(1);
227 XDestroyImage( xim );
228 return -1;
229 }
230
231 *pPimage= xim; return 0;
232 }
233
234 # endif /* USE_MOTIF */
235