📄 gfxbmp.c
字号:
{ offsetd = pSurface->plane[0].uBytePerLine * ((UINT32)uHeight-1 - i + uDesY) + pSurface->plane[0].uPixelSize/8 * uDesX; } offsets = fhead.bfOffBits + uJust*(i+ihead.biHeight-uHeight-uSrcY) + uSrcX*3; fseek(fp, offsets, SEEK_SET); fread(buf, uWidth*3, 1, fp); switch(pSurface->uPlaneConfig) {#if defined(GFX_SURFACE_ARGB_1555) || defined(GFX_SURFACE_ARGB_4444) || defined(GFX_SURFACE_RGB_565)#ifdef GFX_SURFACE_ARGB_1555 case GFX_SURFACE_ARGB_1555:#endif#ifdef GFX_SURFACE_ARGB_4444 case GFX_SURFACE_ARGB_4444:#endif#ifdef GFX_SURFACE_RGB_565 case GFX_SURFACE_RGB_565:#endif { UINT16 *pImg = (UINT16 *)((BYTE *)pSurface->plane[0].pPlane + offsetd); UINT16 ua = (UINT16)(alpha >> (8-pSurface->plane[0].a.uNumbits)) << pSurface->plane[0].a.uOffset; for(j=0, k=0; j<uWidth; j++, k+=3) { pImg[j] = ua | ((UINT16)(buf[k ] >> rb)<<sb) | ((UINT16)(buf[k+1] >> rg)<<sg) | ((UINT16)(buf[k+2] >> rr)<<sr) ; } }#endif case GFX_SURFACE_ARGB_8888: { UINT32 *pImg = (UINT32 *)((BYTE *)pSurface->plane[0].pPlane + offsetd); UINT32 ua = (UINT32)(alpha >> (8-pSurface->plane[0].a.uNumbits)) << pSurface->plane[0].a.uOffset; for(j=0, k=0; j<uWidth; j++, k+=3) { pImg[j] = ua | ((UINT32)(buf[k ] >> rb)<<sb) | ((UINT32)(buf[k+1] >> rg)<<sg) | ((UINT32)(buf[k+2] >> rr)<<sr) ; } } } } fclose(fp); free(buf); return 0;}// load Windows BMP 8 bits files// Return 1 if successfulint gfx_LoadBMP8b(GFX_SURFACE_LOCK_INFO_T *pSurface, GFX_PALETTE_T *pPal, UINT uDesX, UINT uDesY, UINT uWidth, UINT uHeight, const char * fname, UINT uSrcX, UINT uSrcY, BYTE alpha){ UINT32 offsets, offsetd, uJust; BYTE *buf; int i; FILE *fp; BITMAPFILEHEADER fhead; BITMAPINFOHEADER ihead; int reverse=0; if(!pSurface || !fname || !fname[0] ) return -1; if( pSurface->uPlaneConfig != GFX_SURFACE_CLUT8BPP_ARGB && pSurface->uPlaneConfig != GFX_SURFACE_RAW8BPP) { return -1; } fp=fopen(fname, "rb"); if(fp == NULL) return -1; // read filehead fhead.bfType[0] = fgetc(fp); fhead.bfType[1] = fgetc(fp); fhead.bfSize = fgetLong(fp, 1); fhead.bfReserved1 = fgetWord(fp, 1); fhead.bfReserved2 = fgetWord(fp, 1); fhead.bfOffBits = fgetLong(fp, 1); // read infohead ihead.biSize = fgetLong(fp, 1); ihead.biWidth = (INT32)fgetLong(fp, 1); ihead.biHeight = (INT32)fgetLong(fp, 1); ihead.biPlanes = fgetWord(fp, 1); ihead.biBitCount = fgetWord(fp, 1); ihead.biCompression = fgetLong(fp, 1); ihead.biSizeImage = fgetLong(fp, 1); ihead.biXPelsPerMeter = (INT32)fgetLong(fp, 1); ihead.biYPelsPerMeter = (INT32)fgetLong(fp, 1); ihead.biClrUsed = fgetLong(fp, 1); ihead.biClrImportant = fgetLong(fp, 1); if (fhead.bfType[0] != 'B' || fhead.bfType[1] != 'M' // 'BM' || ihead.biPlanes != 1 || ihead.biBitCount != 8 || ihead.biCompression != 0L || ihead.biClrUsed > 256 ) // not a valid 8bits BMP file { fclose(fp); return -1; } if(ihead.biHeight < 0) {ihead.biHeight = -ihead.biHeight; reverse = 1; } if(gfx_ClipBLTRect(ihead.biWidth, ihead.biHeight, pSurface->plane[0].uWidth, pSurface->plane[0].uHeight, uSrcX, uSrcY, uDesX, uDesY, &uWidth, &uHeight)) { fclose(fp); return -1; // null clip } if(pPal) { if((buf = (BYTE *)malloc(256*4)) == NULL) { fclose(fp); return -1; } // can not allocate buffer if(ihead.biClrUsed == 0) ihead.biClrUsed = 256; fread(buf, ihead.biClrUsed*4, 1, fp); for(i=0; i<(int)ihead.biClrUsed; i++) { pPal[i].a = alpha; pPal[i].r = buf[i*4+2]; pPal[i].g = buf[i*4+1]; pPal[i].b = buf[i*4+0]; } for(; i<256; i++) { pPal[i].a = 0; pPal[i].r = 0; pPal[i].g = 0; pPal[i].b = 0; } free(buf); } uJust = (ihead.biWidth+3)&0xffffffc; for(i=0; i<uHeight; i++) { if(reverse) { offsetd = pSurface->plane[0].uBytePerLine * ((UINT32)i+uDesY) + pSurface->plane[0].uPixelSize/8 * uDesX; } else { offsetd = pSurface->plane[0].uBytePerLine * ((UINT32)uHeight-1 - i + uDesY) + pSurface->plane[0].uPixelSize/8 * uDesX; } offsets = fhead.bfOffBits + uJust*(i+ihead.biHeight-uSrcY-uHeight) + uSrcX; fseek(fp, offsets, SEEK_SET); fread((BYTE *)pSurface->plane[0].pPlane + offsetd, uWidth, 1, fp); } fclose(fp); return 0;}int gfx_LoadBMP4b(GFX_SURFACE_LOCK_INFO_T *pSurface, GFX_PALETTE_T *pPal, unsigned int uDesX, unsigned int uDesY, unsigned int uWidth, unsigned int uHeight, const char * fname, unsigned int uSrcX, unsigned int uSrcY, BYTE alpha){ UINT32 offsets, offsetd, uJust; BYTE *buf; int i; FILE *fp; BITMAPFILEHEADER fhead; BITMAPINFOHEADER ihead; int reverse=0; if(!pSurface || !fname || !fname[0] ) return -1; if( pSurface->uPlaneConfig != GFX_SURFACE_CLUT4BPP_ARGB #ifdef GFX_SURFACE_RAW4BPP && pSurface->uPlaneConfig != GFX_SURFACE_RAW4BPP#endif ) { return -1; } fp=fopen(fname, "rb"); if(fp == NULL) return -1; // read filehead fhead.bfType[0] = fgetc(fp); fhead.bfType[1] = fgetc(fp); fhead.bfSize = fgetLong(fp, 1); fhead.bfReserved1 = fgetWord(fp, 1); fhead.bfReserved2 = fgetWord(fp, 1); fhead.bfOffBits = fgetLong(fp, 1); // read infohead ihead.biSize = fgetLong(fp, 1); ihead.biWidth = (INT32)fgetLong(fp, 1); ihead.biHeight = (INT32)fgetLong(fp, 1); ihead.biPlanes = fgetWord(fp, 1); ihead.biBitCount = fgetWord(fp, 1); ihead.biCompression = fgetLong(fp, 1); ihead.biSizeImage = fgetLong(fp, 1); ihead.biXPelsPerMeter = (INT32)fgetLong(fp, 1); ihead.biYPelsPerMeter = (INT32)fgetLong(fp, 1); ihead.biClrUsed = fgetLong(fp, 1); ihead.biClrImportant = fgetLong(fp, 1); if (fhead.bfType[0] != 'B' || fhead.bfType[1] != 'M' // 'BM' || ihead.biPlanes != 1 || ihead.biBitCount != 4 || ihead.biCompression != 0L || ihead.biClrUsed > 16 ) // not a valid 4bits BMP file { fclose(fp); return -1; } if(ihead.biHeight < 0) {ihead.biHeight = -ihead.biHeight; reverse = 1; } uSrcX &= 0xfffffffe; uDesX &= 0xfffffffe; uWidth = (uWidth + 1)&0xfffffffe; if(gfx_ClipBLTRect(ihead.biWidth, ihead.biHeight, pSurface->plane[0].uWidth, pSurface->plane[0].uHeight, uSrcX, uSrcY, uDesX, uDesY, &uWidth, &uHeight)) { fclose(fp); return -1; // null clip } if(pPal) { if((buf = (BYTE *)malloc(16*4)) == NULL) { fclose(fp); return -1; } // can not allocate buffer if(ihead.biClrUsed == 0) ihead.biClrUsed = 16; fread(buf, ihead.biClrUsed*4, 1, fp); for(i=0; i<(int)ihead.biClrUsed; i++) { pPal[i].a = alpha; pPal[i].r = buf[i*4+2]; pPal[i].g = buf[i*4+1]; pPal[i].b = buf[i*4+0]; } for(; i<16; i++) { pPal[i].a = 0; pPal[i].r = 0; pPal[i].g = 0; pPal[i].b = 0; } free(buf); } uJust = ((ihead.biWidth+1)/2+3)&0xffffffc; for(i=0; i<uHeight; i++) { if(reverse) { offsetd = pSurface->plane[0].uBytePerLine * ((UINT32)i+uDesY) + pSurface->plane[0].uPixelSize * uDesX / 8; } else { offsetd = pSurface->plane[0].uBytePerLine * ((UINT32)uHeight-1 - i + uDesY) + pSurface->plane[0].uPixelSize * uDesX / 8; } offsets = fhead.bfOffBits + uJust*(i+ihead.biHeight-uSrcY-uHeight) + uSrcX/2; fseek(fp, offsets, SEEK_SET); fread((BYTE *)pSurface->plane[0].pPlane + offsetd, (uWidth+1)/2, 1, fp); } fclose(fp); return 0;}int gfx_LoadBMP1b(GFX_SURFACE_LOCK_INFO_T *pSurface, GFX_PALETTE_T *pPal, unsigned int uDesX, unsigned int uDesY, unsigned int uWidth, unsigned int uHeight, const char * fname, unsigned int uSrcX, unsigned int uSrcY, BYTE alpha){ UINT32 offsets, offsetd, uJust; BYTE *buf; int i; FILE *fp; BITMAPFILEHEADER fhead; BITMAPINFOHEADER ihead; int reverse=0; if(!pSurface || !fname || !fname[0] ) return -1; if(#ifdef GFX_SURFACE_CLUT1BPP_ARGB pSurface->uPlaneConfig != GFX_SURFACE_CLUT1BPP_ARGB &&#endif pSurface->uPlaneConfig != GFX_SURFACE_RAW1BPP) { return -1; } fp=fopen(fname, "rb"); if(fp == NULL) return -1; // read filehead fhead.bfType[0] = fgetc(fp); fhead.bfType[1] = fgetc(fp); fhead.bfSize = fgetLong(fp, 1); fhead.bfReserved1 = fgetWord(fp, 1); fhead.bfReserved2 = fgetWord(fp, 1); fhead.bfOffBits = fgetLong(fp, 1); // read infohead ihead.biSize = fgetLong(fp, 1); ihead.biWidth = (INT32)fgetLong(fp, 1); ihead.biHeight = (INT32)fgetLong(fp, 1); ihead.biPlanes = fgetWord(fp, 1); ihead.biBitCount = fgetWord(fp, 1); ihead.biCompression = fgetLong(fp, 1); ihead.biSizeImage = fgetLong(fp, 1); ihead.biXPelsPerMeter = (INT32)fgetLong(fp, 1); ihead.biYPelsPerMeter = (INT32)fgetLong(fp, 1); ihead.biClrUsed = fgetLong(fp, 1); ihead.biClrImportant = fgetLong(fp, 1); if (fhead.bfType[0] != 'B' || fhead.bfType[1] != 'M' // 'BM' || ihead.biPlanes != 1 || ihead.biBitCount != 1 || ihead.biCompression != 0L || ihead.biClrUsed > 2 ) // not a valid 1bits BMP file { fclose(fp); return -1; } if(ihead.biHeight < 0) {ihead.biHeight = -ihead.biHeight; reverse = 1; } uSrcX &= 0xfffffff8; uDesX &= 0xfffffff8; uWidth = (uWidth + 7)&0xfffffff8; if(gfx_ClipBLTRect(ihead.biWidth, ihead.biHeight, pSurface->plane[0].uWidth, pSurface->plane[0].uHeight, uSrcX, uSrcY, uDesX, uDesY, &uWidth, &uHeight)) { fclose(fp); return -1; // null clip } if(pPal) { if((buf = (BYTE *)malloc(2*4)) == NULL) { fclose(fp); return -1; } // can not allocate buffer if(ihead.biClrUsed == 0) ihead.biClrUsed = 2; fread(buf, ihead.biClrUsed*4, 1, fp); for(i=0; i<(int)ihead.biClrUsed; i++) { pPal[i].a = alpha; pPal[i].r = buf[i*4+2]; pPal[i].g = buf[i*4+1]; pPal[i].b = buf[i*4+0]; } for(; i<2; i++) { pPal[i].a = 0; pPal[i].r = 0; pPal[i].g = 0; pPal[i].b = 0; } free(buf); } uJust = ((ihead.biWidth+7)/8+3)&0xffffffc; for(i=0; i<uHeight; i++) { if(reverse) { offsetd = pSurface->plane[0].uBytePerLine * ((UINT32)i+uDesY) + pSurface->plane[0].uPixelSize * uDesX / 8; } else { offsetd = pSurface->plane[0].uBytePerLine * ((UINT32)uHeight-1 - i + uDesY) + pSurface->plane[0].uPixelSize * uDesX / 8; } offsets = fhead.bfOffBits + uJust*(i+ihead.biHeight-uSrcY-uHeight) + uSrcX/8; fseek(fp, offsets, SEEK_SET); fread((BYTE *)pSurface->plane[0].pPlane + offsetd, (uWidth+7)/8, 1, fp); } fclose(fp); return 0;}int gfx_GetBMPInfo(const char * fname, UINT *upWidth, UINT *upHeight, UINT *upPixelSize){ FILE *fp; BITMAPFILEHEADER fhead; BITMAPINFOHEADER ihead; if(!fname || !fname[0]) return -1; fp=fopen(fname, "rb"); if(fp == NULL) return -1; // read filehead fhead.bfType[0] = fgetc(fp); fhead.bfType[1] = fgetc(fp); fhead.bfSize = fgetLong(fp, 1); fhead.bfReserved1 = fgetWord(fp, 1); fhead.bfReserved2 = fgetWord(fp, 1); fhead.bfOffBits = fgetLong(fp, 1); // read infohead ihead.biSize = fgetLong(fp, 1); ihead.biWidth = (INT32)fgetLong(fp, 1); ihead.biHeight = (INT32)fgetLong(fp, 1); ihead.biPlanes = fgetWord(fp, 1); ihead.biBitCount = fgetWord(fp, 1); ihead.biCompression = fgetLong(fp, 1); ihead.biSizeImage = fgetLong(fp, 1); ihead.biXPelsPerMeter = (INT32)fgetLong(fp, 1); ihead.biYPelsPerMeter = (INT32)fgetLong(fp, 1); ihead.biClrUsed = fgetLong(fp, 1); ihead.biClrImportant = fgetLong(fp, 1); fclose(fp); if (fhead.bfType[0] != 'B' || fhead.bfType[1] != 'M' // 'BM' || ihead.biPlanes != 1 || ihead.biCompression != 0L ) // not a valid 8bits BMP file { return -1; } if(ihead.biHeight < 0) {ihead.biHeight = -ihead.biHeight; }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -