Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
TASUtils.cxx
Go to the documentation of this file.
1// @(#)root/asimage:$Id$
2// Author: Sergey Linev 16/10/2026
3
4/*************************************************************************
5 * Copyright (C) 1995-2026, Rene Brun and Fons Rademakers *
6 * All rights reserved. *
7 * *
8 * For the licensing terms see $ROOTSYS/LICENSE. *
9 * For the list of contributors see $ROOTSYS/README/CREDITS. *
10 *************************************************************************/
11
12#include "TASUtils.h"
13#include "RConfigure.h"
14#include "TVirtualX.h"
15#include "TSystem.h"
16#include "TVirtualPad.h"
17#include "TError.h"
18
19
20#ifdef NO_ASIMAGE
21
22#include "TImage.h"
23
24ASVisual *TASUtils::GetX11Visual()
25{
26 return nullptr;
27}
28
29
31{
32 ::Error("TASUtils::GetPixmap", "Not implemented, ASImage is not compiled");
33 return 0;
34}
35
37{
38 ::Error("TASUtils::GetMask", "Not implemented, ASImage is not compiled");
39 return 0;
40}
41
43{
44 ::Error("TASUtils::PaintImage", "Not implemented, ASImage is not compiled");
45 return kFALSE;
46}
47
49{
50 ::Error("TASUtils::FillImageFromWindow", "Not implemented, ASImage is not compiled");
51 return kFALSE;
52}
53
54std::unique_ptr<TImage> TASUtils::FromWindow(Window_t)
55{
56 ::Error("TASUtils::FromWindow", "Not implemented, ASImage is not compiled");
57 return nullptr;
58}
59
60std::unique_ptr<TImage> TASUtils::FromPixmap(Pixmap_t, Pixmap_t)
61{
62 ::Error("TASUtils::FromPixmap", "Not implemented, ASImage is not compiled");
63 return nullptr;
64}
65
66#else // NO_ASIMAGE
67
68#include "TASImage.h"
69
71{
72#ifndef WIN32
73#ifndef R__HAS_COCOA
74
75 thread_local struct ASHolder {
76 ASVisual *fVisual = nullptr;
77 ~ASHolder()
78 {
79 if (fVisual) {
81 fVisual = nullptr;
82 }
83 }
84 } holder;
85
86 static Bool_t x11 = gVirtualX->InheritsFrom("TGX11");
87
88 if (x11) {
89 if (!holder.fVisual)
90 holder.fVisual =
91 TASImage::CreateX11Visual(gVirtualX->GetDisplay(), gVirtualX->GetScreen(), gVirtualX->GetDepth(),
92 gVirtualX->GetVisual(), gVirtualX->GetColormap());
93 return holder.fVisual;
94 }
95
96#endif
97#endif
98 return nullptr;
99}
100
101
102////////////////////////////////////////////////////////////////////////////////
103/// Returns image pixmap.
104/// The pixmap must deleted by user.
105/// Replacement for deprecated TImage::GetPixmap method
106/// Special handling for X11-based displays where ASImage has optimized implementation
107
109{
110 if (!timg) {
111 ::Error("TASUtils::GetPixmap", "Image not provided");
112 return 0;
113 }
114
115 auto vis = GetX11Visual();
116
117 if (vis) {
118 auto asimg = dynamic_cast<TASImage *>(timg);
119 if (!asimg) {
120 ::Warning("TASUtils::GetPixmap", "ASImage not provided");
121 return 0;
122 }
123
124 ASImage *img = asimg->fScaledImage ? asimg->fScaledImage->fImage : asimg->fImage;
125
126 return TASImage::ASImage2Pixmap(vis, gVirtualX->GetDefaultRootWindow(), img);
127 }
128
129 auto argb = timg->GetArgbArray();
130 if (!argb) {
131 ::Error("TASUtils::GetPixmap", "ARGB array is not exists");
132 return 0;
133 }
134
135 return gVirtualX->CreatePixmapFromData((unsigned char*) argb, timg->GetScaledWidth(), timg->GetScaledHeight());
136}
137
138////////////////////////////////////////////////////////////////////////////////
139/// Returns image mask pixmap (alpha channel).
140/// The pixmap must deleted by user.
141/// Replacement for deprecated TImage::GetMask() method
142
144{
145 Pixmap_t pixmap = 0;
146
147 UInt_t width = 0, height = 0;
148 std::vector<char> bits;
149
150 if (!img)
151 ::Error("TGWindow::GetImageMask", "Image not provided");
152 else if (img->GetMaskBits(width, height, bits))
153 pixmap = gVirtualX->CreateBitmap(gVirtualX->GetDefaultRootWindow(), bits.data(), width, height);
154
155 return pixmap;
156}
157
158////////////////////////////////////////////////////////////////////////////////
159/// Create an image from the given window
160
162{
163 auto asimg = dynamic_cast<TASImage *> (img);
164 if (!asimg || (width < 0) || (height < 0))
165 return kFALSE;
166
167 asimg->DestroyImage();
168 asimg->DestroyScaledImage();
169
170 gVirtualX->Update(1);
171 if (!gThreadXAR) {
172 gSystem->Sleep(10);
174 gSystem->Sleep(10);
176 }
177
178 auto vis = GetX11Visual();
179
180 if (vis) {
181 asimg->fImage = TASImage::Pixmap2ASImage(vis, wid, 0, 0, width, height);
182 } else {
183 unsigned char *bits = gVirtualX->GetColorBits(wid, 0, 0, width, height);
184 if (!bits) {
185 ::Error("TASUtils::FillImageFromWindow", "Fail to extract color bits for specified window");
186 return kFALSE;
187 }
188 asimg->FromBitmap(bits, width, height);
189 delete [] bits;
190 }
191
192 if (!asimg->IsValid())
193 return kFALSE;
194
195 return kTRUE;
196}
197
198////////////////////////////////////////////////////////////////////////////////
199/// Create an image from the given window
200
201std::unique_ptr<TImage> TASUtils::FromWindow(Window_t wid)
202{
203 Int_t x = 0, y = 0;
204 UInt_t w = 0, h = 0;
205
206 gVirtualX->GetWindowSize(wid, x, y, w, h);
207
208 (void) x; // position of window is not used
209 (void) y;
210
211 auto asimg = std::make_unique<TASImage>();
212
213 if (!FillImageFromWindow(asimg.get(), wid, w, h))
214 return nullptr;
215
216 return asimg;
217}
218
219////////////////////////////////////////////////////////////////////////////////
220/// Create image from pixmap.
221
223{
224 auto asimg = std::make_unique<TASImage>();
225
226 Int_t xy;
227 UInt_t w, h;
228 gVirtualX->GetWindowSize(pxm, xy, xy, w, h);
229
230 asimg->SetName(TString::Format("img_%dx%d",w, h));
231
232 auto vis = GetX11Visual();
233
234 if (vis) { //use built-in optimized version
235 asimg->fImage = TASImage::Picture2ASImage(vis, pxm, mask, w, h);
236 } else {
237 unsigned char *bits = gVirtualX->GetColorBits(pxm, 0, 0, w, h);
238 if (!bits) // error
239 return nullptr;
240
241 unsigned char *mask_bits = mask ? gVirtualX->GetColorBits(mask, 0, 0, w, h) : nullptr;
242
243 asimg->FromBitmap(bits, w, h, mask_bits);
244
245 if (mask_bits)
246 delete [] mask_bits;
247 delete [] bits;
248 }
249
250 if (!asimg->IsValid())
251 return nullptr;
252
253 return asimg;
254}
255
256////////////////////////////////////////////////////////////////////////////////
257/// Paint image on specified window
258
260{
261 auto asimg = dynamic_cast<TASImage *>(timg);
262 if (!asimg) {
263 ::Warning("TASUtils::PaintImage", "Image not provided");
264 return kFALSE;
265 }
266
268
269 Bool_t isX11 = gVirtualX->InheritsFrom("TGX11");
270
271 if (isX11) {
272 UInt_t mwidth = 0, mheight = 0;
273 std::vector<char> bits;
274 if (asimg->GetMaskBits(mwidth, mheight, bits))
275 mask = gVirtualX->CreateBitmap(gVirtualX->GetDefaultRootWindow(), bits.data(), mwidth, mheight);
276 }
277
278 GCValues_t gv;
280 gv.fClipMask = mask;
281 gv.fClipXOrigin = x;
282 gv.fClipYOrigin = y;
283
284 GContext_t fGC = gVirtualX->CreateGC(gVirtualX->GetDefaultRootWindow(), &gv);
285
286 UInt_t width = asimg->GetScaledWidth();
287 UInt_t height = asimg->GetScaledHeight();
288
289 Bool_t res = kTRUE;
290
291 auto vis = GetX11Visual();
292
293 if (isX11 && vis) { //use built-in optimized version
294 ASImage *im = asimg->fScaledImage ? asimg->fScaledImage->fImage : asimg->fImage;
295 if (im)
296 TASImage::ASImage2Drawable(vis, im, wid, fGC, 0, 0, x, y, width, height);
297 else
298 res = kFALSE;
299 } else {
300 auto argb = asimg->GetArgbArray();
301
302 Pixmap_t pic = argb ? gVirtualX->CreatePixmapFromData((unsigned char *) argb, width, height) : 0;
303
304 if (pic) {
305 if (!opaque) {
306 SETBIT(width, 31);
307 SETBIT(height, 31);
308 }
309 gVirtualX->CopyArea(pic, wid, fGC, 0, 0, width, height, x, y);
310 gVirtualX->DeletePixmap(pic);
311 } else
312 res = kFALSE;
313 }
314
315 // free mask pixmap
316 if (mask != kNone)
317 gVirtualX->DeletePixmap(mask);
318
319 gv.fMask = kGCClipMask;
320 gv.fClipMask = kNone;
321 if (fGC) {
322 gVirtualX->ChangeGC(fGC, &gv);
323 gVirtualX->DeleteGC(fGC);
324 }
325
326 return res;
327}
328
329#endif // NO_ASIMAGE
Handle_t Pixmap_t
Pixmap handle.
Definition GuiTypes.h:31
const Mask_t kGCClipXOrigin
Definition GuiTypes.h:304
Handle_t Window_t
Window handle.
Definition GuiTypes.h:29
Handle_t GContext_t
Graphics context handle.
Definition GuiTypes.h:39
Handle_t Drawable_t
Drawable handle.
Definition GuiTypes.h:32
const Handle_t kNone
Definition GuiTypes.h:89
const Mask_t kGCClipYOrigin
Definition GuiTypes.h:305
const Mask_t kGCClipMask
Definition GuiTypes.h:306
#define h(i)
Definition RSha256.hxx:106
bool Bool_t
Boolean (0=false, 1=true) (bool)
Definition RtypesCore.h:78
int Int_t
Signed integer 4 bytes (int)
Definition RtypesCore.h:60
constexpr Bool_t kFALSE
Definition RtypesCore.h:109
constexpr Bool_t kTRUE
Definition RtypesCore.h:108
#define SETBIT(n, i)
Definition Rtypes.h:91
void Error(const char *location, const char *msgfmt,...)
Use this function in case an error occurred.
Definition TError.cxx:208
void Warning(const char *location, const char *msgfmt,...)
Use this function in warning situations.
Definition TError.cxx:252
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t Float_t Float_t Int_t Int_t UInt_t UInt_t Rectangle_t mask
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize wid
Option_t Option_t TPoint xy
Option_t Option_t width
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void pxm
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t height
R__EXTERN TSystem * gSystem
Definition TSystem.h:582
R__EXTERN Int_t(* gThreadXAR)(const char *xact, Int_t nb, void **ar, Int_t *iret)
#define gVirtualX
Definition TVirtualX.h:379
Image class.
Definition TASImage.h:34
static ASImage * Picture2ASImage(struct ASVisual *visual, Pixmap_t pxm, Pixmap_t mask, UInt_t width, UInt_t height)
Convert picture (X11 pixmap with mask) to ASImage For internal use in TASUtils class.
static void ASImage2Drawable(struct ASVisual *visual, ASImage *image, Drawable_t wid, GContext_t gc, Int_t xsrc, Int_t ysrc, Int_t x, Int_t y, UInt_t width, UInt_t height)
Output ASImage on X11 drawable For internal use in TASUtils class.
static ASImage * Pixmap2ASImage(struct ASVisual *visual, Window_t wid, Int_t x, Int_t y, UInt_t width, UInt_t height)
Convert region of X11 window to ASImage For internal use in TASUtils class.
void DestroyImage()
Destroy image.
Definition TASImage.cxx:214
static struct ASVisual * CreateX11Visual(Display_t display, Int_t screen, Int_t depth, Visual_t visual, Colormap_t cmap)
Create ASVisual for X11 display For internal use in TASUtils class.
static Pixmap_t ASImage2Pixmap(struct ASVisual *visual, Window_t root_window, ASImage *image)
Convert ASImage to pixmap for X11 For internal use in TASUtils class.
static void DestroyVisual(struct ASVisual *visual)
Destroy ASVisual for X11 display For internal use in TASUtils class.
static Pixmap_t GetPixmap(TImage *timg)
Returns image pixmap.
Definition TASUtils.cxx:108
static std::unique_ptr< TImage > FromWindow(Window_t wid)
Create an image from the given window.
Definition TASUtils.cxx:201
static Bool_t FillImageFromWindow(TImage *img, Window_t wid, Int_t width, Int_t height)
Create an image from the given window.
Definition TASUtils.cxx:161
static Bool_t PaintImage(TImage *timg, Drawable_t wid, Int_t x, Int_t y, Bool_t opaque=kFALSE)
Paint image on specified window.
Definition TASUtils.cxx:259
static Pixmap_t GetMask(TImage *timg)
Returns image mask pixmap (alpha channel).
Definition TASUtils.cxx:143
static std::unique_ptr< TImage > FromPixmap(Pixmap_t pxm, Pixmap_t mask)
Create image from pixmap.
Definition TASUtils.cxx:222
static struct ASVisual * GetX11Visual()
Definition TASUtils.cxx:70
An abstract interface to image processing library.
Definition TImage.h:30
virtual Bool_t GetMaskBits(UInt_t &, UInt_t &, std::vector< char > &)
Definition TImage.h:253
virtual UInt_t GetScaledWidth() const
Definition TImage.h:245
virtual UInt_t * GetArgbArray()
Definition TImage.h:255
virtual UInt_t GetScaledHeight() const
Definition TImage.h:246
static TString Format(const char *fmt,...)
Static method which formats a string using a printf style format descriptor and return a TString.
Definition TString.cxx:2460
virtual void Sleep(UInt_t milliSec)
Sleep milliSec milli seconds.
Definition TSystem.cxx:440
virtual Bool_t ProcessEvents()
Process pending events (GUI, timers, sockets).
Definition TSystem.cxx:419
Double_t y[n]
Definition legend1.C:17
Double_t x[n]
Definition legend1.C:17
Graphics context structure.
Definition GuiTypes.h:225
Pixmap_t fClipMask
bitmap clipping; other calls for rects
Definition GuiTypes.h:248
Int_t fClipYOrigin
Definition GuiTypes.h:247
Int_t fClipXOrigin
origin for clipping
Definition GuiTypes.h:246
Mask_t fMask
bit mask specifying which fields are valid
Definition GuiTypes.h:252