📄 fmt_bitmap.cpp
字号:
/*######################################################################## The contents of this file are subject to the Mozilla Public License Version 1.0(the "License"); You may NOT use this file except in compliance with the License. You may obtain a copy of the License at http:// www.mozilla.org/MPL/ Software distributed under the License is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for the specific language governing rights and limitations under the License. The Initial Developer of the Original Code is Shivang Patel. Copyright(C) 2002-2003. All Rights Reserved. Authors: Shivang Patel Jaap de Haan(jdh) This file contains functions to read and write BMP files########################################################################*/#include "stdafx.h"#include <string.h>#include <stdlib.h>#include "../ip-header/fmt_bitmap.h"typedef struct FvsBitmapFileHeader_t{ uint16_t bfType; uint32_t bfSize; uint16_t bfReserved1; uint16_t bfReserved2; uint32_t bfOffBits; uint32_t biSize; int32_t biWidth; int32_t biHeight; uint16_t biPlanes; uint16_t biBitCount; uint32_t biCompression; uint32_t biSizeImage; int32_t biXPelsPerMeter; int32_t biYPelsPerMeter; uint32_t biClrUsed; uint32_t biClrImportant;} FvsBitmapFileHeader_t;#define TYPE_BM 0x4d42/* private prototype definition *//* import functions */static FvsError_t Import32BMP (FvsFile_t in, FvsImage_t out, FvsByte_t* pal);static FvsError_t Import24BMP (FvsFile_t in, FvsImage_t out, FvsByte_t* pal);static FvsError_t Import16BMP (FvsFile_t in, FvsImage_t out, FvsByte_t* pal);static FvsError_t Import4BMP (FvsFile_t in, FvsImage_t out, FvsByte_t* pal);static FvsError_t Import8BMP (FvsFile_t in, FvsImage_t out, FvsByte_t* pal);static FvsError_t Import4RLE (FvsFile_t in, FvsImage_t out, FvsByte_t* pal);static FvsError_t Import8RLE (FvsFile_t in, FvsImage_t out, FvsByte_t* pal);static FvsError_t BitmapReadPalette (FvsFile_t in, /*@out@*/ FvsByte_t* pal, FvsBitmapFileHeader_t* bmpheader);static FvsError_t BitmapReadHeader (FvsFile_t in, /*@out@*/ FvsBitmapFileHeader_t* bmpheader);/* export functions */static FvsError_t Export8BMP (FvsFile_t out, const FvsImage_t img);static FvsError_t BitmapWritePalette (FvsFile_t out);static FvsError_t BitmapWriteHeader (FvsFile_t out, /*@in@*/ FvsBitmapFileHeader_t* bmpheader);FvsError_t BitmapImport(FvsFile_t in, FvsImage_t img){ FvsError_t nRet = FvsOK; FvsBitmapFileHeader_t bmpheader; FvsByte_t pal[256]; memset(&bmpheader, 0, sizeof(bmpheader)); if (FileIsOpen(in)==FvsTrue) { nRet = FileSeekToBegin(in); if (nRet==FvsOK) nRet = BitmapReadHeader(in, &bmpheader); if (nRet==FvsOK) { if (bmpheader.bfType!=TYPE_BM || bmpheader.biSize!=40) nRet = FvsBadFormat; else { nRet = BitmapReadPalette(in, pal, &bmpheader); if (nRet==FvsOK) { /* jump to the bitmap bits */ nRet = FileSeek(in, (FvsUint_t)bmpheader.bfOffBits); if (nRet==FvsOK) { nRet = ImageSetSize(img, (FvsInt_t)bmpheader.biWidth, (FvsInt_t)bmpheader.biHeight); if (nRet==FvsOK) { switch(bmpheader.biBitCount) { case 4: if (bmpheader.biCompression==0) nRet = Import4BMP(in, img, pal); else nRet = Import4RLE(in, img, pal); break; case 8: if (bmpheader.biCompression==0) nRet = Import8BMP(in, img, pal); else nRet = Import8RLE(in, img, pal); break; case 16: nRet = Import16BMP(in, img, pal); break; case 24: nRet = Import24BMP(in, img, pal); break; case 32: nRet = Import32BMP(in, img, pal); break; case 1: default: nRet = FvsBadFormat; break; } } } } else nRet = FvsBadFormat; } } else nRet = FvsBadFormat; } else nRet = FvsBadParameter; return nRet;}FvsError_t BitmapExport(FvsFile_t out, const FvsImage_t img){ uint32_t len; FvsError_t nRet = FvsOK; FvsBitmapFileHeader_t bmpheader; if (FileIsOpen(out)==FvsTrue) { nRet = FileSeekToBegin(out); if (nRet!=FvsOK) return nRet; len = sizeof(bmpheader); memset(&bmpheader, 0, sizeof(bmpheader)); bmpheader.bfType = TYPE_BM; bmpheader.biSize = 40; bmpheader.biWidth = (int32_t)ImageGetWidth(img); bmpheader.biHeight = (int32_t)ImageGetHeight(img); bmpheader.biPlanes = 1; bmpheader.biBitCount = 8; bmpheader.biClrImportant= 256; bmpheader.biClrUsed = 256; bmpheader.biSizeImage = (uint32_t)ImageGetSize(img); nRet = BitmapWriteHeader(out, &bmpheader);
if (nRet==FvsOK) { nRet = BitmapWritePalette(out); if (nRet==FvsOK) { bmpheader.bfOffBits = (uint32_t)FileGetPosition(out); nRet = Export8BMP(out, img); bmpheader.bfSize = (uint32_t)FileGetPosition(out); if (nRet==FvsOK) { (void)FileSeekToBegin(out); nRet = BitmapWriteHeader(out, &bmpheader); } } } else nRet = FvsIoError; } else nRet = FvsBadParameter; return nRet;}static FvsError_t Import32BMP(FvsFile_t in, FvsImage_t img, FvsByte_t* pal){ FvsError_t nRet = FvsOK; FvsByte_t* pDest = ImageGetBuffer(img); FvsInt_t nMax = ImageGetSize(img); FvsByte_t r,g,b; FvsInt_t i; if (pDest==NULL) return FvsMemory; for (i = 0; i < nMax; i++) { r = FileGetByte(in); g = FileGetByte(in); b = FileGetByte(in); (void)FileGetByte(in); /* skipping */ *pDest++ = pal[((r+(g<<1)+b)>>2)&0xFF]; } return nRet;}static FvsError_t Import24BMP(FvsFile_t in, FvsImage_t img, FvsByte_t* pal){ FvsError_t nRet = FvsOK; FvsByte_t* pDest = ImageGetBuffer(img); FvsInt_t nMax = ImageGetSize(img); FvsByte_t r,g,b; FvsInt_t i; if (pDest==NULL) return FvsMemory; for (i = 0; i < nMax; i++) { r = FileGetByte(in); g = FileGetByte(in); b = FileGetByte(in); *pDest++ = pal[((r+(g<<1)+b)>>2)&0xFF]; } return nRet;}static FvsError_t Import16BMP(FvsFile_t in, FvsImage_t img, FvsByte_t* pal){ FvsError_t nRet = FvsOK; FvsByte_t* pDest = ImageGetBuffer(img); FvsInt_t nMax = ImageGetSize(img); uint16_t w; FvsInt_t i; if (pDest==NULL) return FvsMemory; for (i = 0; i < nMax; i++) { w = FileGetWord(in); *pDest++ = pal[( ((w&0xF800)>>11)+((w&0x07E0)>>5)+(w&0x001F))&0xFF]; } return nRet;}static FvsError_t Import4BMP(FvsFile_t in, FvsImage_t img, FvsByte_t* pal){ FvsError_t nRet = FvsOK; FvsByte_t* pDest = ImageGetBuffer(img); FvsInt_t nMax = ImageGetSize(img); FvsByte_t b; FvsInt_t i; if (pDest==NULL) return FvsMemory; for (i = 0; i < nMax; i+=2) { b = FileGetByte(in); *pDest++ = pal[(b>>4)&0x0F]; *pDest++ = pal[b&0xF]; } return nRet;}static FvsError_t Import8BMP(FvsFile_t in, FvsImage_t img, FvsByte_t* pal){ FvsError_t nRet = FvsOK; FvsByte_t* pDest = ImageGetBuffer(img); FvsInt_t nMax = ImageGetSize(img); FvsByte_t b; FvsInt_t i; if (pDest==NULL) { nRet = FvsMemory; } else { for (i = 0; i < nMax; i++) { b = FileGetByte(in); *pDest++ = pal[b]; } } return nRet;}static FvsError_t Export8BMP(FvsFile_t out, const FvsImage_t img){ FvsUint_t size = (FvsUint_t)ImageGetSize(img); FvsByte_t* buffer = ImageGetBuffer(img); if (buffer==NULL) return FvsMemory; return (FileWrite(out, buffer, size)==size)?FvsOK:FvsIoError;}static FvsError_t Import4RLE(FvsFile_t in, FvsImage_t img, FvsByte_t* pal)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -