Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
trans_graph.C
Go to the documentation of this file.
1/// \file
2/// \ingroup tutorial_image
3/// \notebook
4/// Demonstrates how to access and manipulate ARGB pixel values of an image +...
5/// - how to make a part of an image to be transparent.
6/// - how to merge/alphablend an image with transparent colors
7/// with some background image.
8///
9/// \macro_image
10/// \macro_output
11/// \macro_code
12///
13/// \author Valeriy Onuchin
14
15#include "TColor.h"
16#include "TImage.h"
17#include "TImageDump.h"
18#include "TVirtualPad.h"
19#include "TROOT.h"
20#include "TFrame.h"
21
22UInt_t color2rgb(TColor *col)
23{
24 // returns RGB value of color
25
26 return ((UInt_t(col->GetRed() * 255) << 16) + (UInt_t(col->GetGreen() * 255) << 8) + UInt_t(col->GetBlue() * 255));
27}
28
29void trans_graph()
30{
31 // remember if we are in batch mode
32 Bool_t batch = gROOT->IsBatch();
33
34 // switch to batch mode
35 gROOT->SetBatch(kTRUE);
36
37 // execute graph.C macro
38 gROOT->Macro("$ROOTSYS/tutorials/visualisation/graphs/graph.C");
39
40 // create gVirtualPS object
41 TImageDump dmp("dummy.png");
42 TImage *fore = dmp.GetImage(); // image associated with image_dump
43
44 // resize canvas
45 gPad->SetCanvasSize(400, 300);
46 gPad->Paint(); // paint gPad on fore image associated with TImageDump
47
48 // open background image
49 TImage *back = TImage::Open("$ROOTSYS/tutorials/visualisation/image/rose512.jpg");
50
51 // choose colors to be transparent
52 TColor *bk1 = gROOT->GetColor(gPad->GetFillColor());
53 TColor *bk2 = gROOT->GetColor(gPad->GetFrame()->GetFillColor());
54 UInt_t rgb1 = color2rgb(bk1);
55 UInt_t rgb2 = color2rgb(bk2);
56
57 // get directly accessible ARGB array
58 UInt_t *argb = fore->GetArgbArray();
59 UInt_t w = fore->GetWidth();
60 UInt_t h = fore->GetHeight();
61
62 // scan all pixels in fore image and
63 // make rgb1, rgb2 colors transparent.
64 for (UInt_t i = 0; i < h; i++) {
65 for (UInt_t j = 0; j < w; j++) {
66 Int_t idx = i * w + j;
67
68 // RGB part of ARGB color
69 UInt_t col = argb[idx] & 0xffffff;
70
71 // 24..31 bits define transparency of the color in the range 0 - 0xff
72 // for example, 0x00000000 - black color with 100% transparency
73 // 0xff000000 - non-transparent black color
74
75 if ((col == rgb1) || (col == rgb2)) { //
76 argb[idx] = 0; // 100% transparent
77 } else {
78 argb[idx] = 0xff000000 + col; // make other pixels non-transparent
79 }
80 }
81 }
82
83 // alphablend back and fore images
84 back->Merge(fore, "alphablend", 20, 20);
85
86 // write result image in PNG format
87 back->WriteImage("trans_graph.png");
88 printf("*************** File trans_graph.png created ***************\n");
89
90 delete back;
91
92 // switch back to GUI mode
93 if (!batch)
94 gROOT->SetBatch(kFALSE);
95}
#define h(i)
Definition RSha256.hxx:106
bool Bool_t
Definition RtypesCore.h:63
int Int_t
Definition RtypesCore.h:45
unsigned int UInt_t
Definition RtypesCore.h:46
constexpr Bool_t kFALSE
Definition RtypesCore.h:94
constexpr Bool_t kTRUE
Definition RtypesCore.h:93
#define gROOT
Definition TROOT.h:406
#define gPad
The color creation and management class.
Definition TColor.h:21
Float_t GetRed() const
Definition TColor.h:60
static Int_t GetColor(const char *hexcolor)
Static method returning color number for color specified by hex color string of form: "#rrggbb",...
Definition TColor.cxx:1921
Float_t GetBlue() const
Definition TColor.h:62
Float_t GetGreen() const
Definition TColor.h:61
Save canvas as an image (GIF, JPEG, PNG, XPM, TIFF etc.).
Definition TImageDump.h:22
An abstract interface to image processing library.
Definition TImage.h:29
static TImage * Open(const char *file, EImageFileTypes type=kUnknown)
Open a specified image file.
Definition TImage.cxx:118
virtual UInt_t * GetArgbArray()
Definition TImage.h:237
virtual UInt_t GetWidth() const
Definition TImage.h:228
virtual void WriteImage(const char *, EImageFileTypes=TImage::kUnknown)
Definition TImage.h:115
virtual void Merge(const TImage *, const char *="alphablend", Int_t=0, Int_t=0)
Definition TImage.h:172
virtual UInt_t GetHeight() const
Definition TImage.h:229
virtual void Paint(Option_t *option="")
This method must be overridden if a class wants to paint itself.
Definition TObject.cxx:624