Save image as grayscale with TImage class

HI,

I’m triying to write a flat BMP with TImage class as a Grayscale, saving as normal RGB works perfectly, but when i try img5->Gray(val); then it fails with segmentation violation.
Does anyone has ever save a file as a grayscale bmp? or similar.

This is part of the code that fails…

//save as a flat BMP
int N=ydim, M=xdim, Q=8,i,j; // rows, cols, grayscale
double valfloat;
TArrayD imageData;
imageData->Set(xdimydim);
char filename[]=“GCE01180.bmp”;
TImage
imagen = TImage::Create();

TImagePalette *paleta= new TImagePalette(256,0);

   for(i=0; i<N; i++)
       for(j=0; j<M; j++)
       {
           valfloat = RAD[4][j][i][0];

           if (valfloat<200.0) {
               imageData->AddAt(0,i*xdim+j);
           } else if (valfloat>320.0){
                imageData->AddAt(255,i*xdim+j);
           }else {
                imageData->AddAt((int)((valfloat-200.0)*2.125),i*xdim+j);
           }
       }
     gStyle->SetPalette(2,0);

   imagen->SetImage(imageData,ydim,0);
   imagen->WriteImage(filename);

// below this part of the code fails....
    Bool_t *val=kTRUE;
    char filenameg[]="graypic.bmp";
   TImage *img5 = TImage::Open("GCE01180.bmp");
   // open the color editor
   img5->Vectorize(256);
   img5->Gray(val);
   img5->WriteImage(filenameg);

Jose Alberto Morales de los Rios Pappa.
Space Plasma and Astroparticle Group(SPAS).
Dpto. de Física. Universidad de Alcalá.
Alcalá de Henares, Madrid.
Telf:+34918854954
www2.uah.es/spas

It would be good if you provide a small macro we can run showing this problem.

attached is full macro. input is very heavy, more than 150Mb.
radiance.C (13 KB)

Once I had removed all the commented code it is not so big.
But your macro does not work for me.
I get:

root [0] 
Processing radiance.C...
Warning: wrong member access operator '->' radiance.C:218:
Warning: wrong member access operator '->' radiance.C:231:
Warning: wrong member access operator '->' radiance.C:233:
Warning: wrong member access operator '->' radiance.C:235:
Error: Can't call TImage::Gray(val) in current scope radiance.C:242:
Possible candidates are...
(in TImage)
/Users/couet/root/lib/libGraf.so  -1:-1   0 public: virtual void TImage::Gray(Bool_t=kTRUE);
(in TAttImage)
*** Interpreter error recovered ***

Yap, thats the error i get when i try to turn on the Gray option.
Error: Can’t call TImage::Gray(val) in current scope radiance.C:242:
Possible candidates are…

The only thing i want is to load the image in the TImage object, then turn the Gray option to convert it to grayscale format and save as BMP. But the Gray option doen’t seems to work.

Hi,

Replace the “[color=#0000FF]imageData->[/color]” by “[color=#0000FF]imageData.[/color]” and replace also “[color=#0000FF]Bool_t *val=kTRUE;[/color]” by “[color=#0000FF]Bool_t val=kTRUE;[/color]” (i.e. use a Bool_t value and not a pointer…)

Cheers, Bertrand.

Changes made but error continues if i uncomment “->Gray(val);” lines.

The code after changes:

[code] //save as a flat BMP
int N=ydim, M=xdim, Q=8,i,j; // rows, cols, grayscale
double valfloat;
Bool_t val=kTRUE;
TArrayD imageData;
imageData.Set(xdimydim);
char filename[]=“GCE01180.bmp”;
TImage
imagen = TImage::Create();

TImagePalette *paleta= new TImagePalette(256,0);


for(i=0; i<N; i++)
    for(j=0; j<M; j++)
    {
        valfloat = RAD[4][j][i][0];

        if (valfloat<200.0)
        {
            imageData.AddAt(0,i*xdim+j);
        }
        else if (valfloat>320.0)
        {
            imageData.AddAt(255,i*xdim+j);
        }
        else
        {
            imageData.AddAt((int)((valfloat-200.0)*2.125),i*xdim+j);
        }
    }
gStyle->SetPalette(2,0);

imagen->SetImage(imageData,ydim,0);
imagen->Gray(val);
imagen->WriteImage(filename);

// below this part of the code fails…
Bool_t val=kTRUE;
char filenameg[]=“GCE01180_gray.bmp”;
TImage *img5 = TImage::Open(“GCE01180.bmp”);
// open the color editor

img5->Vectorize(256);
//img5->Gray(val);
img5->WriteImage(filenameg);

[/code]
Then if i uncomment the Gray option, error:

root [0] Processing radiance.C... *** glibc detected *** /usr/bin/root.exe: double free or corruption (out): 0x00007fff5017ad00 *** ======= Backtrace: ========= /lib64/libc.so.6[0x39a0476d63] /usr/lib64/root/libASImage.so(_ZN8TASImage4GrayEb+0x2ff)[0x7f68222bff5f] /usr/lib64/root/libGraf.so[0x3c26f05e3c] /usr/lib64/root/libCint.so.5.28(_ZN4Cint19G__ExceptionWrapperEPFiP8G__valuePKcP8G__paramiES1_PcS5_i+0x39)[0x3c2291b5f9] /usr/lib64/root/libCint.so.5.28(G__execute_call+0x61)[0x3c22930881] /usr/lib64/root/libCint.so.5.28(G__call_cppfunc+0x2a0)[0x3c22930c80] /usr/lib64/root/libCint.so.5.28(G__interpret_func+0x24bb)[0x3c228a42fb] /usr/lib64/root/libCint.so.5.28(G__getfunction+0x160e)[0x3c228691de] /usr/lib64/root/libCint.so.5.28(_Z15G__getstructmemiR18G__FastAllocStringPciS1_PiP12G__var_arrayi+0x6e3)[0x3c228ed433] /usr/lib64/root/libCint.so.5.28(G__getvariable+0x1c7c)[0x3c228e554c] /usr/lib64/root/libCint.so.5.28(G__getitem+0x81)[0x3c2287a081] /usr/lib64/root/libCint.so.5.28(G__getexpr+0x5c19)[0x3c22881579] /usr/lib64/root/libCint.so.5.28(G__exec_statement+0x559a)[0x3c2290263a] /usr/lib64/root/libCint.so.5.28(G__interpret_func+0x3472)[0x3c228a52b2] /usr/lib64/root/libCint.so.5.28(G__getfunction+0x189f)[0x3c2286946f] /usr/lib64/root/libCint.so.5.28(G__getitem+0x7fa)[0x3c2287a7fa] /usr/lib64/root/libCint.so.5.28(G__getexpr+0x5c19)[0x3c22881579] /usr/lib64/root/libCint.so.5.28(G__calc_internal+0x3bc)[0x3c2288b26c] /usr/lib64/root/libCint.so.5.28(G__process_cmd+0x2f7c)[0x3c228716bc] /usr/lib64/root/libCore.so.5.28(_ZN5TCint11ProcessLineEPKcPN12TInterpreter10EErrorCodeE+0x4d8)[0x3c23a11968] /usr/lib64/root/libCore.so.5.28(_ZN5TCint16ProcessLineSynchEPKcPN12TInterpreter10EErrorCodeE+0xb3)[0x3c23a08b63] /usr/lib64/root/libCore.so.5.28(_ZN12TApplication11ExecuteFileEPKcPib+0x754)[0x3c239e8e84] /usr/lib64/root/libCore.so.5.28(_ZN12TApplication11ProcessLineEPKcbPi+0x6cb)[0x3c239e7c6b] /usr/lib64/root/libRint.so.5.28(_ZN5TRint3RunEb+0x42a)[0x3c2421e74a] /usr/bin/root.exe(main+0x4c)[0x40105c] /lib64/libc.so.6(__libc_start_main+0xfd)[0x39a041ee5d] /usr/bin/root.exe[0x400f49] ======= Memory map: ========
Does anyone has an example to save a matrix as a BMP image? and then convert as a grayscale BMP image? PPM and PGM format are also welcome.

Best regards…
Jose Alberto Morales de los Rios

Can you repost the complete macro after the changes please ?