⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 wingraph.c

📁 MinGUI 可视化程序代码
💻 C
字号:
 /******************************************************************************** Copyright  2006 National ASIC Center, All right Reserved** FILE NAME:      wingraph.c* PROGRAMMER:     ming.c* Date of Creation:   2006/08/8** DESCRIPTION:** NOTE:** FUNCTIONS LIST:* -----------------------------------------------------------------------------** -----------------------------------------------------------------------------** MODIFICATION HISTORY*     LastModify  2006/10/30******************************************************************************/#include "mingui.h"//---------------------------------------------------------------------------
TBitmap *CreateBitmap(void);
void FreeBitmap(TBitmap *bitmap);
BOOL LoadBitmapFromFile(TBitmap *bitmap, const char* file_name);
//---------------------------------------------------------------------------
extern int  CM_DecodeJPG(TImageHead *imgHead, STREAM *stream);
extern int  CM_DecodeBMP(TImageHead *imgHead, STREAM *stream);
extern int  CM_DecodeLCDIMG(TImageHead *imgHead, const BYTE *pImage);

static int  CM_ExpandBitmap(TBitmap *bitmap,TImageHead *imgHead);
static char *get_extension (const char* filename);
//---------------------------------------------------------------------------
TBitmap *CreateBitmap(void)
{ int AllocateSize=sizeof(TBitmap)+sizeof(TWndCanvas);
  TBitmap *bitmap=(TBitmap *)GetMem(AllocateSize);
  if(bitmap)
  { memset(bitmap,0,AllocateSize);
    bitmap->Handle=(HDC)((unsigned long)bitmap+sizeof(TBitmap));
  }
  return bitmap;
}
//---------------------------------------------------------------------------
void DestroyBitmap(TBitmap *bitmap)
{ if(bitmap && bitmap->Handle)
  { TWndCanvas *bmCanvas=(TWndCanvas *)bitmap->Handle;
    if(bmCanvas->Vram)
    { FreeVRAM(bmCanvas->Vram);
      bmCanvas->Vram=NULL;
    }
    bitmap->Handle=NULL;
    bitmap->Width=0;
    bitmap->Height=0;
    FreeMem(bitmap);
  }
}
//---------------------------------------------------------------------------
#if (BITS_PER_PIXEL<8)
  #define Expand_Bitmap_Extr_define   int unitbits=0,desByteBits=0;
  #define Expand_Bitmap_Line_Finish()\
     if(!desByteBits) \
     { *LinePos=unitbits;\
       unitbits=desByteBits=0;\
     }\
     imgLineHead+=imgPitch;\
     LineHead+=vram->BytePerLine;
#else
  #define Expand_Bitmap_Extr_define   
  #define Expand_Bitmap_Line_Finish()\
     imgLineHead+=imgPitch;\
     LineHead+=vram->BytePerLine;   
#endif
 
#if (BITS_PER_PIXEL==PIXEL_UNIT) /* 8 16 32*/
  #define Expand_Bitmap_Judge() \
          { *(PIXEL *)LinePos=mappixel;\
            LinePos+=UNIT_BYTE;\
          } 
#elif (BITS_PER_PIXEL==24)
  #define Expand_Bitmap_Judge()\
          { *LinePos++=(BYTE)mappixel;\
            *LinePos++=(BYTE)(mappixel>>8);\
            *LinePos++=(BYTE)(mappixel>>16);\
          }
#elif (BITS_PER_PIXEL<8)
  #define Expand_Bitmap_Judge()\
          { unitbits|=OPT_OFFSET_PIXEL(mappixel,BITS_PER_PIXEL,desByteBits);\
            desByteBits+=BITS_PER_PIXEL;\
            if(desByteBits==8)\
            { *LinePos++=unitbits;\
              unitbits=desByteBits=0;\
            }\
          }
#endif
//---------------------------------------------------------------------------
BOOL Expand_Bitmap_Color_Less(VRAM *vram, TImageHead *imgHead)
{ Expand_Bitmap_Extr_define 
  int imgHeight=imgHead->height; 
  int imgWidth=imgHead->width; 
  int imgPitch=imgHead->pitch; 
  PIXEL *imgTransPal=imgHead->transPal; 
  BYTE *imgLineHead=imgHead->bits; 
  BYTE *LineHead=(BYTE *)vram->FrameBuffer; 
  BYTE *LinePos,*imgBits; 
  PIXEL mappixel; 
  DWORD mapcolor;
  int   w;
  int   srcByteBits;  
  int   imgBpp=imgHead->bpp;
  int   imgBppMask=BITS_MASK(imgBpp);
  while (--imgHeight >= 0) 
  { imgBits=imgLineHead;
    LinePos=LineHead;
    srcByteBits=0;
    for(w=0;w<imgWidth;w++)
    { if(srcByteBits==0)
      { mapcolor=*imgBits++;
        srcByteBits=8-imgBpp;
      }
      else 
      { srcByteBits-=imgBpp;
      }
      mappixel=imgTransPal[(mapcolor>>srcByteBits)&imgBppMask];
      Expand_Bitmap_Judge();
   }
   Expand_Bitmap_Line_Finish();
  }
  return true;
}
//---------------------------------------------------------------------------
BOOL Expand_Bitmap_Color_More(VRAM *vram, TImageHead *imgHead)
{ Expand_Bitmap_Extr_define 
  int imgHeight=imgHead->height; 
  int imgWidth=imgHead->width; 
  int imgPitch=imgHead->pitch; 
  PIXEL *imgTransPal=imgHead->transPal; 
  BYTE *imgLineHead=imgHead->bits; 
  BYTE *LineHead=(BYTE *)vram->FrameBuffer; 
  BYTE *LinePos,*imgBits;
  PIXEL mappixel; 
  int w; 
  int imgBpp=imgHead->bpp;
  DWORD mapcolor;
  RGBQUAD rgbColor;
  *(DWORD *)&rgbColor=0;
  while (--imgHeight >= 0) 
  { imgBits=(BYTE *)imgLineHead;
    LinePos=LineHead;
    for(w=0;w<imgWidth;w++)
    { switch(imgBpp)
      { case 8:  mappixel=imgTransPal[*imgBits++];
                 break;
        case 15: /* RGB 555 */ 
                 #if(INDEX_RED_BITWIDTH==5 && INDEX_GREEN_BITWIDTH==5 && INDEX_BLUE_BITWIDTH==5)
                 mappixel=*(WORD *)imgBits;
                 #else
                 mapcolor=*(WORD *)imgBits;
                 rgbColor.r = (BYTE)( (mapcolor>>7)&0xf8 );
                 rgbColor.g = (BYTE)( (mapcolor>>2)&0xf8 );
                 rgbColor.b = (BYTE)(  mapcolor<<3 );
                 mappixel=ColorMapToPixel(&rgbColor);
                 #endif
                 imgBits+=2;
                 break;
        case 16: /* RGB 565 */ 
                 #if(INDEX_RED_BITWIDTH==5 && INDEX_GREEN_BITWIDTH==6 && INDEX_BLUE_BITWIDTH==5)
                 mappixel=*(WORD *)imgBits;
                 #else
                 mapcolor=*(WORD *)imgBits;
                 rgbColor.r = (BYTE)( (mapcolor>>7)&0xf8 );
                 rgbColor.g = (BYTE)( (mapcolor>>2)&0xf8 );
                 rgbColor.b = (BYTE)(  mapcolor<<3 );
                 mappixel=ColorMapToPixel(&rgbColor);
                 #endif
                 imgBits+=2;
                 break;
        case 24: rgbColor.b = *imgBits++;
                 rgbColor.g = *imgBits++;
                 rgbColor.r = *imgBits++;
                 #if(BITS_PER_PIXEL==24)
                 mappixel=*(DWORD *)&rgbColor;
                 #else
                 mappixel=ColorMapToPixel(&rgbColor);
                 #endif
                 break;
        case 32: rgbColor.b = *imgBits++;
                 rgbColor.g = *imgBits++;
                 rgbColor.r = *imgBits++;
                 rgbColor.a = *imgBits++;
                 #if(BITS_PER_PIXEL==24)
                 mappixel=*(DWORD *)&rgbColor;
                 #else
                 mappixel=ColorMapToPixel(&rgbColor);
                 #endif
                 break;

      }
      Expand_Bitmap_Judge();
   }
   Expand_Bitmap_Line_Finish();
  }
  return true;
}
//---------------------------------------------------------------------------
BOOL OptimizeExpandBitmap(VRAM *vram,TImageHead *imgHead)
{ BOOL pal_matched=true;
  if(imgHead->bpp==SYS_EFFECT_BPP)
  { if(imgHead->bpp<=8)
    { int i,palsize=imgHead->palsize;
      PIXEL *transPal=imgHead->transPal;
      for(i=0;i<palsize;i++)
      { if(transPal[i]!=(PIXEL)i)
        { pal_matched=false;
          break;
        }
      }
    }
    if(pal_matched)
    { if(imgHead->pitch == vram->BytePerLine)
      { DWORD datasize=imgHead->pitch * imgHead->height;
        memcpy(vram->FrameBuffer,imgHead->bits,datasize);
      }
      else
      { int imgHeight=vram->Height;
        int lineBytesCount=min(imgHead->pitch,vram->BytePerLine);
        BYTE *imgBits=imgHead->bits;
        BYTE *vramBits=(BYTE *)vram->FrameBuffer;
        while (--imgHeight >= 0) 
        { memcpy(vramBits,imgBits,lineBytesCount);
          vramBits+=vram->BytePerLine;
          imgBits+=imgHead->pitch;
        }
      }
      return true;
    }
  }
  return false;
}
//---------------------------------------------------------------------------
int CM_ExpandBitmap(TBitmap *bitmap,TImageHead *imgHead)
{ if(bitmap && bitmap->Handle)
  { TWndCanvas *bmCanvas=(TWndCanvas *)bitmap->Handle;
    if(bmCanvas->Vram)
    {  int ret=0;
       if(imgHead->bpp==SYS_EFFECT_BPP)
       { ret=OptimizeExpandBitmap(bmCanvas->Vram,imgHead);
       }
       if(!ret)
       { if(imgHead->bpp<8)
         {  ret=Expand_Bitmap_Color_Less(bmCanvas->Vram,imgHead);
         }
         else
         {  ret=Expand_Bitmap_Color_More(bmCanvas->Vram,imgHead);
         }
       }
       if(ret)
       {  /*以位图左上角像素值作为背静色*/
         bmCanvas->Background=GetPosPixel((HDC)bmCanvas,0,0);
         return true;
       }
    }
  }
  return false;
}
//---------------------------------------------------------------------------
static BOOL ResetBitmapSize(TBitmap *bitmap,int nWidth,int nHeight)
{ if(bitmap && bitmap->Handle)
  { TWndCanvas *bmCanvas=(TWndCanvas *)bitmap->Handle;
    if(bmCanvas->Vram)
    { if(bmCanvas->Vram->Width!=nWidth || bmCanvas->Vram->Height!=nHeight)
      { FreeVRAM(bmCanvas->Vram);
        bmCanvas->Vram=NULL;
      } else return true;
    }
	
    bmCanvas->Vram=CreateVRAM(0,0,nWidth,nHeight,NULL,false);
    if(bmCanvas->Vram)
    { bitmap->Width=nWidth;
      bitmap->Height=nHeight;
	  bmCanvas->ClipRect.left=bmCanvas->ClipRect.top=0;
	  bmCanvas->ClipRect.right=nWidth;
      bmCanvas->ClipRect.bottom=nHeight;
      return true;
    }
  }
  return false;
}
//---------------------------------------------------------------------------
void UnloadBitmap(TBitmap *bitmap)
{ if(bitmap && bitmap->Handle)
  { TWndCanvas *bmCanvas=(TWndCanvas *)bitmap->Handle;
    if(bmCanvas->Vram)
    { FreeVRAM(bmCanvas->Vram);
      bmCanvas->Vram=NULL;
    }
    bitmap->Width=0;
    bitmap->Height=0;
  }
}
//---------------------------------------------------------------------------
BOOL LoadBitmapFromFile(TBitmap *bitmap, const char* file_name)
{ TImageHead imgHead;
  STREAM *bmpFileStream;
  
  int ret=0;

  imgHead.bits=NULL;
  imgHead.transPal=NULL;
  imgHead.palsize=0;

  bmpFileStream = OpenFileStream(file_name);
  if(bmpFileStream)
  { char *file_ext = get_extension (file_name); 
    if(strcasecmp(file_ext,"bmp")==0)
    {  ret = CM_DecodeBMP(&imgHead, bmpFileStream);
    }
    else if(strcasecmp(file_ext,"jpg")==0 || strcasecmp(file_ext,"jpeg")==0)
    {  ret = CM_DecodeJPG(&imgHead, bmpFileStream);
    }
    else
    {  ret = 0;
    }
    CloseStream(bmpFileStream);
  }
  if(ret)
  { ret= ResetBitmapSize(bitmap,imgHead.width,imgHead.height);
    ret= ret && CM_ExpandBitmap(bitmap,&imgHead);
  }
  if(imgHead.transPal)FreeMem(imgHead.transPal);
  if(imgHead.bits)FreeMem(imgHead.bits);
  if(!ret)
  { UnloadBitmap(bitmap);
  }
  return ret;
}

//---------------------------------------------------------------------------
BOOL LoadBitmapFromMem(TBitmap *bitmap, const BYTE *imgData,const int imgDataLen)
{ int ret=0;
  TImageHead imgHead;
  imgHead.bits=NULL;
  imgHead.transPal=NULL;
  imgHead.palsize=0;
      
  if(*imgData==0x42 && *(imgData+1)==0x4d) /* bmp */
  { STREAM *bmpMemStream = OpenMemStream(imgData,imgDataLen);
    if(bmpMemStream)
    {  ret = CM_DecodeBMP(&imgHead, bmpMemStream);
    }
    CloseStream(bmpMemStream);
  }
  if(!ret)
  { if(*imgData==0xFF && *(imgData+1)==0xD8) /* JPG */
    { STREAM *bmpMemStream = OpenMemStream(imgData,imgDataLen);
      if(bmpMemStream)
      {  ret = CM_DecodeJPG(&imgHead, bmpMemStream);
      }
      CloseStream(bmpMemStream);
    }
    if(!ret)
    { ret = CM_DecodeLCDIMG(&imgHead, imgData);
    }
  }

  if(ret)
  { ret= ResetBitmapSize(bitmap,imgHead.width,imgHead.height);
    ret= ret && CM_ExpandBitmap(bitmap,&imgHead);
  }
  if(imgHead.transPal)FreeMem(imgHead.transPal);
  if(imgHead.bits)FreeMem(imgHead.bits);
  if(!ret)
  { UnloadBitmap(bitmap);
  }
  return ret;
}
//---------------------------------------------------------------------------
static char* get_extension (const char* filename)
{
    char* ext;

    ext = strrchr (filename, '.');

    if (ext)
        return ext + 1;

    return NULL;
}
/*---------------------------------------------------------------------------END --- Thank you!                                                  ming.c---------------------------------------------------------------------------*/

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -