PDA

View Full Version : How to Read and display BMP image using QT



agsrinivasan
29th January 2007, 06:47
hi everyone,
i have a bmp file in windows bmp format,i have to display the bmp image into widget..
already i had read the file and read the pixel (RGB datas)...

How to display bmp file in widget from that RGB datas...

here is my code..

#include <qapplication.h>
#include <iostream>
#include <cstdio>
#include <string>
using namespace std;

const short BITMAP_MAGIC_NUMBER=19778;
const int RGB_BYTE_SIZE=3;
int bmpinfohdr=0,bmpwidth=0,bmpheight=0,bmpcompression =0,bmpimagesize=0,bmpxresolution=0,bmpyresolution= 0,bmpcolorentries=0;
int bmpsignificantcolor =0;
short int bmpplanes=0,bmpbpp=0,datasize1=0;
char *data;
int byteWidth=0,padWidth=0;

//File information header
//provides general information about the file

typedef struct tagRGBQuad {
char rgbBlue;
char rgbGreen;
char rgbRed;
char rgbReserved;
} RGBQuad;

class bitmap: public QWidget
{
public:
bitmap(QWidget *parent =0,const char *name=0);
bool loaded;
RGBQuad *colours;
protected:
void loadimage();
int width,height;
unsigned short bpp;
unsigned int dataSize;
private:
//bool convert8(char *);
};

bitmap::bitmap( QWidget *parent, const char *name )
: QWidget( parent, name )
{
//loadimage("/home/website/qt-embedded-free-3.3.5/examples/buttongroups/keynorm.png");
loadimage();
}
void bitmap::loadimage()
{
FILE *in;
int numColours;
size_t result;
in=fopen("/home/invent1/Desktop/8bit.bmp","rb");
if(in==NULL)
{
printf("\nFile not found \n");
fclose(in);
}
/*if(fgetc(in)!='B' ||fgetc(in)!='M')
{
printf("\n not a bitmap file...\n");
fclose(in);
}
else printf("\n valid bitmap file...\n ");
*///if((p = malloc(sizeof(struct address)))==NULL)

//BitmapFileHeader bmfh;
//result = fread(&bmfh,sizeof(BitmapFileHeader),1,in);
/*result = fread(&bmptype,2,1,in);
if(result !=1)
printf("\n error in reading the bitmap type...\n");
else printf("\n bitmap type file read..\n ");
*/
short int bmptype=0;
int bmpsize=0;
short int bmpreserved=0,bmpreserved1=0;
int bmpoffbits=0;

fread(&bmptype,2,1,in);
cout << "BMP TYPE SIZE =" << sizeof(bmptype) << endl;
cout << "BMP TYPE =" << bmptype << endl;

fread(&bmpsize,4,1,in);
cout << "BMP SIZE =" << sizeof(bmpsize) << endl;
cout << "BMP size =" << bmpsize << endl;

fread(&bmpreserved,2,1,in);
cout << "BMP reserved size =" << sizeof(bmpreserved) << endl;
cout << "BMP reserved =" << bmpreserved << endl;

fread(&bmpreserved1,2,1,in);
cout << "BMP reserved1 size =" << sizeof(bmpreserved1) << endl;
cout << "BMP reserved1 =" << bmpreserved1 << endl;

fread(&bmpoffbits,4,1,in);
cout << "BMP offbits size =" << sizeof(bmpoffbits) << endl;
cout << "BMP ofbits =" << bmpoffbits << endl;

int bmpinfohdr=0,bmpwidth=0,bmpheight=0,bmpcompression =0,bmpimagesize=0,bmpxresolution=0,bmpyresolution= 0,bmpcolorentries=0;
int bmpsignificantcolor =0;
short int bmpplanes=0,bmpbpp=0,datasize=0;

fread(&bmpinfohdr,4,1,in);
cout << "BMP infohdr size =" << sizeof(bmpinfohdr) << endl;
cout << "BMP infohdr =" << bmpinfohdr << endl;

fread(&bmpwidth,4,1,in);
cout << "BMP width size =" << sizeof(bmpwidth);
cout << "BMP width =" << bmpwidth << endl;

fread(&bmpheight,4,1,in);
cout << "BMP height size =" << sizeof(bmpheight);
cout << "BMP height =" << bmpheight << endl;

fread(&bmpplanes,2,1,in);
cout << "BMP planes size =" << sizeof(bmpplanes);
cout << "\t BMP planes =" << bmpplanes << endl;

fread(&bmpbpp,2,1,in);
cout << "BMP bpp size =" << sizeof(bmpbpp);
cout << "\t BMP bpp =" << bmpbpp << endl;

fread(&bmpcompression,4,1,in);
cout << "BMP bmpcompression size =" << sizeof(bmpcompression);
cout << "\t BMP bmpcompression =" << bmpcompression << endl;

fread(&bmpimagesize,4,1,in);
cout << "BMP bmpimagesize size =" << sizeof(bmpimagesize);
cout << "\t BMP bmpimagesize =" << bmpimagesize << endl;

fread(&bmpxresolution,4,1,in);
cout << "BMP bmpxresolution size =" << sizeof(bmpxresolution);
cout << "\t BMP bmpxresolution =" << bmpxresolution << endl;

fread(&bmpyresolution,4,1,in);
cout << "BMP bmpyresolution size =" << sizeof(bmpyresolution);
cout << "\t BMP bmpyresolution =" << bmpyresolution << endl;

fread(&bmpcolorentries,4,1,in);
cout << "BMP bmpcolorentries size =" << sizeof(bmpcolorentries);
cout << "\t BMP bmpcolorentries =" << bmpcolorentries << endl;

fread(&bmpsignificantcolor,4,1,in);
cout << "BMP bmpsignificantcolor size =" << sizeof(bmpsignificantcolor);
cout << "\t BMP bmpsignificantcolor =" << bmpsignificantcolor << endl;

unsigned int ab=0,cd=0;
ab=bmpbpp/8.0;
cout <<"\n ab ="<<ab<<endl;
cd=(unsigned int)ab;
cout << "\n cd="<<cd <<endl;
int product=0;
product =bmpwidth*bmpheight;
cout <<"\n product="<< product;
cout <<"\t bmpwidth="<<bmpwidth;
cout <<"\t bmpheight="<<bmpheight<<endl;

int datasize1=0;
datasize1=bmpwidth*bmpheight*cd;
cout << "\n data size1="<< datasize1<<endl;

numColours=1<<bmpbpp;

cout << "\n numColours = " << sizeof(numColours);

cout << "\nNumber of colors =" << numColours;

if(bmpbpp<8)
{
printf("\n File is not 8 or 24 bits per pixel\n");
fclose(in);
}
if(bmpbpp==8)
{
colours=new RGBQuad[numColours];
fread(colours,sizeof(RGBQuad),numColours,in);
}

char *tempPixelData;
tempPixelData=new char[datasize1];

if(tempPixelData==NULL)
fclose(in);
else printf("\n temppixeldata is not null");

fread(tempPixelData,sizeof(char),datasize1,in);

cout << "size of tempPixel data " << sizeof(tempPixelData) << endl;

//cout << "pixel data =" << sizeof(tempPixelData);
/* from here i have to read pixel data and display the Image*/
fclose(in);
}
int main( int argc, char **argv )
{
printf("\n main..");
QApplication a( argc, argv);
bitmap w;
//w.setGeometry(0,0,170,250);
a.setMainWidget( &w );
w.show();
//a.connect( &a, SIGNAL( lastWindowClosed() ), &a, SLOT( quit() ) );
return a.exec();
}




Thanks in advance...


regds...

srini

ChristianEhrlicher
29th January 2007, 07:42
Read the docs: http://doc.trolltech.com/4.2/qimage


QImage img = QImage("/home/invent1/Desktop/8bit.bmp");

agsrinivasan
29th January 2007, 08:09
Read the docs: http://doc.trolltech.com/4.2/qimage


QImage img = QImage("/home/invent1/Desktop/8bit.bmp");

hi thanks...
I am using qt_embedded-free-3.3.5..

Reading and writing of image files support in qt.4.2 right??

how to do the same using qt-3.3..

Thanks in advance

srini

ChristianEhrlicher
29th January 2007, 08:14
QImage should also be available on Qt3 (embed)
-> read the docs: http://doc.trolltech.com/3.3/qimage.html