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
24
26{
27 ::Error("TASUtils::GetPixmap", "Not implemented, ASImage is not compiled");
28 return 0;
29}
30
32{
33 ::Error("TASUtils::GetMask", "Not implemented, ASImage is not compiled");
34 return 0;
35}
36
38{
39 ::Error("TASUtils::PaintImage", "Not implemented, ASImage is not compiled");
40 return kFALSE;
41}
42
44{
45 ::Error("TASUtils::FillImageFromWindow", "Not implemented, ASImage is not compiled");
46 return kFALSE;
47}
48
49std::unique_ptr<TImage> TASUtils::FromWindow(Window_t)
50{
51 ::Error("TASUtils::FromWindow", "Not implemented, ASImage is not compiled");
52 return nullptr;
53}
54
55std::unique_ptr<TImage> TASUtils::FromPixmap(Pixmap_t, Pixmap_t)
56{
57 ::Error("TASUtils::FromPixmap", "Not implemented, ASImage is not compiled");
58 return nullptr;
59}
60
61#else // NO_ASIMAGE
62
63#include "TASImage.h"
64
65#ifdef WIN32
66#define X_DISPLAY_MISSING 1
67#include "Windows4root.h"
68#else
69#ifdef R__HAS_COCOA
70#define X_DISPLAY_MISSING 1
71#endif
72#endif
73
74#include "afterbase.h"
75#include "afterimage.h"
76#include "bmp.h"
77
78#ifndef X_DISPLAY_MISSING
79const unsigned long kAllPlanes = ~0;
80#endif
81
82// Holder of ASVisual instance, destroyed when application closed
84 ASVisual *fVisual = nullptr;
85
86public:
87
88 GContext_t fGC = 0; // context used in PaintImage
89
90 ASVisual *operator()()
91 {
92#ifndef X_DISPLAY_MISSING
93 if (!fVisual) {
94 Display *disp = (Display*) gVirtualX->GetDisplay();
95 Int_t screen = gVirtualX->GetScreen();
96 Int_t depth = gVirtualX->GetDepth();
97 Visual *vis = (Visual*) gVirtualX->GetVisual();
98 Colormap cmap = (Colormap) gVirtualX->GetColormap();
99
100 if (vis && cmap)
101 fVisual = create_asvisual_for_id(disp, screen, depth, XVisualIDFromVisual(vis), cmap, nullptr);
102 }
103#endif
104 if (!fVisual) {
105 fVisual = create_asvisual(nullptr, 0, 0, nullptr);
106 fVisual->dpy = nullptr; // fake (not used)
107 }
108
109 return fVisual;
110 }
111
113 {
114 if (fVisual) {
115 destroy_asvisual(fVisual, 0);
116 fVisual = nullptr;
117 }
118 if (fGC && gVirtualX)
119 gVirtualX->DeleteGC(fGC);
120 }
121};
122
124
125
126////////////////////////////////////////////////////////////////////////////////
127/// Returns image pixmap.
128/// The pixmap must deleted by user.
129/// Replacement for deprecated TImage::GetPixmap method
130/// Special handling for X11-based displays where ASImage has optimized implementation
131
133{
134 if (!timg) {
135 ::Error("TASUtils::GetPixmap", "Image not provided");
136 return 0;
137 }
138 static Bool_t x11 = gVirtualX->InheritsFrom("TGX11");
139
140 if (x11) {
141#ifndef X_DISPLAY_MISSING
142 auto asimg = dynamic_cast<TASImage *>(timg);
143 if (!asimg) {
144 ::Warning("TASUtils::GetPixmap", "ASImage not provided");
145 return 0;
146 }
147
148 auto vis = holder();
149
150 if (!vis) {
151 ::Warning("TASUtils::GetPixmap", "Visual not initiated");
152 return 0;
153 }
154
155 ASImage *img = asimg->fScaledImage ? asimg->fScaledImage->fImage : asimg->fImage;
156
157 return (Pixmap_t)asimage2pixmap(vis, gVirtualX->GetDefaultRootWindow(), img, nullptr, kTRUE);
158#else
159 ::Error("TASUtils::GetPixmap", "Invoke X11-specific methods on unsupported platform");
160 return 0;
161#endif
162 }
163
164 auto argb = timg->GetArgbArray();
165 if (!argb) {
166 ::Error("TASUtils::GetPixmap", "ARGB array is not exists");
167 return 0;
168 }
169
170 return gVirtualX->CreatePixmapFromData((unsigned char*) argb, timg->GetScaledWidth(), timg->GetScaledHeight());
171}
172
173////////////////////////////////////////////////////////////////////////////////
174/// Returns image mask pixmap (alpha channel).
175/// The pixmap must deleted by user.
176/// Replacement for deprecated TImage::GetMask() method
177
179{
180 Pixmap_t pixmap = 0;
181
182 UInt_t width = 0, height = 0;
183 std::vector<char> bits;
184
185 if (!img)
186 ::Error("TGWindow::GetImageMask", "Image not provided");
187 else if (img->GetMaskBits(width, height, bits))
188 pixmap = gVirtualX->CreateBitmap(gVirtualX->GetDefaultRootWindow(), bits.data(), width, height);
189
190 return pixmap;
191}
192
193////////////////////////////////////////////////////////////////////////////////
194/// Create an image from the given window
195
197{
198 auto asimg = dynamic_cast<TASImage *> (img);
199 if (!asimg)
200 return kFALSE;
201
202 asimg->DestroyImage();
203 asimg->DestroyScaledImage();
204
205 gVirtualX->Update(1);
206 if (!gThreadXAR) {
207 gSystem->Sleep(10);
209 gSystem->Sleep(10);
211 }
212
213 static Bool_t x11 = gVirtualX->InheritsFrom("TGX11");
214
215 if (x11) {
216 auto vis = holder();
217
218 if (!vis) {
219 ::Error("TASUtils::FillImageFromWindow", "Visual not initiated");
220 return kFALSE;
221 }
222
223#ifndef X_DISPLAY_MISSING
224 asimg->fImage = pixmap2asimage(vis, wid, 0, 0, width, height, kAllPlanes, 0, 0);
225#else
226 ::Error("TASUtils::FillImageFromWindow", "Invoke X11-specific methods on unsupported platform");
227#endif
228 } else {
229 unsigned char *bits = gVirtualX->GetColorBits(wid, 0, 0, width, height);
230 if (!bits) {
231 ::Error("TASUtils::FillImageFromWindow", "Fail to extract color bits for specified window");
232 return kFALSE;
233 }
234 asimg->FromBitmap(bits, width, height);
235 delete [] bits;
236 }
237
238 if (!asimg->IsValid())
239 return kFALSE;
240
241 return kTRUE;
242}
243
244////////////////////////////////////////////////////////////////////////////////
245/// Create an image from the given window
246
247std::unique_ptr<TImage> TASUtils::FromWindow(Window_t wid)
248{
249 Int_t x = 0, y = 0;
250 UInt_t w = 0, h = 0;
251
252 gVirtualX->GetWindowSize(wid, x, y, w, h);
253
254 (void) x; // position of window is not used
255 (void) y;
256
257 auto asimg = std::make_unique<TASImage>();
258
259 if (!FillImageFromWindow(asimg.get(), wid, w, h))
260 return nullptr;
261
262 return asimg;
263}
264
265////////////////////////////////////////////////////////////////////////////////
266/// Create image from pixmap.
267
269{
270 auto asimg = std::make_unique<TASImage>();
271
272 Int_t xy;
273 UInt_t w, h;
274 gVirtualX->GetWindowSize(pxm, xy, xy, w, h);
275
276 asimg->SetName(TString::Format("img_%dx%d",w, h));
277
278 if (gVirtualX->InheritsFrom("TGX11")) { //use built-in optimized version
279 auto vis = holder();
280
281 if (!vis) {
282 ::Warning("TASUtils::FromPixmap", "Visual not initiated");
283 return nullptr;
284 }
285
286#ifndef X_DISPLAY_MISSING
287 asimg->fImage = picture2asimage(vis, pxm, mask, 0, 0, w, h, kAllPlanes, 1, 0);
288#else
289 ::Error("TASUtils::FromPixmap", "Invoke X11-specific methods on unsupported platform");
290#endif
291 } else {
292 unsigned char *bits = gVirtualX->GetColorBits(pxm, 0, 0, w, h);
293 if (!bits) // error
294 return nullptr;
295
296 unsigned char *mask_bits = mask ? gVirtualX->GetColorBits(mask, 0, 0, w, h) : nullptr;
297
298 asimg->FromBitmap(bits, w, h, mask_bits);
299
300 if (mask_bits)
301 delete [] mask_bits;
302 delete [] bits;
303 }
304
305 if (!asimg->IsValid())
306 return nullptr;
307
308 return asimg;
309}
310
311////////////////////////////////////////////////////////////////////////////////
312/// Paint image on specified window
313
315{
316 auto asimg = dynamic_cast<TASImage *>(timg);
317 if (!asimg) {
318 ::Warning("TASUtils::PaintImage", "Image not provided");
319 return kFALSE;
320 }
321
323
324 Bool_t isX11 = gVirtualX->InheritsFrom("TGX11");
325
326 if (isX11) {
327 UInt_t mwidth = 0, mheight = 0;
328 std::vector<char> bits;
329 if (asimg->GetMaskBits(mwidth, mheight, bits))
330 mask = gVirtualX->CreateBitmap(gVirtualX->GetDefaultRootWindow(), bits.data(), mwidth, mheight);
331 }
332
333 GCValues_t gv;
335 gv.fClipMask = mask;
336 gv.fClipXOrigin = x;
337 gv.fClipYOrigin = y;
338
339 if (!holder.fGC) {
340 holder.fGC = gVirtualX->CreateGC(gVirtualX->GetDefaultRootWindow(), &gv);
341 } else {
342 gVirtualX->ChangeGC(holder.fGC, &gv);
343 }
344
345 UInt_t width = asimg->GetScaledWidth();
346 UInt_t height = asimg->GetScaledHeight();
347
348 Bool_t res = kTRUE;
349
350 if (isX11) { //use built-in optimized version
351 #ifndef X_DISPLAY_MISSING
352 ASImage *im = asimg->fScaledImage ? asimg->fScaledImage->fImage : asimg->fImage;
353 auto vis = holder();
354 if (vis && im)
355 asimage2drawable(vis, wid, im, (GC)holder.fGC, 0, 0, x, y, width, height, 1);
356 else
357 res = kFALSE;
358 #else
359 ::Error("TASUtils::PaintImage", "Invoke X11-specific methods on unsupported platform");
360 res = kFALSE;
361 #endif
362 } else {
363 auto argb = asimg->GetArgbArray();
364
365 Pixmap_t pic = argb ? gVirtualX->CreatePixmapFromData((unsigned char *) argb, width, height) : 0;
366
367 if (pic) {
368 if (!opaque) {
369 SETBIT(width, 31);
370 SETBIT(height, 31);
371 }
372 gVirtualX->CopyArea(pic, wid, holder.fGC, 0, 0, width, height, x, y);
373 gVirtualX->DeletePixmap(pic);
374 } else
375 res = kFALSE;
376 }
377
378 // free mask pixmap
379 if (mask != kNone)
380 gVirtualX->DeletePixmap(mask);
381
382 gv.fMask = kGCClipMask;
383 gv.fClipMask = kNone;
384 if (holder.fGC)
385 gVirtualX->ChangeGC(holder.fGC, &gv);
386
387 return res;
388}
389
390#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
static unsigned long kAllPlanes
Definition TASImage.cxx:125
thread_local ASVisualHolder holder
Definition TASUtils.cxx:123
const unsigned long kAllPlanes
Definition TASUtils.cxx:79
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 void cmap
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
XID Colormap
Definition TGX11.h:37
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
GContext_t fGC
Definition TASUtils.cxx:88
ASVisual * fVisual
Definition TASUtils.cxx:84
ASVisual * operator()()
Definition TASUtils.cxx:90
Image class.
Definition TASImage.h:34
void DestroyImage()
Destroy image.
Definition TASImage.cxx:176
static Pixmap_t GetPixmap(TImage *timg)
Returns image pixmap.
Definition TASUtils.cxx:132
static std::unique_ptr< TImage > FromWindow(Window_t wid)
Create an image from the given window.
Definition TASUtils.cxx:247
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:196
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:314
static Pixmap_t GetMask(TImage *timg)
Returns image mask pixmap (alpha channel).
Definition TASUtils.cxx:178
static std::unique_ptr< TImage > FromPixmap(Pixmap_t pxm, Pixmap_t mask)
Create image from pixmap.
Definition TASUtils.cxx:268
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