gif.py 3.08 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
# -*- coding: utf-8 -*-
#
# diffoscope: in-depth comparison of files, archives, and directories
#
# Copyright © 2017 Chris Lamb <lamby@debian.org>
#
# diffoscope is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# diffoscope is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with diffoscope.  If not, see <https://www.gnu.org/licenses/>.

import re
21
import logging
22
import subprocess
23 24

from diffoscope.tools import tool_required
25
from diffoscope.config import Config
26
from diffoscope.difference import Difference
27

28
from .image import pixel_difference, flicker_difference, same_size
29 30
from .utils.file import File
from .utils.command import Command
31 32

logger = logging.getLogger(__name__)
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49


class Gifbuild(Command):
    RE_FILTERS = (
        re.compile(r'^# GIF information from'),
        re.compile(r'^# End of .* dump'),
    )

    @tool_required('gifbuild')
    def cmdline(self):
        return ['gifbuild', '-d', self.path]

    def filter(self, line):
        if any(x.match(line.decode('utf-8')) for x in self.RE_FILTERS):
            return b""
        return line

50

51
@tool_required('identify')
52 53 54 55 56
def is_image_static(image):
    try:
        return subprocess.check_output((
            'identify',
            '-format', '%n',
57 58
            image.path,
        )) == b'1'
59 60 61
    except subprocess.CalledProcessError:
        return False

62

63
def can_compose_gif_images(image1, image2):
64 65 66 67
    return same_size(image1, image2) and \
        is_image_static(image1) and \
        is_image_static(image2)

68 69

class GifFile(File):
70
    DESCRIPTION = "GIF image files"
71
    FILE_TYPE_RE = re.compile(r'^GIF image data\b')
72 73

    def compare_details(self, other, source=None):
74
        gifbuild_diff = Difference.from_command(
75 76 77
            Gifbuild,
            self.path,
            other.path,
78
            source="gifbuild",
79
        )
80

81
        differences = [gifbuild_diff]
82

83
        if gifbuild_diff is not None and Config().compute_visual_diffs and \
84
                can_compose_gif_images(self, other):
85
            try:
86
                logger.debug(
87
                    "Generating visual difference for %s and %s",
88 89 90 91 92 93 94
                    self.path,
                    other.path,
                )
                content_diff = Difference(
                    None,
                    self.path,
                    other.path,
95
                    source="Image content",
96 97 98 99 100 101
                )
                content_diff.add_visuals([
                    pixel_difference(self.path, other.path),
                    flicker_difference(self.path, other.path),
                ])
                differences.append(content_diff)
102
            except subprocess.CalledProcessError:  # noqa
103
                pass
104

105
        return differences