Logo ROOT  
Reference Guide
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) +
27 (UInt_t(col->GetGreen()*255) << 8) +
28 UInt_t(col->GetBlue()*255));
29}
30
31
32void trans_graph()
33{
34 // remember if we are in batch mode
35 Bool_t batch = gROOT->IsBatch();
36
37 // switch to batch mode
38 gROOT->SetBatch(kTRUE);
39
40 // execute graph.C macro
41 gROOT->Macro("$ROOTSYS/tutorials/graphs/graph.C");
42
43 // create gVirtualPS object
44 TImageDump dmp("dummy.png");
45 TImage *fore = dmp.GetImage(); // image associated with image_dump
46
47 // resize canvas
48 gPad->SetCanvasSize(400, 300);
49 gPad->Paint(); // paint gPad on fore image associated with TImageDump
50
51 // open background image
52 TImage *back = TImage::Open("$ROOTSYS/tutorials/image/rose512.jpg");
53
54 // choose colors to be transparent
55 TColor *bk1 = gROOT->GetColor(gPad->GetFillColor());
56 TColor *bk2 = gROOT->GetColor(gPad->GetFrame()->GetFillColor());
57 UInt_t rgb1 = color2rgb(bk1);
58 UInt_t rgb2 = color2rgb(bk2);
59
60 // get directly accessible ARGB array
61 UInt_t *argb = fore->GetArgbArray();
62 UInt_t w = fore->GetWidth();
63 UInt_t h = fore->GetHeight();
64
65 // scan all pixels in fore image and
66 // make rgb1, rgb2 colors transparent.
67 for (UInt_t i = 0; i < h; i++) {
68 for (UInt_t j = 0; j < w; j++) {
69 Int_t idx = i*w + j;
70
71 // RGB part of ARGB color
72 UInt_t col = argb[idx] & 0xffffff;
73
74 // 24..31 bits define transparency of the color in the range 0 - 0xff
75 // for example, 0x00000000 - black color with 100% transparency
76 // 0xff000000 - non-transparent black color
77
78 if ((col == rgb1) || (col == rgb2)) { //
79 argb[idx] = 0; // 100% transparent
80 } else {
81 argb[idx] = 0xff000000 + col; // make other pixels non-transparent
82 }
83 }
84 }
85
86 // alphablend back and fore images
87 back->Merge(fore, "alphablend", 20, 20);
88
89 // write result image in PNG format
90 back->WriteImage("trans_graph.png");
91 printf("*************** File trans_graph.png created ***************\n");
92
93 delete back;
94
95 // switch back to GUI mode
96 if (!batch) gROOT->SetBatch(kFALSE);
97}
#define h(i)
Definition: RSha256.hxx:106
int Int_t
Definition: RtypesCore.h:41
unsigned int UInt_t
Definition: RtypesCore.h:42
const Bool_t kFALSE
Definition: RtypesCore.h:88
bool Bool_t
Definition: RtypesCore.h:59
const Bool_t kTRUE
Definition: RtypesCore.h:87
#define gROOT
Definition: TROOT.h:415
#define gPad
Definition: TVirtualPad.h:286
The color creation and management class.
Definition: TColor.h:19
Float_t GetRed() const
Definition: TColor.h:57
Float_t GetBlue() const
Definition: TColor.h:59
Float_t GetGreen() const
Definition: TColor.h:58
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:119
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