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

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