void OnCreate(HWND hWnd, WPARAM wParam, LPARAM lParam) { ::SetTimer(hWnd, TIMER_ID, TIMER_ELAPSE, 0); BITMAPINFO* pBMI = (BITMAPINFO*) &bmiBuff[0]; BITMAPINFO& bmi = *pBMI; dstWidth = 1920; dstHeight = 1080; #if 1 RGBQUAD rgb; for (size_t i=0; i<256; ++i) { rgb.rgbBlue = rgb.rgbGreen = rgb.rgbRed = i; bmi.bmiColors[i] = rgb; } hBMP = CreateDIB(dstWidth, -dstHeight, 8, bmi, pBits); #else int bitsPerPixel = 32; hBMP = CreateDIB(dstWidth, -dstHeight, bitsPerPixel, bmi, pBits); #endif HDC hWndDC = ::GetDC(hWnd); hMemDC = ::CreateCompatibleDC(hWndDC); ::SetMapMode(hMemDC, ::GetMapMode(hWndDC)); ::ReleaseDC(hWnd, hWndDC); ::SelectObject(hMemDC, hBMP); int argc; wchar_t** argv = CommandLineToArgvW(GetCommandLine(), &argc); if (argc >= 2) { loadImage(argv[1]); } }
RenderContext::RenderContext(Widget* widget) : widget_(widget) , gdi_(NULL) { HDC wnd_dc = BeginPaint(widget->hwnd(), &ps_); SetGraphicsMode(wnd_dc, GM_ADVANCED); SetMapMode(wnd_dc, MM_TEXT); SetBkMode(wnd_dc, OPAQUE); //SetBkMode(wnd_dc, TRANSPARENT); dc_ = ::CreateCompatibleDC(wnd_dc); //dc_ = wnd_dc; SetGraphicsMode(dc_, GM_ADVANCED); SetMapMode(dc_, MM_TEXT); SetBkMode(dc_, OPAQUE); //SetMapMode(dc_, MM_LOENGLISH); //SetWorldTransform(dc_, &Matrix().ToXFORM()); RECT rc; ::GetClientRect(widget->hwnd(), &rc); rect_ = rc; bitmap_ = CreateDIB(rect_.width(), rect_.height()); bitmap_prev_ = (HBITMAP)::SelectObject(dc_, bitmap_); gdi_ = new Gdiplus::Graphics(dc_); }
void CViewBitmap::SaveBitmapEx( CString sFile ) { // create a DIB bitmap int bmData; HANDLE hDIB = CreateDIB( &bmData ); // get a memory pointer to it LPBYTE lpBitmap = (LPBYTE)::GlobalLock(hDIB); int bmSize = ::GlobalSize( hDIB ); // create file CFile file; file.Open( sFile, CFile::modeCreate|CFile::modeWrite ); // write the bitmap header BITMAPFILEHEADER bmfh; bmfh.bfType ='MB'; // (actually 'BM' for bitmap) bmfh.bfSize = sizeof(BITMAPFILEHEADER) + bmSize; bmfh.bfReserved1 = 0; bmfh.bfReserved2 = 0; bmfh.bfOffBits =bmData; file.Write( &bmfh,sizeof( BITMAPFILEHEADER ) ); // write the bitmap body file.Write(lpBitmap,bmSize ); // cleanup file.Close(); ::GlobalUnlock( hDIB ); ::GlobalFree( hDIB); }
void CChildView::OpenDICOMFile(CString path) { //Allocate KDicomDS object KDicomDS *dicomDS = new KDicomDS; //Load File dicomDS-> LoadDS(path); //Get Parameters samplePerPixel = dicomDS->m_nSamplePerPixel; photometric = dicomDS->m_nPhotometric; width = dicomDS->m_nWidth; height = dicomDS->m_nHeight; bitsAllocated = dicomDS->m_nBitsAllocated; bitsStored = dicomDS->m_nBitsStored; pixelRepresentation = dicomDS->m_nRepresentation; windowCenter = dicomDS->m_dWindowCenter; windowWidth = dicomDS->m_dWindowWidth; if(bitsAllocated == 8) srcStep = width*samplePerPixel; else srcStep = width*2; //Allocate image processing source memory if(srcData) delete[] srcData; srcData = new unsigned char[srcStep*height]; dicomDS-> GetImageData(srcData); //Allocate image processing destination memory if(dstData) delete[] dstData; dstData = new unsigned char[srcStep*height]; memcpy(dstData, srcData, srcStep*height); //Delete DicomDS delete dicomDS; CreateDIB(); Trans16to8(); }
HDIB LoadTIFFinDIB(LPSTR lpFileName) { TIFF *tif; unsigned long imageLength; unsigned long imageWidth; unsigned int BitsPerSample; unsigned long LineSize; unsigned int SamplePerPixel; unsigned long RowsPerStrip; int PhotometricInterpretation; long nrow; unsigned long row; char *buf; LPBITMAPINFOHEADER lpDIB; HDIB hDIB; char *lpBits; HGLOBAL hStrip; int i,l; int Align; tif = TIFFOpen(lpFileName, "r"); if (!tif) goto TiffOpenError; TIFFGetField(tif, TIFFTAG_IMAGEWIDTH, &imageWidth); TIFFGetField(tif, TIFFTAG_IMAGELENGTH, &imageLength); TIFFGetField(tif, TIFFTAG_BITSPERSAMPLE, &BitsPerSample); TIFFGetField(tif, TIFFTAG_ROWSPERSTRIP, &RowsPerStrip); TIFFGetField(tif, TIFFTAG_ROWSPERSTRIP, &RowsPerStrip); TIFFGetField(tif, TIFFTAG_PHOTOMETRIC, &PhotometricInterpretation); LineSize = TIFFScanlineSize(tif); //Number of byte in ine line SamplePerPixel = (int) (LineSize/imageWidth); //Align = Number of byte to add at the end of each line of the DIB Align = 4 - (LineSize % 4); if (Align == 4) Align = 0; //Create a new DIB hDIB = CreateDIB((DWORD) imageWidth, (DWORD) imageLength, (WORD) (BitsPerSample*SamplePerPixel)); lpDIB = (LPBITMAPINFOHEADER) GlobalLock(hDIB); if (!lpDIB) goto OutOfDIBMemory; if (lpDIB) lpBits = FindDIBBits((LPSTR) lpDIB); //In the tiff file the lines are save from up to down //In a DIB the lines must be save from down to up if (lpBits) { lpBits = FindDIBBits((LPSTR) lpDIB); lpBits+=((imageWidth*SamplePerPixel)+Align)*(imageLength-1); //now lpBits pointe on the bottom line hStrip = GlobalAlloc(GHND,TIFFStripSize(tif)); buf = GlobalLock(hStrip); if (!buf) goto OutOfBufMemory; //PhotometricInterpretation = 2 image is RGB //PhotometricInterpretation = 3 image have a color palette if (PhotometricInterpretation == 3) { uint16* red; uint16* green; uint16* blue; int16 i; LPBITMAPINFO lpbmi; int Palette16Bits; TIFFGetField(tif, TIFFTAG_COLORMAP, &red, &green, &blue); //Is the palette 16 or 8 bits ? if (checkcmap(1<<BitsPerSample, red, green, blue) == 16) Palette16Bits = TRUE; else Palette16Bits = FALSE; lpbmi = (LPBITMAPINFO)lpDIB; //load the palette in the DIB for (i = (1<<BitsPerSample)-1; i >= 0; i--) { if (Palette16Bits) { lpbmi->bmiColors[i].rgbRed =(BYTE) CVT(red[i]); lpbmi->bmiColors[i].rgbGreen = (BYTE) CVT(green[i]); lpbmi->bmiColors[i].rgbBlue = (BYTE) CVT(blue[i]); } else { lpbmi->bmiColors[i].rgbRed = (BYTE) red[i]; lpbmi->bmiColors[i].rgbGreen = (BYTE) green[i]; lpbmi->bmiColors[i].rgbBlue = (BYTE) blue[i]; } } } //read the tiff lines and save them in the DIB //with RGB mode, we have to change the order of the 3 samples RGB <=> BGR for (row = 0; row < imageLength; row += RowsPerStrip) { nrow = (row + RowsPerStrip > imageLength ? imageLength - row : RowsPerStrip); if (TIFFReadEncodedStrip(tif, TIFFComputeStrip(tif, row, 0), buf, nrow*LineSize)==-1) { goto TiffReadError; } else { for (l = 0; l < nrow; l++) { if (SamplePerPixel == 3) for (i=0; i< (int) (imageWidth); i++) { lpBits[i*SamplePerPixel+0]=buf[l*LineSize+i*Sample PerPixel+2]; lpBits[i*SamplePerPixel+1]=buf[l*LineSize+i*Sample PerPixel+1]; lpBits[i*SamplePerPixel+2]=buf[l*LineSize+i*Sample PerPixel+0]; } else memcpy(lpBits, &buf[(int) (l*LineSize)], (int) imageWidth*SamplePerPixel); lpBits-=imageWidth*SamplePerPixel+Align; } } } GlobalUnlock(hStrip); GlobalFree(hStrip); GlobalUnlock(hDIB); TIFFClose(tif); }