TIFF to pixel content printout

Dear all,
I am trying to print out each pixel value of a TIFF image. I read some posts, but
it is the first time I work with images. I tried the following, but I get the same value for each pixel.

void run(){

  TH1::AddDirectory(kFALSE);
  TH1::AddDirectory(kFALSE);
  gStyle->SetOptStat(0);
  
   TImage *img = TImage::Open("../Door.tiff");
   if (!img) {
      printf("Could not create an image... exit\n");
      return;
   }
   //img->Draw("xxx");
   
   cout <<"Height: " << img->GetHeight() << " pixels " <<endl;
   cout <<"Width: " << img->GetWidth() << " pixels" <<endl;
    UInt_t yPixels = img->GetHeight();
   UInt_t xPixels = img->GetWidth();
   UInt_t *argb   = img->GetArgbArray();
    

   for (int row=0; row<xPixels; ++row) {
      for (int col=0; col<yPixels; ++col) {
         int index = col*xPixels+row;
         cout << index << " " << float(argb[index]&0xff)/256<< endl;
      }
   }
  
}

Could anyone help me, please?

Thanks,
franciuska

This example loops on pixels of an image. Let me know if it helps.

Why are you doing this operation? If you get the same value everywhere, this is the first suspect to check, since you are only considering the lowest 2 bytes of your data (while integers are 4 bytes wide). It’s proabably a good idea to follow the example linked above.

Hello.

Thanks a lot. It was really useful.
But there is still one thing I do not understand.
In that example, if you write

cout <<  "bk1  " << bk1 <<   endl;

you get bk1 0x1e29bd0

Then, if you ask,

   cout <<  " (UInt_t(bk1->GetRed())) "<< (UInt_t(bk1->GetRed()))  <<"  (UInt_t(bk1->GetGreen())) " << (UInt_t(bk1->GetGreen())) << "  (UInt_t(bk1->GetBlue())) " << (UInt_t(bk1->GetBlue()))<< endl;
   cout <<  " (UInt_t(bk1->GetRed()*255)) "<< (UInt_t(bk1->GetRed()*255))  <<"  (UInt_t(bk1->GetGreen()*255)) " << (UInt_t(bk1->GetGreen()*255)) << "  (UInt_t(bk1->GetBlue()*255)) " << (UInt_t(bk1->GetBlue()*255))<< endl;
   cout << (UInt_t(bk1->GetRed()*255) << 16) << " " <<  (UInt_t(bk1->GetGreen()*255) << 8) << " " << UInt_t(bk1->GetBlue()*255) << endl;

You get

 (UInt_t(bk1->GetRed())) 0  (UInt_t(bk1->GetGreen())) 0  (UInt_t(bk1->GetBlue())) 0
 (UInt_t(bk1->GetRed()*255)) 221  (UInt_t(bk1->GetGreen()*255)) 186  (UInt_t(bk1->GetBlue()*255)) 135
14483456 47616 135
14531207

So, I understood, once you have the results of GetRed(), GetGreen() and GetBlue() how to get the decimal 14531207
(i.e. you make GetRed()* 255* 2^16+GetGreen()* 255 * 2^8 +GetBlue()* 255 * 2^0).

But when I read the result of cout << "bk1 " << bk1 << endl; which is “0x1e29bd0” … How is this number related to 221./255, 186./255, 135./255 please?

In other words, 0x1e29bd0 gives you the fractions of red,green and blue. How do you obtain such fractions from 0x1e29bd0, please?

Thanks

I am not sure I fully understand your question. Imagine I open a canvas, the background is white. If I get the background color and then get the value of red green and blue I get 1 1 1 …ie: white.

root [0] TCanvas C;
root [1] TColor *bk1 = gROOT->GetColor(gPad->GetFillColor());
root [2] printf("R = %g G = %g B = %g\n",bk1->GetRed(), bk1->GetGreen(), bk1->GetBlue());
R = 1 G = 1 B = 1
root [3] 

If the background is white and you ask cout <<bk1 <<< endl;
you get 0x1f415e0
If you then ask
printf(“R = %g G = %g B = %g\n”,bk1->GetRed(), bk1->GetGreen(), bk1->GetBlue());
you get
R = 1 G = 1 B = 1

But how is 0x1f415e0 related to R=1 B=1 G=1, please?
The functions GetRed(), GetGreen() and GetBlue() makes this "translation’ for me. But
how is the translation made, pls?

Thanks,
Franciuska

It is not … 0x1f415e0 is just the address of the TColor object.

OK.

Silly question of me then :blush:

Thanks,
Francesca

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.