#include <stdio.h> #include <stdlib.h> #include <string.h> FILE* in ; FILE* out ; unsigned char srcBMP[320 * 240 * 3] = {0}; unsigned short dstBMP[240][320] = {0}; char inFileName[500] = {0}; //待轉換的圖片的文件名 char outFileName[500] = {0}; //輸出文件名 const unsigned long bmpStart = 1 ; //起始圖片序號 const unsigned long bmpEnd = 5376 ; //結束圖片序號 unsigned short RGB888toRGB565(unsigned char red, unsigned char green, unsigned char blue) { unsigned short B = (blue >> 3) & 0x001F; unsigned short G = ((green >> 2) << 5) & 0x07E0; unsigned short R = ((red >> 3) << 11) & 0xF800; return (unsigned short) (R | G | B); } int main() { for(unsigned long index = bmpStart ; index <=bmpEnd ;index++ ) { // 合成文件名 sprintf(inFileName,"F:\\CG\\jljt\\bmp\\jljt_320x240_%.4ld.bmp",index); printf("convert bmp : %s...\r\n",inFileName); // 讀取RGB888內容 in = fopen(inFileName,"rb+"); if(! in) { printf("open file error...\r\n"); return 1; } fseek(in,54,SEEK_SET); fread(srcBMP,1,320*240*3,in); fclose(in); // RGB 888 轉 RGB 565(從左到右,從下到上) unsigned long line = 239 , col = 0 ; for(unsigned long i=0 ,j=0;i<320*240*3;i+=3 ,j++) { unsigned short color565 = RGB888toRGB565(srcBMP[i+2],srcBMP[i+1],srcBMP[i]); dstBMP[line][col++] = color565; if(col >= 320) { col = 0 ; line-- ; } } //輸出到文件 out = fopen("C:\\Users\\Administrator\\Desktop\\jljt.img","ab+"); if(! out) { printf("open file error...\r\n"); return 1; } fwrite(dstBMP,2,320*240,out); fflush(out); fclose(out); } printf("complete...\r\n"); getchar(); }