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

Last change on this file since 1345 was 1343, checked in by tbretz, 23 years ago
*** empty log message ***
File size: 9.7 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 cout << "Deleting fBuffer..." << endl;
147
148 char **b = fBuffer;
149 while (*b)
150 delete[] *b++;
151 delete[] fBuffer;
152
153 cout << "Deleting Pixmap..." << endl;
154
155 if (fPixmap!=kNone) // @@@
156 gVirtualX->DeletePixmap(fPixmap); // XFreePixmap(fDisplay, (Pixmap) pmap);
157
158 cout << "Deleting GC..." << endl;
159
160 gVirtualX->DeleteGC(fDefGC); // XFreeGC(fDisplay, (GC) gc);
161
162 pthread_mutex_destroy((pthread_mutex_t*)fMuxPixmap);
163
164 cout << "MGImage destroyed." << endl;
165}
166
167/*
168#include <X11/Xlib.h>
169#include <X11/Xutil.h>
170#include <X11/Intrinsic.h>
171XImage *fPix=NULL;
172*/
173
174void MGImage::DoRedraw()
175{
176 // Pixmap_t pm = gVirtualX->CreatePixmap(this->GetId(), buffer,
177 // 768, 576, 0, 0xff, 8);
178 TGFrame::DrawBorder();
179 pthread_mutex_lock((pthread_mutex_t*)fMuxPixmap);
180
181 // Copy a drawable (i.e. pixmap) to another drawable (pixmap, window).
182 // The graphics context gc will be used and the source will be copied
183 // from src_x,src_y,src_x+width,src_y+height to dest_x,dest_y.
184 // XCopyArea(fDisplay, src, dest, (GC) gc, src_x, src_y, width, height,
185 // dest_x, dest_y);
186 if (fPixmap!=kNone) //@@@
187 gVirtualX->CopyArea(fPixmap, fId, fDefGC,
188 0, 0, fWidth, fHeight,
189 GetBorderWidth(), GetBorderWidth());
190/*
191 if (fPix)
192 {
193 cout << "put" << flush;
194 XPutImage((Display*)gVirtualX->GetDisplay(), fId,
195 fDefGC, fPix,
196 0, 0, GetBorderWidth(), GetBorderWidth(),
197 fWidth, fHeight);
198
199 cout << "done " << endl;
200 }
201*/
202 pthread_mutex_unlock((pthread_mutex_t*)fMuxPixmap);
203}
204
205
206//#include <TGClient.h>
207void MGImage::DrawImg(const byte *buffer)
208{
209 if (pthread_mutex_trylock((pthread_mutex_t*)fMuxPixmap))
210 return;
211
212 for (UInt_t y=0; y<fHeight; y++)
213 {
214 for (UInt_t x=0; x<fWidth; x++)
215 {
216 const byte col = buffer[y*fWidth+x];
217
218 fBody[y][x*2] = fColors[col][0];
219 fBody[y][x*2+1] = fColors[col][1];
220 }
221 }
222
223/*
224 cout << "CreateImage" << flush;
225 if (!fPix)
226 {
227 Display *dsp = (Display*)gVirtualX->GetDisplay();
228 Screen *scr = DefaultScreenOfDisplay(dsp);
229 Visual *vis = DefaultVisualOfScreen(scr);
230
231 cout << vis->visualid << endl;
232 cout << vis->c_class << endl;
233 cout << vis->bits_per_rgb << endl;
234 cout << vis->map_entries << endl;
235 Visual visual;
236 visual.c_class = StaticGray;
237
238 int n;
239 XPixmapFormatValues *fmt = XListPixmapFormats(dsp, &n);
240
241 cout << "N: " << n << endl;
242 for (int i=0; i<n; i++)
243 {
244 cout << fmt[i].dww.epth << " " << fmt[i].bits_per_pixel << " "
245 << fmt[i].scanline_pad << endl;
246 }
247
248 Colormap colormap = XCreateColormap(dsp, fId, vis, AllocNone);
249
250 for (int i=0; i<vis->map_entries; i++)
251 {
252 XColor color;
253 char data[4];
254
255 color.flags = DoRed | DoGreen | DoBlue;
256 color.pixel = i;
257 color.red = (256*i/vis->map_entries) << 8;
258 color.green = (256*i/vis->map_entries) << 8;
259 color.blue = (256*i/vis->map_entries) << 8;
260
261 XAllocColor(dsp, colormap, &color);
262
263 cout << color.pixel <<" " << flush;
264 }
265 fPix = XCreateImage(dsp, vis, 8, ZPixmap, 0,
266 buffer, 768, 576, 32, 0);
267
268 cout << "Colors" << visual.visualid << flush;
269
270 }
271 cout << "Done " << (void*)fPix << endl;
272 */
273 Pixmap_t mask = kNone;
274 PictureAttributes_t attr;
275 attr.fMask = kNone;
276 if (fPixmap!=kNone) // @@@
277 gVirtualX->DeletePixmap(fPixmap); // XFreePixmap(fDisplay, (Pixmap) pmap);
278
279 // Create a pixture pixmap from data. The picture attributes
280 // are used for input and output. Returns kTRUE in case of success,
281 // kFALSE otherwise. If mask does not exist it is set to kNone.
282 //
283 // XpmAttributes xpmattr;
284 //
285 // MapPictureAttributes(attr, xpmattr);
286 //
287 // Int_t res = XpmCreatePixmapFromData(fDisplay, id, data, (Pixmap*)&pict,
288 // (Pixmap*)&pict_mask, &xpmattr);
289 //
290 // MapPictureAttributes(attr, xpmattr, kFALSE);
291 // XpmFreeAttributes(&xpmattr);
292 //
293 // if (res == XpmSuccess || res == XpmColorError)
294 // return kTRUE;
295 //
296 // if (pict) {
297 // XFreePixmap(fDisplay, (Pixmap)pict);
298 // pict = kNone;
299 // }
300 // if (pict_mask) {
301 // XFreePixmap(fDisplay, (Pixmap)pict_mask);
302 // pict_mask = kNone;
303 // }
304 // return kFALSE;
305 fPixmap=kNone;
306 if (!gVirtualX->CreatePictureFromData(fId, fBuffer, fPixmap,
307 mask, attr))
308 {
309 cout << "Warning: Error in CreatePictureFromData" << endl;
310 fPixmap=kNone;
311 }
312
313 pthread_mutex_unlock((pthread_mutex_t*)fMuxPixmap);
314 DoRedraw();
315}
316
317void MGImage::DrawColImg(const byte *gbuf, const byte *cbuf)
318{
319 if (pthread_mutex_trylock((pthread_mutex_t*)fMuxPixmap))
320 return;
321
322 for (UInt_t y=0; y<fHeight; y++)
323 {
324 for (UInt_t x=0; x<fWidth; x++)
325 {
326 const byte ccol = cbuf[y*fWidth+x];
327
328 if (ccol)
329 {
330 fBody[y][x*2] = 'f'+ccol/8+1;
331 fBody[y][x*2+1] = 'f'+ccol%8+1;
332 }
333 else
334 {
335 const byte gcol = gbuf[y*fWidth+x];
336 fBody[y][x*2] = fColors[gcol][0];
337 fBody[y][x*2+1] = fColors[gcol][1];
338 }
339 }
340 }
341
342 Pixmap_t mask = kNone;
343 PictureAttributes_t attr;
344 attr.fMask = kNone;
345
346 if (fPixmap!=kNone)
347 gVirtualX->DeletePixmap(fPixmap);
348 fPixmap=kNone;
349
350 if (!gVirtualX->CreatePictureFromData(fId, fBuffer, fPixmap,
351 mask, attr))
352 {
353 cout << "Warning: Error in CreatePictureFromData" << endl;
354 fPixmap=kNone;
355 }
356
357 pthread_mutex_unlock((pthread_mutex_t*)fMuxPixmap);
358 DoRedraw();
359}
360
Note: See TracBrowser for help on using the repository browser.