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
123namespace {
124
125thread_local ASVisualHolder holder;
126
127}
128
129////////////////////////////////////////////////////////////////////////////////
130/// Returns image pixmap.
131/// The pixmap must deleted by user.
132/// Replacement for deprecated TImage::GetPixmap method
133/// Special handling for X11-based displays where ASImage has optimized implementation
134
136{
137 if (!timg) {
138 ::Error("TASUtils::GetPixmap", "Image not provided");
139 return 0;
140 }
141 static Bool_t x11 = gVirtualX->InheritsFrom("TGX11");
142
143 if (x11) {
144#ifndef X_DISPLAY_MISSING
145 auto asimg = dynamic_cast<TASImage *>(timg);
146 if (!asimg) {
147 ::Warning("TASUtils::GetPixmap", "ASImage not provided");
148 return 0;
149 }
150
151 auto vis = holder();
152
153 if (!vis) {
154 ::Warning("TASUtils::GetPixmap", "Visual not initiated");
155 return 0;
156 }
157
158 ASImage *img = asimg->fScaledImage ? asimg->fScaledImage->fImage : asimg->fImage;
159
160 return (Pixmap_t)asimage2pixmap(vis, gVirtualX->GetDefaultRootWindow(), img, nullptr, kTRUE);
161#else
162 ::Error("TASUtils::GetPixmap", "Invoke X11-specific methods on unsupported platform");
163 return 0;
164#endif
165 }
166
167 auto argb = timg->GetArgbArray();
168 if (!argb) {
169 ::Error("TASUtils::GetPixmap", "ARGB array is not exists");
170 return 0;
171 }
172
173 return gVirtualX->CreatePixmapFromData((unsigned char*) argb, timg->GetScaledWidth(), timg->GetScaledHeight());
174}
175
176////////////////////////////////////////////////////////////////////////////////
177/// Returns image mask pixmap (alpha channel).
178/// The pixmap must deleted by user.
179/// Replacement for deprecated TImage::GetMask() method
180
182{
183 Pixmap_t pixmap = 0;
184
185 UInt_t width = 0, height = 0;
186 std::vector<char> bits;
187
188 if (!img)
189 ::Error("TGWindow::GetImageMask", "Image not provided");
190 else if (img->GetMaskBits(width, height, bits))
191 pixmap = gVirtualX->CreateBitmap(gVirtualX->GetDefaultRootWindow(), bits.data(), width, height);
192
193 return pixmap;
194}
195
196////////////////////////////////////////////////////////////////////////////////
197/// Create an image from the given window
198
200{
201 auto asimg = dynamic_cast<TASImage *> (img);
202 if (!asimg)
203 return kFALSE;
204
205 asimg->DestroyImage();
206 asimg->DestroyScaledImage();
207
208 gVirtualX->Update(1);
209 if (!gThreadXAR) {
210 gSystem->Sleep(10);
212 gSystem->Sleep(10);
214 }
215
216 static Bool_t x11 = gVirtualX->InheritsFrom("TGX11");
217
218 if (x11) {
219 auto vis = holder();
220
221 if (!vis) {
222 ::Error("TASUtils::FillImageFromWindow", "Visual not initiated");
223 return kFALSE;
224 }
225
226#ifndef X_DISPLAY_MISSING
227 asimg->fImage = pixmap2asimage(vis, wid, 0, 0, width, height, kAllPlanes, 0, 0);
228#else
229 ::Error("TASUtils::FillImageFromWindow", "Invoke X11-specific methods on unsupported platform");
230#endif
231 } else {
232 unsigned char *bits = gVirtualX->GetColorBits(wid, 0, 0, width, height);
233 if (!bits) {
234 ::Error("TASUtils::FillImageFromWindow", "Fail to extract color bits for specified window");
235 return kFALSE;
236 }
237 asimg->FromBitmap(bits, width, height);
238 delete [] bits;
239 }
240
241 if (!asimg->IsValid())
242 return kFALSE;
243
244 return kTRUE;
245}
246
247////////////////////////////////////////////////////////////////////////////////
248/// Create an image from the given window
249
250std::unique_ptr<TImage> TASUtils::FromWindow(Window_t wid)
251{
252 Int_t x = 0, y = 0;
253 UInt_t w = 0, h = 0;
254
255 gVirtualX->GetWindowSize(wid, x, y, w, h);
256
257 (void) x; // position of window is not used
258 (void) y;
259
260 auto asimg = std::make_unique<TASImage>();
261
262 if (!FillImageFromWindow(asimg.get(), wid, w, h))
263 return nullptr;
264
265 return asimg;
266}
267
268////////////////////////////////////////////////////////////////////////////////
269/// Create image from pixmap.
270
272{
273 auto asimg = std::make_unique<TASImage>();
274
275 Int_t xy;
276 UInt_t w, h;
277 gVirtualX->GetWindowSize(pxm, xy, xy, w, h);
278
279 asimg->SetName(TString::Format("img_%dx%d",w, h));
280
281 if (gVirtualX->InheritsFrom("TGX11")) { //use built-in optimized version
282 auto vis = holder();
283
284 if (!vis) {
285 ::Warning("TASUtils::FromPixmap", "Visual not initiated");
286 return nullptr;
287 }
288
289#ifndef X_DISPLAY_MISSING
290 asimg->fImage = picture2asimage(vis, pxm, mask, 0, 0, w, h, kAllPlanes, 1, 0);
291#else
292 ::Error("TASUtils::FromPixmap", "Invoke X11-specific methods on unsupported platform");
293#endif
294 } else {
295 unsigned char *bits = gVirtualX->GetColorBits(pxm, 0, 0, w, h);
296 if (!bits) // error
297 return nullptr;
298
299 unsigned char *mask_bits = mask ? gVirtualX->GetColorBits(mask, 0, 0, w, h) : nullptr;
300
301 asimg->FromBitmap(bits, w, h, mask_bits);
302
303 if (mask_bits)
304 delete [] mask_bits;
305 delete [] bits;
306 }
307
308 if (!asimg->IsValid())
309 return nullptr;
310
311 return asimg;
312}
313
314////////////////////////////////////////////////////////////////////////////////
315/// Paint image on specified window
316
318{
319 auto asimg = dynamic_cast<TASImage *>(timg);
320 if (!asimg) {
321 ::Warning("TASUtils::PaintImage", "Image not provided");
322 return kFALSE;
323 }
324
326
327 Bool_t isX11 = gVirtualX->InheritsFrom("TGX11");
328
329 if (isX11) {
330 UInt_t mwidth = 0, mheight = 0;
331 std::vector<char> bits;
332 if (asimg->GetMaskBits(mwidth, mheight, bits))
333 mask = gVirtualX->CreateBitmap(gVirtualX->GetDefaultRootWindow(), bits.data(), mwidth, mheight);
334 }
335
336 GCValues_t gv;
338 gv.fClipMask = mask;
339 gv.fClipXOrigin = x;
340 gv.fClipYOrigin = y;
341
342 if (!holder.fGC) {
343 holder.fGC = gVirtualX->CreateGC(gVirtualX->GetDefaultRootWindow(), &gv);
344 } else {
345 gVirtualX->ChangeGC(holder.fGC, &gv);
346 }
347
348 UInt_t width = asimg->GetScaledWidth();
349 UInt_t height = asimg->GetScaledHeight();
350
351 Bool_t res = kTRUE;
352
353 if (isX11) { //use built-in optimized version
354 #ifndef X_DISPLAY_MISSING
355 ASImage *im = asimg->fScaledImage ? asimg->fScaledImage->fImage : asimg->fImage;
356 auto vis = holder();
357 if (vis && im)
358 asimage2drawable(vis, wid, im, (GC)holder.fGC, 0, 0, x, y, width, height, 1);
359 else
360 res = kFALSE;
361 #else
362 ::Error("TASUtils::PaintImage", "Invoke X11-specific methods on unsupported platform");
363 res = kFALSE;
364 #endif
365 } else {
366 auto argb = asimg->GetArgbArray();
367
368 Pixmap_t pic = argb ? gVirtualX->CreatePixmapFromData((unsigned char *) argb, width, height) : 0;
369
370 if (pic) {
371 if (!opaque) {
372 SETBIT(width, 31);
373 SETBIT(height, 31);
374 }
375 gVirtualX->CopyArea(pic, wid, holder.fGC, 0, 0, width, height, x, y);
376 gVirtualX->DeletePixmap(pic);
377 } else
378 res = kFALSE;
379 }
380
381 // free mask pixmap
382 if (mask != kNone)
383 gVirtualX->DeletePixmap(mask);
384
385 gv.fMask = kGCClipMask;
386 gv.fClipMask = kNone;
387 if (holder.fGC)
388 gVirtualX->ChangeGC(holder.fGC, &gv);
389
390 return res;
391}
392
393#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
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:135
static std::unique_ptr< TImage > FromWindow(Window_t wid)
Create an image from the given window.
Definition TASUtils.cxx:250
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:199
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:317
static Pixmap_t GetMask(TImage *timg)
Returns image mask pixmap (alpha channel).
Definition TASUtils.cxx:181
static std::unique_ptr< TImage > FromPixmap(Pixmap_t pxm, Pixmap_t mask)
Create image from pixmap.
Definition TASUtils.cxx:271
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