PDA

View Full Version : Dev Image Library's ilGetPixles and ilSetPixels



SpiderPig500
04-30-2011, 02:37 AM
I'm trying to copy a certain part of an image and then assign that part back to the original image.
But I can't figure out how to use these commands in sequence.
Does any one have any code on how they use these functions?
I can't find any decent documentation on them any where...

Thanks,

mobeen
04-30-2011, 03:39 AM
Usually I will get the data pointer directly and then write to it.


GLubyte* pixels = (GLubyte*)ilGetData();
//do copying using memcpy directly.

SpiderPig500
04-30-2011, 02:18 PM
I haven't tried that because I want to edit the pixel data.
Unless there's a way to do it with ilGetData()?

The Little Body
04-30-2011, 02:46 PM
SpiderPig500,

A speed jump to ilGetData (http://www-f9.ijs.si/~matevz/docs/DevIL/il/f00125.htm) say



ilGetData returns an unsigned byte pointer to the current bound image's data to allow direct access and modification to the contents of the image.


You want to copy one part of a image A into an image B ???
Or it's something else ?

SpiderPig500
05-01-2011, 02:17 AM
Yes, I want to copy a part of image A into a part of image B.
How is this done with an unsigned byte pointer?

The Little Body
05-01-2011, 02:32 AM
I think the function for to make it is :

ILvoid ilCopyPixels(
ILuint XOff,
ILuint YOff,
ILuint ZOff,
ILuint Width,
ILuint Height,
ILuint Depth,
ILenum Format,
ILenum Type,
ILvoid *Data
);

Note that I have never developped with devIL ... :(

But it's never too late for lear something new :)

=> I learn a little this API and give you quickly a solution

SpiderPig500
05-01-2011, 03:31 AM
That's what I've been trying to use - I posted this topic to see if any one knew how to use them, here is a portion of the code that I'm using;



GLuint image=ilutGLLoadImage("test.png");//256x256 in size and has 4 pixel components (RGBA)
int memoryneeded = 128 * 128 * 4 * sizeof(unsigned char);//I want only 1/4 of the image's pixel data
ILubyte* pixdata=( (ILubyte*)malloc(memoryneeded) );
ilCopyPixels(0,0,0,128,128,1,IL_RGBA,IL_UNSIGNED_B YTE,pixdata);//copies 1/4 of the image

ilDeleteImage(image);
image=ilGenImage();//reset image with a new one
ilBindImage(image);/bind it and then set the copied data to it
ilSetPixels(0,0,0,128,128,1,IL_RGBA,IL_UNSIGNED_BY TE,pixdata);


It's untested - (it's just what I'm trying to do but consolidated).

Thanks,

The Little Body
05-01-2011, 06:25 AM
Hi,

I have something that work :)




gcc testil.c -o testil -L/usr/X11R6/lib -lGL -lGLU -lglut -lIL

#include <GL/glut.h> // Header File For The GLUT Library
#include <GL/gl.h> // Header File For The OpenGL32 Library
#include <GL/glu.h> // Header File For The GLu32 Library
#include <unistd.h> // Header file for sleeping.
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <IL/il.h>

/* ascii code for the escape key */
#define ESCAPE 27

#define FALSE 0
#define TRUE 1

/* The number of our GLUT window */
int window;

#ifndef CDS_FULLSCREEN // CDS_FULLSCREEN Is Not Defined By Some
#define CDS_FULLSCREEN 4 // Compilers. By Defining It This Way,
#endif

ILuint ilTexture, ilSubTexture; // We Can Avoid Errors
GLuint Textures[2];
int texture=1;
char *dataTexture, *dataSubTexture;
int w,h, f,bpp;
int wsub,hsub, fsub, bppsub;

int i , j, k;


void InitDevIL()
{

glEnable(GL_TEXTURE_2D);
glGenTextures(2,Textures);
glBindTexture(GL_TEXTURE_2D, Textures[texture]);
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexEnvf ( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);


ilInit();

// Load the big picture with DevIL
ilGenImages(1, &amp;ilTexture);
ilBindImage(ilTexture);
if( ilLoadImage("test1.jpg") != 0)
printf("ilLoadImage %s OK \n", "test1.jpg");
else
printf("ilLoadImage %s KO \n", "test1.jpg");
dataTexture = ilGetData();
w = ilGetInteger(IL_IMAGE_WIDTH);
h = ilGetInteger(IL_IMAGE_HEIGHT);
f = ilGetInteger(IL_IMAGE_FORMAT);
bpp = ilGetInteger(IL_IMAGE_BPP);
printf("ilGetData = %x w=%d, h=%d bpp=%d \n", dataTexture,w,h,bpp);

// Load the subpicture
ilGenImages(1, &amp;ilSubTexture);
ilBindImage(ilSubTexture);
if( ilLoadImage("test2.jpg") != 0)
printf("ilLoadImage %s OK \n", "test2.jpg");
else
printf("ilLoadImage %s KO \n", "test2.jpg");
dataSubTexture = ilGetData();
wsub = ilGetInteger(IL_IMAGE_WIDTH);
hsub = ilGetInteger(IL_IMAGE_HEIGHT);
fsub = ilGetInteger(IL_IMAGE_FORMAT);
bppsub = ilGetInteger(IL_IMAGE_BPP);
printf("ilGetData = %x w=%d, h=%d bpp=%d \n", dataTexture,wsub,hsub,bppsub);

// Include the subpicture into the original picture
ilBindImage(ilTexture);
ilSetPixels(0,0,0,wsub,hsub/2,1,fsub,IL_UNSIGNED_BYTE, dataSubTexture);

glBindTexture(GL_TEXTURE_2D, Textures[texture]);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, w, h, 0, GL_RGB, GL_UNSIGNED_BYTE, dataTexture);
}

void DrawTest()
{

glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear The Buffers
glLoadIdentity (); // Reset The Matrix

glPushMatrix();
glBindTexture(GL_TEXTURE_2D, Textures[texture]);

glBegin(GL_QUADS);
//glColor3f(0,1,0);
glTexCoord2i(1,0);
glVertex3f(-1,-1,-1);

//glColor3f(0,0,1);
glTexCoord2i(0,0);
glVertex3f(1,-1,-1);

//glColor3f(1,0,0);
glTexCoord2i(0,1);
glVertex3f(1,1,-1);

//glColor3f(1,0,0);
glTexCoord2i(1,1);
glVertex3f(-1,1,-1);
glEnd();

glPopMatrix();
glutSwapBuffers();

}


/* A general OpenGL initialization function. Sets all of the initial parameters. */
void InitGL(int Width, int Height) // We call this right after our OpenGL window is created.
{


//g_window = window;
//g_keys = keys;

// Start Of User Initialization
glHint (GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST); // Realy Nice perspective calculations

glClearColor (0.7f, 0.7f, 0.7f, 0.0f); // Light Grey Background
glClearDepth (1.0f); // Depth Buffer Setup

//glEnable (GL_DEPTH_TEST); // Enable Depth Testing
//glDepthFunc (GL_LESS); // The Type Of Depth Test To Do

//glShadeModel (GL_SMOOTH); // Enables Smooth Color Shading ( NEW )
//glDisable (GL_LINE_SMOOTH); // Initially Disable Line Smoothing ( NEW )

//glEnable (GL_CULL_FACE); // Enable OpenGL Face Culling ( NEW )

//glDisable (GL_LIGHTING); // Disable OpenGL Lighting ( NEW )

InitDevIL();

}

/* The function called when our window is resized (which shouldn't happen, because we're fullscreen) */
void ReSizeGLScene(int Width, int Height)
{
if (Height==0) // Prevent A Divide By Zero If The Window Is Too Small
Height=1;

glViewport(0, 0, Width, Height); // Reset The Current Viewport And Perspective Transformation

glMatrixMode(GL_PROJECTION);
glLoadIdentity();

gluPerspective(45.0f,(GLfloat)Width/(GLfloat)Height,0.1f,100.0f);
glMatrixMode(GL_MODELVIEW);
}


void Deinitialize (void) // Any User DeInitialization Goes Here
{
glDeleteTextures (2, Textures); // Delete The Shader Texture ( NEW )
}

/* The function called whenever a key is pressed. */
void keyPressed(unsigned char key, int x, int y)
{
/* avoid thrashing this procedure */
usleep(100);

/* If escape is pressed, kill everything. */

switch (key) {

case ESCAPE: // kill everything.
/* shut down our window */
Deinitialize();
glutDestroyWindow(window);

/* exit the program...normal termination. */
exit(1);
break; // redundant.

default:
printf ("Key %d pressed. No action there yet.\n", key);
break;
}

// Update Angle Based On The Clock
}

void specialKeyPressed(int key, int x, int y)
{
/* avoid thrashing this procedure */
usleep(100);

switch (key) {

case GLUT_KEY_UP: // decrease x rotation speed;
// outlineWidth++;
break;

case GLUT_KEY_DOWN: // increase x rotation speed;
// outlineWidth--;
break;

default:
break;
}
}

int main(int argc, char **argv)
{
/* Initialize GLUT state - glut will take any command line arguments that pertain to it or
X Windows - look at its documentation at http://reality.sgi.com/mjk/spec3/spec3.html */
glutInit(&amp;argc, argv);

/* Select type of Display mode:
Double buffer
RGBA color
Alpha components supported
Depth buffer */
glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_ALPHA | GLUT_DEPTH);

/* get a 640 x 480 window */
glutInitWindowSize(640, 480);

/* the window starts at the upper left corner of the screen */
glutInitWindowPosition(0, 0);

/* Open a window */
window = glutCreateWindow("YLP : first test with DevIL");

/* Register the function to do all our OpenGL drawing. */
glutDisplayFunc(&amp;DrawTest);

//* Go fullscreen. This is as soon as possible. */
//glutFullScreen();

/* Even if there are no events, redraw our gl scene. */
// glutIdleFunc(&amp;DrawTest);

/* Register the function called when our window is resized. */
glutReshapeFunc(&amp;ReSizeGLScene);

/* Register the function called when the keyboard is pressed. */
glutKeyboardFunc(&amp;keyPressed);

glutSpecialFunc(&amp;specialKeyPressed);

/* Initialize our window. */
InitGL(640, 480);

/* Start Event Processing Engine */
glutMainLoop();

return 1;
}



It include a subpicture (test2.jpg 256x256) into a more big picture (test1.jpg 500x329).

The Little Body
05-01-2011, 06:38 AM
I don't understand why, but I have a problem when I want to include all the subpicture :(



ilSetPixels(0,0,0,wsub,hsub/4*3,1,fsub,IL_UNSIGNED_BYTE, dataSubTexture);


=> seem work

but


ilSetPixels(0,0,0,wsub,hsub,1,fsub,IL_UNSIGNED_BYT E, dataSubTexture);


give a too big subpicture in height :(

The Little Body
05-01-2011, 07:28 AM
I have a little modified the source for to can see more precisely the problem




#include <GL/glut.h> // Header File For The GLUT Library
#include <GL/gl.h> // Header File For The OpenGL32 Library
#include <GL/glu.h> // Header File For The GLu32 Library
#include <unistd.h> // Header file for sleeping.
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <IL/il.h>
#include <string.h>

/* ascii code for the escape key */
#define ESCAPE 27

#define FALSE 0
#define TRUE 1

/* The number of our GLUT window */
int window;


ILuint ilTexture, ilSubTexture;
GLuint Textures[2];
int texture=0;
char *dataTexture, *dataSubTexture, *initialTexture;
int w,h, f,bpp;
int wsub,hsub, fsub, bppsub;

int subheight=100;

int i , j, k;


void IncludeSubPicture()
{
// Include the subpicture into the original picture
ilBindImage(ilTexture);
ilSetPixels(0,0,0,w,h,1,fsub,IL_UNSIGNED_BYTE, initialTexture);
ilSetPixels(0,0,0,wsub,subheight,1,fsub,IL_UNSIGNE D_BYTE, dataSubTexture);
printf("subheight=%d \n", subheight);

glBindTexture(GL_TEXTURE_2D, Textures[0]);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, w, h, 0, GL_RGB, GL_UNSIGNED_BYTE, dataTexture);
}

void InitDevIL()
{

glEnable(GL_TEXTURE_2D);
glGenTextures(2,Textures);
glBindTexture(GL_TEXTURE_2D, Textures[0]);
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexEnvf ( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
glBindTexture(GL_TEXTURE_2D,Textures[1]);
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexEnvf ( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);

ilInit();

// Load the big picture with DevIL
ilGenImages(1, &amp;ilTexture);
ilBindImage(ilTexture);
if( ilLoadImage("test1.jpg") != 0)
printf("ilLoadImage %s OK \n", "test1.jpg");
else
printf("ilLoadImage %s KO \n", "test1.jpg");
dataTexture = ilGetData();
w = ilGetInteger(IL_IMAGE_WIDTH);
h = ilGetInteger(IL_IMAGE_HEIGHT);
f = ilGetInteger(IL_IMAGE_FORMAT);
bpp = ilGetInteger(IL_IMAGE_BPP);
printf("ilGetData = %x w=%d, h=%d bpp=%d \n", dataTexture,w,h,bpp);
initialTexture=malloc(w*h*bpp);
memcpy(initialTexture,dataTexture,w*h*bpp);

// Load the subpicture
ilGenImages(1, &amp;ilSubTexture);
ilBindImage(ilSubTexture);
if( ilLoadImage("test2.jpg") != 0)
printf("ilLoadImage %s OK \n", "test2.jpg");
else
printf("ilLoadImage %s KO \n", "test2.jpg");
dataSubTexture = ilGetData();
wsub = ilGetInteger(IL_IMAGE_WIDTH);
hsub = ilGetInteger(IL_IMAGE_HEIGHT);
fsub = ilGetInteger(IL_IMAGE_FORMAT);
bppsub = ilGetInteger(IL_IMAGE_BPP);
printf("ilGetData = %x w=%d, h=%d bpp=%d \n", dataSubTexture,wsub,hsub,bppsub);

IncludeSubPicture();
}

void DrawTest()
{

glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear The Buffers
glLoadIdentity (); // Reset The Matrix

glPushMatrix();
glBindTexture(GL_TEXTURE_2D, Textures[texture]);

glBegin(GL_QUADS);
//glColor3f(0,1,0);
glTexCoord2i(1,0);
glVertex3f(-1,-1,-1);

//glColor3f(0,0,1);
glTexCoord2i(0,0);
glVertex3f(1,-1,-1);

//glColor3f(1,0,0);
glTexCoord2i(0,1);
glVertex3f(1,1,-1);

//glColor3f(1,0,0);
glTexCoord2i(1,1);
glVertex3f(-1,1,-1);
glEnd();

glPopMatrix();
glutSwapBuffers();

}


/* A general OpenGL initialization function. Sets all of the initial parameters. */
void InitGL(int Width, int Height) // We call this right after our OpenGL window is created.
{

// Start Of User Initialization
glHint (GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST); // Realy Nice perspective calculations

glClearColor (0.7f, 0.7f, 0.7f, 0.0f); // Light Grey Background
glClearDepth (1.0f); // Depth Buffer Setup

InitDevIL();

}

/* The function called when our window is resized (which shouldn't happen, because we're fullscreen) */
void ReSizeGLScene(int Width, int Height)
{
if (Height==0) // Prevent A Divide By Zero If The Window Is Too Small
Height=1;

glViewport(0, 0, Width, Height); // Reset The Current Viewport And Perspective Transformation

glMatrixMode(GL_PROJECTION);
glLoadIdentity();

gluPerspective(45.0f,(GLfloat)Width/(GLfloat)Height,0.1f,100.0f);
glMatrixMode(GL_MODELVIEW);
}


void Deinitialize (void) // Any User DeInitialization Goes Here
{
glDeleteTextures (2, Textures); // Delete The Shader Texture ( NEW )
}

/* The function called whenever a key is pressed. */
void keyPressed(unsigned char key, int x, int y)
{
/* avoid thrashing this procedure */
usleep(100);

/* If escape is pressed, kill everything. */

switch (key) {

case ESCAPE: // kill everything.
/* shut down our window */
Deinitialize();
glutDestroyWindow(window);

/* exit the program...normal termination. */
exit(1);
break; // redundant.

case 56 : subheight++; printf("suhbeight=%d \n",subheight); IncludeSubPicture(); break;
case 50 : subheight--; printf("suhbeight=%d \n",subheight); IncludeSubPicture(); break;

default:
printf ("Key %d pressed. No action there yet.\n", key);
break;
}

glutPostRedisplay();
}

void specialKeyPressed(int key, int x, int y)
{
/* avoid thrashing this procedure */
usleep(100);

switch (key) {

case GLUT_KEY_UP: // decrease x rotation speed;
subheight++;
break;

case GLUT_KEY_DOWN: // increase x rotation speed;
subheight--;
break;

default:
break;
}
}

int main(int argc, char **argv)
{
/* Initialize GLUT state - glut will take any command line arguments that pertain to it or
X Windows - look at its documentation at http://reality.sgi.com/mjk/spec3/spec3.html */
glutInit(&amp;argc, argv);

/* Select type of Display mode:
Double buffer
RGBA color
Alpha components supported
Depth buffer */
glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_ALPHA | GLUT_DEPTH);

/* get a 640 x 480 window */
glutInitWindowSize(640, 480);

/* the window starts at the upper left corner of the screen */
glutInitWindowPosition(0, 0);

/* Open a window */
window = glutCreateWindow("YLP : first test with DevIL");

/* Register the function to do all our OpenGL drawing. */
glutDisplayFunc(&amp;DrawTest);


/* Register the function called when our window is resized. */
glutReshapeFunc(&amp;ReSizeGLScene);

/* Register the function called when the keyboard is pressed. */
glutKeyboardFunc(&amp;keyPressed);

glutSpecialFunc(&amp;specialKeyPressed);

/* Initialize our window. */
InitGL(640, 480);

/* Start Event Processing Engine */
glutMainLoop();

return 1;
}



(it use the numerics 8 and 2 keys for to change the subpicture height)

=> in first view, textures seem zoomed ... :(

The Little Body
05-01-2011, 07:58 AM
I can't use ilu functions :(
(I have only /usr/local/IL/il.h and devil_cpp_wrapper.hpp files)

This can to be the problem ???

SpiderPig500
05-02-2011, 01:25 PM
I will take a look at the code over the next few days and see what I come up with, thanks (I've got a lot on). :)

SpiderPig500
05-03-2011, 02:52 AM
I can't use ilu functions frown
(I have only /usr/local/IL/il.h and devil_cpp_wrapper.hpp files)

This can to be the problem ???

What do you mean you can't use ilu functions? You mean you haven't got the .h file or they're just not working?

I have tried to compile the last set of code you posted, but it seems my computer does not have -

#include <unistd.h>
any where on its system. Do you know where I can get this header from?

Thanks,

Alfonse Reinheart
05-03-2011, 02:56 AM
If you don't have unistd.h, then you don't have POSIX. Which means you also won't have the POSIX library that the header references. So finding the header will do you no good.

SpiderPig500
05-03-2011, 02:59 AM
Okay then. So how do I use the functions that require the header unistd.h? Is there a replacement header somewhere in Windows?

Alfonse Reinheart
05-03-2011, 03:13 AM
So how do I use the functions that require the header unistd.h?

You don't; The Little Body gave you Linux-specific code. For some reason.

You should remove the line including the header from the source code. Then try to compile it. The compiler will complain about some functions that don't exist. Delete every line the compiler complains about.

SpiderPig500
05-03-2011, 03:19 AM
Told the compiler to ignore the header and it's functions - and the whole program goes wild with errors. I'm going to have to reorganize the code a lot if I'm going to get it to work.

@The Little Boy - Maybe Linux is why it isn't working for you....