📄 fmt_bitmap.cpp
字号:
{ FvsError_t nRet = FvsOK; FvsByte_t* pDest = ImageGetBuffer(img); FvsInt_t nMax = ImageGetSize(img); FvsBool_t bEOL = FvsFalse; FvsBool_t bEOF = FvsFalse; FvsByte_t nLen; FvsInt_t h = ImageGetHeight(img); FvsInt_t y = 0; if (pDest==NULL) return FvsMemory; for (y=0; (y<h) && bEOF==FvsFalse; y++) { bEOL = FvsFalse; while (bEOL==FvsFalse && bEOF==FvsFalse && FileIsAtEOF(in)==FvsFalse && nMax>0) { nLen = FileGetByte(in); if (nLen==0) { nLen = FileGetByte(in); switch (nLen) { case 1: /* End of file escape ( 0x00 0x01 ) */ case 2: /* treat seldom used delta escape the same way... */ bEOF = FvsTrue; bEOL = FvsTrue; break; /* fallthrough */ case 0: /* End of line escape ( 0x00 0x00 ) */ bEOL = FvsTrue; break; default: nMax-=nLen; if (nMax>=0) { FvsByte_t i; /* Literal */ for (i = 0; i<nLen;) { FvsByte_t b = FileGetByte(in); *pDest++=pal[b>>4]; i++; if (i<nLen) { *pDest++=pal[b&0xF]; i++; } } /* Word alignment at end of literal packet. */ if ((nLen & 1)!=0) (void)FileGetByte(in); /* ignore */ } break; } } else { nMax-=nLen; if (nMax>=0) { /* RunLength pixels, all with the same value */ FvsByte_t val = pal[FileGetByte(in)]; memset(pDest, (int)val, nLen); pDest+=nLen; } } } } return nRet;}FvsError_t Import8RLE(FvsFile_t in, FvsImage_t img, FvsByte_t* pal){ FvsError_t nRet = FvsOK; FvsByte_t* pDest = ImageGetBuffer(img); FvsBool_t bEOL = FvsFalse; FvsBool_t bEOF = FvsFalse; FvsByte_t nLen, i; FvsInt_t w = ImageGetWidth(img); FvsInt_t h = ImageGetHeight(img); FvsInt_t nMax = h*w; FvsByte_t val = 0; FvsInt_t y = 0; if (pDest==NULL) return FvsMemory; for (y=0; (y<h) && bEOF==FvsFalse; y++) { bEOL = FvsFalse; /* start of line */ while (bEOL==FvsFalse && bEOF==FvsFalse && FileIsAtEOF(in)==FvsFalse && nMax>0) { nLen = FileGetByte(in); if (nLen==0) { nLen = FileGetByte(in); switch (nLen) { case 1: /* End of file escape ( 0x00 0x01 ) */ case 2: /* treat seldom used delta escape the same way...*/ bEOF = FvsTrue; bEOL = FvsTrue; break; /* fallthrough */ case 0: /* End of line escape ( 0x00 0x00 ) */ bEOL = FvsTrue; break; default: nMax-=nLen; if (nMax>=0) { /* Literal */ for (i = 0; i<nLen; i++) *pDest++=pal[FileGetByte(in)]; /* Word alignment at end of literal packet. */ if ((nLen & 1)!=0) (void)FileGetByte(in); /* skip */ } break; } } else { nMax-=nLen; if (nMax>=0) { /* RunLength pixels, all with the same value */ val = pal[FileGetByte(in)]; memset(pDest, (int)val, nLen); pDest+=nLen; } } } } return nRet;}/*** Write the palette into the file*/static FvsError_t BitmapWritePalette(FvsFile_t out){ FvsUint_t nBytes; FvsUint_t nColors = 256; FvsUint_t i; FvsUint_t _pal[256]; memset(_pal, 0, 256); for (i=0; i<nColors; i++) _pal[i] = (( (i<<8) + i ) << 8) + i; nBytes = FileWrite(out, _pal, nColors*4); return (nBytes==nColors*4)?FvsOK:FvsIoError;}/*** Adapter to read in the header without complicated packing options** and relying on memory alignment. This was already present in the** first version of the bitmap import function from Shivang Patel.*/static FvsError_t BitmapReadHeader(FvsFile_t in, FvsBitmapFileHeader_t* bmpheader){ if (FileRead(in, &bmpheader->bfType, 2)!=2) return FvsIoError; if (FileRead(in, &bmpheader->bfSize, 4)!=4) return FvsIoError; if (FileRead(in, &bmpheader->bfReserved1, 2)!=2) return FvsIoError; if (FileRead(in, &bmpheader->bfReserved2, 2)!=2) return FvsIoError; if (FileRead(in, &bmpheader->bfOffBits, 4)!=4) return FvsIoError; if (FileRead(in, &bmpheader->biSize, 4)!=4) return FvsIoError; if (FileRead(in, &bmpheader->biWidth, 4)!=4) return FvsIoError; if (FileRead(in, &bmpheader->biHeight, 4)!=4) return FvsIoError; if (FileRead(in, &bmpheader->biPlanes, 2)!=2) return FvsIoError; if (FileRead(in, &bmpheader->biBitCount, 2)!=2) return FvsIoError; if (FileRead(in, &bmpheader->biCompression, 4)!=4) return FvsIoError; if (FileRead(in, &bmpheader->biSizeImage, 4)!=4) return FvsIoError; if (FileRead(in, &bmpheader->biXPelsPerMeter, 4)!=4) return FvsIoError; if (FileRead(in, &bmpheader->biYPelsPerMeter, 4)!=4) return FvsIoError; if (FileRead(in, &bmpheader->biClrUsed, 4)!=4) return FvsIoError; if (FileRead(in, &bmpheader->biClrImportant, 4)!=4) return FvsIoError; return FvsOK;}/*** Adpater function to write out the header.*/static FvsError_t BitmapWriteHeader(FvsFile_t out, FvsBitmapFileHeader_t* bmpheader){ if (FileWrite(out, &bmpheader->bfType, 2)!=2) return FvsIoError; if (FileWrite(out, &bmpheader->bfSize, 4)!=4) return FvsIoError; if (FileWrite(out, &bmpheader->bfReserved1, 2)!=2) return FvsIoError; if (FileWrite(out, &bmpheader->bfReserved2, 2)!=2) return FvsIoError; if (FileWrite(out, &bmpheader->bfOffBits, 4)!=4) return FvsIoError; if (FileWrite(out, &bmpheader->biSize, 4)!=4) return FvsIoError; if (FileWrite(out, &bmpheader->biWidth, 4)!=4) return FvsIoError; if (FileWrite(out, &bmpheader->biHeight, 4)!=4) return FvsIoError; if (FileWrite(out, &bmpheader->biPlanes, 2)!=2) return FvsIoError; if (FileWrite(out, &bmpheader->biBitCount, 2)!=2) return FvsIoError; if (FileWrite(out, &bmpheader->biCompression, 4)!=4) return FvsIoError; if (FileWrite(out, &bmpheader->biSizeImage, 4)!=4) return FvsIoError; if (FileWrite(out, &bmpheader->biXPelsPerMeter, 4)!=4) return FvsIoError; if (FileWrite(out, &bmpheader->biYPelsPerMeter, 4)!=4) return FvsIoError; if (FileWrite(out, &bmpheader->biClrUsed, 4)!=4) return FvsIoError; if (FileWrite(out, &bmpheader->biClrImportant, 4)!=4) return FvsIoError; return FvsOK;}/*** Read in the palette, in order to do this correctly for all formats, we** need some information from the bitmap header.*/static FvsError_t BitmapReadPalette(FvsFile_t in, FvsByte_t* pal, FvsBitmapFileHeader_t* bmpheader){ FvsError_t nRet = FvsOK; FvsUint_t nColors = 0; FvsUint_t i; /* default palette */ for (i = 0; i < 256; i++) pal[i] = (FvsByte_t)i; switch (bmpheader->biBitCount) { case 1: case 4: case 8: nColors = (bmpheader->biClrUsed!=0)? (FvsUint_t)bmpheader->biClrUsed: (FvsUint_t)(1<<bmpheader->biBitCount); break; case 16: case 24: case 32: /* OK */ break; default: nRet = FvsBadFormat; } if (nColors!=0) { FvsUint_t _pal[256]; FvsUint_t nBytes = FileRead(in, _pal, nColors*4); if (nBytes==nColors*4) { /* Correct the byte ordering & copy the data. */ for (i=0; i<nColors; i++) { FvsUint_t r = (_pal[i])&0xFF; FvsUint_t g = (_pal[i]>>8)&0xFF; FvsUint_t b = (_pal[i]>>16)&0xFF; pal[i] = (FvsByte_t)(((r+(g<<1)+b)>>2)&0xFF); } } else nRet = FvsBadFormat; } return nRet;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -