buildframework/helium/external/python/lib/common/Sphinx-0.5.1-py2.5.egg/sphinx/util/png.py
author wbernard
Wed, 23 Dec 2009 19:29:07 +0200
changeset 179 d8ac696cc51f
permissions -rw-r--r--
helium_7.0-r14027
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
179
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
     1
# -*- coding: utf-8 -*-
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
     2
"""
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
     3
    sphinx.util.png
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
     4
    ~~~~~~~~~~~~~~~
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
     5
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
     6
    PNG image manipulation helpers.
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
     7
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
     8
    :copyright: 2008 by Georg Brandl.
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
     9
    :license: BSD.
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
    10
"""
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
    11
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
    12
import struct
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
    13
import binascii
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
    14
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
    15
LEN_IEND = 12
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
    16
LEN_DEPTH = 22
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
    17
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
    18
DEPTH_CHUNK_LEN = struct.pack('!i', 10)
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
    19
DEPTH_CHUNK_START = 'tEXtDepth\x00'
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
    20
IEND_CHUNK = '\x00\x00\x00\x00IEND\xAE\x42\x60\x82'
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
    21
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
    22
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
    23
def read_png_depth(filename):
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
    24
    """
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
    25
    Read the special tEXt chunk indicating the depth from a PNG file.
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
    26
    """
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
    27
    result = None
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
    28
    f = open(filename, 'rb')
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
    29
    try:
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
    30
        f.seek(- (LEN_IEND + LEN_DEPTH), 2)
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
    31
        depthchunk = f.read(LEN_DEPTH)
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
    32
        if not depthchunk.startswith(DEPTH_CHUNK_LEN + DEPTH_CHUNK_START):
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
    33
            # either not a PNG file or not containing the depth chunk
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
    34
            return None
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
    35
        result = struct.unpack('!i', depthchunk[14:18])[0]
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
    36
    finally:
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
    37
        f.close()
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
    38
    return result
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
    39
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
    40
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
    41
def write_png_depth(filename, depth):
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
    42
    """
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
    43
    Write the special tEXt chunk indicating the depth to a PNG file.
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
    44
    The chunk is placed immediately before the special IEND chunk.
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
    45
    """
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
    46
    data = struct.pack('!i', depth)
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
    47
    f = open(filename, 'r+b')
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
    48
    try:
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
    49
        # seek to the beginning of the IEND chunk
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
    50
        f.seek(-LEN_IEND, 2)
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
    51
        # overwrite it with the depth chunk
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
    52
        f.write(DEPTH_CHUNK_LEN + DEPTH_CHUNK_START + data)
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
    53
        # calculate the checksum over chunk name and data
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
    54
        f.write(struct.pack('!i', binascii.crc32(DEPTH_CHUNK_START + data)))
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
    55
        # replace the IEND chunk
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
    56
        f.write(IEND_CHUNK)
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
    57
    finally:
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
    58
        f.close()