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 |
|
---|
10 | //////////////////////////////////////////////////////////////////////////////
|
---|
11 | //
|
---|
12 | // MGImage
|
---|
13 | //
|
---|
14 | // If sync-mode is enabled the Redraw function is secured by a mutex (ignore
|
---|
15 | // error messages about it comming from root) This has the advantage that
|
---|
16 | // if you use a timer for screen update reading and writing the image is
|
---|
17 | // synchronized. In this way you don't get flickering half images.
|
---|
18 | //
|
---|
19 | //////////////////////////////////////////////////////////////////////////////
|
---|
20 | #include "MGImage.h"
|
---|
21 |
|
---|
22 | #include <iostream>
|
---|
23 |
|
---|
24 | #include <TGX11.h>
|
---|
25 | #include <TMutex.h>
|
---|
26 |
|
---|
27 | ClassImp(MGImage);
|
---|
28 |
|
---|
29 | using namespace std;
|
---|
30 |
|
---|
31 | MGImage::MGImage(const TGWindow* p, UInt_t w, UInt_t h, UInt_t options, ULong_t back)
|
---|
32 | : TGFrame(p, w, h, options, back), fWidth(w), fHeight(h)
|
---|
33 | {
|
---|
34 | // p = pointer to MainFrame (not owner)
|
---|
35 | // w = width of frame
|
---|
36 | // h = width of frame
|
---|
37 |
|
---|
38 | //
|
---|
39 | // Creat drawing semaphore
|
---|
40 | //
|
---|
41 | fMuxPixmap = new TMutex;
|
---|
42 |
|
---|
43 | Resize(GetWidth(), GetHeight());
|
---|
44 |
|
---|
45 | //
|
---|
46 | // create empty pixmap
|
---|
47 | //
|
---|
48 | fDefGC = gVirtualX->CreateGC(fId, 0);
|
---|
49 | fImage = (XImage*)gVirtualX->CreateImage(fWidth, fHeight);
|
---|
50 |
|
---|
51 | cout << "Detected Color Depth: " << gVirtualX->GetDepth() << endl;
|
---|
52 | }
|
---|
53 |
|
---|
54 | MGImage::~MGImage()
|
---|
55 | {
|
---|
56 | if (fMuxPixmap->Lock()==13)
|
---|
57 | cout << "MGImage::~MGImage - mutex is already locked by this thread" << endl;
|
---|
58 |
|
---|
59 | cout << "Deleting MGImage..." << endl;
|
---|
60 |
|
---|
61 | gVirtualX->DeleteGC(fDefGC);
|
---|
62 | gVirtualX->DeleteImage((Drawable_t)fImage);
|
---|
63 |
|
---|
64 | delete fMuxPixmap;
|
---|
65 |
|
---|
66 | cout << "MGImage destroyed." << endl;
|
---|
67 | }
|
---|
68 |
|
---|
69 | void MGImage::DoRedraw()
|
---|
70 | {
|
---|
71 | if (TestBit(kSyncMode))
|
---|
72 | while (fMuxPixmap->Lock()==13)
|
---|
73 | usleep(1);
|
---|
74 |
|
---|
75 | // gVirtualX->DrawLine(fId, fDefGC, 0, 0, fWidth+2, 0);
|
---|
76 | // gVirtualX->DrawLine(fId, fDefGC, 0, 0, 0, fHeight+2);
|
---|
77 | // gVirtualX->DrawLine(fId, fDefGC, fWidth+2, 0, fWidth+2, fHeight+2);
|
---|
78 | // gVirtualX->DrawLine(fId, fDefGC, 0, fHeight+2, fWidth+2, fHeight+2);
|
---|
79 |
|
---|
80 | // if (TestBit(kNeedRedraw))
|
---|
81 | {
|
---|
82 | gVirtualX->PutImage(fId, fDefGC, (Drawable_t)fImage, 0, 0, 0, 0,
|
---|
83 | fWidth, fHeight);
|
---|
84 | ResetBit(kNeedRedraw);
|
---|
85 | }
|
---|
86 |
|
---|
87 | if (TestBit(kSyncMode))
|
---|
88 | if (fMuxPixmap->UnLock()==13)
|
---|
89 | cout << "MGImage::DoRedraw - tried to unlock mutex locked by other thread." << endl;
|
---|
90 | }
|
---|
91 |
|
---|
92 | void MGImage::DrawImg16(unsigned short *d, char *s, char *e)
|
---|
93 | {
|
---|
94 | // d=destination, s=source, e=end
|
---|
95 | // rrrrrggg gggbbbbb
|
---|
96 | //
|
---|
97 | while (s<e)
|
---|
98 | {
|
---|
99 | // 11111100 11111000 11111000
|
---|
100 | *d++ = (*s&0xfc) | (*s&0xf8)<<5 | (*s&0xf8)<<11;
|
---|
101 | s++;
|
---|
102 | }
|
---|
103 | }
|
---|
104 |
|
---|
105 | void MGImage::DrawImg24(char *d, char *s, char *e)
|
---|
106 | {
|
---|
107 | // d=destination, s=source, e=end
|
---|
108 | // rrrrrrrr gggggggg bbbbbbbb aaaaaaaa
|
---|
109 | //
|
---|
110 | while (s<e)
|
---|
111 | {
|
---|
112 | *d++ = *s;
|
---|
113 | *d++ = *s;
|
---|
114 | *d++ = *s++;
|
---|
115 | d++;
|
---|
116 | }
|
---|
117 | }
|
---|
118 |
|
---|
119 | void MGImage::DrawImg(const byte *buffer)
|
---|
120 | {
|
---|
121 | if (TestBit(kSyncMode))
|
---|
122 | while (fMuxPixmap->Lock()==13)
|
---|
123 | usleep(1);
|
---|
124 | else
|
---|
125 | {
|
---|
126 | const Int_t rc = fMuxPixmap->Lock();
|
---|
127 | if (rc==13)
|
---|
128 | cout << "MGImage::DrawImg - mutex is already locked by this thread" << endl;
|
---|
129 | if (rc)
|
---|
130 | return;
|
---|
131 | }
|
---|
132 |
|
---|
133 | switch (gVirtualX->GetDepth())
|
---|
134 | {
|
---|
135 | case 8:
|
---|
136 | memcpy(fImage->data, buffer, fWidth*fHeight);
|
---|
137 | break;
|
---|
138 | case 16:
|
---|
139 | DrawImg16((unsigned short*)fImage->data, (char*)buffer, (char*)(buffer+fWidth*fHeight));
|
---|
140 | break;
|
---|
141 | case 24:
|
---|
142 | DrawImg24(fImage->data, (char*)buffer, (char*)(buffer+fWidth*fHeight));
|
---|
143 | break;
|
---|
144 | default:
|
---|
145 | cout << "Sorry, " << gVirtualX->GetDepth() << "bit color depth not yet implemented." << endl;
|
---|
146 | }
|
---|
147 |
|
---|
148 | SetBit(kNeedRedraw);
|
---|
149 |
|
---|
150 | if (fMuxPixmap->UnLock()==13)
|
---|
151 | cout << "MGImage::DrawImage - tried to unlock mutex locked by other thread." << endl;
|
---|
152 | }
|
---|
153 |
|
---|
154 | void MGImage::DrawColImg16(unsigned short *d, char *s1, char *s2, char *e)
|
---|
155 | {
|
---|
156 | // d=destination, s1=source1, s2=source2, e=end
|
---|
157 | // d: rrrrrggg gggbbbbb
|
---|
158 | // s2: 00rrggbb
|
---|
159 | //
|
---|
160 | while (s1<e)
|
---|
161 | {
|
---|
162 | if (*s2)
|
---|
163 | {
|
---|
164 | // 00000011 00001100 00110000
|
---|
165 | //*d++ = (*s2&0x3) | (*s2&0xb)<<3 | (*s2&0x30)<<7;
|
---|
166 | *d++ = (*s2&0x3)<<3 | (*s2&0xb)<<6 | (*s2&0x30)<<10;
|
---|
167 | }
|
---|
168 | else
|
---|
169 | {
|
---|
170 | // 11111100 11111000 11111100
|
---|
171 | *d++ = (*s1&0xfc) | (*s1&0xf8)<<5 | (*s1&0xfc)<<11;
|
---|
172 | }
|
---|
173 | s1++;
|
---|
174 | s2++;
|
---|
175 | }
|
---|
176 | }
|
---|
177 |
|
---|
178 | void MGImage::DrawColImg24(char *d, char *s1, char *s2, char *e)
|
---|
179 | {
|
---|
180 | // d=destination, s1=source1, s2=source2, e=end
|
---|
181 | while (s1<e)
|
---|
182 | {
|
---|
183 | if (*s2)
|
---|
184 | {
|
---|
185 | *d++ = ((*s2>>4)&0x3)*85;
|
---|
186 | *d++ = ((*s2>>2)&0x3)*85;
|
---|
187 | *d++ = ((*s2++ )&0x3)*85;
|
---|
188 | d++;
|
---|
189 | s1++;
|
---|
190 | }
|
---|
191 | else
|
---|
192 | {
|
---|
193 | *d++ = *s1;
|
---|
194 | *d++ = *s1;
|
---|
195 | *d++ = *s1++;
|
---|
196 | d++;
|
---|
197 | s2++;
|
---|
198 | }
|
---|
199 | }
|
---|
200 | }
|
---|
201 |
|
---|
202 | void MGImage::DrawColImg(const byte *gbuf, const byte *cbuf)
|
---|
203 | {
|
---|
204 | if (TestBit(kSyncMode))
|
---|
205 | while (fMuxPixmap->Lock()==13)
|
---|
206 | usleep(1);
|
---|
207 | else
|
---|
208 | {
|
---|
209 | const Int_t rc = fMuxPixmap->Lock();
|
---|
210 | if (rc==13)
|
---|
211 | cout << "MGImage::DrawColImg - mutex is already locked by this thread" << endl;
|
---|
212 | if (rc)
|
---|
213 | return;
|
---|
214 | }
|
---|
215 |
|
---|
216 | // FROM libAfterImage:
|
---|
217 | // -------------------
|
---|
218 | //#define ALPHA_TRANSPARENT 0x00
|
---|
219 | //#define ALPHA_SEMI_TRANSPARENT 0x7F
|
---|
220 | //#define ALPHA_SOLID 0xFF
|
---|
221 | // * Lowermost 8 bits - Blue channel
|
---|
222 | // * bits 8 to 15 - Green channel
|
---|
223 | // * bits 16 to 23 - Red channel
|
---|
224 | // * bits 24 to 31 - Alpha channel
|
---|
225 | //#define ARGB32_White 0xFFFFFFFF
|
---|
226 | //#define ARGB32_Black 0xFF000000
|
---|
227 |
|
---|
228 | // FIXME: This loop depends on the screen color depth
|
---|
229 | switch (gVirtualX->GetDepth())
|
---|
230 | {
|
---|
231 | case 16:
|
---|
232 | DrawColImg16((unsigned short*)fImage->data, (char*)gbuf, (char*)cbuf, (char*)(gbuf+fWidth*fHeight));
|
---|
233 | break;
|
---|
234 | case 24:
|
---|
235 | DrawColImg24(fImage->data, (char*)gbuf, (char*)cbuf, (char*)(gbuf+fWidth*fHeight));
|
---|
236 | break;
|
---|
237 | default:
|
---|
238 | cout << "Sorry, " << gVirtualX->GetDepth() << "bit color depth not yet implemented." << endl;
|
---|
239 | }
|
---|
240 |
|
---|
241 | SetBit(kNeedRedraw);
|
---|
242 |
|
---|
243 | if (fMuxPixmap->UnLock()==13)
|
---|
244 | cout << "MGImage::DrawColImage - tried to unlock mutex locked by other thread." << endl;
|
---|
245 | }
|
---|