Logo ROOT  
Reference Guide
trans_graph.C File Reference

Detailed Description

View in nbviewer Open in SWAN Demonstrates how to access and manipulate ARGB pixel values of an image +...

  • how to make a part of an image to be transparent.
  • how to merge/alphablend an image with transparent colors with some background image.
i 0 0.000000 1.986693
i 1 0.100000 2.955202
i 2 0.200000 3.894183
i 3 0.300000 4.794255
i 4 0.400000 5.646425
i 5 0.500000 6.442177
i 6 0.600000 7.173561
i 7 0.700000 7.833269
i 8 0.800000 8.414710
i 9 0.900000 8.912074
i 10 1.000000 9.320391
i 11 1.100000 9.635582
i 12 1.200000 9.854497
i 13 1.300000 9.974950
i 14 1.400000 9.995736
i 15 1.500000 9.916648
i 16 1.600000 9.738476
i 17 1.700000 9.463001
i 18 1.800000 9.092974
i 19 1.900000 8.632094
*************** File trans_graph.png created ***************
#include "TColor.h"
#include "TImage.h"
#include "TImageDump.h"
#include "TVirtualPad.h"
#include "TROOT.h"
#include "TFrame.h"
UInt_t color2rgb(TColor *col)
{
// returns RGB value of color
return ((UInt_t(col->GetRed()*255) << 16) +
(UInt_t(col->GetGreen()*255) << 8) +
UInt_t(col->GetBlue()*255));
}
void trans_graph()
{
// remember if we are in batch mode
Bool_t batch = gROOT->IsBatch();
// switch to batch mode
gROOT->SetBatch(kTRUE);
// execute graph.C macro
gROOT->Macro("$ROOTSYS/tutorials/graphs/graph.C");
// create gVirtualPS object
TImageDump dmp("dummy.png");
TImage *fore = dmp.GetImage(); // image associated with image_dump
// resize canvas
gPad->SetCanvasSize(400, 300);
gPad->Paint(); // paint gPad on fore image associated with TImageDump
// open background image
TImage *back = TImage::Open("$ROOTSYS/tutorials/image/rose512.jpg");
// choose colors to be transparent
TColor *bk1 = gROOT->GetColor(gPad->GetFillColor());
TColor *bk2 = gROOT->GetColor(gPad->GetFrame()->GetFillColor());
UInt_t rgb1 = color2rgb(bk1);
UInt_t rgb2 = color2rgb(bk2);
// get directly accessible ARGB array
UInt_t *argb = fore->GetArgbArray();
UInt_t w = fore->GetWidth();
UInt_t h = fore->GetHeight();
// scan all pixels in fore image and
// make rgb1, rgb2 colors transparent.
for (UInt_t i = 0; i < h; i++) {
for (UInt_t j = 0; j < w; j++) {
Int_t idx = i*w + j;
// RGB part of ARGB color
UInt_t col = argb[idx] & 0xffffff;
// 24..31 bits define transparency of the color in the range 0 - 0xff
// for example, 0x00000000 - black color with 100% transparency
// 0xff000000 - non-transparent black color
if ((col == rgb1) || (col == rgb2)) { //
argb[idx] = 0; // 100% transparent
} else {
argb[idx] = 0xff000000 + col; // make other pixels non-transparent
}
}
}
// alphablend back and fore images
back->Merge(fore, "alphablend", 20, 20);
// write result image in PNG format
back->WriteImage("trans_graph.png");
printf("*************** File trans_graph.png created ***************\n");
delete back;
// switch back to GUI mode
if (!batch) gROOT->SetBatch(kFALSE);
}
#define h(i)
Definition: RSha256.hxx:106
int Int_t
Definition: RtypesCore.h:43
unsigned int UInt_t
Definition: RtypesCore.h:44
const Bool_t kFALSE
Definition: RtypesCore.h:90
bool Bool_t
Definition: RtypesCore.h:61
const Bool_t kTRUE
Definition: RtypesCore.h:89
#define gROOT
Definition: TROOT.h:406
#define gPad
Definition: TVirtualPad.h:287
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
Author
Valeriy Onuchin

Definition in file trans_graph.C.