fabio.nb
December 2nd, 2005, 09:53 PM
Hi, I am having a problem with SetDIBitsToDevice function. Let me explain my situation:
I collect images from web cam (using DirectShow) and files (using libtiff http://www.remotesensing.org/libtiff/ and bmp http://www.csit.fsu.edu/~burkardt/cpp_src/bmp_io/bmp_io.html).
Well, I store the color information in a class called CorRGB, that contains the red, green and blue factor. And the imagem is stored in an CorRGB array with 3 * width * height positions. For To make easier the use of SetDIBitsToDevice function, I store the colors in the array at this way:
Suppose an image 2 (width) x 4 (height):
1 2
3 4
5 6
7 8
My array will be like this:
7B 7G 7R 8B 8G 8R 5B 5G 5R 6B 6G 6R 3B 3G 3R 4B 4G 4R 1B 1G 1R 2B 2G 2R
How we can see, I store the imagem starting from bottom to up and left to right, with the first component blue, green and finally red.
The problem is when I load an image from a file. Look this image:
http://www.myjavaserver.com/~jonlordbr/Problem1.png
The first image was taken from the web cam, and it seems alright. The second I load from a tif file and the last from a bmp file.
The code to draw this images is:
// Set the pixel colors in DC according to the buffer
SetDIBitsToDevice(hdc, x, y, largura, altura, 0, 0, 0, altura, imagem, &info, DIB_RGB_COLORS);
I tested my buffer to see if it is right:
http://www.myjavaserver.com/~jonlordbr/SetPixel.png
// Pass through the lines
for (int i = 0; i < altura; i++) {
// Pass through the columns
for (int j = 0; j < largura; j++) {
// Get the pixel color
CorRGB cor = getPixel(j, i);
// Set the pixel color in DC
SetPixel(hdc, x + j, y + i, RGB(cor.getR(), cor.getG(), cor.getB()));
}
}
How we can see, the imagem was loaded correctly.
The second test I have done, I tried to put the images in a new buffer and draw at the top the buffer with SetDIBitsToDevice function and at bottom with SetPixel:
http://www.myjavaserver.com/~jonlordbr/Problem2.png
BYTE* buffer = new BYTE[4 * largura * altura];
// If width and height are greater than 0
if (largura > 0 && altura > 0) {
// Clean up the memory
memset(buffer, 0, 4 * largura * altura);
int atual = 0;
// Pass through the lines
for (int i = 0; i < altura; i++) {
// Pass through the columns
for (int j = 0; j < largura; j++) {
// Get the pixel color
CorRGB cor = getPixel(j, altura - i - 1);
// Set the pixel color in buffer
buffer[atual++] = cor.getB();
buffer[atual++] = cor.getG();
buffer[atual++] = cor.getR();
// Set the pixel color in DC
SetPixel(hdc, x + j, altura - 1 - y - i + 240 + 10, RGB(cor.getR(), cor.getG(), cor.getB()));
}
}
// Set the pixel colors in DC according to the buffer
SetDIBitsToDevice(hdc, x, y, largura, altura, 0, 0, 0, altura, buffer, &info, DIB_RGB_COLORS);
}
This test showed us that the images buffer is correct (the bottom images), but with the use of SetDIBitsToDevice is incorrect for the images loaded from files.
The last test was that:
http://www.myjavaserver.com/~jonlordbr/Problem3.png
BYTE* buffer = new BYTE[4 * largura * altura];
// If width and height are greater than 0
if (largura > 0 && altura > 0) {
// Clean up the memory
memset(buffer, 0, 4 * largura * altura);
int atual = 0;
// Pass through the lines
for (int i = 0; i < altura; i++) {
// Pass through the columns
for (int j = 0; j < largura; j++) {
// Get the pixel color
CorRGB cor = getPixel(j, altura - i - 1);
// Set the pixel color in buffer
buffer[atual++] = cor.getB();
buffer[atual++] = cor.getG();
buffer[atual++] = cor.getR();
// Set the pixel color in DC
SetPixel(hdc, x + j, altura - 1 - y - i + 240 + 10, RGB(cor.getR(), cor.getG(), cor.getB()));
}
// Skip 2 positions in buffer
atual += 2;
}
// Set the pixel colors in DC according to the buffer
SetDIBitsToDevice(hdc, x, y, largura, altura, 0, 0, 0, altura, buffer, &info, DIB_RGB_COLORS);
}
Well, this test looks a bit strange. I skiped 2 buffer positions at the end of each row. The images loaded from files are ok now, otherwise the image taken from webcam is wrong.
I don't know what I am doing wrong. One thing is certain, the images loaded from webcams and files are taken and stored correctly. But with the use of the SetDIBitsToDevice the images don't appears correctly.
Please, someone could help me?
Thanks
I collect images from web cam (using DirectShow) and files (using libtiff http://www.remotesensing.org/libtiff/ and bmp http://www.csit.fsu.edu/~burkardt/cpp_src/bmp_io/bmp_io.html).
Well, I store the color information in a class called CorRGB, that contains the red, green and blue factor. And the imagem is stored in an CorRGB array with 3 * width * height positions. For To make easier the use of SetDIBitsToDevice function, I store the colors in the array at this way:
Suppose an image 2 (width) x 4 (height):
1 2
3 4
5 6
7 8
My array will be like this:
7B 7G 7R 8B 8G 8R 5B 5G 5R 6B 6G 6R 3B 3G 3R 4B 4G 4R 1B 1G 1R 2B 2G 2R
How we can see, I store the imagem starting from bottom to up and left to right, with the first component blue, green and finally red.
The problem is when I load an image from a file. Look this image:
http://www.myjavaserver.com/~jonlordbr/Problem1.png
The first image was taken from the web cam, and it seems alright. The second I load from a tif file and the last from a bmp file.
The code to draw this images is:
// Set the pixel colors in DC according to the buffer
SetDIBitsToDevice(hdc, x, y, largura, altura, 0, 0, 0, altura, imagem, &info, DIB_RGB_COLORS);
I tested my buffer to see if it is right:
http://www.myjavaserver.com/~jonlordbr/SetPixel.png
// Pass through the lines
for (int i = 0; i < altura; i++) {
// Pass through the columns
for (int j = 0; j < largura; j++) {
// Get the pixel color
CorRGB cor = getPixel(j, i);
// Set the pixel color in DC
SetPixel(hdc, x + j, y + i, RGB(cor.getR(), cor.getG(), cor.getB()));
}
}
How we can see, the imagem was loaded correctly.
The second test I have done, I tried to put the images in a new buffer and draw at the top the buffer with SetDIBitsToDevice function and at bottom with SetPixel:
http://www.myjavaserver.com/~jonlordbr/Problem2.png
BYTE* buffer = new BYTE[4 * largura * altura];
// If width and height are greater than 0
if (largura > 0 && altura > 0) {
// Clean up the memory
memset(buffer, 0, 4 * largura * altura);
int atual = 0;
// Pass through the lines
for (int i = 0; i < altura; i++) {
// Pass through the columns
for (int j = 0; j < largura; j++) {
// Get the pixel color
CorRGB cor = getPixel(j, altura - i - 1);
// Set the pixel color in buffer
buffer[atual++] = cor.getB();
buffer[atual++] = cor.getG();
buffer[atual++] = cor.getR();
// Set the pixel color in DC
SetPixel(hdc, x + j, altura - 1 - y - i + 240 + 10, RGB(cor.getR(), cor.getG(), cor.getB()));
}
}
// Set the pixel colors in DC according to the buffer
SetDIBitsToDevice(hdc, x, y, largura, altura, 0, 0, 0, altura, buffer, &info, DIB_RGB_COLORS);
}
This test showed us that the images buffer is correct (the bottom images), but with the use of SetDIBitsToDevice is incorrect for the images loaded from files.
The last test was that:
http://www.myjavaserver.com/~jonlordbr/Problem3.png
BYTE* buffer = new BYTE[4 * largura * altura];
// If width and height are greater than 0
if (largura > 0 && altura > 0) {
// Clean up the memory
memset(buffer, 0, 4 * largura * altura);
int atual = 0;
// Pass through the lines
for (int i = 0; i < altura; i++) {
// Pass through the columns
for (int j = 0; j < largura; j++) {
// Get the pixel color
CorRGB cor = getPixel(j, altura - i - 1);
// Set the pixel color in buffer
buffer[atual++] = cor.getB();
buffer[atual++] = cor.getG();
buffer[atual++] = cor.getR();
// Set the pixel color in DC
SetPixel(hdc, x + j, altura - 1 - y - i + 240 + 10, RGB(cor.getR(), cor.getG(), cor.getB()));
}
// Skip 2 positions in buffer
atual += 2;
}
// Set the pixel colors in DC according to the buffer
SetDIBitsToDevice(hdc, x, y, largura, altura, 0, 0, 0, altura, buffer, &info, DIB_RGB_COLORS);
}
Well, this test looks a bit strange. I skiped 2 buffer positions at the end of each row. The images loaded from files are ok now, otherwise the image taken from webcam is wrong.
I don't know what I am doing wrong. One thing is certain, the images loaded from webcams and files are taken and stored correctly. But with the use of the SetDIBitsToDevice the images don't appears correctly.
Please, someone could help me?
Thanks