summaryrefslogtreecommitdiff
path: root/libmat/images.py
blob: 0c4f3e0c7ad254b29ed9aee63b8dfd4334f54760 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
""" Takes care about pictures formats

References:
    - JFIF: http://www.ecma-international.org/publications/techreports/E-TR-098.htm
    - PNG: http://www.sno.phy.queensu.ca/~phil/exiftool/TagNames/PNG.html
    - PNG: http://www.w3.org/TR/PNG-Chunks.html
"""

import parser


class JpegStripper(parser.GenericParser):
    """ Represents a jpeg file.
        Custom Huffman and Quantization tables
        are stripped: they may leak
        some info, and the quality loss is minor.
    """
    def _should_remove(self, field):
        """ Return True if the field is compromising
        """
        field_list = frozenset([
            'start_image',  # start of the image
            'app0',         # JFIF data
            'start_frame',  # specify width, height, number of components
            'start_scan',   # specify which slice of data the top-to-bottom scan contains
            'data',         # actual data
            'end_image'])   # end of the image
        if field.name in field_list:
            return False
        elif field.name.startswith('quantization['):  # custom Quant. tables
            return False
        elif field.name.startswith('huffman['):  # custom Huffman tables
            return False
        return True


class PngStripper(parser.GenericParser):
    """ Represents a png file
    """
    def _should_remove(self, field):
        """ Return True if the field is compromising
        """
        field_list = frozenset([
            'id',
            'header',    # PNG header
            'physical',  # the intended pixel size or aspect ratio
            'end'])      # end of the image
        if field.name in field_list:
            return False
        if field.name.startswith('data['):  # data
            return False
        return True