Not logged in

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:Add function to determine GIF image dimensions
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1:00fa7b52d28b336c3b70ac6f41299f1ec0a54364
User & Date: brandon 2018-11-08 22:48:40
Context
2018-11-08
23:09
Make the various image functions a bit more robust via exceptions rather than returning None. check-in: fcec9a25cf user: brandon tags: trunk
22:48
Add function to determine GIF image dimensions check-in: 00fa7b52d2 user: brandon tags: trunk
2018-10-22
21:20
Add non-canonical function to test whether an MZ executable is a Windows PE executable check-in: f5b4bc4ee4 user: brandon tags: trunk
Changes

Changes to treatyofbabel/utils/_imgfuncs.py.

1
2
3
4
5
6
7
8
9
10
11
12
..
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
..
99
100
101
102
103
104
105










































106
107
108
109
110
111
112
113
114
# -*- coding: utf-8 -*-
#
#       _imgfuncs.py
#
#       Copyright © 2012, 2013, 2014 Brandon Invergo <brandon@invergo.net>
#
#       This file is part of pyifbabel.
#
#       pyifbabel 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.
................................................................................
#       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 pyifbabel.  If not, see <http://www.gnu.org/licenses/>.


from _binaryfuncs import read_char, read_long


class CoverImage(object):
    def __init__(self, data=None, img_format=None, width=None,
                 height=None, description=None):
        self.data = data
        self.img_format = img_format
................................................................................
        not (img[12] == 'I' and img[13] == 'H' and
             img[14] == 'D' and img[15] == 'R')):
        return (None, None)
    w = read_long(img, 16)
    h = read_long(img, 20)
    return (w, h)












































def deduce_img_format(img):
    (w, h) = get_jpeg_dim(img)
    if w is not None and h is not None:
        return "jpeg"
    (w, h) = get_png_dim(img)
    if w is not None and h is not None:
        return "png"
    return None




|







 







|







 







>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>









1
2
3
4
5
6
7
8
9
10
11
12
..
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
..
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
# -*- coding: utf-8 -*-
#
#       _imgfuncs.py
#
#       Copyright © 2012, 2013, 2014, 2018 Brandon Invergo <brandon@invergo.net>
#
#       This file is part of pyifbabel.
#
#       pyifbabel 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.
................................................................................
#       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 pyifbabel.  If not, see <http://www.gnu.org/licenses/>.


from _binaryfuncs import read_char, read_long, read_short


class CoverImage(object):
    def __init__(self, data=None, img_format=None, width=None,
                 height=None, description=None):
        self.data = data
        self.img_format = img_format
................................................................................
        not (img[12] == 'I' and img[13] == 'H' and
             img[14] == 'D' and img[15] == 'R')):
        return (None, None)
    w = read_long(img, 16)
    h = read_long(img, 20)
    return (w, h)


def get_gif_dim(img):
    # Look for the "GIF" magic number
    if ((read_char(img, 0, '<') != 0x47 or
         read_char(img, 1, '<') != 0x49 or
         read_char(img, 2, '<') != 0x46)):
        print("no GIF header")
        return (None, None)
    header_len = 6
    logscreen_len = 7
    packed_field = read_char(img, header_len+4, '<')
    print(packed_field)
    # The first bit of the packed field determines if there is a
    # Global Color Table
    global_color_tbl = packed_field & 0b10000000 == 0b10000000
    if global_color_tbl:
        # The last three bits determine the size of the GCT.
        # According to
        # http://giflib.sourceforge.net/whatsinagif/bits_and_bytes.html
        # This should be determined by the color resolution in bits
        # 2-4, however that doesn't seem to work.  This does
        gct_size = packed_field & 0b00000111
        # The Global Color Table uses 2^(N+1) bytes per color (RGB),
        # so 3*2^(N+1) bytes long
        gct_len = 3 * (2 ** (gct_size + 1))
    else:
        gct_len = 0
    # Check for a Graphics Control Extension block
    gce_offset = header_len + logscreen_len + gct_len
    gce_block = read_char(img, gce_offset, '<') == 0x21
    if gce_block:
        gce_len = 8
    else:
        gce_len = 0
    # Image Descriptior block
    img_descr_offset = gce_offset + gce_len
    if read_char(img, img_descr_offset, '<') != 0x2C:
        return (None, None)
    w = read_short(img, img_descr_offset+5, '<')
    h = read_short(img, img_descr_offset+7, '<')
    return (w, h)


def deduce_img_format(img):
    (w, h) = get_jpeg_dim(img)
    if w is not None and h is not None:
        return "jpeg"
    (w, h) = get_png_dim(img)
    if w is not None and h is not None:
        return "png"
    return None