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

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