FEDRA emulsion software from the OPERA Collaboration
EdbImage Class Reference

#include <EdbImage.h>

Inheritance diagram for EdbImage:
Collaboration diagram for EdbImage:

Public Member Functions

void AdoptImage (int columns, int rows, char *image, int col=256)
 
Int_t DumpPGM (char *file)
 
 EdbImage ()
 
 EdbImage (char *file, char *format="PGM")
 
 EdbImage (int columns, int rows, char *image, int col=256)
 
Int_t FillBufferDouble (TArrayD &image) const
 
char * GetBuffer () const
 
TH1F * GetHist1 () const
 
TH2F * GetHist2 (int flip=0) const
 
Int_t Height () const
 
Int_t LoadBMP (char *file)
 
Int_t LoadPGM (char *file)
 
Int_t LoadRAW (char *file)
 
int Pixel (int c, int r) const
 
int Pixel (int ipix) const
 
virtual void Print (Option_t *opt="") const
 
virtual void Set0 ()
 
void SetColors (int col)
 
Int_t Width () const
 
virtual ~EdbImage ()
 

Private Attributes

TArrayC * eBuffer
 body of the image buffer More...
 
Int_t eBytes
 number of significative bytes in the buffer More...
 
Int_t eColors
 maximal height of pixel (normally 256) More...
 
Int_t eColumns
 number of columns of the matrix (1 row length) More...
 
Int_t eRows
 number of rows of the matrix More...
 

Constructor & Destructor Documentation

◆ EdbImage() [1/3]

EdbImage::EdbImage ( )

◆ EdbImage() [2/3]

EdbImage::EdbImage ( char *  file,
char *  format = "PGM" 
)
32{
33 Set0();
34 eBuffer = new TArrayC();
35
36 Int_t counter = 0;
37
38 if ( !strcmp(format,"PGM") ) counter = LoadPGM(file);
39 else if ( !strcmp(format,"RAW") ) counter = LoadRAW(file);
40 else if ( !strcmp(format,"BMP") ) counter = LoadBMP(file);
41
42 printf("counter = %d\n",counter);
43 if(counter != eBytes)
44 printf("ERROR: EdbImage::EdbImage: counter != eBytes \n");
45}
Int_t LoadPGM(char *file)
Definition: EdbImage.cxx:217
TArrayC * eBuffer
body of the image buffer
Definition: EdbImage.h:32
Int_t LoadBMP(char *file)
Definition: EdbImage.cxx:308
Int_t LoadRAW(char *file)
Definition: EdbImage.cxx:182
virtual void Set0()
Definition: EdbImage.cxx:81
Int_t eBytes
number of significative bytes in the buffer
Definition: EdbImage.h:33
TFile * file
Definition: write_pvr.C:3

◆ EdbImage() [3/3]

EdbImage::EdbImage ( int  columns,
int  rows,
char *  image,
int  col = 256 
)
92{
93 eColors = col;
94 eColumns = columns;
95 eRows = rows;
96 eBytes = rows*columns;
97
98 int save = 32; // 32 bytes safety margin
99 int s = eBytes + save;
100 char *buffer = new char[s];
101
102 eBuffer = new TArrayC();
103
104 int size = (int)strlen(image); printf("strlen(image) = %d\n",size);
105
106 memcpy((void *)buffer,(const void *)image,eBytes);
107 //strncpy(buffer,image,eBytes); // problems in case of '\0'
108
109 eBuffer->Adopt(s,buffer);
110}
Int_t eColors
maximal height of pixel (normally 256)
Definition: EdbImage.h:30
Int_t eColumns
number of columns of the matrix (1 row length)
Definition: EdbImage.h:27
Int_t eRows
number of rows of the matrix
Definition: EdbImage.h:28
s
Definition: check_shower.C:55

◆ ~EdbImage()

EdbImage::~EdbImage ( )
virtual
49{
50 if(eBuffer) delete eBuffer;
51 Set0();
52}

Member Function Documentation

◆ AdoptImage()

void EdbImage::AdoptImage ( int  columns,
int  rows,
char *  image,
int  col = 256 
)
114{
115 eColors = col;
116 eColumns = columns;
117 eRows = rows;
118 eBytes = rows*columns;
119 if(!eBuffer) eBuffer = new TArrayC();
120 eBuffer->Adopt(eBytes,image);
121}

◆ DumpPGM()

Int_t EdbImage::DumpPGM ( char *  file)

VT: 3/05/2004

283{
285
286 FILE *out = fopen( file, "w" );
287 if(!out) return 0;
288 fprintf(out,"P2\n"); // grey-scale magic identifier
289 fprintf(out,"%d %d\n",Width(),Height()); // image size
290 fprintf(out,"255\n"); // max pixel value
291 int nbytes = Width()*Height();
292 int ibyte=0;
293 unsigned char *buf = (unsigned char*)(eBuffer->GetArray());
294 while(1) {
295 for(int istr=0; istr<16; istr++) {
296 fprintf(out,"%d ",(int)(buf[ibyte]));
297 ibyte++;
298 if(ibyte>=nbytes) break;
299 }
300 fprintf(out,"\n");
301 if(ibyte>nbytes-1) break;
302 }
303 fclose(out);
304 return ibyte;
305}
Int_t Height() const
Definition: EdbImage.h:60
Int_t Width() const
Definition: EdbImage.h:59
fclose(pFile)

◆ FillBufferDouble()

Int_t EdbImage::FillBufferDouble ( TArrayD &  image) const
125{
126 int npix = Width()*Height();
127 if(npix<=0) return 0;
128 image.Set(npix);
129 for(int i=0; i<npix; i++) image[i]=(Double_t)(Pixel(i));
130 return npix;
131}
int Pixel(int c, int r) const
Definition: EdbImage.h:50

◆ GetBuffer()

char * EdbImage::GetBuffer ( ) const
inline
48{ return eBuffer->GetArray(); }

◆ GetHist1()

TH1F * EdbImage::GetHist1 ( ) const
141{
142 TH1F *hist = new TH1F("pix","Pixels distribution",
143 eColors, 0, eColors);
144 int i=0;
145 unsigned char *buf = (unsigned char*)(eBuffer->GetArray());
146
147 for (int icl=0; icl<eColumns; icl++) {
148 for (int ir=0; ir<eRows; ir++) {
149 i = ir*eColumns + icl;
150 hist->Fill( (int)(buf[i]) );
151 }
152 }
153
154 return hist;
155}
void hist()
Definition: init.C:23

◆ GetHist2()

TH2F * EdbImage::GetHist2 ( int  flip = 0) const
159{
160 TH2F *hist = new TH2F("ccd","CCD image",
161 eColumns, 0, eColumns,
162 eRows, 0, eRows);
163 int i=0;
164 unsigned char *buf = (unsigned char*)(eBuffer->GetArray());
165
166 int pix;
167
168 for (int icl=0; icl<eColumns; icl++) {
169 for (int ir=0; ir<eRows; ir++) {
170 i = ir*eColumns + icl;
171 pix = (int)(buf[i]);
172 if(flip) pix = eColors-1-pix;
173 hist->Fill( icl, ir, pix );
174 }
175 }
176
177 return hist;
178}

◆ Height()

Int_t EdbImage::Height ( ) const
inline
60{return eRows;}

◆ LoadBMP()

Int_t EdbImage::LoadBMP ( char *  file)

Loads a BMP format file... Author: Igor Kreslo

309{
314typedef struct {
315 unsigned short int type; /* Magic identifier */
316 unsigned int size; /* File size in bytes */
317 unsigned short int reserved1, reserved2;
318 unsigned int offset; /* Offset to image data, bytes */
319} HEADER;
320
321typedef struct {
322 unsigned int size; /* Header size in bytes */
323 int width,height; /* Width and height of image */
324 unsigned short int planes; /* Number of colour planes */
325 unsigned short int bits; /* Bits per pixel */
326 unsigned int compression; /* Compression type */
327 unsigned int imagesize; /* Image size in bytes */
328 int xresolution,yresolution; /* Pixels per meter */
329 unsigned int ncolours; /* Number of colours */
330 unsigned int importantcolours; /* Important colours */
331} INFOHEADER;
332
333HEADER hdr;
334INFOHEADER ihdr;
335
336//printf("hdr: %d ihdr: %d\n",sizeof(hdr), sizeof(ihdr));
337 char* ptr;
338ifstream raw(file,ios::in | ios::binary); // ios::binary added! 24.11.2003 (Stas)
339 ptr=(char*)&hdr;
340 raw.read(ptr,14);
341 ptr=(char*)&ihdr;
342 raw.read(ptr,40);
343 if(ihdr.bits!=8) { printf("Error: %d bits/pixel bitmap is not supported!\n",ihdr.bits); return -1;}
344 int N=ihdr.width*ihdr.height;
345 char* byte= new char[N];
346 raw.read(byte,1024);
347 raw.read(byte,N);
348 raw.close();
349 eBytes=N;
350 eColumns=ihdr.width;
351 eRows=ihdr.height;
352 eColors=256;
353
354 eBuffer->Adopt(eBytes,byte);
355
356 printf("EdbImage::LoadBMP: file %s is readed...Image %d x %d pixels.\n", file,eColumns,eRows);
357
358 return N;
359}
Int_t type
Definition: testBGReduction_By_ANN.C:15

◆ LoadPGM()

Int_t EdbImage::LoadPGM ( char *  file)

Loads a PGM format file... Author: Rosario Peluso

218{
223 int colors, pix, i, j, p2, p5=0;
224 char Id[10], Buffer[BUFSIZ];
225 FILE *in = fopen( file, "rb" );
226 int NRows, NCols;
227 int save = 32; //safety margins in bytes
228
229 fgets( Id, 10, in );
230 if( (p2 = strncmp(Id, "P2", 2)) && (p5 = strncmp(Id, "P5", 2)) )
231 {
232 printf("LoadPGM: Error Id\n"); return 0;
233 }
234
235 while( (fgets(Buffer, BUFSIZ, in) != NULL) && (*Buffer == '#') )
236 ;
237
238 if( sscanf(Buffer, "%d %d", &NRows, &NCols) != 2 )
239 { printf("LoadPGM: Error NRows, &NCols\n"); return 0; }
240
241 eRows = NRows;
242 eColumns = NCols;
244
245 if( fscanf(in, "%d\n", &colors) != 1 )
246 { printf("LoadPGM: Error\n"); return 0; }
247
248 eColors = colors;
249
250 if( colors != 255 )
251 printf("LoadPGM: Warning: colors = %d \n", colors);
252
253 char *Pixel = new char[NRows * NCols + save];
254
255 int counter = 0;
256 if( !p2 )
257 {
258 for( j = 0; j < NRows; j++ )
259 for( i = 0; i < NCols; i++ )
260 {
261 if( fscanf(in, "%d", &pix) != 1 ) printf("LoadPGM: Error\n");
262 Pixel[j * NCols + i] = pix;
263 counter++;
264 }
265 }
266 else if( !p5 && ((counter = fread((void *) Pixel, 1, NRows * NCols, in)) != eBytes) )
267 {
268 printf( "LoadPGM: Error loading binary file\n" );
269 return( 0 );
270 }
271
272 fclose( in );
273
274 eBuffer->Adopt(eBytes+save,Pixel);
275
276 printf("EdbImage::LoadPGM: file %s is readed...\n", file);
277
278 return counter;
279}
#define NULL
Definition: nidaqmx.h:84

◆ LoadRAW()

Int_t EdbImage::LoadRAW ( char *  file)

Loads a .raw format file... Author: Alfredo Cocco

eColumns = 512; eRows = 512;

fstream raw(file, ios::in); char byte[262144]; raw.read(byte,262144); raw.close();

eBytes = 262144; int save = 32; //safety margins in bytes

int index; int k = 0;

for(int i=0; i<size; i++) { for(int j=0; j<size; j++) { index = j + i * size; buffer[index] = (int) byte[k++]; } }

eBuffer->Adopt(eBytes+save,byte); return eBytes;

183{
213 return 0;
214}

◆ Pixel() [1/2]

int EdbImage::Pixel ( int  c,
int  r 
) const
inline
51 { if(r<0) return 0; if(r>eRows) return 0;
52 if(c<0) return 0; if(c>eColumns) return 0;
53 return Pixel(eColumns*r + c); }
void r(int rid=2)
Definition: test.C:201

◆ Pixel() [2/2]

int EdbImage::Pixel ( int  ipix) const
inline
55 { return (int)(((unsigned char*)(eBuffer->GetArray()))[ipix]); }

◆ Print()

void EdbImage::Print ( Option_t *  opt = "") const
virtual
135{
136 printf("Image: %d x %d \t : %d bytes\n", eColumns, eRows, eBytes);
137}

◆ Set0()

void EdbImage::Set0 ( )
virtual
82{
83 eRows = 0;
84 eColumns = 0;
85 eBuffer = 0;
86 eBytes = 0;
87 eColors = 0;
88}

◆ SetColors()

void EdbImage::SetColors ( int  col)
inline
47{eColors=col;}

◆ Width()

Int_t EdbImage::Width ( ) const
inline
59{return eColumns;}

Member Data Documentation

◆ eBuffer

TArrayC* EdbImage::eBuffer
private

body of the image buffer

◆ eBytes

Int_t EdbImage::eBytes
private

number of significative bytes in the buffer

◆ eColors

Int_t EdbImage::eColors
private

maximal height of pixel (normally 256)

◆ eColumns

Int_t EdbImage::eColumns
private

number of columns of the matrix (1 row length)

◆ eRows

Int_t EdbImage::eRows
private

number of rows of the matrix


The documentation for this class was generated from the following files: