I am going to put the working code in this answer, whenever I arrive at it. For now, this is the best I have. I think it would work, but the code framed in //'s is currently causing problems. It creates an image buffer of 2,073,600 bytes (the size of raw_img.bin), but then the fread returns 711,148, and then feof(f) evaluates to true.
Sorry to paste so much code, but any help would be appreciated.
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <tchar.h>
#define NPAL_ENT 256
INT WINAPI WinMain( HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPTSTR lpCmdLine,
INT nShowCmd )
{
int width = 1920, height = 1080;
printf("Reading raw img\n");
FILE* f = fopen("\\FlashDisk\\raw_img.bin","r");
if(NULL == f){printf("BAD");exit(1);}
printf("Obtaining size of raw img\n");
fseek (f , 0L , SEEK_END);
DWORD fsize = (DWORD)ftell (f);
fseek (f , 0L , SEEK_SET);
printf("Creating imgData buffer of size %d\n",fsize);
char *imgData = (char*) malloc (sizeof(char)*fsize);
if(NULL == imgData) {printf("NOT imgData");exit(2);}
////////////////////////////////////////////////////////////////////////
printf("Copying contents of file into buffer\n");
DWORD result = fread(imgData,1,fsize,f);
if (result != fsize) {
printf ("Reading error. Expected: %d, Got: %d\n",fsize, result );
if(ferror(f)){printf("An error: %d\n", ferror(f)); }
if(feof(f)) {printf("EOF\n");}
delete[] imgData;
fclose(f);
exit (3);
}
////////////////////////////////////////////////////////////////////////
/* A bitmap has the following components:
* 1. BMP file header
* 2. Bitmap Information (DIB) header
* 3. Color Palette
* 4. Raw Data
*/
printf("Creating a bitmap handle\n");
HBITMAP hBmp=CreateBitmap(width, height, 1, 8, imgData);
printf("1. Creating the file header\n");
BITMAPFILEHEADER bmfh;
memset( &bmfh, 0x00, sizeof( bmfh ) );
bmfh.bfType = 0x4D42; // Magic #
bmfh.bfSize = sizeof( bmfh ) + sizeof( BITMAPINFOHEADER )
+ NPAL_ENT*sizeof(PALETTEENTRY) + fsize; // Total file size
bmfh.bfOffBits = sizeof( bmfh ) + sizeof( BITMAPINFOHEADER )
+ NPAL_ENT*sizeof(PALETTEENTRY);
printf("2. Creating bitmap info\n");
BITMAPINFOHEADER bmih;
bmih.biWidth = width;
bmih.biHeight = height;
bmih.biSize = sizeof(bmih);
bmih.biPlanes = 1;
bmih.biBitCount = 8;
bmih.biCompression = BI_RGB;
printf("3. Creating a logpalette\n");
int palSize = NPAL_ENT*sizeof(PALETTEENTRY);
LOGPALETTE *logpal=(LOGPALETTE*)new BYTE[sizeof(LOGPALETTE)+palSize];
if(!logpal) {delete [] imgData; printf("!logpal\n"); fclose(f); exit(4);}
logpal->palVersion=0x300;
logpal->palNumEntries=NPAL_ENT;
int i=0;
do { //no idea your palette's format, however it looks to be greyscale?
logpal->palPalEntry[i].peRed=i;
logpal->palPalEntry[i].peGreen=i;
logpal->palPalEntry[i].peBlue=i;
logpal->palPalEntry[i].peFlags=NULL;
}while(++i<NPAL_ENT);
HPALETTE hPal=CreatePalette(logpal);
if(!hPal){delete[] imgData; delete[] logpal; printf("!hpal\n"); exit(5);}
printf("4. Bitmap data has already been read\n");
// Complete bitmap is now in memory, time to save it
printf("Determining a unique filename\n");
TCHAR tcFileName[80];
wsprintf( tcFileName, (TCHAR*) TEXT( "\\USBDisk\\test.bmp" ) );
printf("Filename determined\n");
// open the file for writing
HANDLE hFile = CreateFile( tcFileName, GENERIC_WRITE, 0, NULL,
CREATE_ALWAYS, 0, NULL );
if(!hFile) { delete[] imgData; delete[] logpal; fclose(f); exit(6); }
printf("File opened for writing\n");
// write the bitmap file header to file
DWORD bytesWrit;
WriteFile( hFile, &bmfh, sizeof( bmfh ), &bytesWrit, NULL );
printf("bmp file header written\n");
// write bmp info header to file
WriteFile( hFile, &bmih, sizeof( bmih ), &bytesWrit, NULL );
printf("bmp info header written\n");
// write palette to file
WriteFile( hFile, &logpal->palPalEntry, palSize, &bytesWrit, NULL );
// write bmp bytes to file
DWORD totWrit = 0;
while( totWrit < fsize ) {
WriteFile( hFile, imgData+totWrit, fsize-totWrit, &bytesWrit, NULL);
totWrit += bytesWrit;
}
printf("bmp bytes written\n");
// close the file
CloseHandle( hFile );
printf("File closed\n");
delete [] imgData;
delete [] logpal;
fclose(f);
fclose(hFile);
return 0;
}