source: trunk/MagicSoft/Cosy/gui/MGImage.cc@ 1194

Last change on this file since 1194 was 1111, checked in by tbretz, 23 years ago
*** empty log message ***
File size: 9.5 KB
Line 
1//
2// This File contains the definition of the MGImage-class
3//
4// Author: Thomas Bretz
5// Version: V1.0 (1-8-2000)
6//
7// x11/src/GX11Gui.cxx
8//
9#include "MGImage.h"
10
11#include <iostream.h>
12#include <pthread.h>
13
14ClassImp(MGImage);
15
16MGImage::MGImage(const TGWindow* p, UInt_t w, UInt_t h, UInt_t options, ULong_t back)
17 : TGFrame(p, w, h, options, back), fWidth(w), fHeight(h)
18{
19 // p = pointer to MainFrame (not owner)
20 // w = width of frame
21 // h = width of frame
22
23 //
24 // Set Color Table (Gray scale)
25 //
26 const int cols = 0x100+4*4*4;
27
28 for (int c=0; c<0x100; c++)
29 sprintf(fColors[c], "%02x", c);
30
31 //
32 // create space for the pixmap buffer, initialize pointer to data area
33 //
34 fBuffer = new char*[1+cols+fHeight+1];
35 fBody = &fBuffer[1+cols];
36
37 //
38 // fill buffer with header informations
39 //
40 fBuffer[0] = new char[14];
41 sprintf(fBuffer[0], "%3d %3d %3d %1d", fWidth, fHeight, cols, 2);
42
43 for (int k=0; k<0x100; k++)
44 {
45 const int l = k+1;
46 fBuffer[l] = new char[13];
47 fBuffer[l][0] = fColors[k][0];
48 fBuffer[l][1] = fColors[k][1];
49 fBuffer[l][2] = '\t';
50 fBuffer[l][3] = 'c';
51 fBuffer[l][4] = ' ';
52 fBuffer[l][5] = '#';
53 fBuffer[l][6] = fColors[k][0];
54 fBuffer[l][7] = fColors[k][1];
55 fBuffer[l][8] = fColors[k][0];
56 fBuffer[l][9] = fColors[k][1];
57 fBuffer[l][10] = fColors[k][0];
58 fBuffer[l][11] = fColors[k][1];
59 fBuffer[l][12] = '\0';
60 }
61
62 for (int b=0; b<4; b++)
63 for (int g=0; g<4; g++)
64 for (int r=0; r<4; r++)
65 {
66 const int nr = r+(g<<2)+(b<<4);
67 const int l = 0x100+nr+1;
68
69 fBuffer[l] = new char[13];
70 fBuffer[l][0] = 'f'+nr/8+1;
71 fBuffer[l][1] = 'f'+nr%8+1;
72 fBuffer[l][2] = '\t';
73 fBuffer[l][3] = 'c';
74 fBuffer[l][4] = ' ';
75 fBuffer[l][5] = '#';
76 fBuffer[l][6] = fColors[r*85][0];
77 fBuffer[l][7] = fColors[r*85][1];
78 fBuffer[l][8] = fColors[g*85][0];
79 fBuffer[l][9] = fColors[g*85][1];
80 fBuffer[l][10] = fColors[b*85][0];
81 fBuffer[l][11] = fColors[b*85][1];
82 fBuffer[l][12] = '\0';
83 }
84
85 //
86 // mark end of lines of the data area
87 //
88 for (UInt_t y=0; y<fHeight; y++)
89 {
90 fBody[y] = new char[fWidth*2+1];
91 fBody[y][fWidth*2] = '\0';
92 }
93 //
94 // mark end of buffer
95 //
96 fBuffer[1+cols+fHeight] = '\0';
97
98 //
99 // get frame id
100 //
101 fId = GetId();
102
103 //
104 // Create Default Graphic Context (XCreateGC)
105 //
106 fDefGC = gVirtualX->CreateGC(fId, 0); // GetBckgndGC(); //
107
108 //
109 // Creat drawing semaphore
110 //
111 fMuxPixmap = new pthread_mutex_t;
112 pthread_mutex_init((pthread_mutex_t*)fMuxPixmap, NULL);
113
114 //
115 // create empty (black) pixmap
116 // return (Pixmap_t) XCreatePixmap(fDisplay, (Drawable) id, w, h,
117 // DefaultDepth(fDisplay, DefaultScreen(fDisplay)));
118 //
119 fPixmap = kNone; //@@@gVirtualX->CreatePixmap(fId, fWidth, fHeight);
120
121 Resize(w, h);
122}
123
124void MGImage::Resize(UInt_t w, UInt_t h)
125{
126 TGFrame::Resize(w+2*GetBorderWidth(), h+2*GetBorderWidth());
127 // FIXME: RESIZE THE PIXMAP
128}
129
130void MGImage::Resize(TGDimension size)
131{
132 TGFrame::Resize(size.fWidth+2*GetBorderWidth(), size.fHeight+2*GetBorderWidth());
133 // FIXME: RESIZE THE PIXMAP
134}
135
136void MGImage::MoveResize(Int_t x, Int_t y, UInt_t w, UInt_t h)
137{
138 TGFrame::MoveResize(x, y, w+2*GetBorderWidth(), h+2*GetBorderWidth());
139 // FIXME: RESIZE THE PIXMAP
140}
141
142MGImage::~MGImage()
143{
144 pthread_mutex_lock((pthread_mutex_t*)fMuxPixmap);
145
146 char *b = *fBuffer;
147 while (*b)
148 delete[] b++;
149 delete[] fBuffer;
150
151 if (fPixmap!=kNone) // @@@
152 gVirtualX->DeletePixmap(fPixmap); // XFreePixmap(fDisplay, (Pixmap) pmap);
153 gVirtualX->DeleteGC(fDefGC); // XFreeGC(fDisplay, (GC) gc);
154
155 pthread_mutex_destroy((pthread_mutex_t*)fMuxPixmap);
156
157 cout << "MGImage destroyed." << endl;
158}
159
160/*
161#include <X11/Xlib.h>
162#include <X11/Xutil.h>
163#include <X11/Intrinsic.h>
164XImage *fPix=NULL;
165*/
166
167void MGImage::DoRedraw()
168{
169 // Pixmap_t pm = gVirtualX->CreatePixmap(this->GetId(), buffer,
170 // 768, 576, 0, 0xff, 8);
171 TGFrame::DrawBorder();
172 pthread_mutex_lock((pthread_mutex_t*)fMuxPixmap);
173
174 // Copy a drawable (i.e. pixmap) to another drawable (pixmap, window).
175 // The graphics context gc will be used and the source will be copied
176 // from src_x,src_y,src_x+width,src_y+height to dest_x,dest_y.
177 // XCopyArea(fDisplay, src, dest, (GC) gc, src_x, src_y, width, height,
178 // dest_x, dest_y);
179 if (fPixmap!=kNone) //@@@
180 gVirtualX->CopyArea(fPixmap, fId, fDefGC,
181 0, 0, fWidth, fHeight,
182 GetBorderWidth(), GetBorderWidth());
183/*
184 if (fPix)
185 {
186 cout << "put" << flush;
187 XPutImage((Display*)gVirtualX->GetDisplay(), fId,
188 fDefGC, fPix,
189 0, 0, GetBorderWidth(), GetBorderWidth(),
190 fWidth, fHeight);
191
192 cout << "done " << endl;
193 }
194*/
195 pthread_mutex_unlock((pthread_mutex_t*)fMuxPixmap);
196}
197
198
199//#include <TGClient.h>
200void MGImage::DrawImg(const byte *buffer)
201{
202 if (pthread_mutex_trylock((pthread_mutex_t*)fMuxPixmap))
203 return;
204
205 for (UInt_t y=0; y<fHeight; y++)
206 {
207 for (UInt_t x=0; x<fWidth; x++)
208 {
209 const byte col = buffer[y*fWidth+x];
210
211 fBody[y][x*2] = fColors[col][0];
212 fBody[y][x*2+1] = fColors[col][1];
213 }
214 }
215
216/*
217 cout << "CreateImage" << flush;
218 if (!fPix)
219 {
220 Display *dsp = (Display*)gVirtualX->GetDisplay();
221 Screen *scr = DefaultScreenOfDisplay(dsp);
222 Visual *vis = DefaultVisualOfScreen(scr);
223
224 cout << vis->visualid << endl;
225 cout << vis->c_class << endl;
226 cout << vis->bits_per_rgb << endl;
227 cout << vis->map_entries << endl;
228 Visual visual;
229 visual.c_class = StaticGray;
230
231 int n;
232 XPixmapFormatValues *fmt = XListPixmapFormats(dsp, &n);
233
234 cout << "N: " << n << endl;
235 for (int i=0; i<n; i++)
236 {
237 cout << fmt[i].dww.epth << " " << fmt[i].bits_per_pixel << " "
238 << fmt[i].scanline_pad << endl;
239 }
240
241 Colormap colormap = XCreateColormap(dsp, fId, vis, AllocNone);
242
243 for (int i=0; i<vis->map_entries; i++)
244 {
245 XColor color;
246 char data[4];
247
248 color.flags = DoRed | DoGreen | DoBlue;
249 color.pixel = i;
250 color.red = (256*i/vis->map_entries) << 8;
251 color.green = (256*i/vis->map_entries) << 8;
252 color.blue = (256*i/vis->map_entries) << 8;
253
254 XAllocColor(dsp, colormap, &color);
255
256 cout << color.pixel <<" " << flush;
257 }
258 fPix = XCreateImage(dsp, vis, 8, ZPixmap, 0,
259 buffer, 768, 576, 32, 0);
260
261 cout << "Colors" << visual.visualid << flush;
262
263 }
264 cout << "Done " << (void*)fPix << endl;
265 */
266 Pixmap_t mask = kNone;
267 PictureAttributes_t attr;
268 attr.fMask = kNone;
269 if (fPixmap!=kNone) // @@@
270 gVirtualX->DeletePixmap(fPixmap); // XFreePixmap(fDisplay, (Pixmap) pmap);
271
272 // Create a pixture pixmap from data. The picture attributes
273 // are used for input and output. Returns kTRUE in case of success,
274 // kFALSE otherwise. If mask does not exist it is set to kNone.
275 //
276 // XpmAttributes xpmattr;
277 //
278 // MapPictureAttributes(attr, xpmattr);
279 //
280 // Int_t res = XpmCreatePixmapFromData(fDisplay, id, data, (Pixmap*)&pict,
281 // (Pixmap*)&pict_mask, &xpmattr);
282 //
283 // MapPictureAttributes(attr, xpmattr, kFALSE);
284 // XpmFreeAttributes(&xpmattr);
285 //
286 // if (res == XpmSuccess || res == XpmColorError)
287 // return kTRUE;
288 //
289 // if (pict) {
290 // XFreePixmap(fDisplay, (Pixmap)pict);
291 // pict = kNone;
292 // }
293 // if (pict_mask) {
294 // XFreePixmap(fDisplay, (Pixmap)pict_mask);
295 // pict_mask = kNone;
296 // }
297 // return kFALSE;
298 fPixmap=kNone;
299 if (!gVirtualX->CreatePictureFromData(fId, fBuffer, fPixmap,
300 mask, attr))
301 {
302 cout << "Warning: Error in CreatePictureFromData" << endl;
303 fPixmap=kNone;
304 }
305
306 pthread_mutex_unlock((pthread_mutex_t*)fMuxPixmap);
307 DoRedraw();
308}
309
310void MGImage::DrawColImg(const byte *gbuf, const byte *cbuf)
311{
312 if (pthread_mutex_trylock((pthread_mutex_t*)fMuxPixmap))
313 return;
314
315 for (UInt_t y=0; y<fHeight; y++)
316 {
317 for (UInt_t x=0; x<fWidth; x++)
318 {
319 const byte ccol = cbuf[y*fWidth+x];
320
321 if (ccol)
322 {
323 fBody[y][x*2] = 'f'+ccol/8+1;
324 fBody[y][x*2+1] = 'f'+ccol%8+1;
325 }
326 else
327 {
328 const byte gcol = gbuf[y*fWidth+x];
329 fBody[y][x*2] = fColors[gcol][0];
330 fBody[y][x*2+1] = fColors[gcol][1];
331 }
332 }
333 }
334
335 Pixmap_t mask = kNone;
336 PictureAttributes_t attr;
337 attr.fMask = kNone;
338
339 if (fPixmap!=kNone)
340 gVirtualX->DeletePixmap(fPixmap);
341 fPixmap=kNone;
342
343 if (!gVirtualX->CreatePictureFromData(fId, fBuffer, fPixmap,
344 mask, attr))
345 {
346 cout << "Warning: Error in CreatePictureFromData" << endl;
347 fPixmap=kNone;
348 }
349
350 pthread_mutex_unlock((pthread_mutex_t*)fMuxPixmap);
351 DoRedraw();
352}
353
Note: See TracBrowser for help on using the repository browser.