bmp.py :  » Media-Sound-Audio » Python-Audio-Tools » audiotools-2.14 » audiotools » construct » formats » graphics » Python Open Source

Home
Python Open Source
1.3.1.2 Python
2.Ajax
3.Aspect Oriented
4.Blog
5.Build
6.Business Application
7.Chart Report
8.Content Management Systems
9.Cryptographic
10.Database
11.Development
12.Editor
13.Email
14.ERP
15.Game 2D 3D
16.GIS
17.GUI
18.IDE
19.Installer
20.IRC
21.Issue Tracker
22.Language Interface
23.Log
24.Math
25.Media Sound Audio
26.Mobile
27.Network
28.Parser
29.PDF
30.Project Management
31.RSS
32.Search
33.Security
34.Template Engines
35.Test
36.UML
37.USB Serial
38.Web Frameworks
39.Web Server
40.Web Services
41.Web Unit
42.Wiki
43.Windows
44.XML
Python Open Source » Media Sound Audio » Python Audio Tools 
Python Audio Tools » audiotools 2.14 » audiotools » construct » formats » graphics » bmp.py
"""
Windows/OS2 Bitmap (BMP)
this could have been a perfect show-case file format, but they had to make
it ugly (all sorts of alignment or 
"""
from construct import *


#===============================================================================
# pixels: uncompressed
#===============================================================================
def UncompressedRows(subcon, align_to_byte = False):
    """argh! lines must be aligned to a 4-byte boundary, and bit-pixel
    lines must be aligned to full bytes..."""
    if align_to_byte:
        line_pixels = Bitwise(
            Aligned(Array(lambda ctx: ctx.width, subcon), modulus = 8)
        )
    else:
        line_pixels = Array(lambda ctx: ctx.width, subcon)
    return Array(lambda ctx: ctx.height, 
        Aligned(line_pixels, modulus = 4)
    )

uncompressed_pixels = Switch("uncompressed", lambda ctx: ctx.bpp,
    {
        1 : UncompressedRows(Bit("index"), align_to_byte = True),
        4 : UncompressedRows(Nibble("index"), align_to_byte = True),
        8 : UncompressedRows(Byte("index")),
        24 : UncompressedRows(
            Sequence("rgb", Byte("red"), Byte("green"), Byte("blue"))
        ),
    }
)

#===============================================================================
# pixels: Run Length Encoding (RLE) 8 bit
#===============================================================================
class RunLengthAdapter(Adapter):
    def _encode(self, obj):
        return len(obj), obj[0]
    def _decode(self, (length, value)):
        return [value] * length

rle8pixel = RunLengthAdapter(
    Sequence("rle8pixel", 
        Byte("length"), 
        Byte("value")
    )
)

#===============================================================================
# file structure
#===============================================================================
def iff(cond, thenval, elseval):
    if cond:
        return thenval
    else:
        return elseval

bitmap_file = Struct("bitmap_file",
    # header
    Const(String("signature", 2), "BM"),
    ULInt32("file_size"),
    Padding(4),
    ULInt32("data_offset"),
    ULInt32("header_size"),
    Enum(Alias("version", "header_size"),
        v2 = 12,
        v3 = 40,
        v4 = 108,
    ),
    ULInt32("width"),
    ULInt32("height"),
    Value("number_of_pixels", lambda ctx: ctx.width * ctx.height),
    ULInt16("planes"),
    ULInt16("bpp"), # bits per pixel
    Enum(ULInt32("compression"),
        Uncompressed = 0,
        RLE8 = 1,
        RLE4 = 2,
        Bitfields = 3,
        JPEG = 4,
        PNG = 5,
    ),
    ULInt32("image_data_size"), # in bytes
    ULInt32("horizontal_dpi"),
    ULInt32("vertical_dpi"),
    ULInt32("colors_used"),
    ULInt32("important_colors"),
    
    # palette (24 bit has no palette)
    OnDemand(
        Array(lambda ctx: iff(ctx.bpp <= 8, 2 ** ctx.bpp, 0), 
            Struct("palette",
                Byte("blue"),
                Byte("green"),
                Byte("red"),
                Padding(1),
            )
        )
    ),
    
    # pixels
    OnDemandPointer(lambda ctx: ctx.data_offset, 
        Switch("pixels", lambda ctx: ctx.compression,
            {
                "Uncompressed" : uncompressed_pixels,
            }
        ),
    ),
)


if __name__ == "__main__":
    obj = bitmap_file.parse_stream(open("../../test/bitmap8.bmp", "rb"))
    print obj 
    print repr(obj.pixels.value)























www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.