⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 convertbmpview.cpp

📁 bmp_to_c.rar
💻 CPP
📖 第 1 页 / 共 2 页
字号:
    {
        char chr[2];
        sprintf( chr, "%d", bmpInfoHeader.biBitCount);
        strcpy(sError, "This is a ");
        strcat(sError, chr);
        strcat(sError, "-bits color BMP!\n Suggestion: use ACDsee to convert the BMP to 16-bits color BMP");
        fclose(Bitmap);
        return false;
    }

    int nPitch;
    int iReturn, iTmpSize;
    BYTE *bTmp;


	/* I am not sure if we should care the RGB infomation */
    iReturn = bmpFileHeader.bfOffBits - sizeof(BITMAPFILEHEADER) - sizeof (BITMAPINFOHEADER);
	iTmpSize = sizeof(RGBQUAD) *(1 << bmpInfoHeader.biBitCount);

    if ( iReturn >= iTmpSize )
	{
		bmiColors = new RGBQUAD[1 << bmpInfoHeader.biBitCount];
		fread( bmiColors, iTmpSize, 1,Bitmap);
		iReturn = iReturn - iTmpSize;
	}

	if(iReturn > 0)
    {
        bTmp = new BYTE[iReturn ];
        iReturn = fread( bTmp, iReturn, 1,Bitmap);

		delete(bTmp);
    }

    int    BMPSize = bmpFileHeader.bfSize - bmpFileHeader.bfOffBits;

	if (bmpInfoHeader.biBitCount == 1)
	{
        nPitch = (bmpInfoHeader.biWidth + 31) >> 5 << 2;
	}
    else /* if (bmpInfoHeader.biBitCount == 16 or 24) */
    {
		nPitch = BMPSize/bmpInfoHeader.biHeight;
	}

    BYTE    *binfo;
    binfo = new BYTE[BMPSize];

    iReturn = fread(binfo,BMPSize,1,Bitmap);
    int iError = ferror ( Bitmap );

    /*------------ delete by Tree for we don't care RGB info now --------    
    RGBQUAD*    bquad = new RGBQUAD[1 << bmpInfoHeader.biBitCount];

    fread(bquad,sizeof(bquad),(1 << bmpInfoHeader.biBitCount) ,Bitmap);

    int nPitch;
    if (bmpInfoHeader.biBitCount == 1)
        nPitch = (bmpInfoHeader.biWidth + 31) >> 5 << 2;
    else if (bmpInfoHeader.biBitCount == 16)
        nPitch = bmpInfoHeader.biWidth*2;

    int    BMPSize = bmpInfoHeader.biHeight * nPitch;

    BYTE             *binfo;
    binfo = new BYTE[BMPSize*2];

    fread(binfo,BMPSize*BYTELENGTH,1,Bitmap);
    iReturn = fread(binfo,2,1,Bitmap); //BMPSize


    // TREE------The heigth of the BMP doesn't need to be 8*X
    //if (bmpInfoHeader.biHeight % BYTELENGTH != 0)
    //{
    //    strcpy(sError , "The Height of this Bitmap must be divided by 8");
    //    fclose(Bitmap);
    //    return false;
    //} 
    -------------------- delete end -----------------------*/

    /******************* Generate the BMP header ************************/
    FILE*           ByteArray = fopen(szFileName,"w+");
    int             Index = 0;
    char            strTmp[50] ;
//	unsigned char   byRed, byGreen, byBlue, byTmpData;
	WORD   wPixcel, wRed, wGreen, wBlue =0;


    // The first line of the result file is the width and the height

    strcpy( strTmp, "const OP_UINT16 imageData[] = {" );
    fwrite( strTmp, strlen(strTmp), 1, ByteArray);
    fwrite( "\n", strlen("\n"), 1, ByteArray);


/*************** Generate the BYTE Array ********************************/
    if ( (bmpInfoHeader.biBitCount * bmpInfoHeader.biWidth) % BYTELENGTH )
        iTmp = (bmpInfoHeader.biBitCount * bmpInfoHeader.biWidth)/BYTELENGTH + 1;
    else 
        iTmp = (bmpInfoHeader.biBitCount * bmpInfoHeader.biWidth)/BYTELENGTH;


	if(bmpInfoHeader.biBitCount == 24)
	{
		iCount = 0;
		for (int Height = bmpInfoHeader.biHeight; Height > 0 ;Height --)
		{
			for (int Width = nPitch ; Width > (nPitch - iTmp) ; Width = Width - 3)
			{
				/* 
				 * first, I should caculate the index for our bitmap
				 * for Windows OS, the bitmap will start to show from left-bottom
				 * but for our display format, the bitmap will start to show from left-up
				 * the following code are to convert 24-bits color to 16-bits color
				 */
				Index = (Height * nPitch) - Width;

				//get the third byte -- Blue byte
				wPixcel = (binfo[Index] >> 3) & 0x1f;

				//get the second byte -- Green byte
				wGreen = (binfo[Index + 1] >> 2) & 0x3f;
                wPixcel |= (wGreen<<5)&0x7E0;

				//get the firt byte -- Red byte
				wRed = (binfo[Index + 2] >> 3) & 0x1f;
				wPixcel |= (wRed<<11)&0xF800;

				sprintf( strTmp, "0x%04X,", wPixcel); // add the end of the bmp header
				fwrite(strTmp,strlen(strTmp),1,ByteArray);

				iCount++;/*tree add for word format*/
				if ( !(iCount % 12) )
				{
					fwrite("\n",strlen("\n"),1,ByteArray);
				}
			}
		}
	} /* end of the process of 24-bits color bitmap */

	else if(bmpInfoHeader.biBitCount == 16)/* process 16-bits color bitmap */
	{
		iCount = 0;

		for (int Height = bmpInfoHeader.biHeight; Height > 0 ;Height --)
		{
			for (int Width = nPitch ; Width > (nPitch - iTmp) ; Width = Width - 2)
			{
				// first, I should caculate the index for our bitmap
				// for Windows OS, the bitmap will start to show from left-bottom
				// but for our display format, the bitmap will start to show from left-up
				Index = (Height * nPitch) - Width;


				// blue
                wPixcel = binfo[Index]&0x1F;

                // green
                wGreen = (binfo[Index+1]&0x7)<<3;
				wGreen |= (binfo[Index]&0xE0)>>5;
                wPixcel |= (wGreen<<6)&0x7C0;

				//red
				wRed = (binfo[Index+1]>>3)&0x1F;
                wPixcel |= (wRed<<11)&0xF800;

				sprintf( strTmp, "0x%04X,", wPixcel); // add the end of the bmp header
				fwrite(strTmp,strlen(strTmp),1,ByteArray);

				iCount++;/*tree add for word format*/
				if ( !(iCount % 12) )
				{
					fwrite("\n",strlen("\n"),1,ByteArray);
				}

			}
		}
	}
	else if(bmpInfoHeader.biBitCount == 8)/* process 16-bits color bitmap */
	{
		iCount = 0;


        for(int colori = 0; colori<256; colori++)
        {
			//blue
			wPixcel = (bmiColors[colori].rgbBlue >> 3) & 0x1f;

			// green
			wGreen = (bmiColors[colori].rgbGreen >> 2) & 0x3f;;
			wPixcel |= wGreen<<5;

			//red
			wRed = (bmiColors[colori].rgbRed >> 3) & 0x1f;
			wPixcel |= wRed<<11;

    		sprintf( strTmp, "0x%04X,", wPixcel); // add the end of the bmp header
			fwrite(strTmp,strlen(strTmp),1,ByteArray);
        }

        strcpy( strTmp, "}\nconst OP_UINT16 imageData[] = {" );
        fwrite( strTmp, strlen(strTmp), 1, ByteArray);
        fwrite( "\n", strlen("\n"), 1, ByteArray);
		for (int Height = bmpInfoHeader.biHeight; Height > 0 ;Height --)
		{
			for (int Width = nPitch ; Width > (nPitch - iTmp) ; Width = Width - 1)
			{
				Index = (Height * nPitch) - Width;

				sprintf( strTmp, "0x%02X,", binfo[Index]); // add the end of the bmp header
				fwrite(strTmp,strlen(strTmp),1,ByteArray);

				iCount++;/*tree add for word format*/
				if ( !(iCount % 12) )
				{
					fwrite("\n",strlen("\n"),1,ByteArray);
				}
			}
		}
	}
	else if(bmpInfoHeader.biBitCount == 4)/* process 16-bits color bitmap */
	{
		iCount = 0;
		BYTE   first = 0;

		for (int Height = bmpInfoHeader.biHeight; Height > 0 ;Height --)
		{
			for (int Width = nPitch ; Width > (nPitch - iTmp) ; Width = Width - 1)
			{
				Index = (Height * nPitch) - Width;
//first pixcel
				for( int nn=0; nn<2; nn++)
				{
					if(nn == 0)
					{
						first = (binfo[Index]>>4) & 0x0F;
					}
					else
					{
						first = binfo[Index]&0x0F;
					}
					//blue
					wPixcel = (bmiColors[first].rgbBlue >> 3) & 0x1f;

					// green
					wGreen = (bmiColors[first].rgbGreen >> 2) & 0x3f;;
					wPixcel |= wGreen<<5;

					//red
					wRed = (bmiColors[first].rgbRed >> 3) & 0x1f;
					wPixcel |= wRed<<11;

    				sprintf( strTmp, "0x%04X,", wPixcel); // add the end of the bmp header
					fwrite(strTmp,strlen(strTmp),1,ByteArray);

					iCount++;/*tree add for word format*/
				}

				if ( !(iCount % 12) )
				{
					fwrite("\n",strlen("\n"),1,ByteArray);
				}
			}
		}
	}
    else
    {
    
    }

    strcpy( strTmp, "\n};\n" ); // add the end of the bmp header
    fwrite(strTmp,strlen(strTmp),1,ByteArray);

    fwrite(START_STRING_FOR_BMP,strlen(START_STRING_FOR_BMP),1,ByteArray);
    fwrite("\n",strlen("\n"),1,ByteArray);

    strcpy( strTmp, "    16" ); // add a bitcount
    fwrite(strTmp,strlen(strTmp),1,ByteArray);
    //sprintf(strTmp,"%d" , bmpInfoHeader.biBitCount );
	//fwrite(strTmp,strlen(strTmp),1,ByteArray);

    fwrite(",",sizeof(char),1,ByteArray);
    fwrite("\n",strlen("\n"),1,ByteArray);

    strcpy( strTmp, "    " ); // add a width
    fwrite(strTmp,strlen(strTmp),1,ByteArray);
    sprintf(strTmp, "%d", bmpInfoHeader.biWidth );
    fwrite(strTmp,strlen(strTmp),1,ByteArray);
    fwrite(",",sizeof(char),1,ByteArray);
    fwrite("\n",strlen("\n"),1,ByteArray);

    strcpy( strTmp, "    " ); // add a height
    fwrite(strTmp,strlen(strTmp),1,ByteArray);
    sprintf(strTmp,"%d" ,bmpInfoHeader.biHeight );
    fwrite(strTmp,strlen(strTmp),1,ByteArray);
    fwrite(",",sizeof(char),1,ByteArray);
    fwrite("\n",strlen("\n"),1,ByteArray);

    strcpy( strTmp, "    (OP_UINT16 *)imageData" );
    fwrite(strTmp,strlen(strTmp),1,ByteArray);
    fwrite("\n",strlen("\n"),1,ByteArray);
    
    strcpy( strTmp, "};\n" ); // add the end of the bmp header
    fwrite(strTmp,strlen(strTmp),1,ByteArray);

    fclose(ByteArray);
    /*************************************************************************/
    fclose(Bitmap);
    delete(binfo);
    return true;
}

char CConvertBMPView::GetHex(BYTE bySrc,bool bFlag, int bitCount)
{
    int    num;
    
    if ( bitCount == 1) //if the bitmap is a monochrome color BMP! 
        bySrc = ~bySrc;

    if (bFlag)
        num = bySrc >> 4;
    else
        num = bySrc & 0x0f;

    if (num < 10)
        return '0' + num;
    else
        return 'A' + num - 10;
}



void CConvertBMPView::OnEditConvert() 
{

    CFileDialog dlg(FALSE, "c", "*.c");
    if (dlg.DoModal() != IDOK) {
        return;
    }
    
    // seve the filename, I will write the bmp data to this file
    CString szFileName = dlg.GetPathName();

    char szError[100];
    if ( (ConvertBmp(szFileName, szError)) == false)
        AfxMessageBox( szError);
	else
		AfxMessageBox( "OK");
    
}

void CConvertBMPView::OnEditAddtofile() 
{
    InputBmpId idDlg;
    idDlg.DoModal();    
	
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -