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

📄 bmp.cpp

📁 In term project, we will take the baseline JPEG codec in ARM-based platform system as an example to
💻 CPP
字号:
#include "type.h"
#include <stdio.h>
#include <stdlib.h>

#define FIX_0_300(val) ((val*3  )/10 )
#define FIX_0_600(val) ((val*6  )/10 )
#define FIX_0_100(val) ((val    )/10 )
#define FIX_0_150(val) ((val*15 )/100)
#define FIX_0_450(val) ((val*45 )/100)
#define FIX_0_437(val) ((val*7  )/16 )
#define FIX_0_375(val) ((val*6  )/16 )
#define FIX_0_062(val) ((val    )/16 )

#define FIX_0_333(val) ((val*3  )/10 )
#define FIX_1_600(val) ((val*160)/100)
#define FIX_0_800(val) ((val*80 )/100)

#define Add_0_5(val) ( ((val)*8+4) / 8 )
#define Sub_0_5(val) ( ((val)*8-4) / 8 )

DWord RGB2YCbCr(DWord rgb)
{int r = ( rgb&(0x00FF0000) )>>16;
 int g = ( rgb&(0x0000FF00) )>>8 ;
 int b = ( rgb&(0x000000FF) )    ;

 int yy, cb, cr;

 yy =  FIX_0_300(r) + FIX_0_600(g) + FIX_0_100(b);

 cb = -FIX_0_150(r) - FIX_0_300(g) + FIX_0_450(b); cb+=128;

 cr =  FIX_0_437(r) - FIX_0_375(g) - FIX_0_062(b); cr+=128;

 if( yy>=256 ) yy=0xFF;
 if( cb>=256 ) cb=0xFF;
 if( cr>=256 ) cr=0xFF;

 return ( (yy<<16) | (cb<<8) | cr );
}

DWord YCbCr2RGB(DWord ycbcr)
{int yy =( ( ycbcr&(0x00FF0000) )>>16 );
 int cb =( ( ycbcr&(0x0000FF00) )>>8  )-128;
 int cr =( ( ycbcr&(0x000000FF) )     )-128;

 int r, g, b;

 r =  yy + 0             + FIX_1_600(cr);
 g =  yy - FIX_0_333(cb) - FIX_0_800(cr);
 b =  yy + 2*cb          + 0            ;

 if( r>=256 ) r=0xFF; else if( r<0 ) r=0;
 if( g>=256 ) g=0xFF; else if( g<0 ) g=0;
 if( b>=256 ) b=0xFF; else if( b<0 ) b=0;

 return ( (r<<16) | (g<<8) | b );
}

/*----------------------------------------------------------------------------*/

bool ReadBMP(const char* file, Picture* pic)
{BMP b;

 if( pic==0 ) return false;

 FILE* handle=fopen(file,"rb"); if( handle==NULL ){ return false; }

 fread(&(b.Identify        ), 1, 2, handle);
 fread(&(b.File_Size       ), 1, 4, handle);
 fread(&(b.Reseseve        ), 1, 4, handle);
 fread(&(b.Data_Offset     ), 1, 4, handle);
 fread(&(b.Header_Size     ), 1, 4, handle);
 fread(&(b.Width           ), 1, 4, handle);
 fread(&(b.Height          ), 1, 4, handle);
 fread(&(b.Planes          ), 1, 2, handle);
 fread(&(b.Bit_Per_Pixel   ), 1, 2, handle); if( b.Bit_Per_Pixel!=24 ){ fclose(handle); return false; }
 fread(&(b.Compression     ), 1, 4, handle);
 fread(&(b.Bitmap_Data_Size), 1, 4, handle);
 fread(&(b.HResolution     ), 1, 4, handle);
 fread(&(b.VResolution     ), 1, 4, handle);
 fread(&(b.Colors          ), 1, 4, handle);
 fread(&(b.Importat_Colors ), 1, 4, handle);

 pic->Create( b.Width , b.Height );

 DWord ycbcr, rgb=0;
 Byte  align_width = (b.Width*3)%4;

 for(int y=(int)b.Height-1; y>=0          ; y-- )
    { for(int x=0; x<(int)b.Width; x++ )
         { fread(&rgb,3,1,handle);
           ycbcr = RGB2YCbCr(rgb);
           pic->Pixel[y][x] = ycbcr;
         }
      /*讽 Pixel 

⌨️ 快捷键说明

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