From 64b0577a02f160f4b2a095a50752d00dc6c5f3d1 Mon Sep 17 00:00:00 2001 From: xdrm-brackets Date: Sun, 6 Sep 2015 19:06:44 +0200 Subject: [PATCH 1/2] =?UTF-8?q?classe=20=20cr=C3=A9=C3=A9e?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- code/bmp.py | 12 +++--------- code/classes.py | 39 ++++++++++++++++++++++++++++++++++++--- 2 files changed, 39 insertions(+), 12 deletions(-) diff --git a/code/bmp.py b/code/bmp.py index cd96659..e3b73e7 100644 --- a/code/bmp.py +++ b/code/bmp.py @@ -8,17 +8,11 @@ from classes import * import sys -fileData = "" -with open(sys.argv[1]) as f: - for byte in f.read(): - fileData += byte +img = BMPFile( sys.argv[1] ) -headerSize = 54 +print img.readableData -header = BMPHeader( fileData[:headerSize] ) -content = BMPContent( fileData[header.offset:], header ) - -for line in content.map: +for line in img.map: for pixel in line: print "rgb(%s, %s, %s)" % (pixel.r, pixel.g, pixel.b) print \ No newline at end of file diff --git a/code/classes.py b/code/classes.py index 9d04d41..3162fb8 100644 --- a/code/classes.py +++ b/code/classes.py @@ -1,5 +1,6 @@ # ~*~ encoding: utf-8 ~*~ # +import sys ################################################# # classe qui parse le header (binaire) en objet # @@ -49,15 +50,19 @@ class BMPContent: # taille avec un padding de 1 correctSizes = [ - ( 0 + header.width * header.bpp/8 ) * header.height, ( 1 + header.width * header.bpp/8 ) * header.height, ( 2 + header.width * header.bpp/8 ) * header.height ] # si le fichier a une mauvaise taille donc mauvais format + padding = 0 if not len(binContent) in correctSizes: print "Mauvais format" exit + elif len(binContent) == correctSizes[0]: + padding = 1 + else: + padding = 2 # attribution de la map self.map = [] @@ -77,7 +82,7 @@ class BMPContent: i += 3 # on passe à la suite - i += 2 # on saute le padding de saut de ligne + i += padding # on saute le padding de saut de ligne self.map = self.map[::-1] # on inverse les lignes @@ -89,4 +94,32 @@ class RGBPixel: def __init__(self, r, g, b): self.r = r self.g = g - self.b = b \ No newline at end of file + self.b = b + + + +#################################################### +# classe qui parse un fichier BMP complet en objet # +#################################################### +class BMPFile: + # CONSTRUCTEUR # + # à partir du fichier + # parse le header dans une classe + # parse le contenu dans une classe + def __init__(self, filename): + # gestion du format + if not ".bmp" in filename[-4:]: + print "must be a .bmp file" + exit + + self.fileData = "" + self.readableData = "" + with open(sys.argv[1]) as file: + for byte in file.read(): + self.fileData += byte; + self.readableData += str(ord(byte)) + " " + + headerSize = 54 + self.header = BMPHeader ( self.fileData[:headerSize] ) + self.content = BMPContent( self.fileData[self.header.offset:], self.header ) + self.map = self.content.map # lien \ No newline at end of file From 8a067dda5202f77af50a8620f967438ad1fa45f1 Mon Sep 17 00:00:00 2001 From: xdrm-brackets Date: Sun, 6 Sep 2015 19:06:54 +0200 Subject: [PATCH 2/2] =?UTF-8?q?classe=20=20cr=C3=A9=C3=A9e?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- code/classes.pyc | Bin 0 -> 3632 bytes 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 code/classes.pyc diff --git a/code/classes.pyc b/code/classes.pyc new file mode 100644 index 0000000000000000000000000000000000000000..8257713be7a7f7f18d5184bf51ff8b5b141ba04e GIT binary patch literal 3632 zcmcInU2oh}5FOk5k~S>$ECFEi3Q(CULTBZ+yKW zLCI6#8S&05|AZgH4**_xMS^q28)u7D323`n&yHv2-nn<~%()ZqkE!wBcYb};k>SU~ z@27a|6OagBN4g?wU|fmDCGzB3kt2~W*Bpr|5=}@{Ek!V?Ni-=@y%c#8jY%{mQKJ-L zlq0LnX*h`g!e=FKfL*?K|BeYGla)(xD8*x62QgR#JPK!7gC9p3PlDB^3w4L2I(%;8 z*M0&5t3y}zu1hX*;K-39x1PQwPh2_lX7MBN^2?vX}?>@ZqXnC>S>$m#*`DU6dcGAc!cDkXp#$JO=-KKYw zd;$~LF;3{zoOx%#(TFfAT$Dy+8{QFx1CbUOkuC7pxbFQDLO=zgP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