www.pudn.com > libpcx.rar > PCX.CPP


 
#include  
#include  
 
#include "pcx.h" 
#include "..\bmp.h" 
 
int LoadPcx(const char* srcName,BmpInfo* bmpinfo) 
{ 
   PCXHead sHeader; 
   int clFileSize; 
   FILE *pFile; 
   BYTE *pabFileData; 
   int iScanLineSize; 
   ldiv_t sDivResult; 
   long clImageSize; 
   BITMAPINFO * psBmpInfo ; 
   long lDataPos; 
   long lPos;     // That's where the data begins 
   UINT uiValue; 
   BYTE* pabRawBitmap ; 
   int BmpSizey,BmpSizex; 
   int iY,iX; 
   BYTE Color; 
   UINT bRepeat; 
   int Entry; 
   // Open the file and put its entire content in memory 
   pFile = fopen( srcName, "rb" ); 
   if ( !pFile )return -1; 
 
   clFileSize = filelength(_fileno(pFile)); 
   pabFileData = (BYTE *)malloc(clFileSize); 
 
   fread(pabFileData,clFileSize,1,pFile); 
   fclose(pFile); 
   // Get the header 
   memcpy(&sHeader,pabFileData,sizeof(PCXHead)); 
 
   // Each scan line MUST have a size that can be divided by a 'long' data type 
   iScanLineSize = sHeader.NumPlanes * sHeader.BPL; 
   sDivResult = ldiv( iScanLineSize, sizeof(long)); 
   if ( sDivResult.rem > 0 ) 
      iScanLineSize = (iScanLineSize/sizeof(long)+1) * sizeof(long); 
 
   // Set the bitmap size data member 
   BmpSizex = sHeader.X2-sHeader.X1+1; 
   BmpSizey = sHeader.Y2-sHeader.Y1+1; 
   clImageSize = iScanLineSize * BmpSizey; 
 
   // Set the bitmap information 
   psBmpInfo = (BITMAPINFO *)malloc( sizeof(BITMAPINFOHEADER) + 
                                             (sizeof(RGBQUAD)*256)); 
   psBmpInfo->bmiHeader.biSize           = sizeof(BITMAPINFOHEADER); 
   psBmpInfo->bmiHeader.biWidth          = BmpSizex; 
   psBmpInfo->bmiHeader.biHeight         = -BmpSizey; 
   psBmpInfo->bmiHeader.biPlanes         = sHeader.NumPlanes; 
   psBmpInfo->bmiHeader.biBitCount       = sHeader.BitPerPixel; 
   psBmpInfo->bmiHeader.biCompression    = BI_RGB; 
   psBmpInfo->bmiHeader.biSizeImage      = 0; 
   psBmpInfo->bmiHeader.biXPelsPerMeter  = 0; 
   psBmpInfo->bmiHeader.biYPelsPerMeter  = 0; 
   psBmpInfo->bmiHeader.biClrUsed        = 0; 
   psBmpInfo->bmiHeader.biClrImportant   = 0; 
 
   // Prepare a buffer large enough to hold the image 
   pabRawBitmap = (BYTE *)malloc(clImageSize); 
   if ( !pabRawBitmap ) 
   { 
      free(pabFileData); 
      free(psBmpInfo); 
      return -1; 
   } 
 
   // Get the compressed image 
   lDataPos = 0; 
   lPos = 128;     // That's where the data begins 
   for(iY=0;iY 192 ) {  // Two high bits are set = Repeat 
            uiValue -= 192;                  // Repeat how many times? 
            Color = pabFileData[lPos++];  // What color? 
 
            if ( iX <= BmpSizex ) 
            {  // Image data.  Place in the raw bitmap. 
               for ( bRepeat=0; bRepeat < uiValue; bRepeat++ ) 
               { 
                  pabRawBitmap[lDataPos++] = Color; 
                  iX++; 
               } 
            } 
            else 
               iX += uiValue; // Outside the image.  Skip. 
         } 
         else 
         { 
            if ( iX <= BmpSizex ) 
               pabRawBitmap[lDataPos++] = uiValue; 
            iX++; 
         } 
      } 
 
      // Pad the rest with zeros 
      if ( iX < iScanLineSize ) 
      { 
         for ( ;iX < iScanLineSize; iX++ ) 
            pabRawBitmap[lDataPos++] = 0; 
      } 
   } 
 
   if ( pabFileData[lPos++] == 12 )          // Simple validation 
      // Get the palette 
      for (Entry=0; Entry < 256; Entry++ ) 
      { 
         psBmpInfo->bmiColors[Entry].rgbRed       = pabFileData[lPos++]; 
         psBmpInfo->bmiColors[Entry].rgbGreen     = pabFileData[lPos++]; 
         psBmpInfo->bmiColors[Entry].rgbBlue      = pabFileData[lPos++]; 
         psBmpInfo->bmiColors[Entry].rgbReserved  = 0; 
      } 
 
   free(pabFileData); 
   bmpinfo->pInfo= psBmpInfo; 
   bmpinfo->pData= pabRawBitmap; 
   return 1; 
} 
 
 
int PcxToBmp(const char* srcName,const char* DesName) 
{ 
        BmpInfo* bmpinfo=(BmpInfo* ) malloc(sizeof(BmpInfo)); 
        bmpinfo->pInfo=NULL; 
        bmpinfo->pData=NULL; 
        if(LoadPcx(srcName,bmpinfo)>0) 
        { 
                SaveBmpToFile(bmpinfo,DesName); 
        } 
        else 
        { 
                free(bmpinfo->pInfo); 
                free(bmpinfo->pData); 
                return -1; 
        } 
        free(bmpinfo->pInfo); 
        free(bmpinfo->pData); 
        free(bmpinfo); 
        return 1; 
} 
 
int ggg() 
{ 
        ;; 
        return -1; 
};