Friday, July 25, 2014

Study Memo of ROOT - TImage::CopyARea 学習メモ

Original Image 1
Original Image 2
Composite Image in mosaic tiles 

Source code is as below.

#include "TImage.h"
#include "TCanvas.h"
#include "TArrayD.h"
#include "TROOT.h"
#include "TColor.h"
#include "TAttImage.h"
#include "TEnv.h"

void imgProc01()
{
TImage *img1 = TImage::Open("IMGP4782.png");
TImage *img2 = TImage::Open("IMGP4783.png");

int w = img1->GetWidth();
int h = img1->GetHeight();

int gridX = 3;
int gridY = 3;
int tileW = w/gridX;
int tileH = h/gridY;

for (int i=0; i<gridY; i++) {
  for (int j=0; j<gridX; j++) {
    if ((j%2 == 0 && i%2 == 0) || (j%2 == 1 && i%2 == 1)) {
      img1->CopyArea(img2, i*tileW, j*tileH, tileW, tileH, i*tileW, j*tileH);
}
  }
}
img2->WriteImage("MregeFunOverlay.png");
}


The same result can be achieved by the following code too, using FillRectangle and Merge.

#include "TImage.h"
#include "TCanvas.h"
#include "TArrayD.h"
#include "TROOT.h"
#include "TColor.h"
#include "TAttImage.h"
#include "TEnv.h"

void imgProc01()
{
TImage *img1 = TImage::Open("IMGP4782.png");
TImage *img2 = TImage::Open("IMGP4783.png");

int w = img1->GetWidth();
int h = img1->GetHeight();

int gridX = 3;
int gridY = 3;
int tileW = w/gridX;
int tileH = h/gridY;

for (int i=0; i<gridY; i++) {
  for (int j=0; j<gridX; j++) {
    if ((j%2 == 0 && i%2 == 0) || (j%2 == 1 && i%2 == 1)) {
      img1->FillRectangle("#FFFFFF", i*tileW, j*tileH, tileW, tileH);
}
    if ((j%2 == 0 && i%2 == 1) || (j%2 == 1 && i%2 == 0)) {
      img2->FillRectangle("#FFFFFF", i*tileW, j*tileH, tileW, tileH);
}
  }
}
img2->Merge(img1, "tint");
img2->WriteImage("Mrege-Overlay-tile-sub.png");
}

No comments:

Post a Comment