Editing Bits in CImage  
Author Message
veryhotsausage





PostPosted: Visual C++ General, Editing Bits in CImage Top

Hi,
Could anyone shed some light on how to best convert 2D arrays into Images for display onscreen and back again Currently, I am using the CImage class but is running into problems, It'll be good to know about other simple to use classes that can convert a Bitmap object that can easily be displayed on the screen to the form:

int[width][height] red, blue, green;



void CChildView::GetBits(CImage* image)
{
const int maxY = image->GetHeight(), maxX = image->GetWidth();
__int8 *byte_value = (__int8*)image->GetBits();
int bitsperpixel = image->GetBPP();
int imagepitch = image->GetPitch();

int MaxColors = image->GetMaxColorTableEntries();
RGBQUAD* ColorTable = new RGBQUAD[MaxColors];

int steps = (24/8);
int*pixeldata = new int[maxY*maxX];

int value;
if(imagepitch < 0)
{
for(int h = 0; h < maxY-1; h++)
{
for(int i = 0; i < maxX; i++)
{
pixeldata[h*maxY+i] = 0;

for(int j = 0; j < steps; j++)
{
value = *(byte_value+h*imagepitch-i*steps-j);
pixeldata[h*maxY+i] += value<<8*j;
}
}
}
}
}

The bitmap that I am extracting the pixels from is 24 bits long. In my code,becuase every pixel was 24 bits long, I had to iterate a 8 bit pointer 3 times to read one pixel, and even then I still don't know what I'm reading out.

I don't know anymore about the information that is contained in the CImage class that would tell me more about how those bits are seperated into RGB, whether they are big or little endian. Also, the CImage::GetPitch() returns a negative value. What significance does that have apart from having to go backwards when addressing the array pointer

I am thinking of making a struct 3 bytes long to read in the data, but I don't know what is contained in the 24 bits.

What I really want to do is to make 3 2D arrays representing Red, Green and Blue for manipulation of the Image and back again. How would I be able to do that quickly with the GetBits() method in CImage Or any other way apart from GetPixel(int x, int y) which in my opinion is a very slow method of editing a picture.






Visual C++11  
 
 
Sarita Bafna





PostPosted: Visual C++ General, Editing Bits in CImage Top

Hi,

A negative pitch value only indicates that it is a bottom-up DIB and its origin is the lower left corner. .Now since your each pixel is 24 bits long; each byte represents the relative intensities of blue, green and red respectively for each image.

The easiest and quickest way here could be:

1. Get the pointer to the pixel buffer using GetBits along with the other data (like width, height, BPP, pitch etc)

2. Calculate size of the image in bytes.

3. Then create three arrays blue, green, red of size (size of image in bytes/3)

4. Start reading byte wise from the buffer and store them in the respective arrays (for example byte 1 in the blue array, byte 2 in the green array and byte 3 in the red array, again byte 4 in the blue array, byte 5 in the green and so on)

5. It will be easy from here to get the data for pixel (x, y) and also to reconstruct.

I am a little unsure what you are trying to achieve especially in the latter part of the code:

value = *(byte_value+h*imagepitch-i*steps-j);

pixeldata[h*maxY+i] += value<<8*j;

Thanks

Sarita Bafna

Visual C++ Team


 
 
Peter Blanchfield





PostPosted: Visual C++ General, Editing Bits in CImage Top

You did not say anything that is not in the library. If you use get bits on a 24 bit image the pitch will be the image width. If you are using a COLORREF array to store the data why will the image not be just imageWidth * imageHeight colorref variables in size I know its not because it just throws a memory exception if you assume this
 
 
Rafal Struzyk





PostPosted: Visual C++ General, Editing Bits in CImage Top

You might try my simple performance optimization wrapper for CImage that does just what I think you need - it lets you use GetPixel and SetPixel as you normally would with the CImage object but with the performance of direct bits access.

Hope it helps.