PDA

View Full Version : Display and divide the image



kiransu123
2nd March 2007, 15:24
hi
i m kiran suryawanshi
i m final year student i m working on the project called as "tiled image viewer"
In this i suppose to display image on tiled display i.e four monitors clustered together
for that i hv to display and divide the image
so i write the following code but i had problem saying that "Segmentation fault"

i m represting problem area btn <problem></problem> tag
plz help me


#include "qgl.h"
#include "qapplication.h"
#include "qimage.h"
#include "iostream.h"
#include "GL/gl.h"
class Radha : public QGLWidget
{
public:
Radha(const QString &filename,QWidget *parent=0);
void paintGL();
void resizeGL(int w,int h);
void divide();
protected:
QImage data,gldata;
};

Radha::Radha(const QString &filename,QWidget *parent):QGLWidget(parent){
data.load(filename);
gldata=QGLWidget::convertToGLFormat(data);
resize(data.size());

}

void Radha::paintGL(){
int k,i,j,count=0,flag=1;
<PROBLEM>
uchar *temp;
uchar *img;
temp=gldata.bits();

for(j=0;j<data.width()*data.height();j++){ //for reading the half image in img array
if(count==data.width()/2){
flag==1-flag;
count=0;
}
if(flag==1)
img[j++]=temp[i];
count++;
}

glDrawPixels(data.width(),data.height(),GL_RGBA,GL _UNSIGNED_BYTE,img);

}


void Radha::resizeGL(int w,int h){
glViewport(0,0,w,h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0,w,0,h,-1,1);
glMatrixMode(GL_MODELVIEW);

}

int main(int argc,char** argv){
QApplication app(argc,argv);
Radha pw(argv[1]);
pw.showFullScreen();
return app.exec();
}

jacek
2nd March 2007, 15:43
Where do you initialize i and img and what do you need k for?

kiransu123
2nd March 2007, 15:46
k is useless
is there any need to initialize the img
it is not showing some error but after executing it is showing "Segmentation fault"

jacek
2nd March 2007, 15:53
k is useless
So there is no point in declaring it.


is there any need to initialize the img
Of course there is. It points at some random place in memory, so you can't use it until you initialize it.

kiransu123
2nd March 2007, 16:59
can u tell me how to divide the image in 4 parts

jacek
2nd March 2007, 17:15
can u tell me how to divide the image in 4 parts
Use QImage::copy().

kiransu123
2nd March 2007, 17:42
Use QImage::copy().

how QImage::copy() is used bcz for this how can u specify the image
its syntax is copy(x,y,w,h)const
wht this const signifies

Brandybuck
2nd March 2007, 17:58
QImage QImage::copy ( const QRect & rectangle = QRect() ) const
Returns a sub-area of the image as a new image.

If you want to subdivide an image into four sub-images, then you can use QImage::copy(). It is simply a matter of rectangles! Take one large rectangle and divide it into four small rectangles. You do not even need to know arithmetic, because QRect::center() will give you the QPoint in the center of the rectangle.

p.s. The "const" in the method signature means that it is a constant function, which does not modify the object. The use of QImage::copy() will not modify the QImage it is called with.

kiransu123
2nd March 2007, 18:22
If you want to subdivide an image into four sub-images, then you can use QImage::copy(). It is simply a matter of rectangles! Take one large rectangle and divide it into four small rectangles. You do not even need to know arithmetic, because QRect::center() will give you the QPoint in the center of the rectangle.

p.s. The "const" in the method signature means that it is a constant function, which does not modify the object. The use of QImage::copy() will not modify the QImage it is called with.

then how can i display using glDrowPixels() bcz the last argument require for it is pointer to image so wht should i pass to it
thanks for solution

fullmetalcoder
2nd March 2007, 18:27
then how can i display using glDrowPixels() bcz the last argument require for it is pointer to image so wht should i pass to it
thanks for solution
I think you're handling this in a wrong way. Qt has built-in facilities for rendering images (with or without OpenGL) and manipulating them. Instead of mixing Qt classes and OpenGl calls you should choose a single approach : either plain Qt (using QtOpenGl module) or plain C/OpenGl with possibly some helper libs to handle images loading...

kiransu123
2nd March 2007, 18:36
I think you're handling this in a wrong way. Qt has built-in facilities for rendering images (with or without OpenGL) and manipulating them. Instead of mixing Qt classes and OpenGl calls you should choose a single approach : either plain Qt (using QtOpenGl module) or plain C/OpenGl with possibly some helper libs to handle images loading...

can u tell me how should i proceed further to divide the image in 4 parts using QT
wht r built in facililies for this problem

fullmetalcoder
2nd March 2007, 19:04
:o I checked out the doc of QGlWidget and it seems that raw OpenGL calls are still needed when playing with this one. However as I have already said, if all you need is displaying the image, other widgets can handle it very well.


can u tell me how should i proceed further to divide the image in 4 parts using QT
wht r built in facililies for this problem
The QImage and QPixmap classes provide very convinient interfaces for manipulating images of various formats. As mentioned earlier in this thread, the copy() method is basically what you need. The first thing to do is to decide how you want your image to be split (you said 4 parts but are they effectively equal? beware, this includes positioning on the screens...) then you have to obtain image size with the so-called method and compute sizes of splitted parts. Finally you can generated four images/pixmaps from the original one using the computed sizes (and some basic offset calculation of course ;)) and the copy() method ...

BTW you talk about splitting so as to display in clustered screens but I don't see anything like this in the code you posted...

wysota
2nd March 2007, 19:07
Ok, maybe this will sound stupid, but why are you using OpenGL here?

minimoog
3rd March 2007, 12:33
......
GLuint texID = bindTexture(data);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
......

void Radha::paintGL()
{
glBindTexture(GL_TEXTURE_2D, texID);

glBegin(GL_QUADS);
glTexCoord2f(0.0f, 0.0f); glVertex2f(0.0f, 0.0f);
gltexCoord2f(0.5f, 0.0f); glVertex2f(float(width()), 0.0f);
glTexCoord2f(0.5f, 0.5f); glVertex2f(float(width()), float(height()));
glTexCoord2f(0.0f, 0.5f); glVertex2f(0.0f, float(height()));
glEnd();
}

This will show quarter image (half width, half height) on the whole widget.

Or if your card doesn't support non_power_of_two textures (I don't know how QT handles them) you can use this similar code:


......
GLuint texID = bindTexture(data);
glTexParameteri(GL_TEXTURE_RECTANGLE_ARB, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_RECTANGLE_ARB, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_RECTANGLE_ARB, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_RECTANGLE_ARB, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
......

void Radha::paintGL()
{
glBindTexture(GL_TEXTURE_RECTANGLE_ARB, texID);

glBegin(GL_QUADS);
glTexCoord2f(0.0f, 0.0f); glVertex2f(0.0f, 0.0f);
gltexCoord2f(float(width())/2.0f, 0.0f); glVertex2f(float(width()), 0.0f);
glTexCoord2f(float(width())/2.0f, float(height())/2.0f); glVertex2f(float(width()), float(height()));
glTexCoord2f(0.0f, float(height())/2.0f); glVertex2f(0.0f, float(height()));
glEnd();
}

kiransu123
4th March 2007, 05:32
Hi I m kiran
i want to display an image then divide it and again display the part of the image.
Is is possibe in QImage to read the image in the array so i can perform some manipulation on the image

i m reading the image as follows the problem is that how can i read the image the array
I indicate the problem area in <problem></problem> tag in paintGL() function.


#include "qgl.h"
#include "qapplication.h"
#include "qimage.h"
class PaintWidget : public QGLWidget{
public:
PaintWidget(const QString &filename,QWidget *parent=0);
void paintGL();
void resizeGL(int w,int h);
protected:
QImage data,gldata;
};

PaintWidget::PaintWidget(const QString &filename,QWidget *parent):QGLWidget(parent){
data.load(filename);
gldata=QGLWidget::convertToGLFormat(data);
resize(data.size());
}


void PaintWidget::paintGL(){
int k,i,j,count_h=0,count_v=0,flag=1;
uchar *temp;
uchar *img;
temp=gldata.bits();
//<problem>
for(i=0;i<gldata.width()*gldata.height();i++){
if(count_h==gldata.width()/2){ //checking for first half of image
flag=1-flag; // flag =1 : 1st half flag=0 : second half
count_h=0; // for counting next half counter make zero
}
if(flag==1)
img[++j]=temp[i]; //img[j++]will read only half of the image therefore j++
count_h++;
}


glDrawPixels(data.width(),data.height(),GL_RGBA,GL _UNSIGNED_BYTE,img);

//</problem> //glDrawPixels(data.width(),data.height(),GL_RGBA,GL _UNSIGNED_BYTE,gldata.bits());
}

void PaintWidget::resizeGL(int w, int h){
glViewport(0,0,w,h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0,w,0,h,-1,1);
glMatrixMode(GL_MODELVIEW);
}

int main(int argc, char** argv){
QApplication app(argc,argv);
PaintWidget pw(argv[1]);
pw.show();
return app.exec();

}

kiransu123
4th March 2007, 13:51
Hi I m kiran
i want to display an image then divide it and again display the part of the image.
Is is possibe in QImage to read the image in the array so i can perform some manipulation on the image

i m reading the image as follows the problem is that how can i read the image the array
I indicate the problem area in <problem></problem> tag in paintGL() function.


#include "qgl.h"
#include "qapplication.h"
#include "qimage.h"
class PaintWidget : public QGLWidget{
public:
PaintWidget(const QString &filename,QWidget *parent=0);
void paintGL();
void resizeGL(int w,int h);
protected:
QImage data,gldata;
};

PaintWidget::PaintWidget(const QString &filename,QWidget *parent):QGLWidget(parent){
data.load(filename);
gldata=QGLWidget::convertToGLFormat(data);
resize(data.size());
}


void PaintWidget::paintGL(){
int k,i,j,count_h=0,count_v=0,flag=1;
uchar *temp;
uchar *img;
temp=gldata.bits();
//<problem>
for(i=0;i<gldata.width()*gldata.height();i++){
if(count_h==gldata.width()/2){ //checking for first half of image
flag=1-flag; // flag =1 : 1st half flag=0 : second half
count_h=0; // for counting next half counter make zero
}
if(flag==1)
img[++j]=temp[i]; //img[j++]will read only half of the image therefore j++
count_h++;
}


glDrawPixels(data.width(),data.height(),GL_RGBA,GL _UNSIGNED_BYTE,img);

//</problem> //glDrawPixels(data.width(),data.height(),GL_RGBA,GL _UNSIGNED_BYTE,gldata.bits());
}

void PaintWidget::resizeGL(int w, int h){
glViewport(0,0,w,h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0,w,0,h,-1,1);
glMatrixMode(GL_MODELVIEW);
}

int main(int argc, char** argv){
QApplication app(argc,argv);
PaintWidget pw(argv[1]);
pw.show();
return app.exec();

}



......
GLuint texID = bindTexture(data);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
......

void Radha::paintGL()
{
glBindTexture(GL_TEXTURE_2D, texID);

glBegin(GL_QUADS);
glTexCoord2f(0.0f, 0.0f); glVertex2f(0.0f, 0.0f);
gltexCoord2f(0.5f, 0.0f); glVertex2f(float(width()), 0.0f);
glTexCoord2f(0.5f, 0.5f); glVertex2f(float(width()), float(height()));
glTexCoord2f(0.0f, 0.5f); glVertex2f(0.0f, float(height()));
glEnd();
}

This will show quarter image (half width, half height) on the whole widget.

Or if your card doesn't support non_power_of_two textures (I don't know how QT handles them) you can use this similar code:


......
GLuint texID = bindTexture(data);
glTexParameteri(GL_TEXTURE_RECTANGLE_ARB, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_RECTANGLE_ARB, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_RECTANGLE_ARB, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_RECTANGLE_ARB, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
......

void Radha::paintGL()
{
glBindTexture(GL_TEXTURE_RECTANGLE_ARB, texID);

glBegin(GL_QUADS);
glTexCoord2f(0.0f, 0.0f); glVertex2f(0.0f, 0.0f);
gltexCoord2f(float(width())/2.0f, 0.0f); glVertex2f(float(width()), 0.0f);
glTexCoord2f(float(width())/2.0f, float(height())/2.0f); glVertex2f(float(width()), float(height()));
glTexCoord2f(0.0f, float(height())/2.0f); glVertex2f(0.0f, float(height()));
glEnd();
}


i tried this code but it is showing
In constructor `Radha::Radha(const QString&,
QWidget*)':
/usr/lib/qt-3.1/include/qgl.h:257: `QGLContext::QGLContext()' is private
radha.cpp:19: within this context
radha.cpp:25: `bindTexture' undeclared (first use this function)
radha.cpp:25: (Each undeclared identifier is reported only once for each
function it appears in.)

minimoog
4th March 2007, 19:30
BIG Sorry, I didn't noticed that you use QT3, my code was for QT4.

However I analyze your code.



PaintWidget::PaintWidget(const QString &filename,QWidget *parent):QGLWidget(parent){
data.load(filename);
gldata=QGLWidget::convertToGLFormat(data); //This is error you must have valid OpenGL context
resize(data.size());
}

For displaying half image you can use, QImage::copy()



void PaintWidget::paintGL(){
QImage halfImage = data.copy(0, 0, data.width()/2, data.height()/2);
gldata = QGLWidget::convertToGLFormat(halfImage);
glDrawPixels(gldata.width(), gldata.height(), GL_RGBA, GL_UNSIGNED_TYPE, gldata.bits());
}

kiransu123
5th March 2007, 17:32
BIG Sorry, I didn't noticed that you use QT3, my code was for QT4.

However I analyze your code.



PaintWidget::PaintWidget(const QString &filename,QWidget *parent):QGLWidget(parent){
data.load(filename);
gldata=QGLWidget::convertToGLFormat(data); //This is error you must have valid OpenGL context
resize(data.size());
}

For displaying half image you can use, QImage::copy()



void PaintWidget::paintGL(){
QImage halfImage = data.copy(0, 0, data.width()/2, data.height()/2);
gldata = QGLWidget::convertToGLFormat(halfImage);
glDrawPixels(gldata.width(), gldata.height(), GL_RGBA, GL_UNSIGNED_TYPE, gldata.bits());
}


Thank you very much
can you tell me how to display small image or some copied image by copy() on full screen

wysota
5th March 2007, 17:48
How about just:

QLabel *label = new QLabel;
QPixmap pix;
pix.convertFromImage(image.copy(QRect(0,0, 200, 200)));
label->setPixmap(pix);
label->setScaledContents(true);
label->showFullScreen();

I don't really see a reason to use OpenGL to display an image...

minimoog
5th March 2007, 20:40
Do you must use OpenGL?