From 70efbfe5b5afd8b7487a1d092fe1b6a7f2b65212 Mon Sep 17 00:00:00 2001 From: xdrm-brackets Date: Wed, 9 Sep 2015 18:18:21 +0200 Subject: [PATCH] =?UTF-8?q?1er=20commit=20de=20la=20prise=20en=20charge=20?= =?UTF-8?q?des=20diff=C3=A9rents=20bpp=20(classe=20RGBPixel=20impl=C3=A9me?= =?UTF-8?q?nt=C3=A9e)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- code/BMPFile.py | 64 +++++++++++++++++++++++++++++++++++++++++++-- code/BMPFile.readme | 48 ++++++++++++++++++++++++++++++++++ code/bmp.py | 6 ++--- 3 files changed, 113 insertions(+), 5 deletions(-) diff --git a/code/BMPFile.py b/code/BMPFile.py index 04d6461..13df904 100644 --- a/code/BMPFile.py +++ b/code/BMPFile.py @@ -245,7 +245,7 @@ class BMPContent: self.binData = "" for line in self.map[::-1]: for pixel in line: - self.binData += chr(pixel.b) + chr(pixel.g) + chr(pixel.r) + self.binData += chr(pixel.binData) for zero in range(0, headerHandler.padding): self.binData += chr(0) @@ -258,10 +258,70 @@ class BMPContent: # classe contenant un pixel RGB # ################################# class RGBPixel: - def __init__(self, r, g, b): + def __init__(self, r, g, b, bpp=24): + if bpp not in [1,4,8,24]: + self.bpp = 24 + else: + self.bpp = bpp + + self.r = r self.g = g self.b = b + + + # gestion des différents bpp + if bpp == 1: + self.intData = [ int( (r+g+b)/3 > 256/2 ) ] + self.binData = chr( self.intData ) + elif bpp == 4: + self.intData = [ int( 16 * ((r+g+b)/3) / 256 ) ] + self.binData = chr( self.intData ) + elif bpp == 8: + self.intData = [ int( (r+g+b) / 3 ) ] + self.binData = chr( self.intData ) + else: + self.intData = [ r, g, b ] + self.binData = chr(b) + chr(g) + chr(r) + + + + def setRGB(self, r, g, b, bpp=24): + self.__init__(r, g, b, bpp); + + def setBin(self, binData, bpp=24): + if bpp not in [1,4,8,24]: + self.bpp = 24 + else: + self.bpp = bpp + + # gestion des différents bpp + if bpp == 1: + self.intData = [ 255 * int(binData) ] + self.r = self.intData[0] + self.g = self.intData[0] + self.b = self.intData[0] + elif bpp == 4: + self.intData = [ 256 * ord(binData) / 16 ] + self.r = self.intData[0] + self.g = self.intData[0] + self.b = self.intData[0] + elif bpp == 8: + self.intData = [ ord(binData) ] + self.r = self.intData[0] + self.g = self.intData[0] + self.b = self.intData[0] + else: + self.intData = [ ord(binData[2]), ord(binData[1]), ord(binData[0]) ] + self.r = self.intData[0] + self.g = self.intData[1] + self.b = self.intData[2] + + self.r = r + self.g = g + self.b = b + + diff --git a/code/BMPFile.readme b/code/BMPFile.readme index 1692ffe..21b7106 100644 --- a/code/BMPFile.readme +++ b/code/BMPFile.readme @@ -146,3 +146,51 @@ Classes: [x] gestion de la palette (attribut au ) définit par défaut au [x] faire que le unparse du content créée le header [ ] prise en charge des formats 1, 4, et 8 bpp + + + +####################################################################### +####### RECHERCHE DE PRISE EN CHARGE DES DIFFERENTS FORMATS BPP ####### +####################################################################### +PixelColor(r, g, b, bpp=24) + , , respectivement les composantes bleue, rouge, verte + correspond aux bpp + +CAS BPP=1 (noir et blanc) + (r+g+b)/3 >= 256 / 2 + + 1 - blanc + 2 - noir + +CAS BPP=4 (niveaux de gris) + 16 * ((r+g+b)/3) / 256 + +CAS BPP=8 + (r+g+b) / 3 + +class PixelColor(r, g, b, bpp=24): + if bpp not in [1,4,8,24]: + self.bpp = 24 + else: + self.bpp = bpp + + + self.r = r + self.g = g + self.b = b + + + # gestion des différents bpp + if bpp == 1: + self.intColor = int( (r+g+b)/3 > 256/2 ) + self.binColor = chr( self.intColor ) + elif bpp == 4: + self.intColor = int( 16 * ((r+g+b)/3) / 256 ) ) + self.binColor = chr( self.intColor ) + elif bpp == 8: + self.intColor = int( (r+g+b) / 3 ) + self.binColor = chr( self.intColor ) + else: + self.intColor = [r, g, b] + self.binColor = chr(b) + chr(g) + chr(r) + diff --git a/code/bmp.py b/code/bmp.py index d18d1ee..de47454 100644 --- a/code/bmp.py +++ b/code/bmp.py @@ -48,7 +48,7 @@ def testManualCreation(): for y in range(0, 100): img.content.map.append( [] ) for x in range(0, 1000): - img.content.map[y].append( PixelColor( + img.content.map[y].append( RGBPixel( random.randint(0, 255), random.randint(0, 255), random.randint(0, 255) @@ -59,5 +59,5 @@ def testManualCreation(): print img.binData # MAIN -#testManualCreation() -testFileIntegrity() \ No newline at end of file +testManualCreation() +#testFileIntegrity() \ No newline at end of file