中文字幕在线观看,亚洲а∨天堂久久精品9966,亚洲成a人片在线观看你懂的,亚洲av成人片无码网站,亚洲国产精品无码久久久五月天

C/C++實(shí)現(xiàn)RGB565轉(zhuǎn)換成BMP位圖

2018-07-20    來(lái)源:open-open

容器云強(qiáng)勢(shì)上線(xiàn)!快速搭建集群,上萬(wàn)Linux鏡像隨意使用
//主函數(shù)
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include "rgb2bmp.h"
int  main()
{    FILE* p;
/***************  input data  ***********
    filename      :RGB數(shù)據(jù)文件名稱(chēng)
    nWidth        :所生成文件的水平像素
    nHeight       :所生成文件的垂直像素
    newFile       :最終生成文件的名稱(chēng)
***********************************************/
    char* filename = "rgb565_800_480_woman";
    int nWidth = 800;
    int nHeight = 480;
    char* newFile = "rgb565_800_480_woman_0x7f.bmp";
    p = fopen(filename,"rb");
    if (p == NULL)
    {
        printf("!!!file %s open failed.n", filename);
        return 0;
    }
    printf("file %s open success.n",filename);
/***********  read Image Data  **************/    
    long nData = nWidth*nHeight;
    unsigned short* rgb_buffer = malloc(nData*sizeof(short));   
    fread(rgb_buffer,2,nData,p);
    long total = nWidth*nHeight*3;
    unsigned char* pVisit =  malloc(total*sizeof(char));
    unsigned char* tmp = pVisit;
    long i =0;
    unsigned char R,G,B;
    while(i<nData)
    {
       R = *rgb_buffer&0x1f;
    G = (*rgb_buffer>>5)&0x3f;
    B = (*rgb_buffer>>11)&0x1f;
    B = B<<3;
    G = G<<2;
    R = R<<3;
      *pVisit=R;pVisit++;
      *pVisit=G;pVisit++;
      *pVisit=B;pVisit++;
      rgb_buffer++;
      i++; 
     }
    printf("read file over.nData%ldn",nData);
/*****************************************************/
/***********  write file *******************/
    FILE *result = fopen(newFile,"wb");
    if (result == NULL)
    {
       printf("open new file failed.n");
       return -1;
    }
    RGB2BMP(tmp,nWidth,nHeight,result);
    fclose(result);
    return 0;
}
   
//rgb2bmp.h文件
#include <stdio.h>
typedef unsigned char  BYTE;
typedef unsigned short WORD;
// BMP圖像各部分說(shuō)明如下
/***********
    第一部分    位圖文件頭
該結(jié)構(gòu)的長(zhǎng)度是固定的,為14個(gè)字節(jié),各個(gè)域的依次如下:
    2byte   :文件類(lèi)型,必須是0x4d42,即字符串"BM"。
    4byte   :整個(gè)文件大小
    4byte   :保留字,為0
    4byte   :從文件頭到實(shí)際的位圖圖像數(shù)據(jù)的偏移字節(jié)數(shù)。
*************/
typedef struct
{    long imageSize;
    long blank;
    long startPosition;
}BmpHead;
/*********************
/*********************
第二部分    位圖信息頭
該結(jié)構(gòu)的長(zhǎng)度也是固定的,為40個(gè)字節(jié),各個(gè)域的依次說(shuō)明如下:
    4byte   :本結(jié)構(gòu)的長(zhǎng)度,值為40
    4byte   :圖像的寬度是多少象素。
    4byte   :圖像的高度是多少象素。
    2Byte   :必須是1。
    2Byte   :表示顏色時(shí)用到的位數(shù),常用的值為1(黑白二色圖)、4(16色圖)、8(256色圖)、24(真彩色圖)。
    4byte   :指定位圖是否壓縮,有效值為BI_RGB,BI_RLE8,BI_RLE4,BI_BITFIELDS。Windows位圖可采用RLE4和RLE8的壓縮格式,BI_RGB表示不壓縮。
    4byte   :指定實(shí)際的位圖圖像數(shù)據(jù)占用的字節(jié)數(shù),可用以下的公式計(jì)算出來(lái):
     圖像數(shù)據(jù) = Width' * Height * 表示每個(gè)象素顏色占用的byte數(shù)(即顏色位數(shù)/8,24bit圖為3,256色為1)
     要注意的是:上述公式中的biWidth'必須是4的整數(shù)倍(不是biWidth,而是大于或等于biWidth的最小4的整數(shù)倍)。
     如果biCompression為BI_RGB,則該項(xiàng)可能為0。
    4byte   :目標(biāo)設(shè)備的水平分辨率。
    4byte   :目標(biāo)設(shè)備的垂直分辨率。
    4byte   :本圖像實(shí)際用到的顏色數(shù),如果該值為0,則用到的顏色數(shù)為2的(顏色位數(shù))次冪,如顏色位數(shù)為8,2^8=256,即256色的位圖
    4byte   :指定本圖像中重要的顏色數(shù),如果該值為0,則認(rèn)為所有的顏色都是重要的。
***********************************/
typedef struct
  
{
    long    Length;
    long    width;
    long    height;
    WORD    colorPlane;
    WORD    bitColor;
    long    zipFormat;
    long    realSize;
    long    xPels;
    long    yPels;
    long    colorUse;
    long    colorImportant;
  /*  void show()
  
    {    
        printf("infoHead Length:%dn",Length);
        printf("width&height:%d*%dn",width,height);
        printf("colorPlane:%dn",colorPlane);
        printf("bitColor:%dn",bitColor);
        printf("Compression Format:%dn",zipFormat);
        printf("Image Real Size:%dn",realSize);
        printf("Pels(X,Y):(%d,%d)n",xPels,yPels);
        printf("colorUse:%dn",colorUse);    
        printf("Important Color:%dn",colorImportant);
    }*/
}InfoHead;
/***************************
/***************************
    第三部分    調(diào)色盤(pán)結(jié)構(gòu)  顏色表
    對(duì)于256色BMP位圖,顏色位數(shù)為8,需要2^8 = 256個(gè)調(diào)色盤(pán);
    對(duì)于24bitBMP位圖,各象素RGB值直接保存在圖像數(shù)據(jù)區(qū),不需要調(diào)色盤(pán),不存在調(diào)色盤(pán)區(qū)
    rgbBlue:   該顏色的藍(lán)色分量。
    rgbGreen:  該顏色的綠色分量。
    rgbRed:    該顏色的紅色分量。
    rgbReserved:保留值。
************************/
typedef struct
{         BYTE   rgbBlue;
         BYTE   rgbGreen;
         BYTE   rgbRed;
         BYTE   rgbReserved;
      /*   void show(void)
         {
            printf("Mix Plate B,G,R:%d %d %dn",rgbBlue,rgbGreen,rgbRed);
         }*/
}RGBMixPlate;
/****************************
      RGB加上頭部信息轉(zhuǎn)換成BMP
      參數(shù)說(shuō)明:
      rgb_buffer        :RGB數(shù)據(jù)文件中的信息
      nData             :RGB數(shù)據(jù)的長(zhǎng)度
      nWidth            :圖像寬度的像素?cái)?shù)
      nHeight           :圖像高度的像素?cái)?shù)
      fp1               :所存放的文件
*****************************/
int RGB2BMP(char *rgb_buffer,int nWidth,int nHeight,FILE*fp1)
{
     BmpHead m_BMPHeader;       
     char bfType[2]={'B','M'};
     m_BMPHeader.imageSize=3*nWidth*nHeight+54;
     m_BMPHeader.blank=0;
     m_BMPHeader.startPosition=54;
  
     fwrite(bfType,1,sizeof(bfType),fp1);
     fwrite(&m_BMPHeader.imageSize,1,sizeof(m_BMPHeader.imageSize),fp1);
     fwrite(&m_BMPHeader.blank,1,sizeof(m_BMPHeader.blank),fp1);
     fwrite(&m_BMPHeader.startPosition,1,sizeof(m_BMPHeader.startPosition),fp1);
         
     InfoHead  m_BMPInfoHeader;
     m_BMPInfoHeader.Length=40;
     m_BMPInfoHeader.width=nWidth;
     m_BMPInfoHeader.height=nHeight;
     m_BMPInfoHeader.colorPlane=1;
     m_BMPInfoHeader.bitColor=24;
     m_BMPInfoHeader.zipFormat=0;
     m_BMPInfoHeader.realSize=3*nWidth*nHeight;
     m_BMPInfoHeader.xPels=0;
     m_BMPInfoHeader.yPels=0;
     m_BMPInfoHeader.colorUse=0;
     m_BMPInfoHeader.colorImportant=0;
  
     fwrite(&m_BMPInfoHeader.Length,1,sizeof(m_BMPInfoHeader.Length),fp1);
     fwrite(&m_BMPInfoHeader.width,1,sizeof(m_BMPInfoHeader.width),fp1);
     fwrite(&m_BMPInfoHeader.height,1,sizeof(m_BMPInfoHeader.height),fp1);
     fwrite(&m_BMPInfoHeader.colorPlane,1,sizeof(m_BMPInfoHeader.colorPlane),fp1);
     fwrite(&m_BMPInfoHeader.bitColor,1,sizeof(m_BMPInfoHeader.bitColor),fp1);
     fwrite(&m_BMPInfoHeader.zipFormat,1,sizeof(m_BMPInfoHeader.zipFormat),fp1);
     fwrite(&m_BMPInfoHeader.realSize,1,sizeof(m_BMPInfoHeader.realSize),fp1);
     fwrite(&m_BMPInfoHeader.xPels,1,sizeof(m_BMPInfoHeader.xPels),fp1);
     fwrite(&m_BMPInfoHeader.yPels,1,sizeof(m_BMPInfoHeader.yPels),fp1);
     fwrite(&m_BMPInfoHeader.colorUse,1,sizeof(m_BMPInfoHeader.colorUse),fp1);
     fwrite(&m_BMPInfoHeader.colorImportant,1,sizeof(m_BMPInfoHeader.colorImportant),fp1);
     fwrite(rgb_buffer,3*nWidth*nHeight,1,fp1);
     return 0;
}

標(biāo)簽: b2b

版權(quán)申明:本站文章部分自網(wǎng)絡(luò),如有侵權(quán),請(qǐng)聯(lián)系:west999com@outlook.com
特別注意:本站所有轉(zhuǎn)載文章言論不代表本站觀點(diǎn)!
本站所提供的圖片等素材,版權(quán)歸原作者所有,如需使用,請(qǐng)與原作者聯(lián)系。

上一篇:C++求數(shù)組的全排列之字典序法

下一篇:PHP支持發(fā)送HTML格式郵件的發(fā)送類(lèi)