Subversion Repositories Kolibri OS

Rev

Go to most recent revision | Blame | Last modification | View Log | Download | RSS feed

  1. #include "..\kosSyst.h"
  2. #include "bmp.h"
  3.  
  4. int BMPFile::LoadBMPFile(Byte* filebuff, Dword filesize)
  5. {
  6.   Dword offset;
  7.  
  8.   memcpy((Byte*)&Bmp_head,(Byte*)filebuff,sizeof(tagBITMAPFILEHEADER));
  9.   memcpy((Byte*)&Info_head,(Byte*)filebuff+14,sizeof(tagBITMAPINFOHEADER));
  10.  
  11.   width=Info_head.biWidth;
  12.   height=Info_head.biHeight;
  13.   offset=(Dword)Bmp_head.bfOffbits;
  14.  
  15.   int state=0;
  16.   if (Bmp_head.bfType==0x4d42 && !Info_head.biCompression)
  17.   {
  18.     Byte* cBuffer;
  19.     Byte* pImg;
  20.     Byte* pPal;
  21.     Dword x,y;
  22.     Byte  r;
  23.     Dword s,p;
  24.     Dword cWidth;
  25.     int i;
  26.    
  27.     buffer=kos_GetMemory(width*height*3);
  28.     pImg=filebuff+offset;
  29.     pPal=filebuff+53;
  30.    
  31.     int align_bytes = (4 - ((width * 3) % 4)) % 4;
  32.     Dword bpp = Info_head.biBitCount;
  33.    
  34.     switch(Info_head.biBitCount)
  35.     {
  36.       /* 16,24,32-bit decoding */
  37.       case 32:
  38.       case 24:
  39.       case 16:
  40.         for(y=height-1;y!=-1;y--)
  41.         {
  42.           for(x=0;x<width;x++)
  43.           {
  44.             cBuffer=buffer+(y*width*3)+x*3;
  45.             if (Info_head.biBitCount==16)
  46.             {
  47.               *(cBuffer+0)=(Byte)((*(Word*)(pImg)) & 31)<<3;
  48.               *(cBuffer+1)=(Byte)((*(Word*)(pImg)>>5) & 31)<<3;
  49.               *(cBuffer+2)=(Byte)((*(Word*)(pImg)>>10) & 31)<<3;
  50.             } else {
  51.               *(cBuffer+0)=*(pImg+0);
  52.               *(cBuffer+1)=*(pImg+1);
  53.               *(cBuffer+2)=*(pImg+2);
  54.             }
  55.             pImg=pImg+Info_head.biBitCount/8;
  56.           }
  57.           pImg=pImg+align_bytes;
  58.         }
  59.       break;
  60.       /* 8-bit decoding */
  61.       case 8:
  62.         for(y=height-1;y!=-1;y--)
  63.         {
  64.           for(x=0;x<width;x++)
  65.           {
  66.             r=*(pImg); pImg++;
  67.             cBuffer=buffer+(y*width*3)+x*3;
  68.             *(cBuffer+0)=(Byte)(pPal[r*4+1]);
  69.             *(cBuffer+1)=(Byte)(pPal[r*4+2]);
  70.             *(cBuffer+2)=(Byte)(pPal[r*4+3]);
  71.           }
  72.         }
  73.       break;
  74.       /* 1,4-bit decode */
  75.       case 4:
  76.       case 1:
  77.         for(y=height-1;y!=-1;y--)
  78.         {
  79.           x=0;
  80.           while(x<width-1)
  81.           {
  82.             s=*(Dword*)(pImg);
  83.             pImg=pImg+4;
  84.             __asm
  85.             {
  86.               mov  eax,s
  87.               bswap eax
  88.               mov  s,eax
  89.             }
  90.             for(i=0;i<32/bpp;i++)
  91.             {
  92.               if (x>=width) break;
  93.               __asm
  94.               {
  95.                 mov  eax,s
  96.                 mov  ecx,bpp
  97.                 rol  eax,cl
  98.                 mov  s,eax
  99.                 mov  ebx,1
  100.                 shl  ebx,cl
  101.                 dec  ebx
  102.                 and  eax,ebx
  103.                 mov  p,eax
  104.              }
  105.               cBuffer=buffer+(y*width*3)+x*3;
  106.               *(cBuffer+0)=(Byte)(pPal[p*4+1]);
  107.               *(cBuffer+1)=(Byte)(pPal[p*4+2]);
  108.               *(cBuffer+2)=(Byte)(pPal[p*4+3]);
  109.               x++;
  110.             }
  111.           }
  112.         }
  113.       break;
  114.       default:
  115.         state=1;
  116.       break;
  117.     }
  118.   } else {
  119.     state=1;
  120.   }
  121.   return state;
  122. }