1er commit de la prise en charge des différents bpp (classe RGBPixel implémentée)
This commit is contained in:
parent
4b34b3625e
commit
70efbfe5b5
|
@ -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
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
|
|
@ -146,3 +146,51 @@ Classes:
|
|||
[x] gestion de la palette (attribut au <parse>) définit par défaut au <unparse>
|
||||
[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)
|
||||
<r>, <g>, <b> respectivement les composantes bleue, rouge, verte
|
||||
<bpp> 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)
|
||||
|
||||
|
|
|
@ -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()
|
||||
testManualCreation()
|
||||
#testFileIntegrity()
|
Loading…
Reference in New Issue