麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

首頁 > 編程 > C > 正文

C語言讀取BMP圖像數(shù)據(jù)的源碼

2020-01-26 16:20:50
字體:
供稿:網(wǎng)友

復(fù)制代碼 代碼如下:

/* File name:   bmpTest.c
   Author:      WanChuan XianSheng
   Date:        Oct 01, 2011
   Description: Show all Info a bmp file has. including
   FileHeader Info, InfoHeader Info and Data Part.

   Reference: BMP圖像數(shù)據(jù)的C語言讀取源碼
*/

#include <stdio.h>
#include <stdlib.h>

#define BITMAPFILEHEADERLENGTH 14   // The bmp FileHeader length is 14
#define BM 19778                    // The ASCII code for BM

/* Test the file is bmp file or not */
void bmpFileTest(FILE* fpbmp);
/* To get the OffSet of header to data part */
void bmpHeaderPartLength(FILE* fpbmp);
/* To get the width and height of the bmp file */
void BmpWidthHeight(FILE* fpbmp);
/* Show bmp file tagBITMAPFILEHEADER info */
void bmpFileHeader(FILE* fpbmp);
/* Show bmp file tagBITMAPINFOHEADER info */
void bmpInfoHeader(FILE* fpbmp);
/* Show the Data Part of bmp file */
void bmpDataPart(FILE* fpbmp);

unsigned int OffSet = 0;    // OffSet from Header part to Data Part
long BmpWidth = 0;          // The Width of the Data Part
long BmpHeight = 0;         // The Height of the Data Part


int main(int argc, char* argv[])
{
     /* Open bmp file */
     FILE *fpbmp = fopen("lena.bmp", "r+");
     if (fpbmp == NULL)
     {
      fprintf(stderr, "Open lena.bmp failed!!!/n");
      return 1;
     }

     bmpFileTest(fpbmp);                //Test the file is bmp file or not
     bmpHeaderPartLength(fpbmp);        //Get the length of Header Part
     BmpWidthHeight(fpbmp);             //Get the width and width of the Data Part
     //bmpFileHeader(fpbmp);            //Show the FileHeader Information
     //bmpInfoHeader(fpbmp);            //Show the InfoHeader Information
     bmpDataPart(fpbmp);                //Reserve the data to file

     fclose(fpbmp);
     return 0;
}

/* Test the file is bmp file or not */
void bmpFileTest(FILE* fpbmp)
{    
     unsigned short bfType = 0;
     fseek(fpbmp, 0L, SEEK_SET);
     fread(&bfType, sizeof(char), 2, fpbmp);
     if (BM != bfType)
     {
      fprintf(stderr, "This file is not bmp file.!!!/n");
      exit(1);
     }
}

/* To get the OffSet of header to data part */
void bmpHeaderPartLength(FILE* fpbmp)
{
     fseek(fpbmp, 10L, SEEK_SET);
     fread(&OffSet, sizeof(char), 4, fpbmp);   
     //printf("The Header Part is of length %d./n", OffSet);
}

/* To get the width and height of the bmp file */
void BmpWidthHeight(FILE* fpbmp)
{
     fseek(fpbmp, 18L, SEEK_SET);
     fread(&BmpWidth, sizeof(char), 4, fpbmp);
     fread(&BmpHeight, sizeof(char), 4, fpbmp);
     //printf("The Width of the bmp file is %ld./n", BmpWidth);
     //printf("The Height of the bmp file is %ld./n", BmpHeight);
}

/* Show bmp file tagBITMAPFILEHEADER info */
void bmpFileHeader(FILE* fpbmp)
{
     unsigned short bfType;              //UNIT        bfType;
     unsigned int   bfSize;              //DWORD       bfSize;
     unsigned short bfReserved1;         //UINT        bfReserved1;
     unsigned short bfReserved2;         //UINT        bfReserved2;
     unsigned int   bfOffBits;           //DWORD       bfOffBits;

     fseek(fpbmp, 0L, SEEK_SET);

     fread(&bfType,      sizeof(char), 2, fpbmp);
     fread(&bfSize,      sizeof(char), 4, fpbmp);
     fread(&bfReserved1, sizeof(char), 2, fpbmp);
     fread(&bfReserved2, sizeof(char), 2, fpbmp);
     fread(&bfOffBits,   sizeof(char), 4, fpbmp);

     printf("************************************************/n");
     printf("*************tagBITMAPFILEHEADER info***********/n");
     printf("************************************************/n");
     printf("bfType              is %d./n", bfType);
     printf("bfSize              is %d./n", bfSize);
     printf("bfReserved1         is %d./n", bfReserved1);
     printf("bfReserved2         is %d./n", bfReserved2);
     printf("bfOffBits           is %d./n", bfOffBits);
}

/* Show bmp file tagBITMAPINFOHEADER info */
void bmpInfoHeader(FILE* fpbmp)
{
     unsigned int biSize;              // DWORD        biSize;
     long         biWidth;                // LONG         biWidth;
     long         biHeight;               // LONG         biHeight;
     unsigned int biPlanes;               // WORD         biPlanes;
     unsigned int biBitCount;             // WORD         biBitCount;
     unsigned int biCompression;          // DWORD        biCompression;
     unsigned int biSizeImage;            // DWORD        biSizeImage;
     long         biXPelsPerMerer;        // LONG         biXPelsPerMerer;
     long           biYPelsPerMerer;        // LONG         biYPelsPerMerer;
     unsigned int biClrUsed;              // DWORD        biClrUsed;
     unsigned int biClrImportant;         // DWORD        biClrImportant;

     fseek(fpbmp, 14L, SEEK_SET);

     fread(&biSize,          sizeof(char), 4, fpbmp);
     fread(&biWidth,         sizeof(char), 4, fpbmp);
     fread(&biHeight,        sizeof(char), 4, fpbmp);
     fread(&biPlanes,        sizeof(char), 4, fpbmp);
     fread(&biBitCount,      sizeof(char), 4, fpbmp);
     fread(&biCompression,   sizeof(char), 4, fpbmp);
     fread(&biSizeImage,     sizeof(char), 4, fpbmp);
     fread(&biXPelsPerMerer, sizeof(char), 4, fpbmp);
     fread(&biYPelsPerMerer, sizeof(char), 4, fpbmp);
     fread(&biClrUsed,       sizeof(char), 4, fpbmp);
     fread(&biClrImportant,  sizeof(char), 4, fpbmp);

     printf("************************************************/n");
     printf("*************tagBITMAPINFOHEADER info***********/n");
     printf("************************************************/n");
     printf("biSize              is %d. /n", biSize);
     printf("biWidth             is %ld./n", biWidth);
     printf("biHeight            is %ld./n", biHeight);
     printf("biPlanes            is %d. /n", biPlanes);
     printf("biBitCount          is %d. /n", biBitCount);
     printf("biCompression       is %d. /n", biCompression);
     printf("biSizeImage         is %d. /n", biSizeImage);
     printf("biXPelsPerMerer     is %ld./n", biXPelsPerMerer);
     printf("biYPelsPerMerer     is %ld./n", biYPelsPerMerer);
     printf("biClrUsed           is %d. /n", biClrUsed);
     printf("biClrImportant      is %d. /n", biClrImportant);
}

/* Show the Data Part of bmp file */
void bmpDataPart(FILE* fpbmp)
{
     int i, j;
     unsigned char bmpPixel[BmpWidth][BmpHeight];
     unsigned char* bmpPixelTmp = NULL;
     FILE* fpDataBmp;

     /* New a file to save the data matrix */
     if((fpDataBmp=fopen("bmpData.dat","w+")) == NULL)
     {
      fprintf(stderr, "Failed to construct file bmpData.dat.!!!");
      exit(1);
     }

     fseek(fpbmp, OffSet, SEEK_SET);
     if ((bmpPixelTmp=(unsigned char*)malloc(sizeof(char)*BmpWidth*BmpHeight))==NULL)
     {
      fprintf(stderr, "Data allocation failed.!!!/n");
      exit(1);
     }
     fread(bmpPixelTmp, sizeof(char), BmpWidth*BmpHeight, fpbmp);

     /* Read the data to Matrix and save it in file bmpData.dat */
     for(i =0; i < BmpHeight; i++)
     {
      fprintf(fpDataBmp, "The data in line %-3d:/n", i+1);
      for(j = 0; j < BmpWidth; j++)
      {
           bmpPixel[i][j] = bmpPixelTmp[BmpWidth*(BmpHeight-1-i)+j];
           //fwrite(&chartmp, sizeof(char), 1, fpDataBmp);
           fprintf(fpDataBmp, "%-3d ", bmpPixel[i][j]);
           if ((j+1)%32 == 0)
           {
            fprintf(fpDataBmp, "/n");
           }
      }
     }
     /* Used to test the data read is true or false
    You can open the file using Matlab to compare the data */
     //printf("bmpPixel[2][3]   is %d./n", bmpPixel[2][3]);
     //printf("bmpPixel[20][30] is %d./n", bmpPixel[20][30]);

     free(bmpPixelTmp);
     fclose(fpDataBmp);
}

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表

圖片精選

主站蜘蛛池模板: 精品久久久久久中文字幕 | 天堂二区| 欧美日韩经典在线 | chinese xxxx hd pron8 tube | 色999中文字幕 | 国产精品久久久久久影视 | 国产成人高潮免费观看精品 | 超碰99在线观看 | 2021狠狠操| 曰批全过程120分钟免费69 | 精品一区二区电影 | 成人毛片100部| 欧美成人免费tv在线播放 | 久久新网址 | 噜噜噜在线 | 欧美a级大胆视频 | 国产免费一区二区三区在线能观看 | 久久久亚洲欧美综合 | 欧美一级黄色免费 | 精品亚洲国产视频 | 一区二区三区精品国产 | 国产一级午夜 | 黄色电影免费网址 | 日韩在线播放中文字幕 | 久久精品免费国产 | 毛片一区二区三区四区 | 国产精品视频久久久 | 小情侣嗯啊哦视频www | 亚洲国产超高清a毛毛片 | 国产a级久久 | 欧美日比视频 | 欧美成人精品欧美一级乱黄 | 免费观看一级欧美大 | 国产精品欧美久久久久一区二区 | 国产91小视频在线观看 | 国产69精品久久久久久野外 | 最新se94se在线欧美 | 日本不卡一区二区三区在线 | 欧美成人一二三区 | 精品国产一区二区三区久久久 | 亚洲欧美国产视频 |