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

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