📄 gfxbmp.c
字号:
//pallas/drv/gfx/test/gfxbmp.c/*----------------------------------------------------------------------------+|| This source code has been made available to you by IBM on an AS-IS| basis. Anyone receiving this source is licensed under IBM| copyrights to use it in any way he or she deems fit, including| copying it, modifying it, compiling it, and redistributing it either| with or without modifications. No license under IBM patents or| patent applications is to be implied by the copyright license.|| Any user of this software should understand that IBM cannot provide| technical support for this software and will not be responsible for| any consequences resulting from the use of this software.|| Any person who transfers this source code or any derivative work| must include the IBM copyright notice, this paragraph, and the| preceding two paragraphs in the transferred software.|| COPYRIGHT I B M CORPORATION 1998| LICENSED MATERIAL - PROGRAM PROPERTY OF I B M+----------------------------------------------------------------------------*/////Comment: // MS Windoze 8/24 bits Bitmap access//Revision Log: // Nov/13/2001 Created by YYD#include <stdlib.h> // for rand#include <stdio.h> // #define __JPEG_LIB_READY__ // IJG's libjpeg 6b#ifdef __JPEG_LIB_READY__ // IJG's libjpeg 6b#include <setjmp.h>#define XMD_H // force it to ignore it's own define of INT32#include <jpeglib.h>#undef XMD_H#endif#include "gfxlib.h"static UINT16 fgetWord(FILE *fp, BYTE intel){ UINT16 i,j; i=(UINT16)(fgetc(fp)&0xff); j=(UINT16)(fgetc(fp)&0xff); if(intel) return(i+(j<<8)); else return ((i<<8)+j);}static UINT32 fgetLong(FILE *fp, BYTE intel){ UINT32 i,j,k,l; i=(UINT32)(fgetc(fp) & 0xff); j=(UINT32)(fgetc(fp) & 0xff); k=(UINT32)(fgetc(fp) & 0xff); l=(UINT32)(fgetc(fp) & 0xff); if(intel) return(i+(j<<8)+(k<<16)+(l<<24)); else return(l+(k<<8)+(j<<16)+(i<<24));}static int fputWord(UINT16 wd, FILE *fp, BYTE intel){ int rtn; if(intel) { rtn = fputc(wd&0xff, fp); rtn = fputc((wd>>8)&0xff, fp); } else { rtn = fputc((wd>>8)&0xff, fp); rtn = fputc(wd&0xff, fp); } return rtn;}static int fputLong(UINT32 dw, FILE *fp, BYTE intel){ int rtn; if(intel) { rtn = fputc(dw&0xff, fp); rtn = fputc((dw>>8)&0xff, fp); rtn = fputc((dw>>16)&0xff, fp); rtn = fputc((dw>>24)&0xff, fp); } else { rtn = fputc((dw>>24)&0xff, fp); rtn = fputc((dw>>16)&0xff, fp); rtn = fputc((dw>>8)&0xff, fp); rtn = fputc(dw&0xff, fp); } return rtn;}typedef struct tagBITMAPINFOHEADER{ UINT32 biSize; INT32 biWidth; INT32 biHeight; UINT16 biPlanes; UINT16 biBitCount; UINT32 biCompression; UINT32 biSizeImage; INT32 biXPelsPerMeter; INT32 biYPelsPerMeter; UINT32 biClrUsed; UINT32 biClrImportant;} BITMAPINFOHEADER;typedef struct tagBITMAPFILEHEADER { BYTE bfType[2]; UINT32 bfSize; UINT16 bfReserved1; UINT16 bfReserved2; UINT32 bfOffBits;} BITMAPFILEHEADER;// save image as a Windows BMP 24 bits image// only 16/32 bit color image is supported// return 0 if successful// -1 on failint gfx_SaveBMP24b(const char * fname, GFX_SURFACE_LOCK_INFO_T *pSurface, UINT uStartX, UINT uStartY, UINT uWidth, UINT uHeight){ UINT32 offset; BYTE * buf; int i, j, k; FILE * fp; BITMAPFILEHEADER fhead; BITMAPINFOHEADER ihead; UINT mr, sr, mg, sg, mb, sb; if(!pSurface || !fname || !fname[0] ) return -1; if( #ifdef GFX_SURFACE_ARGB_1555 pSurface->uPlaneConfig != GFX_SURFACE_ARGB_1555 &&#endif#ifdef GFX_SURFACE_ARGB_4444 pSurface->uPlaneConfig != GFX_SURFACE_ARGB_4444 &&#endif#ifdef GFX_SURFACE_RGB_565 pSurface->uPlaneConfig != GFX_SURFACE_RGB_565 &&#endif pSurface->uPlaneConfig != GFX_SURFACE_ARGB_8888 ) { return -1; } if(gfx_ClipRect(pSurface->plane[0].uWidth, pSurface->plane[0].uHeight, uStartX, uStartY, &uWidth, &uHeight)) { return -1; // null clip } if ((buf = (BYTE *)malloc((uWidth*3L+3)&0xffffffc)) == NULL) return -1; fp=fopen(fname, "wb"); if(fp == NULL) { free(buf); return -1; } fhead.bfType[0] = 'B'; fhead.bfType[1] = 'M'; fhead.bfSize = 0x36L+((uWidth*3L+3)&0xffffffc)*uHeight; fhead.bfReserved1 = 0; fhead.bfReserved2 = 0; fhead.bfOffBits= 0x36L; ihead.biSize = 0x28L; ihead.biWidth = (INT32)uWidth; ihead.biHeight = (INT32)uHeight; ihead.biPlanes = 1; ihead.biBitCount = 24; ihead.biCompression = 0L; ihead.biXPelsPerMeter=2835; // 72 dpi ihead.biYPelsPerMeter=2835; ihead.biClrUsed=0; ihead.biClrImportant=0; ihead.biSizeImage = ((uWidth*3L+3)&0xffffffc)*uHeight; // write filehead fputc(fhead.bfType[0], fp); fputc(fhead.bfType[1], fp); fputLong(fhead.bfSize, fp, 1); fputWord(fhead.bfReserved1, fp, 1); fputWord(fhead.bfReserved2, fp, 1); fputLong(fhead.bfOffBits, fp, 1); // write infohead fputLong(ihead.biSize, fp, 1); fputLong((UINT32)ihead.biWidth, fp, 1); fputLong((UINT32)ihead.biHeight, fp, 1); fputWord(ihead.biPlanes, fp, 1); fputWord(ihead.biBitCount, fp, 1); fputLong(ihead.biCompression, fp, 1); fputLong(ihead.biSizeImage, fp, 1); fputLong((UINT32)ihead.biXPelsPerMeter, fp, 1); fputLong((UINT32)ihead.biYPelsPerMeter, fp, 1); fputLong(ihead.biClrUsed, fp, 1); fputLong(ihead.biClrImportant, fp, 1); mr = 0xff >> (8-pSurface->plane[0].r.uNumbits); sr = pSurface->plane[0].r.uOffset; mg = 0xff >> (8-pSurface->plane[0].g.uNumbits); sg = pSurface->plane[0].g.uOffset; mb = 0xff >> (8-pSurface->plane[0].b.uNumbits); sb = pSurface->plane[0].b.uOffset; for(i=uHeight-1; i>=0; i--) { offset = pSurface->plane[0].uBytePerLine * ((UINT32)i+uStartY) + pSurface->plane[0].uPixelSize/8 * uStartX; 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 + offset); for(j=0, k=0; j<uWidth; j++, k+=3) { buf[k ] = (BYTE)((pImg[j]>>sb)&mb); buf[k+1] = (BYTE)((pImg[j]>>sg)&mg); buf[k+2] = (BYTE)((pImg[j]>>sr)&mr); } }#endif case GFX_SURFACE_ARGB_8888: { UINT32 *pImg = (UINT32 *)((BYTE *)pSurface->plane[0].pPlane + offset); for(j=0, k=0; j<uWidth; j++, k+=3) { buf[k ] = (BYTE)((pImg[j]>>sb)&mb); buf[k+1] = (BYTE)((pImg[j]>>sg)&mg); buf[k+2] = (BYTE)((pImg[j]>>sr)&mr); } } } fwrite(buf, (uWidth*3L+3)&0xffffffc, 1, fp); } fclose(fp); free(buf); return 0;}// save image as a Windows BMP 8 bits image// conversion will be added if needed.// return 0 if successfulint gfx_SaveBMP8b(const char * fname, GFX_SURFACE_LOCK_INFO_T *pSurface, GFX_PALETTE_T *pPal, UINT uStartX, UINT uStartY, UINT uWidth, UINT uHeight){ UINT32 offset, uJust; BYTE * buf; int i; FILE * fp; BITMAPFILEHEADER fhead; BITMAPINFOHEADER ihead; if(!pSurface || !fname || !fname[0] ) return -1; if( pSurface->uPlaneConfig != GFX_SURFACE_CLUT8BPP_ARGB && pSurface->uPlaneConfig != GFX_SURFACE_RAW8BPP) { return -1; } if(gfx_ClipRect(pSurface->plane[0].uWidth, pSurface->plane[0].uHeight, uStartX, uStartY, &uWidth, &uHeight)) { return -1; // null clip } uJust = (uWidth&3) ? 4 - (uWidth&3) : 0; if ((buf = (BYTE *)malloc(1024)) == NULL) return -1; fp=fopen(fname, "wb"); if(fp == NULL) { free(buf); return -1; } ihead.biSize = 0x28L; ihead.biWidth = (INT32)uWidth; ihead.biHeight = (INT32)uHeight; ihead.biPlanes = 1; ihead.biBitCount = 8; ihead.biCompression = 0L; ihead.biXPelsPerMeter=2835; // 72 dpi ihead.biYPelsPerMeter=2835; ihead.biClrUsed=256; ihead.biClrImportant=0; ihead.biSizeImage = (((long)uWidth+3)&0xfffffffc)*uHeight; fhead.bfType[0] = 'B'; fhead.bfType[1] = 'M'; fhead.bfReserved1 = 0; fhead.bfReserved2 = 0; fhead.bfSize = 0x36L+ihead.biSizeImage+ihead.biClrUsed*4; fhead.bfOffBits= 0x36L+ihead.biClrUsed*4; // write filehead fputc(fhead.bfType[0], fp); fputc(fhead.bfType[1], fp); fputLong(fhead.bfSize, fp, 1); fputWord(fhead.bfReserved1, fp, 1); fputWord(fhead.bfReserved2, fp, 1); fputLong(fhead.bfOffBits, fp, 1); // write infohead fputLong(ihead.biSize, fp, 1); fputLong((UINT32)ihead.biWidth, fp, 1); fputLong((UINT32)ihead.biHeight, fp, 1); fputWord(ihead.biPlanes, fp, 1); fputWord(ihead.biBitCount, fp, 1); fputLong(ihead.biCompression, fp, 1); fputLong(ihead.biSizeImage, fp, 1); fputLong((UINT32)ihead.biXPelsPerMeter, fp, 1); fputLong((UINT32)ihead.biYPelsPerMeter, fp, 1); fputLong(ihead.biClrUsed, fp, 1); fputLong(ihead.biClrImportant, fp, 1); if(pPal) { for(i=0; i<256; i++) { buf[i*4+3] = pPal[i].a; buf[i*4+2] = pPal[i].r; buf[i*4+1] = pPal[i].g; buf[i*4+0] = pPal[i].b; } } else { for(i=0; i<256; i++) { buf[i*4+3] = 255; buf[i*4+2] = i; buf[i*4+1] = i; buf[i*4+0] = i; } } fwrite(buf, 1024, 1, fp); // write palette memset(buf, 0, 4); for(i=uHeight-1; i>=0; i--) { offset = pSurface->plane[0].uBytePerLine * ((UINT32)i+uStartY) + pSurface->plane[0].uPixelSize/8 * uStartX; fwrite((BYTE *)pSurface->plane[0].pPlane + offset, uWidth, 1, fp); if(uJust) fwrite(buf, uJust, 1, fp); } fclose(fp); free(buf); return 0;}// load Windows BMP 24 bits files// Return 0 if successfulint gfx_LoadBMP24b(GFX_SURFACE_LOCK_INFO_T *pSurface, 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, j, k; FILE * fp; BITMAPFILEHEADER fhead; BITMAPINFOHEADER ihead; UINT rr, sr, rg, sg, rb, sb; int reverse = 0; if(!pSurface || !fname || !fname[0] ) return -1; if( #ifdef GFX_SURFACE_ARGB_1555 pSurface->uPlaneConfig != GFX_SURFACE_ARGB_1555 &&#endif#ifdef GFX_SURFACE_ARGB_4444 pSurface->uPlaneConfig != GFX_SURFACE_ARGB_4444 &&#endif#ifdef GFX_SURFACE_RGB_565 pSurface->uPlaneConfig != GFX_SURFACE_RGB_565 &&#endif pSurface->uPlaneConfig != GFX_SURFACE_ARGB_8888 ) { 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 != 24 || ihead.biCompression != 0L) // not a valid 24bits 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 } uJust = (ihead.biWidth*3L+3)&0xffffffc; if ((buf = (BYTE *)malloc(uJust)) == NULL) { fclose(fp); return -1; } rr = (8-pSurface->plane[0].r.uNumbits); sr = pSurface->plane[0].r.uOffset; rg = (8-pSurface->plane[0].g.uNumbits); sg = pSurface->plane[0].g.uOffset; rb = (8-pSurface->plane[0].b.uNumbits); sb = pSurface->plane[0].b.uOffset; for(i=0; i<uHeight; i++) { if(reverse) { offsetd = pSurface->plane[0].uBytePerLine * ((UINT32)i+uDesY) + pSurface->plane[0].uPixelSize/8 * uDesX; } else
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -