24 True Color map to 4-bit bitmap

Source: Internet
Author: User
Tags readfile

Be sure to note the data alignment%%%%%%%%%%%%%

BOOL cphotodlg::convert24to4 (LPCTSTR lpszsrcfile, LPCTSTR lpszdestfile)
{
FILE*FP;
fp = fopen ("Aa.txt", "WR");
Bitmapfileheader Bmhdr;
Bitmapinfoheader Bminfo;
HANDLE hfile, Hnewfile;
DWORD dwbytewritten = 0;

Open source file handle
hfile = CreateFile (Lpszsrcfile,
Generic_read,
File_share_read,
Null
Open_existing,
File_attribute_normal,
NULL);

if (hfile = = INVALID_HANDLE_VALUE)
return FALSE;
Hnewfile = CreateFile (Lpszdestfile,
generic_read| Generic_write,
file_share_read| File_share_write,
Null
Create_always,
File_attribute_normal,
NULL);
if (Hnewfile = = INVALID_HANDLE_VALUE)
{
CloseHandle (hfile);
return false;
}
Read the original file BMP headers and files Sydney city
ReadFile (hfile, &BMHDR, sizeof (BMHDR), &dwbytewritten, NULL);
ReadFile (hfile, &bminfo, sizeof (Bminfo), &dwbytewritten, NULL);
Handle only 24 bits for compressed images
if (bminfo.bibitcount!= | | | bminfo.bicompression!= 0)
{
CloseHandle (Hnewfile);
CloseHandle (hfile);
DeleteFile (Lpszdestfile);
return false;
}
Calculate image Data Size
DWORD dwoldsize = bminfo.bisizeimage;
if (dwoldsize = 0)
{
Dwoldsize = bmhdr.bfsize-sizeof (BMHDR)-sizeof (BMINFO);
}
/*
Long wid = bminfo.biwidth% 4;
if (wid > 0)
{
WID = 4-wid;
}
Wid + = Bminfo.biwidth;
*/
Long wid = Bminfo.biwidth;
Long Loldwidth = (Bminfo.biwidth * 3 +3)/4*4;
Long Lnewwidth = ((bminfo.biwidth+1)/2 +3)/4*4;
DWORD dwnewsize;
Dwnewsize = Lnewwidth *bminfo.biheight; Size of picture after conversion
Read RAW data
unsigned char *pbuffer = NULL;
pbuffer = new unsigned char[dwoldsize];
if (pbuffer = NULL)
{
CloseHandle (Hnewfile);
CloseHandle (hfile);
DeleteFile (Lpszdestfile);
return false;
}
ReadFile (hfile, pbuffer, Dwoldsize, &dwbytewritten, NULL);
Fwrite (pbuffer, dwoldsize, 1, FP);
Fclose (FP);
unsigned char *pnew = new unsigned char [dwnewsize];
unsigned char color = 0;
DWORD dwindex = 0, dwoldindex = 0;
int I, j, K;
Long pos = 0;

unsigned char R, G, B;
//
for (j = 0; J < Bminfo.biheight; J + +)
{
pos = Loldwidth * j;
for (i = 0; i< loldwidth i = i+6)
{
B = Pbuffer[pos+i];
G = pbuffer[pos+i+1];
R = pbuffer[pos+i+2];
unsigned char maxcolor = 0.299*r+0.587*g+0.114*b;
Maxcolor/= 17;
B = pbuffer[pos+i+3];
G = pbuffer[pos+i+4];
R = pbuffer[pos+i+5];
unsigned char maxcolor2 = 0.299*r+0.587*g+0.114*b;
Maxcolor2/= 17;
Pnew[j*lnewwidth + I/6] = (maxcolor<<4) |maxcolor2;
}
}
Fwrite ("------------------", sizeof ("------------------"), 1, FP);
Fwrite (Pnew, dwnewsize, 1, FP);
Fclose (FP);
/////////////////////////////////
Save the results to a new file
modifying properties
bmhdr.bfsize = sizeof (BMHDR) +sizeof (bminfo) +sizeof (rgbquad) *16 + dwnewsize;
Bmhdr.bfoffbits = bmhdr.bfsize-dwnewsize;
Bminfo.bibitcount = 4;
Bminfo.bisizeimage = dwnewsize;

 //Create palette
 rgbquad pa[16];
 unsigned char c;
 for (i = 0; i < i++)
 {
  c = i * 17;
  pa[i].rgbblue =c;
  pa[i].rgbgreen = c;
  pa[i].rgbred= C;
  pa[i].rgbreserved = 0;
 }
 //bmp Header
 writefile (hnewfile, &BMHDR, sizeof (BMHDR), &dwbytewritten, NULL);
  WriteFile (Hnewfile, &bminfo, sizeof (Bminfo), &dwbytewritten, NULL);
 writefile (Hnewfile, PA, sizeof (Rgbquad) *16, &dwbytewritten, NULL);
 writefile (Hnewfile, Pnew, Dwnewsize, &dwbytewritten, NULL);
 delete []pbuffer;
 delete []pnew;
 closehandle (Hnewfile);
 closehandle (hfile);
 return true;
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.