Can read in the BMP data correctly ,but the size is not right?

88888 Dihedral dihedral88888 at googlemail.com
Mon Apr 29 14:12:38 EDT 2013


Jimmie He於 2013年4月30日星期二UTC+8上午1時20分49秒寫道:
> I'm trying to read in the BMP data by the the code below,and I'm check the data array with WINHEX,and it is correct,but which confuse me is why the size is 0x180,but the actual picture should be 48*48 = 0x120 bytes because I use 1-bit BMP not the 24bit BMP,could any one give some hints?
> 
> 
> 
> 
> 
> --------------------------------------------------------------------------------
> 
> __Head_Info = [
> 
>             [ 'Type'         ,0 , 2],#BM
> 
>             [ 'FSize'        ,2 , 4],#File Size
> 
>             [ 'Reserved'     ,6 , 4],#0x00000000
> 
>             [ 'OffBits'      ,10 , 4],#Offset of Image
> 
>             [ 'SSize'        ,14 , 4],# 40
> 
>             [ 'Width'        ,18 , 4],#Width
> 
>             [ 'Height'       ,22 , 4],#Hight
> 
>             [ 'Planes'       ,26 , 2],#1
> 
>             [ 'BitCount'     ,28 , 2],#{1,2,4,8,24}
> 
>             [ 'Compress'     ,30 , 4],#0
> 
>             [ 'SizeImage'    ,34 , 4],#Bytes Per Line
> 
>             [ 'XPM'          ,38 , 4],#2835
> 
>             [ 'YPM'          ,42 , 4],#2835
> 
>             [ 'ClrUsed'      ,46 , 4],#0
> 
>             [ 'ClrImportant' ,50 , 4]#0
> 
>             ]
> 
> _Type          =0;
> 
> _FSize         =1;
> 
> _Reserved      =2;
> 
> _OffBits       =3;
> 
> _SSize         =4;
> 
> _Width         =5;
> 
> _Height        =6;
> 
> _Planes        =7;
> 
> _BitCount      =8;
> 
> _Compress      =9;
> 
> _SizeImage     =10;
> 
> _XPM           =11;
> 
> _YPM           =12;
> 
> _ClrUsed       =13;
> 
> _ClrImportant =14;
> 
> 
> 
> def __getInt( b, idx):
> 
>     return binToInt(b,__Head_Info[idx][1],__Head_Info[idx][2])
> 
> 
> 
> def saveMatrixtoASC(bmpfilename,ascfilename):
> 
>     try:
> 
>         handle1=open( bmpfilename ,"rb")
> 
>         raw = bytearray(handle1.read( ))
> 
>         handle1.close
> 
>     except Exception as E:
> 
>         return "error:"+ str(E),""
> 
>     
> 
>     datastart=__getInt(raw, _OffBits)
> 
>     datasize =__getInt(raw, _SizeImage)
> 
>     print ('Image Offset = 0x%X'%datastart)
> 
>     print ('Image Size   = 0x%X'%datasize)
> 
>     handle2=open( ascfilename ,"w")
> 
>     for i in range(0,datasize):
> 
>        handle2.write('0x%02X,'%raw[datastart+i])
> 
>        if (i+1) % 16 == 0 :
> 
>           handle2.write("\n")
> 
>     handle2.close

The start of each line of bytes must be in the 32 bit=4byte 
boundary in the MS BMP format.

Please read the MS specs.



More information about the Python-list mailing list