{ "cells": [ { "cell_type": "markdown", "id": "50d58bcd", "metadata": {}, "source": [ "# trans_graph\n", "Demonstrates how to access and manipulate ARGB pixel values of an image +...\n", " - how to make a part of an image to be transparent.\n", " - how to merge/alphablend an image with transparent colors\n", " with some background image.\n", "\n", "\n", "\n", "\n", "**Author:** Valeriy Onuchin \n", "This notebook tutorial was automatically generated with ROOTBOOK-izer from the macro found in the ROOT repository on Tuesday, May 19, 2026 at 08:38 PM." ] }, { "cell_type": "markdown", "id": "611715e4", "metadata": {}, "source": [ " Definition of a helper function: " ] }, { "cell_type": "code", "execution_count": 1, "id": "977de68d", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:38:47.293027Z", "iopub.status.busy": "2026-05-19T20:38:47.292916Z", "iopub.status.idle": "2026-05-19T20:38:47.299171Z", "shell.execute_reply": "2026-05-19T20:38:47.298633Z" } }, "outputs": [], "source": [ "%%cpp -d\n", "\n", "#include \"TColor.h\"\n", "#include \"TImage.h\"\n", "#include \"TImageDump.h\"\n", "#include \"TVirtualPad.h\"\n", "#include \"TROOT.h\"\n", "#include \"TFrame.h\"\n", "\n", "UInt_t color2rgb(TColor *col)\n", "{\n", " // returns RGB value of color\n", "\n", " return ((UInt_t(col->GetRed() * 255) << 16) + (UInt_t(col->GetGreen() * 255) << 8) + UInt_t(col->GetBlue() * 255));\n", "}" ] }, { "cell_type": "markdown", "id": "0565a751", "metadata": {}, "source": [ "remember if we are in batch mode" ] }, { "cell_type": "code", "execution_count": 2, "id": "22a6f27a", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:38:47.300282Z", "iopub.status.busy": "2026-05-19T20:38:47.300165Z", "iopub.status.idle": "2026-05-19T20:38:47.630279Z", "shell.execute_reply": "2026-05-19T20:38:47.629576Z" } }, "outputs": [], "source": [ "Bool_t batch = gROOT->IsBatch();" ] }, { "cell_type": "markdown", "id": "7b32f87a", "metadata": {}, "source": [ "switch to batch mode" ] }, { "cell_type": "code", "execution_count": 3, "id": "92f6c394", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:38:47.631694Z", "iopub.status.busy": "2026-05-19T20:38:47.631564Z", "iopub.status.idle": "2026-05-19T20:38:47.843824Z", "shell.execute_reply": "2026-05-19T20:38:47.843159Z" } }, "outputs": [], "source": [ "gROOT->SetBatch(kTRUE);" ] }, { "cell_type": "markdown", "id": "2370deb2", "metadata": {}, "source": [ "execute gr001_simple.C macro" ] }, { "cell_type": "code", "execution_count": 4, "id": "ba8181b6", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:38:47.845300Z", "iopub.status.busy": "2026-05-19T20:38:47.845185Z", "iopub.status.idle": "2026-05-19T20:38:48.048410Z", "shell.execute_reply": "2026-05-19T20:38:48.047850Z" } }, "outputs": [ { "data": { "text/html": [ "\n", "\n", "
\n", "
\n", "\n", "\n", "\n" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "name": "stdout", "output_type": "stream", "text": [ " i 0 0.000000 1.986693 \n", " i 1 0.100000 2.955202 \n", " i 2 0.200000 3.894183 \n", " i 3 0.300000 4.794255 \n", " i 4 0.400000 5.646425 \n", " i 5 0.500000 6.442177 \n", " i 6 0.600000 7.173561 \n", " i 7 0.700000 7.833269 \n", " i 8 0.800000 8.414710 \n", " i 9 0.900000 8.912074 \n", " i 10 1.000000 9.320391 \n", " i 11 1.100000 9.635582 \n", " i 12 1.200000 9.854497 \n", " i 13 1.300000 9.974950 \n", " i 14 1.400000 9.995736 \n", " i 15 1.500000 9.916648 \n", " i 16 1.600000 9.738476 \n", " i 17 1.700000 9.463001 \n", " i 18 1.800000 9.092974 \n", " i 19 1.900000 8.632094 \n", " i 0 -3.000000 -0.989992 \n", " i 1 -2.684211 -0.897212 \n", " i 2 -2.368421 -0.715699 \n", " i 3 -2.052632 -0.463406 \n", " i 4 -1.736842 -0.165284 \n", " i 5 -1.421053 0.149185 \n", " i 6 -1.105263 0.448899 \n", " i 7 -0.789474 0.704219 \n", " i 8 -0.473684 0.889894 \n", " i 9 -0.157895 0.987561 \n", " i 10 0.157895 0.987561 \n", " i 11 0.473684 0.889894 \n", " i 12 0.789474 0.704219 \n", " i 13 1.105263 0.448899 \n", " i 14 1.421053 0.149185 \n", " i 15 1.736842 -0.165284 \n", " i 16 2.052632 -0.463406 \n", " i 17 2.368421 -0.715699 \n", " i 18 2.684211 -0.897212 \n", " i 19 3.000000 -0.989992 \n" ] } ], "source": [ "gROOT->Macro(\"$ROOTSYS/tutorials/visualisation/graphs/gr001_simple.C\");" ] }, { "cell_type": "markdown", "id": "369b3e99", "metadata": {}, "source": [ "create gVirtualPS object" ] }, { "cell_type": "code", "execution_count": 5, "id": "6c728b36", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:38:48.049632Z", "iopub.status.busy": "2026-05-19T20:38:48.049499Z", "iopub.status.idle": "2026-05-19T20:38:48.257212Z", "shell.execute_reply": "2026-05-19T20:38:48.256566Z" } }, "outputs": [], "source": [ "TImageDump dmp(\"dummy.png\");\n", "TImage *fore = dmp.GetImage(); // image associated with image_dump" ] }, { "cell_type": "markdown", "id": "3c849cda", "metadata": {}, "source": [ "resize canvas" ] }, { "cell_type": "code", "execution_count": 6, "id": "63d4a835", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:38:48.258643Z", "iopub.status.busy": "2026-05-19T20:38:48.258513Z", "iopub.status.idle": "2026-05-19T20:38:48.466388Z", "shell.execute_reply": "2026-05-19T20:38:48.465545Z" } }, "outputs": [], "source": [ "gPad->SetCanvasSize(400, 300);\n", "gPad->Paint(); // paint gPad on fore image associated with TImageDump" ] }, { "cell_type": "markdown", "id": "97bab7f0", "metadata": {}, "source": [ "open background image" ] }, { "cell_type": "code", "execution_count": 7, "id": "b4c7d124", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:38:48.468028Z", "iopub.status.busy": "2026-05-19T20:38:48.467906Z", "iopub.status.idle": "2026-05-19T20:38:48.670345Z", "shell.execute_reply": "2026-05-19T20:38:48.669668Z" } }, "outputs": [], "source": [ "TImage *back = TImage::Open(\"$ROOTSYS/tutorials/visualisation/image/rose512.jpg\");" ] }, { "cell_type": "markdown", "id": "6e4c5cfc", "metadata": {}, "source": [ "choose colors to be transparent" ] }, { "cell_type": "code", "execution_count": 8, "id": "f927bdcf", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:38:48.672415Z", "iopub.status.busy": "2026-05-19T20:38:48.672293Z", "iopub.status.idle": "2026-05-19T20:38:48.884657Z", "shell.execute_reply": "2026-05-19T20:38:48.883890Z" } }, "outputs": [], "source": [ "TColor *bk1 = gROOT->GetColor(gPad->GetFillColor());\n", "TColor *bk2 = gROOT->GetColor(gPad->GetFrame()->GetFillColor());\n", "UInt_t rgb1 = color2rgb(bk1);\n", "UInt_t rgb2 = color2rgb(bk2);" ] }, { "cell_type": "markdown", "id": "9fa52be2", "metadata": {}, "source": [ "get directly accessible ARGB array" ] }, { "cell_type": "code", "execution_count": 9, "id": "78f79ded", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:38:48.886296Z", "iopub.status.busy": "2026-05-19T20:38:48.886173Z", "iopub.status.idle": "2026-05-19T20:38:49.098532Z", "shell.execute_reply": "2026-05-19T20:38:49.097799Z" } }, "outputs": [], "source": [ "UInt_t *argb = fore->GetArgbArray();\n", "UInt_t w = fore->GetWidth();\n", "UInt_t h = fore->GetHeight();" ] }, { "cell_type": "markdown", "id": "0165ffb8", "metadata": {}, "source": [ "scan all pixels in fore image and\n", "make rgb1, rgb2 colors transparent." ] }, { "cell_type": "code", "execution_count": 10, "id": "50f92eb4", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:38:49.100566Z", "iopub.status.busy": "2026-05-19T20:38:49.100444Z", "iopub.status.idle": "2026-05-19T20:38:49.302767Z", "shell.execute_reply": "2026-05-19T20:38:49.302089Z" } }, "outputs": [], "source": [ "for (UInt_t i = 0; i < h; i++) {\n", " for (UInt_t j = 0; j < w; j++) {\n", " Int_t idx = i * w + j;\n", "\n", " // RGB part of ARGB color\n", " UInt_t col = argb[idx] & 0xffffff;\n", "\n", " // 24..31 bits define transparency of the color in the range 0 - 0xff\n", " // for example, 0x00000000 - black color with 100% transparency\n", " // 0xff000000 - non-transparent black color\n", "\n", " if ((col == rgb1) || (col == rgb2)) { //\n", " argb[idx] = 0; // 100% transparent\n", " } else {\n", " argb[idx] = 0xff000000 + col; // make other pixels non-transparent\n", " }\n", " }\n", "}" ] }, { "cell_type": "markdown", "id": "a2f6e573", "metadata": {}, "source": [ "alphablend back and fore images" ] }, { "cell_type": "code", "execution_count": 11, "id": "f9c2a9b1", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:38:49.304784Z", "iopub.status.busy": "2026-05-19T20:38:49.304652Z", "iopub.status.idle": "2026-05-19T20:38:49.517056Z", "shell.execute_reply": "2026-05-19T20:38:49.516368Z" } }, "outputs": [], "source": [ "back->Merge(fore, \"alphablend\", 20, 20);" ] }, { "cell_type": "markdown", "id": "57e58003", "metadata": {}, "source": [ "write result image in PNG format" ] }, { "cell_type": "code", "execution_count": 12, "id": "637b3b74", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:38:49.519109Z", "iopub.status.busy": "2026-05-19T20:38:49.518974Z", "iopub.status.idle": "2026-05-19T20:38:49.731521Z", "shell.execute_reply": "2026-05-19T20:38:49.730870Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "*************** File trans_graph.png created ***************\n" ] } ], "source": [ "back->WriteImage(\"trans_graph.png\");\n", "printf(\"*************** File trans_graph.png created ***************\\n\");\n", "\n", "delete back;" ] }, { "cell_type": "markdown", "id": "f8296a1b", "metadata": {}, "source": [ "switch back to GUI mode" ] }, { "cell_type": "code", "execution_count": 13, "id": "befe08db", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:38:49.733092Z", "iopub.status.busy": "2026-05-19T20:38:49.732967Z", "iopub.status.idle": "2026-05-19T20:38:49.945410Z", "shell.execute_reply": "2026-05-19T20:38:49.944619Z" } }, "outputs": [], "source": [ "if (!batch)\n", " gROOT->SetBatch(kFALSE);" ] }, { "cell_type": "markdown", "id": "8f14ae90", "metadata": {}, "source": [ "Draw all canvases " ] }, { "cell_type": "code", "execution_count": 14, "id": "3efa83a7", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:38:49.946935Z", "iopub.status.busy": "2026-05-19T20:38:49.946818Z", "iopub.status.idle": "2026-05-19T20:38:50.159159Z", "shell.execute_reply": "2026-05-19T20:38:50.158317Z" } }, "outputs": [ { "data": { "text/html": [ "\n", "\n", "
\n", "
\n", "\n", "\n", "\n" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "gROOT->GetListOfCanvases()->Draw()" ] } ], "metadata": { "kernelspec": { "display_name": "ROOT C++", "language": "c++", "name": "root" }, "language_info": { "codemirror_mode": "text/x-c++src", "file_extension": ".C", "mimetype": " text/x-c++src", "name": "c++" } }, "nbformat": 4, "nbformat_minor": 5 }