Having trouble with C++ Bitmaps - c++

I am attempting to produce code that yields an image with 1 pixel set to black, the problem is that the output is 8 horizontal pixels of black when I attempt to set a single pixel to black. I also attempted to set the pixel above to black, but it resulted in a horizontal line 8 pixels above the original.
So my question is, how can I change my code so I end up working with bits instead of bytes in my image? I just want to be able to set a single pixel to black instead of a byte of pixels.
Here is my code:
#include <iostream>
#include <fstream>
#include "windows.h"
using namespace std;
#define IMAGE_SIZE 256
int main(int argc, char* argv[])
{
BITMAPFILEHEADER bmfh;
BITMAPINFOHEADER bmih;
char colorTable[8] = { 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff };
char bits[IMAGE_SIZE][IMAGE_SIZE];
ofstream bmpOut("foo.bmp", ios::out + ios::binary);
if (!bmpOut) {
cout << "could not open file.";
return -1;
}
bmfh.bfType = 0x4d42;
bmfh.bfReserved1 = 0;
bmfh.bfReserved2 = 0;
bmfh.bfOffBits = sizeof(bmfh) + sizeof(bmih) + sizeof(colorTable);
bmfh.bfSize = bmfh.bfOffBits + sizeof(bits);
bmih.biSize = 40;
bmih.biWidth = IMAGE_SIZE;
bmih.biHeight = IMAGE_SIZE;
bmih.biPlanes = 1;
bmih.biBitCount = 1;
bmih.biCompression = 0;
bmih.biSizeImage = 0;
bmih.biXPelsPerMeter = 2835;
bmih.biYPelsPerMeter = 2835;
bmih.biClrUsed = 0;
bmih.biClrImportant = 0;
// Here I am initializing a white background for the bitmap image
for (int i = 0; i < IMAGE_SIZE; i++) {
for (int j = 0; j < IMAGE_SIZE; j++) {
bits[i][j] = 255;
}
}
// Here I attempt to set the most bottom left pixel to black and the pixel above it to black as well
bits[0][0] = 0;
bits[1][0] = 0;
char* workPtr;
workPtr = (char*)&bmfh;
bmpOut.write(workPtr, 14);
workPtr = (char*)&bmih;
bmpOut.write(workPtr, 40);
workPtr = &colorTable[0];
bmpOut.write(workPtr, 8);
workPtr = &bits[0][0];
bmpOut.write(workPtr, IMAGE_SIZE*IMAGE_SIZE);
bmpOut.close();
system("mspaint foo.bmp");
return 0;
}
Here is a link to the bitmap image produced:
Scaled up for clarity
Thanks

Based on BITMAPINFOHEADER documentation it looks like you are setting your bitmap to be read as monochrome by setting bmih.biBitCount = 1. This way every pixel is represented by one bit (black or white only), not byte. Thus, to set bottom-left pixel to black, you need to do bits[0][0] = 127 (127 = 0x7F = 01111111b). To set one above it, I think you would need to do bits[1][0] = 127. In this case I also believe
char bits[IMAGE_SIZE][IMAGE_SIZE/8];
...
for (int i = 0; i < IMAGE_SIZE; i++) {
for (int j = 0; j < IMAGE_SIZE/8; j++) {
bits[i][j] = 255;
}
}
...
workPtr = &bits[0][0];
bmpOut.write(workPtr, IMAGE_SIZE*(IMAGE_SIZE/8));
would be enough.
If you intended to use different color palette, you should set different biBitCount, as described in the docs I linked to at the beginning, I guess you would want bmih.biBitCount = 8 as a healthy default for your code.

Related

Capture mouse cursor and apply transparency masking in Win32

Im trying to capture mouse cursor using windows API GetCursorInfo and taking in to CURSORINFO structure after I reading ICONINFO using GetIconInfo so I will get hbmMask and hbmColor bitmap
The hbmMask bitmap is first applied with an AND raster operation, then the hbmColor bitmap is applied with an XOR raster operation. This results in an opaque cursor and a transparent background but this is not happening in my POC complete code in below example.
After doing AND raster on mask data and XOR on color data the final result will be cursor and covered with white colored rectangle 32*32.
void save_as_bitmap(unsigned char *bitmap_data, int rowPitch, int width, int height, char *filename)
{
// A file is created, this is where we will save the screen capture.
FILE *f;
BITMAPFILEHEADER bmfHeader;
BITMAPINFOHEADER bi;
bi.biSize = sizeof(BITMAPINFOHEADER);
bi.biWidth = width;
//Make the size negative if the image is upside down.
bi.biHeight = -height;
//There is only one plane in RGB color space where as 3 planes in YUV.
bi.biPlanes = 1;
//In windows RGB, 8 bit - depth for each of R, G, B and alpha.
bi.biBitCount = 32;
//We are not compressing the image.
bi.biCompression = BI_RGB;
// The size, in bytes, of the image. This may be set to zero for BI_RGB bitmaps.
bi.biSizeImage = 0;
bi.biXPelsPerMeter = 0;
bi.biYPelsPerMeter = 0;
bi.biClrUsed = 0;
bi.biClrImportant = 0;
// rowPitch = the size of the row in bytes.
DWORD dwSizeofImage = rowPitch * height;
// Add the size of the headers to the size of the bitmap to get the total file size
DWORD dwSizeofDIB = dwSizeofImage + sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER);
//Offset to where the actual bitmap bits start.
bmfHeader.bfOffBits = (DWORD)sizeof(BITMAPFILEHEADER) + (DWORD)sizeof(BITMAPINFOHEADER);
//Size of the file
bmfHeader.bfSize = dwSizeofDIB;
//bfType must always be BM for Bitmaps
bmfHeader.bfType = 0x4D42; //BM
// TODO: Handle getting current directory
fopen_s(&f, filename, "wb");
DWORD dwBytesWritten = 0;
dwBytesWritten += fwrite(&bmfHeader, sizeof(BITMAPFILEHEADER), 1, f);
dwBytesWritten += fwrite(&bi, sizeof(BITMAPINFOHEADER), 1, f);
dwBytesWritten += fwrite(bitmap_data, 1, dwSizeofImage, f);
fclose(f);
}
//ST484 : HBIMAPtoBYTE : Convert BITMAP to BYTE array.
std::vector<BYTE> HBIMAPtoBYTE( HBITMAP hBitmap,
int &hBitmapSize,
bool &bResult,
int &nWidth,
int &nHeight )
{
bResult = true;
BITMAP bmp;
if (!GetObject(hBitmap, sizeof(BITMAP), (LPVOID)&bmp))
{
DeleteObject(hBitmap);
bResult = false;
}
int rpcbiPlanes = 32;
BITMAPINFO info;
memset(&info, 0, sizeof(BITMAPINFO));
info.bmiHeader.biSize = sizeof(info.bmiHeader);
info.bmiHeader.biWidth = bmp.bmWidth;
info.bmiHeader.biHeight = -bmp.bmHeight;
info.bmiHeader.biPlanes = 1;
info.bmiHeader.biBitCount = rpcbiPlanes;
info.bmiHeader.biCompression= BI_RGB;
size_t pixelSize = info.bmiHeader.biBitCount / 8;
size_t scanlineSize = (pixelSize * info.bmiHeader.biWidth + 3) & ~3;
size_t bitmapSize = bmp.bmHeight * scanlineSize;
hBitmapSize = bitmapSize;
nWidth = bmp.bmWidth;
nHeight = bmp.bmHeight;
std::vector<BYTE> pixels(bitmapSize);
HDC hdc = ::GetDC(NULL);
if(!GetDIBits(hdc, hBitmap, 0, bmp.bmHeight, &pixels[0], &info, DIB_RGB_COLORS))
{
hBitmapSize = 0;
bResult = false;
}
return pixels;
}
// getHCursor : Capture cursor.
CURSORINFO getHCursor()
{
CURSORINFO cursorInfo;
cursorInfo.cbSize = sizeof(CURSORINFO);
if (GetCursorInfo(&cursorInfo) == 0)
{
MessageBox(NULL, _T("Exception : GetCursorInfo creation failed"),_T("message"),MB_OK|MB_SYSTEMMODAL);
cursorInfo.hCursor = NULL;
return cursorInfo;
}
return cursorInfo;
}
//Main Call
int _tmain(int argc, _TCHAR* argv[])
{
int CountP = 0;
while (true)
{
CURSORINFO CursorInfo = getHCursor();
if (CursorInfo.hCursor == NULL)
{
::Sleep(MinSleep);
continue;
}
ICONINFO iconInfo;
if (!GetIconInfo(CursorInfo.hCursor, &iconInfo))
{
MessageBox(NULL, _T("Exception : GetIconInfo creation failed"),_T("message"),MB_OK|MB_SYSTEMMODAL);
::Sleep(MinSleep);
}
std::vector<BYTE> bColorBitmap;
std::vector<BYTE> bMaskBitmap;
std::vector<BYTE> bDestBitmap;
int sz_hbmColor = 0;
int sz_hbmMask = 0;
int sz_hbDest = 0;
int nWidth = 0;
int nHeight = 0;
bool hbmColor_result = false;
bool hbmMask_result = false;
bool hbmDest_result = false;
int rpcbiPlanes = 32;
bool isColorShape = (iconInfo.hbmColor != NULL);
// read mask and color in to byte vector.
bColorBitmap = HBIMAPtoBYTE(iconInfo.hbmColor,sz_hbmColor,hbmColor_result,nWidth,nHeight);
bMaskBitmap = HBIMAPtoBYTE(iconInfo.hbmMask,sz_hbmMask,hbmMask_result,nWidth,nHeight);
//Create Dummy bitmap using width and height filled with black color.
HBITMAP desBitmap = CreateBitmap(nWidth,nHeight,1,rpcbiPlanes,NULL);
if(desBitmap != NULL)
{
// read dummy bitmap in to byte vector.
bDestBitmap = HBIMAPtoBYTE(desBitmap,sz_hbDest,hbmDest_result,nWidth,nHeight);
}
//the mask bitmap is first applied with an AND raster operation.
for(int i = 0; i < nHeight ; i++)
{
for(int j = 0; j < nWidth; j++)
{
bDestBitmap[i*4*nWidth + j*4 ] &= bMaskBitmap[i*4*nWidth + j*4 ];
bDestBitmap[i*4*nWidth + j*4 + 1] &= bMaskBitmap[i*4*nWidth + j*4 + 1];
bDestBitmap[i*4*nWidth + j*4 + 2] &= bMaskBitmap[i*4*nWidth + j*4 + 2];
bDestBitmap[i*4*nWidth + j*4 + 3] &= bMaskBitmap[i*4*nWidth + j*4 + 3];
}
}
//then the color bitmap is applied with an XOR raster operation.
for(int i = 0; i < nHeight ; i++)
{
for(int j = 0; j < nWidth; j++)
{
bDestBitmap[i*4*nWidth + j*4 ] ^= bColorBitmap[i*4*nWidth + j*4 ];
bDestBitmap[i*4*nWidth + j*4 + 1] ^= bColorBitmap[i*4*nWidth + j*4 + 1];
bDestBitmap[i*4*nWidth + j*4 + 2] ^= bColorBitmap[i*4*nWidth + j*4 + 2];
bDestBitmap[i*4*nWidth + j*4 + 3] ^= bColorBitmap[i*4*nWidth + j*4 + 3];
}
}
//Save Color bitmap.
sprintf_s(file_name,"C:\\Test\\captured\\Cursor_%d.bmp", CountP);
save_as_bitmap(&(bDestBitmap[0]), nWidth*4, nWidth, nHeight, file_name);
CountP++;
Sleep(MaxSleep);
}
return 0;
}
After saving cursor getting image like this
Finally I found excellent result using windows API.
Include comctl32.lib in to your project
HIMAGELIST iCursorList=ImageList_Create(nWidth,nHeight,ILC_COLOR32|ILC_MASK,8,8);
ImageList_AddMasked(iCursorList,iconInfo.hbmColor,000000);
DeleteObject(iconInfo.hbmColor);
TRANSPARENT_HICON = ImageList_GetIcon(iCursorList,0,ILD_TRANSPARENT);
1 ) Create ImageList_Create using width, height, ILC_COLOR32|ILC_MASK
2 ) Add bitmap in to ImageList_AddMasked and apply which color you want to make transparent, 000000 are black color in my code.
3 ) Get transparent icon from ImageList_GetIcon.

Changing monochrome pattern for bitmap file using current loop

I am trying to change the pattern of the current bitmap file but I am having trouble changing my nested for loop to do this. I am trying to get a bitmap file with 8 horizontal bars, 32 pixels in height, alternating black and white. What I currently get now are 64 vertical bars alternating black and white. The bitmap image dimensions are 256 pixels by 256 pixels.
I have messed around with the nested for loop in my code that is responsible for storing the color white (0x0f) in my multidimensional bits array. I have noticed that if I change the white to black (0x0f to 0x00) the entire bitmap file turns black. Below is the part of the code I am focusing on to output the pattern, under that I have the entire code.
// Build monochrome array of bits in image
for (int i = 0; i < IMAGE_SIZE; i++) {
for (int j = 0; j < IMAGE_SIZE / 8; j++) {
bits[i][j] = 0x0f;
}
}
#include <iostream>
#include <fstream>
#include "windows.h"
using namespace std;
// The following defines the size of the square image in pixels.
#define IMAGE_SIZE 256
int main(int argc, char* argv[])
{
BITMAPFILEHEADER bmfh;
BITMAPINFOHEADER bmih;
char colorTable[8] = { 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff };
// The following defines the array which holds the image. The row length
// (number of columns) is the height divided by 8, since there are 8 bits
// in a byte.
char bits[IMAGE_SIZE][IMAGE_SIZE / 8];
// Define and open the output file.
ofstream bmpOut("foo.bmp", ios::out + ios::binary);
if (!bmpOut) {
cout << "...could not open file, ending.";
return -1;
}
// Initialize the bit map file header with static values.
bmfh.bfType = 0x4d42;
bmfh.bfReserved1 = 0;
bmfh.bfReserved2 = 0;
bmfh.bfOffBits = sizeof(bmfh) + sizeof(bmih) + sizeof(colorTable);
bmfh.bfSize = bmfh.bfOffBits + sizeof(bits);
// Initialize the bit map information header with static values.
bmih.biSize = 40;
bmih.biWidth = IMAGE_SIZE;
bmih.biHeight = IMAGE_SIZE;
bmih.biPlanes = 1;
bmih.biBitCount = 1;
bmih.biCompression = 0;
bmih.biSizeImage = 0;
bmih.biXPelsPerMeter = 2835; // magic number, see Wikipedia entry
bmih.biYPelsPerMeter = 2835;
bmih.biClrUsed = 0;
bmih.biClrImportant = 0;
// Build monochrome array of bits in image
for (int i = 0; i < IMAGE_SIZE; i++) {
for (int j = 0; j < IMAGE_SIZE / 8; j++) {
bits[i][j] = 0x0f;
}
}
// Write out the bit map.
char* workPtr;
workPtr = (char*)&bmfh;
bmpOut.write(workPtr, 14);
workPtr = (char*)&bmih;
bmpOut.write(workPtr, 40);
workPtr = &colorTable[0];
bmpOut.write(workPtr, 8);
workPtr = &bits[0][0];
bmpOut.write(workPtr, IMAGE_SIZE*IMAGE_SIZE / 8);
bmpOut.close();
// showing result
system("mspaint foo.bmp");
// Done.
return 0;
}
You are not populating your array of bits correctly.
You are creating a 1-bit (monochrome) bottom-up bitmap. Every individual bit in your array represents a different pixel, where a 0 bit refers to the first color and a 1 bit refers to the second color in your color table. And the first row in the bitmap image is the last row in the array, and the last row in the bitmap image is the first row in the array.
0x0F hex is 00001111 binary. You are setting every 8-bit char in your array to 0x0f, so you are creating an alternating pattern of 4 0000 bits followed by 4 1111 bits for every row, eg:
00001111 00001111 00001111 ... (for 29 more bytes)
00001111 00001111 00001111 ... (for 29 more bytes)
00001111 00001111 00001111 ... (for 29 more bytes)
... (for 253 more rows)
For a 256x256 bitmap, that produces 64 alternating vertical bars that are each 4 pixels wide.
To get the effect you want - 8 alternating horizontal bars that are each 32 pixels high - you need to set every bit in a given row to either all 0s or all 1s, and then you need to alternate that pattern in groups of 32 complete rows, eg:
00000000 00000000 00000000 ... (for 29 more bytes)
00000000 00000000 00000000 ... (for 29 more bytes)
00000000 00000000 00000000 ... (for 29 more bytes)
... (for 29 more rows)
11111111 11111111 11111111 ... (for 29 more bytes)
11111111 11111111 11111111 ... (for 29 more bytes)
11111111 11111111 11111111 ... (for 29 more bytes)
... (for 29 more rows)
... (repeat the above 4 more times)
Try something more like this instead:
// Build monochrome array of bits in image
bool isWhite = false;
for (int i = 0; i < IMAGE_SIZE; ) {
char ch = isWhite ? 0xFF : 0x00;
int row = (IMAGE_SIZE - 1) - i; // use row = i for a top-down bitmap ...
for (int col = 0; col < (IMAGE_SIZE / 8); ++col) {
bits[row][col] = ch;
}
// alternatively to the above loop:
// memset(bits[row], isWhite ? 0xFF : 0x00, IMAGE_SIZE / 8);
if ((++i % 32) == 0) isWhite = !isWhite;
}
Or:
// Build monochrome array of bits in image
bool isWhite = true;
for (int i = 0; i < IMAGE_SIZE; ++i) {
if ((i % 32) == 0) isWhite = !isWhite;
int row = (IMAGE_SIZE - 1) - i; // use row = i for a top-down bitmap ...
char ch = isWhite ? 0xFF : 0x00;
for (int col = 0; col < (IMAGE_SIZE / 8); ++col) {
bits[row][col] = ch;
}
// alternatively to the above loop:
// memset(bits[row], isWhite ? 0xFF : 0x00, IMAGE_SIZE / 8);
}
Or:
// Build monochrome array of bits in image
for (int i = 0; i < IMAGE_SIZE; ++i) {
char ch = ((i % 64) < 32) ? 0x00 : 0xFF;
int row = (IMAGE_SIZE - 1) - i; // use row = i for a top-down bitmap ...
for (int col = 0; col < IMAGE_SIZE / 8; ++col) {
bits[row][col] = ch;
}
// alternatively to the above loop:
// memset(bits[row], ((i % 64) < 32) ? 0x00 : 0xFF, IMAGE_SIZE / 8);
}
That being said, I would suggest a few additional tweaks to the rest of your code:
#include <iostream>
#include <fstream>
#include <windows.h>
//#include <string.h> // if using memset() above...
// The following defines the size of the square image in pixels.
#define IMAGE_SIZE 256
// The following defines the size of each row in bytes.
#define BYTES_PER_ROW (IMAGE_SIZE / sizeof(BYTE))
int main()
{
// Define and open the output file.
std::ofstream bmpOut("foo.bmp", std::ios::binary);
if (!bmpOut)
{
std::cerr << "could not open file, ending.";
return -1;
}
BITMAPFILEHEADER bmfh;
BITMAPINFOHEADER bmih;
RGBQUAD colorTable[2] = { {0x00,0x00,0x00,0x00}, {0xFF,0xFF,0xFF,0x00} };
// The following defines the array which holds the image bits. The row length
// (number of columns) is the height divided by 8, since there are 8 bits
// in a byte.
BYTE bits[IMAGE_SIZE][BYTES_PER_ROW];
// Initialize the bitmap file header with static values.
bmfh.bfType = 0x4d42;
bmfh.bfReserved1 = 0;
bmfh.bfReserved2 = 0;
bmfh.bfOffBits = sizeof(bmfh) + sizeof(bmih) + sizeof(colorTable);
bmfh.bfSize = bmfh.bfOffBits + sizeof(bits);
// Initialize the bitmap information header with static values.
bmih.biSize = sizeof(bmih);
bmih.biWidth = IMAGE_SIZE;
bmih.biHeight = IMAGE_SIZE; // positive for bottom-up, negative for top-down
bmih.biPlanes = 1;
bmih.biBitCount = 1;
bmih.biCompression = BI_RGB;
bmih.biSizeImage = 0;
bmih.biXPelsPerMeter = 2835; // magic number, see Wikipedia entry
bmih.biYPelsPerMeter = 2835;
bmih.biClrUsed = 0;
bmih.biClrImportant = 0;
// Build monochrome array of bits in image, see above...
// Write out the bitmap.
bmpOut.write(reinterpret_cast<char*>(&bmfh), sizeof(bmfh));
bmpOut.write(reinterpret_cast<char*>(&bmih), sizeof(bmih));
bmpOut.write(reinterpret_cast<char*>(&colorTable), sizeof(colorTable));
bmpOut.write(reinterpret_cast<char*>(&bits), sizeof(bits));
if (!bmpOut)
{
std::cerr << "could not write file, ending.";
return -1;
}
bmpOut.close();
// showing result
ShellExecuteA(NULL, NULL, "foo.bmp", NULL, NULL, SW_SHOW);
// Done.
return 0;
}

Create monochrome BMP from bitset

I could need some help to figure out how to feed the proc below. I need to write a monochrome BMP file. The code below (its from: How to Save monochrome Image as bmp in windows C++ ?) looks like to be able to do this. I'm now stuck on how to convert a std::bitset or preferably boost::dynamic_bitset into this byte* format. All of my attempts so far failed, I wasn't able to write something like an 8x8 checker pattern into the BMP. The proc creates the BMP and it is readable by Photoshop, but the content is a mess. So any suggestions how to solve this are appreciated!
Save1BppImage(byte* ImageData, const char* filename, long w, long h){
int bitmap_dx = w; // Width of image
int bitmap_dy = h; // Height of Image
// create file
std::ofstream file(filename, std::ios::binary | std::ios::trunc);
if(!file) return;
// save bitmap file headers
BITMAPFILEHEADER fileHeader;
BITMAPINFOHEADER * infoHeader;
infoHeader = (BITMAPINFOHEADER*) malloc(sizeof(BITMAPINFOHEADER) );
RGBQUAD bl = {0,0,0,0}; //black color
RGBQUAD wh = {0xff,0xff,0xff,0xff}; // white color
fileHeader.bfType = 0x4d42;
fileHeader.bfSize = 0;
fileHeader.bfReserved1 = 0;
fileHeader.bfReserved2 = 0;
fileHeader.bfOffBits = sizeof(BITMAPFILEHEADER) + (sizeof(BITMAPINFOHEADER));
infoHeader->biSize = (sizeof(BITMAPINFOHEADER) );
infoHeader->biWidth = bitmap_dx;
infoHeader->biHeight = bitmap_dy;
infoHeader->biPlanes = 1;
infoHeader->biBitCount = 1;
infoHeader->biCompression = BI_RGB; //no compression needed
infoHeader->biSizeImage = 0;
infoHeader->biXPelsPerMeter = 0;
infoHeader->biYPelsPerMeter = 0;
infoHeader->biClrUsed = 2;
infoHeader->biClrImportant = 2;
file.write((char*)&fileHeader, sizeof(fileHeader)); //write bitmapfileheader
file.write((char*)infoHeader, (sizeof(BITMAPINFOHEADER) )); //write bitmapinfoheader
file.write((char*)&bl,sizeof(bl)); //write RGBQUAD for black
file.write((char*)&wh,sizeof(wh)); //write RGBQUAD for white
int bytes = (w/8) * h ; //for example for 32X64 image = (32/8)bytes X 64 = 256;
file.write((const char*)ImageData, bytes);
file.close();
}
-edit-
an naive approach of mine was something like this
byte test[64];
for(unsigned int i=0; i<64; ++i)
if(i % 2)
test[i] = 0;
else
test[i] = 1;
Save1BppImage(test, "C:/bitmap.bmp", 8, 8);
The code you have is very close. Here are a few thoughts about where it might be off.
The bfOffBits value must include the size of the palette.
fileHeader.bfOffBits = sizeof(BITMAPFILEHEADER) + (sizeof(BITMAPINFOHEADER)) + 2*sizeof(RGBQUAD);
Some software may interpret 0 as white and 1 as black, regardless of what the palette says. Even though the file format allows you to go either way, you're better off specifying the palette in that order and inverting your bits if necessary.
Each row of a bitmap will start on a 4-byte boundary. If your bitmap width isn't a multiple of 32, you're going to need some padding between each row.
BMP files are ordered from the bottom row to the top row, which is backwards from the way most people organize their arrays.
The last two recommendations are combined to look something like this:
int bytes_in = (w + 7) / 8;
int bytes_out = ((w + 31) / 32) * 4;
const char * zeros[4] = {0, 0, 0, 0};
for (int y = h - 1; y >= 0; --y)
{
file.write(((const char *)ImageData) + (y * bytes_in), bytes_in);
if (bytes_out != bytes_in)
file.write(zeros, bytes_out - bytes_in);
}
Just for the archive, below the working version. It takes a boost bitset as input pixel storage.
void bitsetToBmp(boost::dynamic_bitset<unsigned char> bitset, const char* filename, int width, int height){
//write the bitset to file as 1-bit deep bmp
//bit order 0...n equals image pixels top left...bottom right, row by row
//the bitset must be at least the size of width*height, this is not checked
std::ofstream file(filename, std::ios::binary | std::ios::trunc);
if(!file) return;
// save bitmap file headers
BITMAPFILEHEADER fileHeader;
BITMAPINFOHEADER * infoHeader;
infoHeader = (BITMAPINFOHEADER*) malloc(sizeof(BITMAPINFOHEADER) );
RGBQUAD bl = {0,0,0,0}; //black color
RGBQUAD wh = {0xff,0xff,0xff,0xff}; // white color
fileHeader.bfType = 0x4d42;
fileHeader.bfSize = 0;
fileHeader.bfReserved1 = 0;
fileHeader.bfReserved2 = 0;
fileHeader.bfOffBits = sizeof(BITMAPFILEHEADER) + (sizeof(BITMAPINFOHEADER)) + 2*sizeof(RGBQUAD);
infoHeader->biSize = (sizeof(BITMAPINFOHEADER) );
infoHeader->biWidth = width;
infoHeader->biHeight = height;
infoHeader->biPlanes = 1;
infoHeader->biBitCount = 1;
infoHeader->biCompression = BI_RGB; //no compression needed
infoHeader->biSizeImage = 0;
infoHeader->biXPelsPerMeter = 0;
infoHeader->biYPelsPerMeter = 0;
infoHeader->biClrUsed = 2;
infoHeader->biClrImportant = 2;
file.write((char*)&fileHeader, sizeof(fileHeader)); //write bitmapfileheader
file.write((char*)infoHeader, (sizeof(BITMAPINFOHEADER) )); //write bitmapinfoheader
file.write((char*)&bl,sizeof(bl)); //write RGBQUAD for black
file.write((char*)&wh,sizeof(wh)); //write RGBQUAD for white
// convert the bits into bytes and write the file
int offset, numBytes = ((width + 31) / 32) * 4;
byte* bytes = (byte*) malloc(numBytes * sizeof(byte));
for(int y=height - 1; y>=0; --y){
offset = y * width;
memset(bytes, 0, (numBytes * sizeof(byte)));
for(int x=0; x<width; ++x)
if(bitset[offset++]){
bytes[x / 8] |= 1 << (7 - x % 8);
};
file.write((const char *)bytes, numBytes);
};
free(bytes);
file.close();
}
I wonder if theres a simpler/faster way to put the bits into a file? The whole bitset could instead be overhanded as array of rows to skip the subset extraction.
I have something very similiar...
This approach DOES NOT treat the padding of the BMP format. So You can only make bitmaps with width multiple of 4.
This is NOT a monochromatic bitmap. It's a RGB format, but you can tune it easily.
This is NOT an exactly answer to you, but for sure may be useful for you.
Enjoy it.
void createBitmap( byte * imageData, const char * filename, int width, int height )
{
BITMAPFILEHEADER bitmapFileHeader;
memset( &bitmapFileHeader, 0, sizeof( bitmapFileHeader ) );
bitmapFileHeader.bfType = ( 'B' | 'M' << 8 );
bitmapFileHeader.bfOffBits = sizeof( BITMAPFILEHEADER ) + sizeof( BITMAPINFOHEADER );
bitmapFileHeader.bfSize = bitmapFileHeader.bfOffBits + width * height * 3;
BITMAPINFOHEADER bitmapInfoHeader;
memset( &bitmapInfoHeader, 0, sizeof( bitmapInfoHeader ) );
bitmapInfoHeader.biSize = sizeof( BITMAPINFOHEADER );
bitmapInfoHeader.biWidth = width;
bitmapInfoHeader.biHeight = height;
bitmapInfoHeader.biPlanes = 1;
bitmapInfoHeader.biBitCount = 24;
std::ofstream file( filename, std::fstream::binary );
file.write( reinterpret_cast< char * >( &bitmapFileHeader ), sizeof( bitmapFileHeader ) );
file.write( reinterpret_cast< char * >( &bitmapInfoHeader ), sizeof( bitmapInfoHeader ) );
// the pixels!
file.write( imageData, width * height * 3 );
file.close();
}
int main( int argc, const char * argv[] )
{
int width = 12; // multiple of 4
int height = 12;
byte imageData[ width * height * 3 ];
// fill imageData the way you want, this is just a sample
// on how to set the pixel at any specific (X,Y) position
for ( int y = 0; y < height; ++y )
{
for ( int x = 0; x < width; ++x )
{
int pos = 3 * ( y * width + x );
byte pixelColor = ( x == 2 && y == 2 ) ? 0x00 : 0xff;
imageData[ pos ] = pixelColor;
imageData[ pos + 1 ] = pixelColor;
imageData[ pos + 2 ] = pixelColor;
}
}
createBitmap( imageData, "bitmap.bmp", width, height );
return 0;
}
In this sample we want a white bitmap with a single black pixel at position X = 2, Y = 2.
The BMP format constiders that Y grows up from bottom to top.
If you have a bitmap width a pixel per bit (the real monochromatic bitmap) you can test the bits and fill the imageData. To test a bit in a byte do like myByte >> position & 1 where position is the bit you wanna test from 0 to 7.

Black/White Image with unsigned char is blue instead of black C++

I'm reading an .img Gray Scale Image (512 byte header, 512 x 512 pixels x 8 bits per pixel).
There I'm determining a threshold to turn the Gray-Scale image into a binary image (black/white).
But when I assign 0 to the pixels, it turns out to be a light blue (#01fffd)?!?! When I use the value 2 it's almost black (#020100).
Can you explain me how this comes?
I read the image like this:
int width = 512;
int height = 512;
File* imageFilePointerIn = fopen("image.img", "r+b");
File* binaryImageFilePointerOut = fopen("imageOut.img",
"w+b");
unsigned char* header = new unsigned char[512];
unsigned char** imageData = new unsigned char*[width];
unsigned char** binaryImageArray = new unsigned char*[width];
fread(header, sizeof(char), 512, imageFilePointerIn);
for (int i = 0; i < width; i++) {
data[i] = new unsigned char[height];
fread(data[i], sizeof(char), height, imageFilePointerIn);
}
for (int i = 0; i < width; i++) {
binaryImageArray[i] = new unsigned char[height];
}
char unsigned thresholdBinaryImage = 128;
for (int i = 0; i < width; i++) {
for (int j = 0; j < height; j++) {
if (thresholdBinaryImage <= data[i][j]) {
binaryImageArray[i][j] = 255;
} else {
binaryImageArray[i][j] = 0;
}
}
}
fwrite(header, sizeof(char), headerSize, binaryImageFilePointerOut);
for (int i = 0; i < width; i++) {
fwrite(binaryImageArray[i], sizeof(char), height,
binaryImageFilePointerOut);
}
fclose(binaryImageFilePointerOut);
fclose(imageFilePointerIn);
I have no idea why colors could be displayed at all?
For displaying I transfer the .img file into a .ras file with a code snipped from the internet. But I don't think that the error could be there since I saw others using the same code for transforming .img Grayscales into .ras just for displaying without having colors.
Would be nice to get any suggestion :).

How do I modify this gray-scale palette into a color palette?

I am acquiring images using a digital camera. At first, I was using a mono camera, but recently I upgraded to a color camera. With the mono camera I was having some palette issues until I found this bit of code to alter the palette to a grayscale palette:
for(int i=0; i<256; i++)
{
pbmi->bmiColors[i].rgbRed = BYTE(i);
pbmi->bmiColors[i].rgbGreen = BYTE(i);
pbmi->bmiColors[i].rgbBlue = BYTE(i);
pbmi->bmiColors[i].rgbReserved = BYTE(0);
}
where pbmi is a BITMAPINFO*.
This worked just fine for the mono camera. But now with the color camera I obviously don't want to make the images grayscale. However, if I remove that chunk of code I get the same palette issues that I was getting before with the mono camera. So it seems to me like I need to do something similar as I did before and create a palette, only this time a color palette.
For reference, here is the rest of the pbmi's settings:
//// INFO ////
BITMAPINFO* pbmi = (BITMAPINFO*)alloca( sizeof(BITMAPINFOHEADER) +
sizeof(RGBQUAD)*256);
pbmi->bmiHeader.biSize = sizeof (pbmi->bmiHeader);
pbmi->bmiHeader.biWidth = 2752;
pbmi->bmiHeader.biHeight = -2200;
pbmi->bmiHeader.biPlanes = 1;
pbmi->bmiHeader.biBitCount = 8;
pbmi->bmiHeader.biCompression = BI_RGB;
pbmi->bmiHeader.biSizeImage = 0;
pbmi->bmiHeader.biXPelsPerMeter = 14173;
pbmi->bmiHeader.biYPelsPerMeter = 14173;
pbmi->bmiHeader.biClrUsed = 0;
pbmi->bmiHeader.biClrImportant = 0;
So far, I have tried the following:
for(int i=0,a = 0; i < 64; i++)
{
pbmi->bmiColors[i].rgbRed = BYTE(a);
pbmi->bmiColors[i+64].rgbGreen = BYTE(a);
pbmi->bmiColors[i+64+64].rgbBlue = BYTE(a);
pbmi->bmiColors[i+64+64+64].rgbReserved = BYTE(0);
a += 4;
}
//This created a palette consisting only of cyan, yellow, and magenta colors.
//Didn't work.
for(int i=0,r=0,g=0,b=0; b <= 255; i++)
{
if(r >= 256)
{
r = 0;
g++;
}
if(g >= 256)
{
g = 0;
b++;
}
pbmi->bmiColors[i].rgbRed = BYTE(r);
pbmi->bmiColors[i].rgbGreen = BYTE(g);
pbmi->bmiColors[i].rgbBlue = BYTE(b);
pbmi->bmiColors[i].rgbReserved = BYTE(0);
r++;
}
//Here I was trying to basically count up hexadecimally from 000000 to FFFFFF.
//Caused an access violation error.
I've also tried each of those after changing pbmi->bmiHeader.biBitCount to 16, 24, and 32, none of which worked.
So my question is: How do I create a color palette based on the BITMAPINFO settings I have provided?
If you are trying to create a simple RGB pallete, you just need to change the R, G e B values from 0 to 255:
const int MaxIndex = 255;
for(int r=0; r <= MaxIndex; r++)
for(int g=0; g <= MaxIndex; g++)
for(int b=0; b <= MaxIndex; b++)
{
i = r * MaxIndex * MaxIndex + g * MaxIndex + b
pbmi->bmiColors[i].rgbRed = BYTE(r);
pbmi->bmiColors[i].rgbGreen = BYTE(g);
pbmi->bmiColors[i].rgbBlue = BYTE(b);
pbmi->bmiColors[i].rgbReserved = BYTE(0);
}