Read pixel data from bitmap file

Source: Internet
Author: User

/* ===================================================== ========================================< Br/> * getbmp data. cpp <br/> * This mex-file is used to read pixel data from bmp file directly <br/> * from bitmap file. <br/> * This is a MEX-file for MATLAB. <br/> * Created by visionfans @ 2011.06.16 <br/> * ================== ========================================================== */<br/> # include <stdio. h> <br/> # include <string. h> <br/> # include <windows. h> <br/> # include "mex. h "<br/> BYTE Bit2Byte (BYTE * pData, int iRow, int iCol, int nRowWidth); <br/> void mexFunction (int nlhs, mxArray * plhs [], <br/> int nrhs, const mxArray * prhs []) <br/> {<br/> // read bmp file <br/> // <br/> char * bmp Path = mxArrayToString (prhs [0] ); <br/> FILE * pFile = fopen (BMP path, "rb"); <br/> if (pFile = NULL) {<br/> mexWarnMsgTxt ("Open File Error/n"); <br/>}< br/> // get file size <br/> fseek (pFile, 0L, SEEK_END); <br/> DWORD fileSize = ftell (pFile); <br/> // read the entire file <br/> BYTE * pData = new BYTE [fileSize]; <br/> ZeroMemory (pData, fileSize); <br/> fseek (pFile, 0L, SEEK_SET); <br/> fread (pData, 1, fileSize, pFile ); <br/> // get the image matrix from bmp file <br/> // <br/> BITMAPFILEHEADER Bfh; // file header <br/> LPBITMAPINFO lpBmi; // bitmap info <br/> int BmiSize; <br/> // get bmp image information <br/> // <br/> memcpy (& Bfh, pData, sizeof (BITMAPFILEHEADER )); <br/> BmiSize = Bfh. bfOffBits-sizeof (BITMAPFILEHEADER); <br/> lpBmi = (LPBITMAPINFO) malloc (BmiSize); <br/> memcpy (lpBmi, pData + sizeof (BITMAPFILEHEADER), BmiSize ); <br/> // get image data <br/> // <br/> int nWidth = lpBmi-> bmiHeader. biWidth; <br/> int nHeight = lpBmi-> bmiHeader. biHeight; <br/> int bits = lpBmi-> bmiHeader. biBitCount; <br/> // point to image data <br/> BYTE * pImgData = pData + Bfh. bfOffBits; <br/> // copy all bit data to byte data <br/> // <br/> BYTE * pOutData = (BYTE *) mxCalloc (nWidth * nHeight, sizeof (BYTE); <br/> // copy data <br/> int nRowWidth = (bits * nWidth + 31)> 5) <2; <br/> int arrayIdx = 0; <br/> for (int j = 0; j <nWidth; j ++) <br/> {<br/> for (int I = 0; I <nHeight; I ++) <br/> {<br/> pOutData [arrayIdx ++] = Bit2Byte (pImgData, I, j, nRowWidth ); <br/>}< br/> // <br/> // create output matrix and set data <br/> // <br/> int ndims = 2; <br/> int dims [] = {nHeight, nWidth}; <br/> plhs [0] = mxCreateNumericArray (ndims, dims, mxUINT8_CLASS, mxREAL ); <br/> mxSetData (plhs [0], pOutData); <br/> fclose (pFile); <br/> delete pData; <br/>}</p> <p> BYTE Bit2Byte (BYTE * pData, int iRow, int iCol, int nRowWidth) <br/>{< br/> BYTE byPixel = 0; <br/> // get byte index <br/> int iByteIdx = iCol/8; <br/> // get bit index <br/> int iBitIdx = iCol % 8; <br/> // get pixel value @ [iRow, iCol] <br/> BYTE bitshift = pData [iRow * nRowWidth + iByteIdx]> (7-iBitIdx); <br/> byPixel = bitshift & 1; <br/> return byPixel; <br/>}

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.