HDIB CxDib::Create(DWORD dwWidth, DWORD dwHeight, WORD wBitCount) { LPBITMAPINFOHEADER lpbi; // pointer to BITMAPINFOHEADER DWORD dwLen; // size of memory block if (hDib) free(hDib); hDib=NULL; // Make sure bits per pixel is valid if (wBitCount <= 1) wBitCount = 1; else if (wBitCount <= 4) wBitCount = 4; else if (wBitCount <= 8) wBitCount = 8; else wBitCount = 24; switch (wBitCount){ case 1: m_nColors = 2; break; case 4: m_nColors = 16; break; case 8: m_nColors = 256; break; default: m_nColors = 0; } m_LineWidth = WIDTHBYTES(wBitCount * dwWidth); // initialize BITMAPINFOHEADER m_bi.biSize = sizeof(BITMAPINFOHEADER); m_bi.biWidth = dwWidth; // fill in width from parameter m_bi.biHeight = dwHeight; // fill in height from parameter m_bi.biPlanes = 1; // must be 1 m_bi.biBitCount = wBitCount; // from parameter m_bi.biCompression = BI_RGB; m_bi.biSizeImage = m_LineWidth * dwHeight; m_bi.biXPelsPerMeter = 0; m_bi.biYPelsPerMeter = 0; m_bi.biClrUsed = 0; m_bi.biClrImportant = 0; // calculate size of memory block required to store the DIB. This // block should be big enough to hold the BITMAPINFOHEADER, the color // table, and the bits dwLen = GetSize(); //hDib = malloc(dwLen); // alloc memory block to store our bitmap hDib = new (HDIB[dwLen]); //fixes allocation problem under Win2k if (!hDib) return NULL; // use our bitmap info structure to fill in first part of // our DIB with the BITMAPINFOHEADER lpbi = (LPBITMAPINFOHEADER)(hDib); *lpbi = m_bi; return hDib; //return handle to the DIB }
bool CImageObject::LoadImageBMP( char *filename ) { BITMAPFILEHEADER bmFileHeader; BITMAPINFOHEADER bmInfoHeader; CFile file; if (!file.Open(filename, CFile::modeNoTruncate | CFile::modeRead )) { printf("No file %s", filename); return false; } file.Read(&bmFileHeader, sizeof(BITMAPFILEHEADER)); file.Read(&bmInfoHeader, sizeof(BITMAPINFOHEADER)); m_ImageWidth = bmInfoHeader.biWidth; m_ImageHeight = abs(bmInfoHeader.biHeight); int bytespixel_src = bmInfoHeader.biBitCount/8; BYTE *PixelFlip = new BYTE[m_ImageHeight * m_ImageWidth * bytespixel_src]; file.Read(PixelFlip, m_ImageHeight * m_ImageWidth * bytespixel_src); file.Close(); if (m_rgb_pixel != NULL) delete m_rgb_pixel; if (m_display_pixel != NULL) delete m_display_pixel; int widthbytes_display = WIDTHBYTES(m_ImageWidth,bmInfoHeader.biBitCount); m_rgb_pixel = new BYTE[m_ImageHeight * m_ImageWidth * 3]; // Loading Flip BMP image for (int row=0;row<m_ImageHeight;row++) { for (int col=0;col<m_ImageWidth;col++) { m_rgb_pixel[3 * ((m_ImageHeight - row - 1)*m_ImageWidth+col) ] = PixelFlip[(row*widthbytes_display + col*bytespixel_src) + 0]; m_rgb_pixel[3 * ((m_ImageHeight - row - 1)*m_ImageWidth+col) + 1] = PixelFlip[(row*widthbytes_display + col*bytespixel_src) + 1]; m_rgb_pixel[3 * ((m_ImageHeight - row - 1)*m_ImageWidth+col) + 2] = PixelFlip[(row*widthbytes_display + col*bytespixel_src) + 2]; } } //delete temp2; delete [] PixelFlip; return true; // Invalidate(FALSE); }
//handles copying the image data from the associated bitmap to the 16 bit texture provided static void CopyGlyphBitmapToPixelData( BITMAPINFO const& bmi, uint8 const* pDibBits, const LTRect2n& rectDest, TEXTMETRICW const& textMetric, uint8* pPixelData, SIZE const& sizeTexture ) { //pitches of each surface const int nBitmapPitch = WIDTHBYTES( bmi.bmiHeader.biBitCount, bmi.bmiHeader.biWidth ); const int nPixelDataPitch = WIDTHBYTES( 16, sizeTexture.cx ); //pixel byte widths of the bitmap surface const int nBitmapPixelWidth = (bmi.bmiHeader.biBitCount + 7) / 8; //calculate the width const int nGlyphWidth = rectDest.GetWidth(); //calcualte the height const int nGlyphHeight = rectDest.GetHeight(); // Copy the glyph pixels to the pixeldata. Leave the rgb of the pixeldata set to white. // This lets the font antialias with any color background. for( int y = 0; y < nGlyphHeight; y++ ) { // Find out where to start this row by offset by y using the passed in bitmap pitch. // We only need to copy the glyph pixels, so we advance into the bitmap to // where they start. The source is 24 bpp. const uint8* pSrcPos = pDibBits + ( y * nBitmapPitch ); // The pixeldata pointer will be pointing to some region in // the middle of a larger bitmap. So, we use the pixeldatapitch // passed in to calculate how much to advance per line. We don't want // to write out any columns to the left or right of the actual glyph. uint16* pDestPos = ( uint16* )( pPixelData + (( y + rectDest.Top() ) * nPixelDataPitch ) + (rectDest.Left() * 2) ); // Copy this row. for( int x = 0; x < nGlyphWidth; x++ ) { // The source is 24 bits. Just take r and use it as the alpha value. uint32 nVal = MulDiv( pSrcPos[0], 15, 255 ); *pDestPos = ( *pDestPos & 0x0FFF ) | (( nVal & 0x0F ) << 12 ); pDestPos++; pSrcPos += nBitmapPixelWidth; } } }
char* LoadJpegFile (char *jpegbuf, char *bmpbuf) { DWORD ImgSize; DWORD JpegBufSize; int funcret; LPBITMAPINFOHEADER lpImgData; //JpegBufSize=size; lpJpegBuf=jpegbuf; InitTable(); if((funcret=InitTag())!=FUNC_OK) { showerror(funcret); return NULL; } //create new bitmapfileheader and bitmapinfoheader memset((char *)&bf,0,sizeof(BITMAPFILEHEADER)); memset((char *)&bi,0,sizeof(BITMAPINFOHEADER)); bi.biSize=(DWORD)sizeof(BITMAPINFOHEADER); bi.biWidth=(LONG)(ImgWidth); bi.biHeight=(LONG)(ImgHeight); bi.biPlanes=1; bi.biBitCount=24; bi.biClrUsed=0; bi.biClrImportant=0; bi.biCompression=BI_RGB; NumColors=0; LineBytes=(DWORD)WIDTHBYTES(bi.biWidth*bi.biBitCount); ImgSize=(DWORD)LineBytes*bi.biHeight; bf.bfType=0x4d42; bf.bfSize=/*sizeof(BITMAPFILEHEADER)*/ 14+sizeof(BITMAPINFOHEADER)+NumColors*sizeof(RGBQUAD)+ImgSize; bf.bfOffBits=(DWORD)(NumColors*sizeof(RGBQUAD)+14/*sizeof(BITMAPFILEHEADER)*/+sizeof(BITMAPINFOHEADER)); lpPtr = (char*)bmpbuf + 54; if((SampRate_Y_H==0)||(SampRate_Y_V==0)) { showerror(FUNC_FORMAT_ERROR); return NULL ; } funcret=Decode(); memcpy(bmpbuf, &bf.bfType,sizeof(WORD)); memcpy(bmpbuf+2, ((char*)&bf)+4, 12); memcpy(bmpbuf+14, (LPSTR)&bi,sizeof(BITMAPINFOHEADER)); if(funcret!=FUNC_OK) { showerror(funcret); return NULL; } return lpPtr; }
/**************************************************************************** * * FUNCTION: DIBToIconImage * * PURPOSE: Converts a CF_DIB memory block to an icon image * * PARAMS: LPICONIMAGE lpii - pointer to icon image data * LPBYTE lpDIB - a pointer to the CF_DIB block * BOOL bStretchToFit - TRUE to stretch, FALSE to take * the upper left corner of the DIB * * RETURNS: BOOL - TRUE for success, FALSE for failure * * History: * July '95 - Created * \****************************************************************************/ BOOL DIBToIconImage( LPICONIMAGE lpii, LPBYTE lpDIB, BOOL bStretch ) { LPBYTE lpNewDIB; // Sanity check if( lpDIB == NULL ) return FALSE; // Let the DIB engine convert color depths if need be lpNewDIB = ConvertDIBFormat( (LPBITMAPINFO)lpDIB, lpii->Width, lpii->Height, lpii->Colors, bStretch ); // Now we have a cool new DIB of the proper size/color depth // Lets poke it into our data structures and be done with it // How big is it? lpii->dwNumBytes = sizeof( BITMAPINFOHEADER ) // Header + PaletteSize( (LPSTR)lpNewDIB ) // Palette + lpii->Height * BytesPerLine( (LPBITMAPINFOHEADER)lpNewDIB ) // XOR mask + lpii->Height * WIDTHBYTES( lpii->Width ); // AND mask // If there was already an image here, free it if( lpii->lpBits != NULL ) free( lpii->lpBits ); // Allocate enough room for the new image if( (lpii->lpBits = malloc( lpii->dwNumBytes )) == NULL ) { free( lpii ); return FALSE; } // Copy the bits memcpy( lpii->lpBits, lpNewDIB, sizeof( BITMAPINFOHEADER ) + PaletteSize( (LPSTR)lpNewDIB ) ); // Adjust internal pointers/variables for new image lpii->lpbi = (LPBITMAPINFO)(lpii->lpBits); lpii->lpbi->bmiHeader.biHeight *= 2; lpii->lpXOR = FindDIBBits( (LPSTR)(lpii->lpBits) ); memcpy( lpii->lpXOR, FindDIBBits((LPSTR)lpNewDIB), lpii->Height * BytesPerLine( (LPBITMAPINFOHEADER)lpNewDIB ) ); lpii->lpAND = lpii->lpXOR + lpii->Height * BytesPerLine( (LPBITMAPINFOHEADER)lpNewDIB ); memset( lpii->lpAND, 0, lpii->Height * WIDTHBYTES( lpii->Width ) ); // Free the source free( lpNewDIB ); return TRUE; }
HANDLE AllocRoomForDIB(BITMAPINFOHEADER bi, HBITMAP hBitmap) { DWORD dwLen; HANDLE hDIB; HDC hDC; LPBITMAPINFOHEADER lpbi; HANDLE hTemp; /* Figure out the size needed to hold the BITMAPINFO structure * (which includes the BITMAPINFOHEADER and the color table). */ dwLen = bi.biSize + PaletteSize((LPSTR) &bi); hDIB = GlobalAlloc(GHND,dwLen); /* Check that DIB handle is valid */ if (!hDIB) return NULL; /* Set up the BITMAPINFOHEADER in the newly allocated global memory, * then call GetDIBits() with lpBits = NULL to have it fill in the * biSizeImage field for us. */ lpbi = (VOID FAR *)GlobalLock(hDIB); *lpbi = bi; hDC = GetDC(NULL); GetDIBits(hDC, hBitmap, 0, (WORD) bi.biHeight, NULL, (LPBITMAPINFO) lpbi, DIB_RGB_COLORS); ReleaseDC(NULL, hDC); /* If the driver did not fill in the biSizeImage field, * fill it in -- NOTE: this is a bug in the driver! */ if (lpbi->biSizeImage == 0) lpbi->biSizeImage = WIDTHBYTES((DWORD)lpbi->biWidth * lpbi->biBitCount) * lpbi->biHeight; /* Get the size of the memory block we need */ dwLen = lpbi->biSize + PaletteSize((LPSTR) &bi) + lpbi->biSizeImage; /* Unlock the memory block */ GlobalUnlock(hDIB); /* ReAlloc the buffer big enough to hold all the bits */ if (hTemp = GlobalReAlloc(hDIB,dwLen,0)) return hTemp; else { /* Else free memory block and return failure */ GlobalFree(hDIB); return NULL; } }
//在固定位置将点变为红色 void CIconOperate::SetPointColor(int x, int y, DWORD color) { if(m_IconData == NULL || m_IconDir == NULL) return ; if (x < 0 || x >= m_IconDir->idEntries[0].bwidth || y < 0 || y >= m_IconDir->idEntries[0].bheight ) return ; int index = (y * WIDTHBYTES(m_IconDir->idEntries[0].bwidth,m_IconDir->idEntries[0].wbitcount) + x*4); *(DWORD*)(&m_IconData->data[0].icxor[index]) = color ; }
// load jpeg file BOOL CJpeg::Load(LPCSTR lpstrFileName) { UINT uWidth, uHeight, uWidthDW; // read the jpeg to a packed buffer of RGB bytes BYTE *lpTmpBuffer = ReadJPEGFile(lpstrFileName, &uWidth, &uHeight); if (lpTmpBuffer == NULL) return FALSE; // do this before DWORD-alignment!!! // swap red and blue for display BGRFromRGB(lpTmpBuffer, uWidth, uHeight); // now DWORD-align for display BYTE *lpBuffer = MakeDwordAlign(lpTmpBuffer, uWidth, uHeight, &uWidthDW); FreeBuffer(lpTmpBuffer); // flip for display VertFlipBuf(lpBuffer, uWidthDW, uHeight); BITMAPINFOHEADER bmiHeader; bmiHeader.biSize = sizeof(BITMAPINFOHEADER); bmiHeader.biWidth = uWidth; bmiHeader.biHeight = uHeight; bmiHeader.biPlanes = 1; bmiHeader.biBitCount = 24; bmiHeader.biCompression = BI_RGB; bmiHeader.biSizeImage = 0; bmiHeader.biXPelsPerMeter = 0; bmiHeader.biYPelsPerMeter = 0; bmiHeader.biClrUsed = 0; bmiHeader.biClrImportant = 0; // Allocate enough memory for the new CF_DIB, and copy bits DWORD dwHeaderSize = sizeof(BITMAPINFOHEADER); DWORD dwBitsSize = WIDTHBYTES(uWidth*24) * uHeight; HDIB hDIB = GlobalAlloc(GHND, dwHeaderSize + dwBitsSize); if (hDIB == NULL) return FALSE; LPBYTE lpDIB = (LPBYTE)GlobalLock(hDIB); memcpy(lpDIB, (LPBYTE)&bmiHeader, dwHeaderSize); memcpy(FindDIBBits((LPBYTE)lpDIB), lpBuffer, dwBitsSize); FreeBuffer(lpBuffer); if (m_pDib != NULL) delete m_pDib; m_pDib = new CDib(); m_pDib->Attach(hDIB); return TRUE; }
/**************************************************************************** * * FUNCTION: CreateBlankNewFormatIcon * * PURPOSE: Creates a blank icon image for a new format * * PARAMS: LPICONIMAGE lpii - pointer to icon image data * * RETURNS: BOOL - TRUE for success, FALSE for failure * * History: * July '95 - Created * \****************************************************************************/ BOOL CreateBlankNewFormatIcon( LPICONIMAGE lpii ) { DWORD dwFinalSize; BITMAPINFOHEADER bmih; // Fill in the bitmap header ZeroMemory( &bmih, sizeof( BITMAPINFOHEADER ) ); bmih.biSize = sizeof( BITMAPINFOHEADER ); bmih.biBitCount = lpii->Colors; bmih.biClrUsed = 0; // How big will the final thing be? // Well, it'll have a header dwFinalSize = sizeof( BITMAPINFOHEADER ); // and a color table (even if it's zero length) dwFinalSize += PaletteSize( (LPSTR)&bmih ); // and XOR bits dwFinalSize += lpii->Height * WIDTHBYTES( lpii->Width * lpii->Colors ); // and AND bits. That's about it :) dwFinalSize += lpii->Height * WIDTHBYTES( lpii->Width ); // Allocate some memory for it lpii->lpBits = malloc( dwFinalSize ); ZeroMemory( lpii->lpBits, dwFinalSize ); lpii->dwNumBytes = dwFinalSize; lpii->lpbi = (LPBITMAPINFO)(lpii->lpBits); lpii->lpXOR = (LPSTR)(lpii->lpbi) + sizeof(BITMAPINFOHEADER) + PaletteSize( (LPSTR)&bmih ); lpii->lpAND = lpii->lpXOR + (lpii->Height * WIDTHBYTES( lpii->Width * lpii->Colors )); // The bitmap header is zeros, fill it out lpii->lpbi->bmiHeader.biSize = sizeof( BITMAPINFOHEADER ); lpii->lpbi->bmiHeader.biWidth = lpii->Width; // Don't forget the funky height*2 icon resource thing lpii->lpbi->bmiHeader.biHeight = lpii->Height * 2; lpii->lpbi->bmiHeader.biPlanes = 1; lpii->lpbi->bmiHeader.biBitCount = lpii->Colors; lpii->lpbi->bmiHeader.biCompression = BI_RGB; return TRUE; }
/* * Get a strip-organized image that has * PlanarConfiguration contiguous if SamplesPerPixel > 1 * or * SamplesPerPixel == 1 * * Hacked from the tif_getimage.c file. * * This is set up to allow us to just copy the data to the raster * for 1-bit bitmaps */ static int getStripContig1Bit(TIFFRGBAImage* img, uint32* raster, uint32 w, uint32 h) { TIFF* tif = img->tif; tileContigRoutine put = img->put.contig; uint16 orientation; uint32 row, y, nrow, rowstoread; uint32 pos; u_char* buf; uint32 rowsperstrip; uint32 imagewidth = img->width; tsize_t scanline; int32 fromskew, toskew; tstrip_t strip; tsize_t stripsize; u_char* braster = (u_char*)raster; // byte wide raster uint32 wb = WIDTHBYTES(w); int ret = 1; buf = (u_char*) _TIFFmalloc(TIFFStripSize(tif)); if (buf == 0) { TIFFErrorExt(tif->tif_clientdata, TIFFFileName(tif), "No space for strip buffer"); return (0); } y = setorientation(img, h); orientation = img->orientation; toskew = -(int32) (orientation == ORIENTATION_TOPLEFT ? wb+wb : wb-wb); TIFFGetFieldDefaulted(tif, TIFFTAG_ROWSPERSTRIP, &rowsperstrip); scanline = TIFFScanlineSize(tif); fromskew = (w < imagewidth ? imagewidth - w : 0)/8; for (row = 0; row < h; row += nrow) { rowstoread = rowsperstrip - (row + img->row_offset) % rowsperstrip; nrow = (row + rowstoread > h ? h - row : rowstoread); strip = TIFFComputeStrip(tif,row+img->row_offset, 0); stripsize = ((row + img->row_offset)%rowsperstrip + nrow) * scanline; if (TIFFReadEncodedStrip(tif, strip, buf, stripsize ) < 0 && img->stoponerr) { ret = 0; break; } pos = ((row + img->row_offset) % rowsperstrip) * scanline; (*put)(img, (uint32*)(braster+y*wb), 0, y, w, nrow, fromskew, toskew, buf + pos); y += (orientation == ORIENTATION_TOPLEFT ?-(int32) nrow : (int32) nrow); } _TIFFfree(buf); return (ret); }
// // copies BYTE buffer into DWORD-aligned BYTE buffer // return addr of new buffer // BYTE * CJpeg::MakeDwordAlign(BYTE *dataBuf, UINT widthPix, // pixels!! UINT height, UINT *uiOutWidthBytes) // bytes!!! { //////////////////////////////////////////////////////////// // what's going on here? this certainly means trouble if (dataBuf==NULL) return NULL; //////////////////////////////////////////////////////////// // how big is the smallest DWORD-aligned buffer that we can use? UINT uiWidthBytes; uiWidthBytes = WIDTHBYTES(widthPix * 24); DWORD dwNewsize=(DWORD)((DWORD)uiWidthBytes * (DWORD)height); BYTE *pNew; //////////////////////////////////////////////////////////// // alloc and open our new buffer pNew=(BYTE *)new BYTE[dwNewsize]; if (pNew==NULL) { return NULL; } //////////////////////////////////////////////////////////// // copy row-by-row UINT uiInWidthBytes = widthPix * 3; UINT uiCount; for (uiCount=0;uiCount < height;uiCount++) { BYTE * bpInAdd; BYTE * bpOutAdd; ULONG lInOff; ULONG lOutOff; lInOff=uiInWidthBytes * uiCount; lOutOff=uiWidthBytes * uiCount; bpInAdd= dataBuf + lInOff; bpOutAdd= pNew + lOutOff; memcpy(bpOutAdd,bpInAdd,uiInWidthBytes); } *uiOutWidthBytes=uiWidthBytes; return pNew; }
void GetPoints(int nWidth,int nHeight,BYTE *lpBits,BYTE *lpPoints) { int x,y,p; int nByteWidth = WIDTHBYTES(nWidth*24); //nWidth*3; //if (nByteWidth%4) nByteWidth+=4-(nByteWidth%4); for(y=0;y<nHeight;y++) { for(x=0;x<nWidth;x++) { p=x*3+y*nByteWidth; lpPoints[x+y*nWidth]=(BYTE)(0.299*(float)lpBits[p+2]+0.587*(float)lpBits[p+1]+0.114*(float)lpBits[p]+0.1); } } }
unsigned char * JpegFile::MakeJPG_DWORDAlignedBuf(unsigned char *dataBuf, JPG_UINT widthPix, // pixels!! JPG_UINT height, JPG_UINT *uiOutWidthBytes) // bytes!!! { //////////////////////////////////////////////////////////// // what's going on here? this certainly means trouble if (dataBuf==NULL) return NULL; //////////////////////////////////////////////////////////// // how big is the smallest JPG_DWORD-aligned buffer that we can use? JPG_UINT uiWidthBytes; uiWidthBytes = WIDTHBYTES(widthPix * 24); JPG_DWORD dwNewsize=(JPG_DWORD)((JPG_DWORD)uiWidthBytes * (JPG_DWORD)height); unsigned char *pNew; //////////////////////////////////////////////////////////// // alloc and open our new buffer pNew=(unsigned char *)new unsigned char[dwNewsize]; if (pNew==NULL) { return NULL; } //////////////////////////////////////////////////////////// // copy row-by-row JPG_UINT uiInWidthBytes = widthPix * 3; JPG_UINT uiCount; for (uiCount=0;uiCount < height;uiCount++) { unsigned char * bpInAdd; unsigned char * bpOutAdd; JPG_ULONG lInOff; JPG_ULONG lOutOff; lInOff=uiInWidthBytes * uiCount; lOutOff=uiWidthBytes * uiCount; bpInAdd= dataBuf + lInOff; bpOutAdd= pNew + lOutOff; memcpy(bpOutAdd,bpInAdd,uiInWidthBytes); } *uiOutWidthBytes=uiWidthBytes; return pNew; }
/************************************************************************* 函数名称: SaperaToBmpFmt() 功能描述: 将图像数据从Sapera格式转换到BMP格式 输入参数: 无 输出参数: 无 返 回 值: TRUE:转换成功 FALSE:转换失败(不支持的数据格式) 其它说明: BMP格式数据组织方式和Sapera的不同,BMP格式数据从下到上排列,且每行 字节数为4的倍数,Sapera格式数据从上到下,每行字节数为实际宽度对应 字节数。但GDI显示时只支持BMP格式,为了便于其他项目组直接使用算法库, 故作此转换。此外,Sapera格式数据也是算法库采用的格式。 作 者: 周明才 编写时间: 2005.12.16 *************************************************************************/ BOOL CMyDib::SaperaToBmpFmt() { WORD wBitCount; // DIB bit count wBitCount = m_pBMI->bmiHeader.biBitCount; switch (wBitCount) //只处理8位、24位和32位图像 { case 8: case 24: case 32: break; default: return FALSE; } int iWidth = Width(); int iHeight = Height(); int iLineBytes; //每行字节数(BMP格式) int iWidthBytes; //每行字节数(Sapera格式) UINT nCount; //BMP格式数据大小(BMP格式) LPBYTE pbyDataCpy, pbyTmpRes, pbyTmpDst; // Calculate the number of bytes per line iLineBytes = WIDTHBYTES(wBitCount * Width()); nCount = iLineBytes * iHeight; iWidthBytes = (wBitCount * Width()) / 8; pbyDataCpy = new BYTE[nCount]; memcpy(pbyDataCpy, m_pBits, nCount); //为简单起见,做一份拷贝, int j; pbyTmpDst = m_pBits + nCount - iLineBytes; pbyTmpRes = pbyDataCpy; //从pbyDataCpy的底部开始取各行数据,对m_pBits从顶部开始填充。 for (j=0; j<iHeight; j++) { memcpy(pbyTmpDst, pbyTmpRes, iWidthBytes); pbyTmpDst -= iLineBytes; pbyTmpRes += iWidthBytes; } delete [] pbyDataCpy; return TRUE; }
doublemap_t *doublemap_create(int w, int h) { int pitch = WIDTHBYTES(w*sizeof(double)*8); doublemap_t *m; m = (doublemap_t *)malloc(sizeof(*m)+h*pitch); assert(m); if (m == NULL) goto __error_return__; m->header.width = w; m->header.height = h; m->header.pitch = pitch; m->buffer = (double *)((unsigned char *)m+sizeof(*m)); memset(m->buffer, 0, h*pitch); return m; __error_return__:; return NULL; }
bool Bmp2Jpg(LPBITMAPINFOHEADER head,LPBYTE data, BYTE* &lpJpgBuf, DWORD &nSize) { try{ int width = head->biWidth; int height = head->biHeight; int nchannels= head->biBitCount / 8; IplImage *Img = NULL; Img = cvCreateImage(cvSize(width, height), 8, 3); int i,j; int lw = WIDTHBYTES(head->biBitCount*head->biWidth); for(i=0;i<height;++i) for(j=0;j<width;++j) { Img->imageData[(height-1-i)*Img->widthStep+j*3+0] = data[i*lw+nchannels*j+0]; Img->imageData[(height-1-i)*Img->widthStep+j*3+1] = data[i*lw+nchannels*j+1]; Img->imageData[(height-1-i)*Img->widthStep+j*3+2] = data[i*lw+nchannels*j+2]; } //取得运行程序的绝对路径 CString strPath(__argv[0]); strPath = strPath.Left(strPath.ReverseFind('\\')); CString sFile(strPath+"\\TempJpgFile.jpg"); cvSaveImage(sFile, Img); try{ CFile jpgFile(sFile, CFile::modeRead); nSize = jpgFile.GetLength(); lpJpgBuf = new BYTE[nSize]; jpgFile.Read(lpJpgBuf, nSize); jpgFile.Close(); } catch( CFileException e) { CString ErrorInfoStr; ErrorInfoStr.Format("文件"+e.m_strFileName+"读入失败!"); AfxMessageBox(ErrorInfoStr); } CFile::Remove( sFile ); return true; } catch(...) { ASSERT(NULL); return false; } }
// save Jpeg file BOOL CJpeg::Save(LPCSTR lpstrFileName, CDib* pDib, BOOL bColor, int nQuality) { if (pDib == NULL) pDib = m_pDib; if (pDib == NULL) return FALSE; HDIB hDib = CopyHandle(pDib->GetHandle()); if (hDib == NULL) return FALSE; CDib* pDibTmp = new CDib; pDibTmp->Attach(hDib); if (pDibTmp->GetBitCount() != 24) pDibTmp->ConvertFormat(24); UINT uWidth = pDibTmp->GetWidth(); UINT uHeight = pDibTmp->GetHeight(); // convert from DIB format (DWORD aligned, vertically flipped, red and blue swapped) BYTE* tmp = ClearDwordAlign(pDibTmp->GetBitsPtr(), uWidth, WIDTHBYTES(uWidth * 24), uHeight); if (tmp == NULL) return FALSE; // convert from DIB VertFlipBuf(tmp, uWidth*3, uHeight); BGRFromRGB(tmp, uWidth, uHeight); BOOL bSuccess = WriteJPEGFile(lpstrFileName, tmp, uWidth, uHeight, bColor, nQuality); delete pDibTmp; FreeBuffer(tmp); return bSuccess; }
// 计算长轴长度 double SpermParamCalc::GetLength() { long Width = m_lpDibIHPro->biWidth; long Height= m_lpDibIHPro->biHeight; long ByteWidth = WIDTHBYTES(Width * m_lpDibIHPro->biBitCount); int n = m_HeadEdgePoint.size(); double dist, max = 0; LPBYTE lpSrcI = NULL, lpSrcJ = NULL; int i, j; int maxI, maxJ; maxI = 0; maxJ = 0; for(i=0; i<n; i++) { for(j=i+1; j<n; j++) { lpSrcI = m_lpDibDataPro + m_HeadEdgePoint[i].x*ByteWidth + m_HeadEdgePoint[i].y*3; lpSrcJ = m_lpDibDataPro + m_HeadEdgePoint[j].x*ByteWidth + m_HeadEdgePoint[j].y*3; if( PIEXL(lpSrcI) != PIEXL(lpSrcJ) ) { dist = distance(m_HeadEdgePoint[i], m_HeadEdgePoint[j]); if(dist > max) { max = dist; maxI = i; maxJ = j; } } } } if(n == 0) return sqrt(max); lpSrcI = m_lpDibDataPro + m_HeadEdgePoint[maxI].x*ByteWidth + m_HeadEdgePoint[maxI].y*3; if( PIEXL(lpSrcI) == BLUE_PIEXL ){ m_ProlateTip[0] = m_HeadEdgePoint[maxI]; // 让头尾相接处点存放于m_ProlateTip[0] m_ProlateTip[1] = m_HeadEdgePoint[maxJ]; } else{ m_ProlateTip[0] = m_HeadEdgePoint[maxJ]; // 让头尾相接处点存放于m_ProlateTip[0] m_ProlateTip[1] = m_HeadEdgePoint[maxI]; } return sqrt(max); }
void PutPoints(int nWidth,int nHeight,BYTE *lpBits,BYTE *lpPoints) { int x,y,p,p1; int nByteWidth = WIDTHBYTES(nWidth*24); //nWidth*3; //if (nByteWidth%4) nByteWidth+=4-(nByteWidth%4); for(y=0;y<nHeight;y++) { for(x=0;x<nWidth;x++) { p=x*3+y*nByteWidth; p1=x+y*nWidth; lpBits[p]=lpPoints[p1]; lpBits[p+1]=lpPoints[p1]; lpBits[p+2]=lpPoints[p1]; } } }
/* Bestimmen der DIB - Info aus einem CF_DIB Speicherblock */ bool CBildObjekt :: DIBInfo (BITMAPINFOHEADER *lpBI) { if (m_hDIB) { // BitmapInfoHeader kopieren *lpBI = *(BITMAPINFOHEADER *)GlobalLock (m_hDIB); // DefaultWerte setzen if (lpBI->biSizeImage == 0L) lpBI->biSizeImage = lpBI->biHeight * WIDTHBYTES ((ULONG)lpBI->biWidth*lpBI->biBitCount); if (lpBI->biClrUsed == 0L) lpBI->biClrUsed = DIBNumColors (lpBI); GlobalUnlock (m_hDIB); return true; } return false; }
void CImg::CleanUp() { if(m_lpData != NULL) { int nWidthBytes = WIDTHBYTES((m_pBMIH->biWidth)*m_pBMIH->biBitCount); for(int i=0; i<m_pBMIH->biHeight; i++) { delete[] m_lpData[i]; } delete[] m_lpData; } if(m_pBMIH != NULL) { delete[] m_pBMIH; m_pBMIH = NULL; } }
BOOL CImage::SaveBMP(LPCTSTR lpszFileName) { ///* CFile file; CFileException fe; BITMAPFILEHEADER bmfHdr; LPBITMAPINFOHEADER lpBI; DWORD dwDIBSize; if (!file.Open(lpszFileName, CFile::modeCreate | CFile::modeWrite | CFile::shareDenyWrite, &fe)) return FALSE; if (m_hImage == NULL) return FALSE; lpBI = (LPBITMAPINFOHEADER)::GlobalLock((HGLOBAL)m_hImage); if (lpBI == NULL) return FALSE; bmfHdr.bfType = DIB_HEADER_MARKER; // "BM" dwDIBSize = *(LPDWORD)lpBI + ::PaletteSize((LPSTR)lpBI); if((lpBI->biCompression==BI_RLE8) || (lpBI->biCompression==BI_RLE4)) dwDIBSize += lpBI->biSizeImage; else { DWORD dwBmBitsSize; // Size of Bitmap Bits only dwBmBitsSize = WIDTHBYTES((lpBI->biWidth)*((DWORD)lpBI->biBitCount)) * lpBI->biHeight; dwDIBSize += dwBmBitsSize; lpBI->biSizeImage = dwBmBitsSize; } bmfHdr.bfSize = dwDIBSize + sizeof(BITMAPFILEHEADER); bmfHdr.bfReserved1 = 0; bmfHdr.bfReserved2 = 0; bmfHdr.bfOffBits=(DWORD)sizeof(BITMAPFILEHEADER)+lpBI->biSize + PaletteSize((LPSTR)lpBI); TRY { file.Write((LPSTR)&bmfHdr, sizeof(BITMAPFILEHEADER)); //file.WriteHuge(lpBI, dwDIBSize); file.Write(lpBI, dwDIBSize); } CATCH (CFileException, e) { ::GlobalUnlock((HGLOBAL) m_hImage); THROW_LAST(); }
/**************************************************************************** * * FUNCTION: SetMonoDIBPixel * * PURPOSE: Sets/Clears a pixel in a 1bpp DIB by directly poking the bits. * * PARAMS: LPBYTE pANDBits - pointer to the 1bpp image bits * DWORD dwWidth - width of the DIB * DWORD dwHeight - height of the DIB * DWORD x - x location of pixel to set/clear * DWORD y - y location of pixel to set/clear * BOOL bWhite - TRUE to set pixel, FALSE to clear it * * RETURNS: void * * History: * July '95 - Created * \****************************************************************************/ void SetMonoDIBPixel( LPBYTE pANDBits, DWORD dwWidth, DWORD dwHeight, DWORD x, DWORD y, BOOL bWhite ) { DWORD ByteIndex; BYTE BitNumber; // Find the byte on which this scanline begins ByteIndex = (dwHeight - y - 1) * WIDTHBYTES(dwWidth); // Find the byte containing this pixel ByteIndex += (x >> 3); // Which bit is it? BitNumber = (BYTE)( 7 - (x % 8) ); if( bWhite ) // Turn it on pANDBits[ByteIndex] |= (1<<BitNumber); else // Turn it off pANDBits[ByteIndex] &= ~(1<<BitNumber); }
bool Dib3ToIplImage1(LPBITMAPINFOHEADER head,LPBYTE data, IplImage* img) { try { int i,j; int channels = 1; int lw = WIDTHBYTES(head->biBitCount*head->biWidth); for(i=0;i<head->biHeight;++i) for(j=0;j<head->biWidth;++j) img->imageData[(head->biHeight-1-i)*img->widthStep + j + 0] = data[i*lw + j*3 + 0]; return true; } catch (...) { ASSERT(NULL); return false; } }
// 标记椭圆轮廓 void SpermParamCalc::OnMarkEllipse() { long Width = m_lpDibIHPro->biWidth; long Height= m_lpDibIHPro->biHeight; long ByteWidth = WIDTHBYTES(Width * m_lpDibIHPro->biBitCount); int n = m_HeadEdgePoint.size(); int i; LPBYTE lpSrc; for(i=0; i<n; i++) { lpSrc = m_lpDibDataPro + m_HeadEdgePoint[i].x*ByteWidth + m_HeadEdgePoint[i].y*3; *(lpSrc+ 0) = 0; *(lpSrc+ 1) = 0; *(lpSrc+ 2) = 0; } }
void AdjustDIBBrightness(unsigned char* rgbImg, unsigned char* chImg, int rows, int cols,const int v) { int nDelta = WIDTHBYTES(24*cols) - cols*24/8; BYTE r, g, b; for (int ny=0; ny<rows; ny++) { for (int nx=0; nx<cols/2; nx++){ const unsigned int m = ny*cols*3+3*nx; b = rgbImg[m]; g = rgbImg[m+1]; r = rgbImg[m+2]; ChangeBrightness(v, &r, &g, &b); //ChangeContrast(-v+10, &r, &g, &b); rgbImg[m] = b; rgbImg[m+1] = g; rgbImg[m+2] = r; } } }
bool CBitmapShow::InitialBitmap(int width,int height) { if((Width==width)&&(Height==height)) return true; Width=width; Height=height; if(m_lpBitmapInfo!=NULL) GlobalFreePtr(m_lpBitmapInfo); int colors=GetNumberColors(); if(colors==0) colors=256; m_lpBitmapInfo = (LPBITMAPINFO) GlobalAllocPtr(GHND,sizeof(BITMAPINFOHEADER) + sizeof(RGBQUAD) * colors); m_lpBitmapInfo->bmiHeader.biSize = sizeof(BITMAPINFOHEADER); m_lpBitmapInfo->bmiHeader.biWidth = Width; m_lpBitmapInfo->bmiHeader.biHeight = Height; m_lpBitmapInfo->bmiHeader.biCompression=GetCompressionKind(); m_lpBitmapInfo->bmiHeader.biSizeImage = 0; m_lpBitmapInfo->bmiHeader.biXPelsPerMeter = 0; m_lpBitmapInfo->bmiHeader.biYPelsPerMeter = 0; m_lpBitmapInfo->bmiHeader.biPlanes = 1; m_lpBitmapInfo->bmiHeader.biBitCount =GetBitCount(); m_lpBitmapInfo->bmiHeader.biClrUsed = 0; m_lpBitmapInfo->bmiHeader.biClrImportant = 0; ILineBytes=WIDTHBYTES(Width*GetBitCount()); m_lpBitmapInfo->bmiHeader.biSizeImage=ILineBytes*Height; for(int k=0;k<colors;k++) m_lpBitmapInfo->bmiColors[k].rgbRed=m_lpBitmapInfo->bmiColors[k].rgbGreen=m_lpBitmapInfo->bmiColors[k].rgbBlue=k; if(!CreateDIBPalette()) { if(m_lpBitmapInfo!=NULL) GlobalFreePtr(m_lpBitmapInfo); m_lpBitmapInfo=NULL; return false; } if(lpDIBBits!=NULL) GlobalFreePtr(lpDIBBits); lpDIBBits=NULL; lpDIBBits=(unsigned char*)GlobalAllocPtr(GHND,ILineBytes*Height); if(lpDIBBits==NULL) { if(m_lpBitmapInfo!=NULL) GlobalFreePtr(m_lpBitmapInfo); m_lpBitmapInfo=NULL; if(Palette!=NULL) delete Palette; Palette=NULL; return false; } return true; }
bool CBmp::load(const char* p_file) { FILE* fp = NULL; fp = fopen(p_file, "rb"); if (fp == NULL) return false; head _head; info _info; fread(&_head, 1, sizeof(head), fp); fread(&_info, 1, sizeof(info), fp); if (_head.id != 19778 || _info.compress != 0 || _info.bit_per_pixel != 24) return false; m_w = _info.pixel_width; m_h = _info.pixel_height; u32 l_width = WIDTHBYTES(m_w * _info.bit_per_pixel); u32 color_data_size = m_h * l_width; u8* p_color_data = (u8*)malloc(color_data_size); memset(p_color_data, 0, color_data_size); fread(p_color_data, 1, color_data_size, fp); rgb* p_rgb = NULL; p_rgb = (rgb*)malloc(m_w * m_h * sizeof(rgb)); memset(p_rgb, 0, m_w * m_h * sizeof(rgb)); u32 idx = 0; for (u32 y = 0; y < m_h; y++) { for (u32 x = 0; x < m_w; x++) { u32 k = y * l_width + x * 3; p_rgb[idx].r = p_color_data[k + 2]; p_rgb[idx].g = p_color_data[k + 1]; p_rgb[idx].b = p_color_data[k]; idx++; } } m_p_rgb = (u8*)p_rgb; free(p_color_data); fclose(fp); return true; }
// 计算头部面积 double SpermParamCalc::GetHeadArea() { long Width = m_lpDibIHPro->biWidth; long Height= m_lpDibIHPro->biHeight; long ByteWidth = WIDTHBYTES(Width * m_lpDibIHPro->biBitCount); int area = 0; LPBYTE lpSrc; int i, j; for(i=0; i<Height; i++) { for(j=0; j<Width; j++) { lpSrc = m_lpDibDataPro+ i*ByteWidth + j*3; if( PIEXL(lpSrc) == BLUE_PIEXL ) // 蓝色 area++; } } return area; }
bool DibToIplImage(LPBITMAPINFOHEADER head,LPBYTE data,IplImage* img) { try { int i,j,k; int channels=head->biBitCount/8; for(i=0;i<head->biHeight;++i) for(j=0;j<head->biWidth;++j) for(k=0;k<channels;++k) { img->imageData[(head->biHeight-1-i)*img->widthStep+j*channels+k] = data[i*WIDTHBYTES(head->biBitCount*head->biWidth)+channels*j+k]; } return true; } catch (...) { ASSERT(NULL); return false; } }