ROOT  6.07/01
Reference Guide
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
trans_graph.C
Go to the documentation of this file.
1 /// \file
2 /// \ingroup tutorial_image
3 /// Demonstrates how to access and manipulate ARGB pixel values of an image +...
4 /// - how to make a part of an image to be transparent.
5 /// - how to merge/alphablend an image with transparent colors
6 /// with some background image.
7 ///
8 /// \macro_image
9 /// \macro_output
10 /// \macro_code
11 ///
12 /// \author Valeriy Onuchin
13 
14 #include "TColor.h"
15 #include "TImage.h"
16 #include "TImageDump.h"
17 #include "TVirtualPad.h"
18 #include "TROOT.h"
19 #include "TFrame.h"
20 
21 static UInt_t color2rgb(TColor *col)
22 {
23  // returns RGB value of color
24 
25  return ((UInt_t(col->GetRed()*255) << 16) +
26  (UInt_t(col->GetGreen()*255) << 8) +
27  UInt_t(col->GetBlue()*255));
28 }
29 
30 
31 void trans_graph()
32 {
33  // remember if we are in batch mode
34  Bool_t batch = gROOT->IsBatch();
35 
36  // switch to batch mode
37  gROOT->SetBatch(kTRUE);
38 
39  // execute graph.C macro
40  gROOT->Macro("$ROOTSYS/tutorials/graphs/graph.C");
41 
42  // create gVirtualPS object
43  TImageDump dmp("dummy.png");
44  TImage *fore = dmp.GetImage(); // image associated with image_dump
45 
46  // resize canvas
47  gPad->SetCanvasSize(400, 300);
48  gPad->Paint(); // paint gPad on fore image associated with TImageDump
49 
50  // open background image
51  TImage *back = TImage::Open("$ROOTSYS/tutorials/image/rose512.jpg");
52 
53  // choose colors to be transparent
54  TColor *bk1 = gROOT->GetColor(gPad->GetFillColor());
55  TColor *bk2 = gROOT->GetColor(gPad->GetFrame()->GetFillColor());
56  UInt_t rgb1 = color2rgb(bk1);
57  UInt_t rgb2 = color2rgb(bk2);
58 
59  // get directly accessible ARGB array
60  UInt_t *argb = fore->GetArgbArray();
61  UInt_t w = fore->GetWidth();
62  UInt_t h = fore->GetHeight();
63 
64  // scan all pixels in fore image and
65  // make rgb1, rgb2 colors transparent.
66  for (UInt_t i = 0; i < h; i++) {
67  for (UInt_t j = 0; j < w; j++) {
68  Int_t idx = i*w + j;
69 
70  // RGB part of ARGB color
71  UInt_t col = argb[idx] & 0xffffff;
72 
73  // 24..31 bits define transparency of the color in the range 0 - 0xff
74  // for example, 0x00000000 - black color with 100% transparency
75  // 0xff000000 - non-transparent black color
76 
77  if ((col == rgb1) || (col == rgb2)) { //
78  argb[idx] = 0; // 100% transparent
79  } else {
80  argb[idx] = 0xff000000 + col; // make other pixels non-transparent
81  }
82  }
83  }
84 
85  // alphablend back and fore images
86  back->Merge(fore, "alphablend", 20, 20);
87 
88  // write result image in PNG format
89  back->WriteImage("trans_graph.png");
90  printf("*************** File trans_graph.png created ***************\n");
91 
92  delete back;
93 
94  // switch back to GUI mode
95  if (!batch) gROOT->SetBatch(kFALSE);
96 }
virtual void WriteImage(const char *, EImageFileTypes=TImage::kUnknown)
Definition: TImage.h:131
TH1 * h
Definition: legend2.C:5
virtual UInt_t * GetArgbArray()
Definition: TImage.h:253
#define gROOT
Definition: TROOT.h:344
virtual UInt_t GetWidth() const
Definition: TImage.h:244
int Int_t
Definition: RtypesCore.h:41
bool Bool_t
Definition: RtypesCore.h:59
const Bool_t kFALSE
Definition: Rtypes.h:92
Float_t GetGreen() const
Definition: TColor.h:86
An abstract interface to image processing library.
Definition: TImage.h:45
virtual void Merge(const TImage *, const char *="alphablend", Int_t=0, Int_t=0)
Definition: TImage.h:188
Float_t GetBlue() const
Definition: TColor.h:87
virtual UInt_t GetHeight() const
Definition: TImage.h:245
unsigned int UInt_t
Definition: RtypesCore.h:42
tuple w
Definition: qtexample.py:51
Save canvas as an image (GIF, JPEG, PNG, XPM, TIFF etc.).
Definition: TImageDump.h:33
ClassImp(TMCParticle) void TMCParticle printf(": p=(%7.3f,%7.3f,%9.3f) ;", fPx, fPy, fPz)
The color creation and management class.
Definition: TColor.h:47
Float_t GetRed() const
Definition: TColor.h:85
#define gPad
Definition: TVirtualPad.h:288
static TImage * Open(const char *file, EImageFileTypes type=kUnknown)
Open a specified image file.
Definition: TImage.cxx:110
const Bool_t kTRUE
Definition: Rtypes.h:91