| 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 |  | 
|---|
| 102 | //      11111000       11111100       11111000 | 
|---|
| 103 | *d++ = (*s&0xf8)<<8 | (*s&0xfc)<<3 | (*s>>3); | 
|---|
| 104 | s++; | 
|---|
| 105 | } | 
|---|
| 106 | } | 
|---|
| 107 |  | 
|---|
| 108 | void MGImage::DrawImg24(char *d, char *s, char *e) | 
|---|
| 109 | { | 
|---|
| 110 | // d=destination, s=source, e=end | 
|---|
| 111 | // rrrrrrrr gggggggg bbbbbbbb aaaaaaaa | 
|---|
| 112 | // | 
|---|
| 113 | while (s<e) | 
|---|
| 114 | { | 
|---|
| 115 | *d++ = *s; | 
|---|
| 116 | *d++ = *s; | 
|---|
| 117 | *d++ = *s++; | 
|---|
| 118 | d++; | 
|---|
| 119 | } | 
|---|
| 120 | } | 
|---|
| 121 |  | 
|---|
| 122 | void MGImage::DrawImg(const byte *buffer) | 
|---|
| 123 | { | 
|---|
| 124 | if (TestBit(kSyncMode)) | 
|---|
| 125 | while (fMuxPixmap->Lock()==13) | 
|---|
| 126 | usleep(1); | 
|---|
| 127 | else | 
|---|
| 128 | { | 
|---|
| 129 | const Int_t rc = fMuxPixmap->Lock(); | 
|---|
| 130 | if (rc==13) | 
|---|
| 131 | cout << "MGImage::DrawImg - mutex is already locked by this thread" << endl; | 
|---|
| 132 | if (rc) | 
|---|
| 133 | return; | 
|---|
| 134 | } | 
|---|
| 135 |  | 
|---|
| 136 | switch (gVirtualX->GetDepth()) | 
|---|
| 137 | { | 
|---|
| 138 | case 8: | 
|---|
| 139 | memcpy(fImage->data, buffer, fWidth*fHeight); | 
|---|
| 140 | break; | 
|---|
| 141 | case 16: | 
|---|
| 142 | DrawImg16((unsigned short*)fImage->data, (char*)buffer, (char*)(buffer+fWidth*fHeight)); | 
|---|
| 143 | break; | 
|---|
| 144 | case 24: | 
|---|
| 145 | DrawImg24(fImage->data, (char*)buffer, (char*)(buffer+fWidth*fHeight)); | 
|---|
| 146 | break; | 
|---|
| 147 | default: | 
|---|
| 148 | cout << "Sorry, " << gVirtualX->GetDepth() << "bit color depth not yet implemented." << endl; | 
|---|
| 149 | } | 
|---|
| 150 |  | 
|---|
| 151 | SetBit(kNeedRedraw); | 
|---|
| 152 |  | 
|---|
| 153 | if (fMuxPixmap->UnLock()==13) | 
|---|
| 154 | cout << "MGImage::DrawImage - tried to unlock mutex locked by other thread." << endl; | 
|---|
| 155 | } | 
|---|
| 156 |  | 
|---|
| 157 | void MGImage::DrawColImg16(unsigned short *d, char *s1, char *s2, char *e) | 
|---|
| 158 | { | 
|---|
| 159 | // d=destination, s1=source1, s2=source2, e=end | 
|---|
| 160 | // d:  rrrrrggg gggbbbbb | 
|---|
| 161 | // s2:          00rrggbb | 
|---|
| 162 | // | 
|---|
| 163 | while (s1<e) | 
|---|
| 164 | { | 
|---|
| 165 | if (*s2) | 
|---|
| 166 | { | 
|---|
| 167 | //      00000011   00001100        00110000 | 
|---|
| 168 | //*d++ = (*s2&0x3) | (*s2&0xb)<<3 | (*s2&0x30)<<7; | 
|---|
| 169 | *d++ = (*s2&0x3)<<3 | (*s2&0xb)<<6 | (*s2&0x30)<<10; | 
|---|
| 170 | } | 
|---|
| 171 | else | 
|---|
| 172 | { | 
|---|
| 173 | //      11111100     11111000        11111100 | 
|---|
| 174 | *d++ = (*s1&0xfc) | (*s1&0xf8)<<5 | (*s1&0xfc)<<11; | 
|---|
| 175 | } | 
|---|
| 176 | s1++; | 
|---|
| 177 | s2++; | 
|---|
| 178 | } | 
|---|
| 179 | } | 
|---|
| 180 |  | 
|---|
| 181 | void MGImage::DrawColImg24(char *d, char *s1, char *s2, char *e) | 
|---|
| 182 | { | 
|---|
| 183 | // d=destination, s1=source1, s2=source2, e=end | 
|---|
| 184 | while (s1<e) | 
|---|
| 185 | { | 
|---|
| 186 | if (*s2) | 
|---|
| 187 | { | 
|---|
| 188 | *d++ = ((*s2>>4)&0x3)*85; | 
|---|
| 189 | *d++ = ((*s2>>2)&0x3)*85; | 
|---|
| 190 | *d++ = ((*s2++ )&0x3)*85; | 
|---|
| 191 | d++; | 
|---|
| 192 | s1++; | 
|---|
| 193 | } | 
|---|
| 194 | else | 
|---|
| 195 | { | 
|---|
| 196 | *d++ = *s1; | 
|---|
| 197 | *d++ = *s1; | 
|---|
| 198 | *d++ = *s1++; | 
|---|
| 199 | d++; | 
|---|
| 200 | s2++; | 
|---|
| 201 | } | 
|---|
| 202 | } | 
|---|
| 203 | } | 
|---|
| 204 |  | 
|---|
| 205 | void MGImage::DrawColImg(const byte *gbuf, const byte *cbuf) | 
|---|
| 206 | { | 
|---|
| 207 | if (TestBit(kSyncMode)) | 
|---|
| 208 | while (fMuxPixmap->Lock()==13) | 
|---|
| 209 | usleep(1); | 
|---|
| 210 | else | 
|---|
| 211 | { | 
|---|
| 212 | const Int_t rc = fMuxPixmap->Lock(); | 
|---|
| 213 | if (rc==13) | 
|---|
| 214 | cout << "MGImage::DrawColImg - mutex is already locked by this thread" << endl; | 
|---|
| 215 | if (rc) | 
|---|
| 216 | return; | 
|---|
| 217 | } | 
|---|
| 218 |  | 
|---|
| 219 | // FROM libAfterImage: | 
|---|
| 220 | // ------------------- | 
|---|
| 221 | //#define ALPHA_TRANSPARENT         0x00 | 
|---|
| 222 | //#define ALPHA_SEMI_TRANSPARENT    0x7F | 
|---|
| 223 | //#define ALPHA_SOLID               0xFF | 
|---|
| 224 | // * Lowermost 8 bits - Blue channel | 
|---|
| 225 | // * bits  8 to 15    - Green channel | 
|---|
| 226 | // * bits 16 to 23    - Red channel | 
|---|
| 227 | // * bits 24 to 31    - Alpha channel | 
|---|
| 228 | //#define ARGB32_White              0xFFFFFFFF | 
|---|
| 229 | //#define ARGB32_Black              0xFF000000 | 
|---|
| 230 |  | 
|---|
| 231 | // FIXME: This loop depends on the screen color depth | 
|---|
| 232 | switch (gVirtualX->GetDepth()) | 
|---|
| 233 | { | 
|---|
| 234 | case 16: | 
|---|
| 235 | DrawColImg16((unsigned short*)fImage->data, (char*)gbuf, (char*)cbuf, (char*)(gbuf+fWidth*fHeight)); | 
|---|
| 236 | break; | 
|---|
| 237 | case 24: | 
|---|
| 238 | DrawColImg24(fImage->data, (char*)gbuf, (char*)cbuf, (char*)(gbuf+fWidth*fHeight)); | 
|---|
| 239 | break; | 
|---|
| 240 | default: | 
|---|
| 241 | cout << "Sorry, " << gVirtualX->GetDepth() << "bit color depth not yet implemented." << endl; | 
|---|
| 242 | } | 
|---|
| 243 |  | 
|---|
| 244 | SetBit(kNeedRedraw); | 
|---|
| 245 |  | 
|---|
| 246 | if (fMuxPixmap->UnLock()==13) | 
|---|
| 247 | cout << "MGImage::DrawColImage - tried to unlock mutex locked by other thread." << endl; | 
|---|
| 248 | } | 
|---|