parse/unparse fonctionnells mais tjs pb avec header_complémentaire...

This commit is contained in:
xdrm-brackets 2015-09-08 18:17:57 +02:00
parent 4b15aad155
commit 81e24e5601
2 changed files with 33 additions and 45 deletions

View File

@ -41,7 +41,7 @@ class BMPHeader:
else: else:
parsingData = self.binData parsingData = self.binData
self.binData = parsingData.join("") # header brut (format initial: bin) self.binData = parsingData # header brut (format initial: bin)
self.signature = self.toInt( parsingData[ 0: 2] ) # signature (4D42) self.signature = self.toInt( parsingData[ 0: 2] ) # signature (4D42)
self.fileSize = self.toInt( parsingData[ 2: 6] ) # taille du fichier bmp (bytes) self.fileSize = self.toInt( parsingData[ 2: 6] ) # taille du fichier bmp (bytes)
@ -65,10 +65,8 @@ class BMPHeader:
self.intData = [] self.intData = []
self.hexData = []
for byte in parsingData: for byte in parsingData:
self.intData.append( ord(byte) ) self.intData.append( ord(byte) )
self.hexData.append( hex( ord(byte) ) )
@ -76,10 +74,11 @@ class BMPHeader:
# fonction qui créer <self.bin> à partir des attributs # fonction qui créer <self.binData> à partir des attributs
def unparse(self): def unparse(self):
self.binData = "" # on définit le tableau d'entier à partir des attributs
self.intData = []
self.fromInt( self.signature, 2) # signature self.fromInt( self.signature, 2) # signature
self.fromInt( self.fileSize, 4) # taille fichier BMP self.fromInt( self.fileSize, 4) # taille fichier BMP
@ -97,12 +96,10 @@ class BMPHeader:
self.fromInt( self.colorNb, 4) # nombre de couleurs de l'image (ou 0) self.fromInt( self.colorNb, 4) # nombre de couleurs de l'image (ou 0)
self.fromInt( self.colorINb, 4) # nombre de couleurs importantes de l'image (ou 0) self.fromInt( self.colorINb, 4) # nombre de couleurs importantes de l'image (ou 0)
# human-readable data
self.intData = [] self.binData = ""
self.hexData = [] for byte in self.intData:
for byte in self.binData: self.binData += chr( byte )
self.intData.append( str( ord( byte ) ) )
self.hexData.append( hex( ord( byte ) ) )
# Affiche au format humain, toutes les infos du header # Affiche au format humain, toutes les infos du header
@ -146,25 +143,19 @@ class BMPHeader:
intReturn = 0; intReturn = 0;
for i, byte in enumerate(bytes): for i, byte in enumerate(bytes):
intReturn += ord(byte) * (256 ** i) intReturn += ord(byte) * (256 ** i)
return intReturn return intReturn
# écrit le valeur entière <value> en octet bourrés jusqu'à la taille <size> # écrit le valeur entière <value> en octet bourrés jusqu'à la taille <size> dans le tableau <intData>
def fromInt(self, value, size): def fromInt(self, value, size):
s = '0' + str( bin(value)[2:] ) s = '0' + str( bin(value)[2:] )
temp = "" for byte in range(0, size):
if s=="":
self.intData.append( 0 )
else:
self.intData.append( int(s[-8:],2) )
s = s[:-8]
while s != "":
print chr( int(s[:8], 2) )
temp += chr( int(s[:8], 2) )
s = s[8:]
print temp
if size > len(temp):
temp = chr(int('0' * (size-len(temp)), 2) ) + temp
self.binData += temp
@ -217,10 +208,12 @@ class BMPContent:
self.map = self.map[::-1] # on inverse les lignes self.map = self.map[::-1] # on inverse les lignes
self.bin = binContent self.binData = binContent
for byte in binContent: # human-readable data
self.readableData += str(ord(byte)) + " " self.intData = []
for byte in self.binData:
self.intData.append( ord( byte ) )
# unparse une map de pixels en binaire # unparse une map de pixels en binaire
@ -255,36 +248,31 @@ class RGBPixel:
#################################################### ####################################################
class BMPFile: class BMPFile:
# parse à partir du nom du fichier en > objets <BMPHeader> et <BMPContent> # parse à partir de <binFile> en objets <BMPHeader> et <BMPContent>
def parse(self, filename): def parse(self, binFile=""):
# si on a défini le fichier # si on a défini le fichier
if filename == "": if binFile == "":
print "Missing argument 1: filename.bmp" print "<BMPFile.parse()> need an argument"
exit() exit()
# gestion du format # human-readable data
if not ".bmp" in filename[-4:]: self.binData = binFile
print "must be a .bmp file" self.intData = []
exit() self.hexData = []
self.bin = "" for byte in binFile:
self.readableData = "" self.intData.append( ord(byte) )
self.hexData.append( hex( ord(byte) ) )
# lecture du fichier
with open( filename ) as file:
for byte in file.read():
self.bin += byte;
self.readableData += str(ord(byte)) + " "
headerSize = 54 headerSize = 54
# parsing header # parsing header
self.header = BMPHeader() self.header = BMPHeader()
self.header.parse( self.bin[:headerSize] ) self.header.parse( self.binData[:headerSize] )
# parsing content # parsing content
self.content = BMPContent() self.content = BMPContent()
self.content.parse( self.bin[self.header.offset:], self.header ) self.content.parse( self.binData[self.header.offset:], self.header )
# unparse à partir d'un <BMPHeader> et d'un <BMPContent> # unparse à partir d'un <BMPHeader> et d'un <BMPContent>
def unparse(self): def unparse(self):

Binary file not shown.