ROOT logo
//  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.
//Author: Valeriy Onuchin

#include "TColor.h"
#include "TImage.h"
#include "TImageDump.h"
#include "TVirtualPad.h"
#include "TROOT.h"
#include "TFrame.h"

static 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);
}
 trans_graph.C:1
 trans_graph.C:2
 trans_graph.C:3
 trans_graph.C:4
 trans_graph.C:5
 trans_graph.C:6
 trans_graph.C:7
 trans_graph.C:8
 trans_graph.C:9
 trans_graph.C:10
 trans_graph.C:11
 trans_graph.C:12
 trans_graph.C:13
 trans_graph.C:14
 trans_graph.C:15
 trans_graph.C:16
 trans_graph.C:17
 trans_graph.C:18
 trans_graph.C:19
 trans_graph.C:20
 trans_graph.C:21
 trans_graph.C:22
 trans_graph.C:23
 trans_graph.C:24
 trans_graph.C:25
 trans_graph.C:26
 trans_graph.C:27
 trans_graph.C:28
 trans_graph.C:29
 trans_graph.C:30
 trans_graph.C:31
 trans_graph.C:32
 trans_graph.C:33
 trans_graph.C:34
 trans_graph.C:35
 trans_graph.C:36
 trans_graph.C:37
 trans_graph.C:38
 trans_graph.C:39
 trans_graph.C:40
 trans_graph.C:41
 trans_graph.C:42
 trans_graph.C:43
 trans_graph.C:44
 trans_graph.C:45
 trans_graph.C:46
 trans_graph.C:47
 trans_graph.C:48
 trans_graph.C:49
 trans_graph.C:50
 trans_graph.C:51
 trans_graph.C:52
 trans_graph.C:53
 trans_graph.C:54
 trans_graph.C:55
 trans_graph.C:56
 trans_graph.C:57
 trans_graph.C:58
 trans_graph.C:59
 trans_graph.C:60
 trans_graph.C:61
 trans_graph.C:62
 trans_graph.C:63
 trans_graph.C:64
 trans_graph.C:65
 trans_graph.C:66
 trans_graph.C:67
 trans_graph.C:68
 trans_graph.C:69
 trans_graph.C:70
 trans_graph.C:71
 trans_graph.C:72
 trans_graph.C:73
 trans_graph.C:74
 trans_graph.C:75
 trans_graph.C:76
 trans_graph.C:77
 trans_graph.C:78
 trans_graph.C:79
 trans_graph.C:80
 trans_graph.C:81
 trans_graph.C:82
 trans_graph.C:83
 trans_graph.C:84
 trans_graph.C:85
 trans_graph.C:86
 trans_graph.C:87
 trans_graph.C:88
 trans_graph.C:89
 trans_graph.C:90