trans_graph.C

Go to the documentation of this file.
00001 //  Demonstrates how to access and manipulate ARGB pixel values of an image +...
00002 //  - how to make a part of an image to be transparent.
00003 //  - how to merge/alphablend an image with transparent colors
00004 //    with some background image.
00005 //Author: Valeriy Onuchin
00006 
00007 #include "TColor.h"
00008 #include "TImage.h"
00009 #include "TImageDump.h"
00010 #include "TVirtualPad.h"
00011 #include "TROOT.h"
00012 #include "TFrame.h"
00013 
00014 static UInt_t color2rgb(TColor *col)
00015 {
00016    // returns RGB value of color
00017 
00018    return ((UInt_t(col->GetRed()*255) << 16) +
00019            (UInt_t(col->GetGreen()*255) << 8) +
00020             UInt_t(col->GetBlue()*255));
00021 }
00022 
00023 
00024 void trans_graph()
00025 {
00026    // remember if  we are in batch mode
00027    Bool_t batch = gROOT->IsBatch();
00028 
00029    // switch to batch mode
00030    gROOT->SetBatch(kTRUE);
00031 
00032    // execute graph.C macro
00033    gROOT->Macro("$ROOTSYS/tutorials/graphs/graph.C");
00034 
00035    // create gVirtualPS object
00036    TImageDump dmp("dummy.png");
00037    TImage *fore = dmp.GetImage();  // image associated with image_dump
00038 
00039    // resize canvas
00040    gPad->SetCanvasSize(400, 300);
00041    gPad->Paint(); // paint gPad on fore image associated with TImageDump
00042 
00043    // open background image
00044    TImage *back = TImage::Open("$ROOTSYS/tutorials/image/rose512.jpg");
00045 
00046    // choose colors to be transparent
00047    TColor *bk1 = gROOT->GetColor(gPad->GetFillColor());
00048    TColor *bk2 = gROOT->GetColor(gPad->GetFrame()->GetFillColor());
00049    UInt_t rgb1 = color2rgb(bk1);
00050    UInt_t rgb2 = color2rgb(bk2);
00051 
00052    // get directly accessible ARGB array
00053    UInt_t *argb = fore->GetArgbArray();
00054    UInt_t w = fore->GetWidth();
00055    UInt_t h = fore->GetHeight();
00056 
00057    // scan all pixels in fore image and
00058    // make rgb1, rgb2 colors transparent.
00059    for (UInt_t i = 0; i < h; i++) {
00060       for (UInt_t j = 0; j < w; j++) {
00061          Int_t idx = i*w + j;
00062 
00063          // RGB part of ARGB color
00064          UInt_t col = argb[idx] & 0xffffff;
00065 
00066          // 24..31 bits define transparency of the color in the range 0 - 0xff
00067          // for example, 0x00000000 - black color with 100% transparency
00068          //              0xff000000 - non-transparent black color
00069 
00070          if ((col == rgb1) || (col == rgb2)) { //
00071             argb[idx] = 0; // 100% transparent
00072          } else {
00073             argb[idx] = 0xff000000 + col;  // make other pixels non-transparent
00074          }
00075       }
00076    }
00077 
00078    // alphablend back and fore images
00079    back->Merge(fore, "alphablend", 20, 20);
00080 
00081    // write result image in PNG format
00082    back->WriteImage("trans_graph.png");
00083    printf("*************** File trans_graph.png created ***************\n");
00084 
00085    delete back;
00086 
00087    // switch back to GUI mode
00088    if (!batch) gROOT->SetBatch(kFALSE);
00089 }

Generated on Tue Jul 5 15:44:48 2011 for ROOT_528-00b_version by  doxygen 1.5.1