示例#1
0
void SaveBitmap(char *fileName,unsigned char *pBuf, int w, int h)
{
     if(!fileName || !pBuf) return;
     FILE *fp = NULL;
     fp = fopen(fileName,"wb");
     if(fp != NULL) 
     {
        BITMAPINFO bmpinfo;
        // bitmapinfoheader 
        BITMAPFILEHEADER  bmpfileheader;
        ZeroMemory(&bmpinfo, sizeof(BITMAPINFO));
        bmpinfo.bmiHeader.biSize=sizeof(BITMAPFILEHEADER);

            
        bmpinfo.bmiHeader.biHeight = -h;
        bmpinfo.bmiHeader.biWidth  = w;
        bmpinfo.bmiHeader.biSizeImage = abs(bmpinfo.bmiHeader.biHeight) * bmpinfo.bmiHeader.biWidth * 4;

        bmpinfo.bmiHeader.biCompression = BI_RGB;
        bmpinfo.bmiHeader.biPlanes = 1;
        bmpinfo.bmiHeader.biBitCount = 32;

        bmpfileheader.bfReserved1=0;
        bmpfileheader.bfReserved2=0;
        bmpfileheader.bfSize=sizeof(BITMAPFILEHEADER)+sizeof(BITMAPINFO)+bmpinfo.bmiHeader.biSizeImage;
        bmpfileheader.bfType = MakeIntelWord('M','B');
        bmpfileheader.bfOffBits=sizeof(BITMAPFILEHEADER)+sizeof(BITMAPINFO);


        fwrite(&bmpfileheader,sizeof(BITMAPFILEHEADER),1,fp);
        fwrite(&bmpinfo.bmiHeader,sizeof(BITMAPINFOHEADER),1,fp);
        fwrite(pBuf, bmpinfo.bmiHeader.biSizeImage, 1, fp);
        fclose(fp);
     }
}
void CAtmoTools::SaveBitmap(HDC hdc,HBITMAP hBmp,char *fileName) 
{
	BITMAPINFO bmpInfo;
	BITMAPFILEHEADER  bmpFileHeader;
	ZeroMemory(&bmpInfo, sizeof(BITMAPINFO));
	bmpInfo.bmiHeader.biSize=sizeof(BITMAPINFOHEADER);

	GetDIBits(hdc,hBmp,0,0,NULL,&bmpInfo,DIB_RGB_COLORS);
	if(bmpInfo.bmiHeader.biSizeImage<=0)
		bmpInfo.bmiHeader.biSizeImage=bmpInfo.bmiHeader.biWidth * abs(bmpInfo.bmiHeader.biHeight)*(bmpInfo.bmiHeader.biBitCount+7)/8;
	void *pBuf = malloc(bmpInfo.bmiHeader.biSizeImage);
	bmpInfo.bmiHeader.biCompression=BI_RGB;

	GetDIBits(hdc,hBmp,0,bmpInfo.bmiHeader.biHeight,pBuf, &bmpInfo, DIB_RGB_COLORS);


	bmpFileHeader.bfReserved1=0;
	bmpFileHeader.bfReserved2=0;
	bmpFileHeader.bfSize=sizeof(BITMAPFILEHEADER)+sizeof(BITMAPINFOHEADER)+bmpInfo.bmiHeader.biSizeImage;
	bmpFileHeader.bfType = MakeIntelWord('M','B');
	bmpFileHeader.bfOffBits=sizeof(BITMAPFILEHEADER)+sizeof(BITMAPINFOHEADER);


	FILE *fp = NULL;
	fp = fopen(fileName,"wb");
	fwrite(&bmpFileHeader,sizeof(BITMAPFILEHEADER),1,fp);
	fwrite(&bmpInfo.bmiHeader,sizeof(BITMAPINFOHEADER),1,fp);
	fwrite(pBuf,bmpInfo.bmiHeader.biSizeImage,1,fp);
	fclose(fp);
}
void CAtmoZoneDefinition::SaveZoneBitmap(char *fileName)
{
     if(!fileName) return;

     BITMAPINFO bmpInfo;
     // BITMAPINFOHEADER
     BITMAPFILEHEADER  bmpFileHeader;
     ZeroMemory(&bmpInfo, sizeof(BITMAPINFO));
     bmpInfo.bmiHeader.biSize=sizeof(BITMAPINFOHEADER);


     bmpInfo.bmiHeader.biHeight = -CAP_HEIGHT;
     bmpInfo.bmiHeader.biWidth  = CAP_WIDTH;
     bmpInfo.bmiHeader.biSizeImage = abs(bmpInfo.bmiHeader.biHeight) * bmpInfo.bmiHeader.biWidth * 3;

     unsigned char *pBuf = (unsigned char *)malloc(bmpInfo.bmiHeader.biSizeImage);
     for(int y=0; y < CAP_HEIGHT; y++ )
     {
         for(int x=0; x < CAP_WIDTH; x++)
         {
             pBuf[y * CAP_WIDTH * 3 + x * 3 ] = 0;
             pBuf[y * CAP_WIDTH * 3 + x * 3 + 1 ] = m_BasicWeight[y * CAP_WIDTH + x];
             pBuf[y * CAP_WIDTH * 3 + x * 3 + 2] = 0;
         }
     }

     bmpInfo.bmiHeader.biCompression = BI_RGB;
     bmpInfo.bmiHeader.biPlanes = 1;
     bmpInfo.bmiHeader.biBitCount = 24;

     bmpFileHeader.bfReserved1=0;
     bmpFileHeader.bfReserved2=0;
     bmpFileHeader.bfSize=sizeof(BITMAPFILEHEADER)+sizeof(BITMAPINFOHEADER)+bmpInfo.bmiHeader.biSizeImage;
     bmpFileHeader.bfType = MakeIntelWord('M','B');
     bmpFileHeader.bfOffBits=sizeof(BITMAPFILEHEADER)+sizeof(BITMAPINFOHEADER);


     FILE *fp = NULL;
     fp = fopen(fileName,"wb");
     fwrite(&bmpFileHeader,sizeof(BITMAPFILEHEADER),1,fp);
     fwrite(&bmpInfo.bmiHeader,sizeof(BITMAPINFOHEADER),1,fp);
     fwrite(pBuf,bmpInfo.bmiHeader.biSizeImage,1,fp);
     fclose(fp);
     free(pBuf);
}
示例#4
0
int CAtmoZoneDefinition::LoadGradientFromBitmap(char *pszBitmap)
{
  // transform 256 color image (gray scale!)
  // into m_basicWeight or use the GREEN value of a 24bit image!
  // channel of a true color bitmap!
  VLC_BITMAPINFO bmpInfo;
  BITMAPFILEHEADER  bmpFileHeader;

  /*
  ATMO_LOAD_GRADIENT_FILENOTFOND
#define ATMO_LOAD_GRADIENT_OK  0
#define ATMO_LOAD_GRADIENT_FAILED_SIZE    1
#define ATMO_LOAD_GRADIENT_FAILED_HEADER  2
  */


   FILE *bmp = fopen(pszBitmap, "rb");
   if(!bmp)
    return ATMO_LOAD_GRADIENT_FILENOTFOND;

    if(fread(&bmpFileHeader, sizeof(BITMAPFILEHEADER), 1, bmp) != 1)
    {
        fclose(bmp);
        return ATMO_LOAD_GRADIENT_FAILED_SIZE;
    }

    if(bmpFileHeader.bfType != MakeIntelWord('M','B'))
    {
        fclose(bmp);
        return ATMO_LOAD_GRADIENT_FAILED_HEADER;
    }

    if(fread(&bmpInfo, sizeof(VLC_BITMAPINFO), 1, bmp) != 1)
    {
        fclose(bmp);
        return ATMO_LOAD_GRADIENT_FAILED_SIZE;
    }

    if(bmpInfo.bmiHeader.biCompression != BI_RGB)
    {
        fclose(bmp);
        return ATMO_LOAD_GRADIENT_FAILED_FORMAT;
    }
    if((bmpInfo.bmiHeader.biBitCount != 8) && (bmpInfo.bmiHeader.biBitCount != 24))
    {
        fclose(bmp);
        return ATMO_LOAD_GRADIENT_FAILED_FORMAT;
    }

    int width = bmpInfo.bmiHeader.biWidth;
    int height = bmpInfo.bmiHeader.biHeight;
    ATMO_BOOL invertDirection = (height > 0);
    height = abs(height);
    if((width != CAP_WIDTH) || (height != CAP_HEIGHT))
    {
        fclose(bmp);
        return ATMO_LOAD_GRADIENT_FAILED_SIZE;
    }

    fseek(bmp, bmpFileHeader.bfOffBits, SEEK_SET);

    int imageSize = width * height * bmpInfo.bmiHeader.biBitCount/8;

    unsigned char *pixelBuffer = (unsigned char *)malloc(imageSize);
    if(fread(pixelBuffer,imageSize,1,bmp) != 1)
    {
        fclose(bmp);
        return ATMO_LOAD_GRADIENT_FAILED_SIZE;
    }

    if(bmpInfo.bmiHeader.biBitCount == 8)
    {
        int ydest;
        for(int y=0;y < CAP_HEIGHT; y++) {
            if(invertDirection) {
                ydest = (CAP_HEIGHT - y - 1);
            } else {
                ydest = y;
            }
            for(int x=0;x < CAP_WIDTH; x++) {
                // palette should be grey scale - so that index 0 is black and
                // index 255 means white!
                // everything else would produce funny results!
                m_BasicWeight[ydest * CAP_WIDTH + x] =
                    pixelBuffer[y * CAP_WIDTH + x];
            }
        }
    }

    if(bmpInfo.bmiHeader.biBitCount == 24)
    {
        int ydest;
        for(int y=0;y < CAP_HEIGHT; y++) {
            if(invertDirection) {
                ydest = (CAP_HEIGHT - y - 1);
            } else {
                ydest = y;
            }
            for(int x=0;x < CAP_WIDTH; x++) {
                // use the green value as reference...
                m_BasicWeight[ydest * CAP_WIDTH + x] =
                    pixelBuffer[y * CAP_WIDTH * 3 + (x*3) + 1 ];
            }
        }
    }
    free(pixelBuffer);
    fclose(bmp);

    return ATMO_LOAD_GRADIENT_OK;
}