mirror of
https://github.com/GNS3/gns3-server
synced 2024-12-27 09:18:09 +00:00
91 lines
4.0 KiB
Python
91 lines
4.0 KiB
Python
|
#!/usr/bin/env python
|
||
|
#
|
||
|
# Copyright (C) 2016 GNS3 Technologies Inc.
|
||
|
#
|
||
|
# This program 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.
|
||
|
#
|
||
|
# This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
|
||
|
|
||
|
import struct
|
||
|
|
||
|
|
||
|
def get_size(data):
|
||
|
"""
|
||
|
Get image size
|
||
|
:param data: A buffer with image content
|
||
|
:return: Tuple (width, height)
|
||
|
"""
|
||
|
|
||
|
# Original version:
|
||
|
# https://github.com/shibukawa/imagesize_py
|
||
|
#
|
||
|
# The MIT License (MIT)
|
||
|
#
|
||
|
# Copyright © 2016 Yoshiki Shibukawa
|
||
|
#
|
||
|
# Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
||
|
#
|
||
|
# The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
||
|
#
|
||
|
# THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||
|
|
||
|
height = 0
|
||
|
width = 0
|
||
|
|
||
|
size = len(data)
|
||
|
# handle GIFs
|
||
|
if size >= 10 and data[:6] in (b'GIF87a', b'GIF89a'):
|
||
|
# Check to see if content_type is correct
|
||
|
try:
|
||
|
width, height = struct.unpack("<hh", data[6:10])
|
||
|
except struct.error:
|
||
|
raise ValueError("Invalid GIF file")
|
||
|
# see png edition spec bytes are below chunk length then and finally the
|
||
|
elif size >= 24 and data.startswith(b'\211PNG\r\n\032\n') and data[12:16] == b'IHDR':
|
||
|
try:
|
||
|
width, height = struct.unpack(">LL", data[16:24])
|
||
|
except struct.error:
|
||
|
raise ValueError("Invalid PNG file")
|
||
|
# Maybe this is for an older PNG version.
|
||
|
elif size >= 16 and data.startswith(b'\211PNG\r\n\032\n'):
|
||
|
# Check to see if we have the right content type
|
||
|
try:
|
||
|
width, height = struct.unpack(">LL", data[8:16])
|
||
|
except struct.error:
|
||
|
raise ValueError("Invalid PNG file")
|
||
|
# handle JPEGs
|
||
|
elif size >= 2 and data.startswith(b'\377\330'):
|
||
|
try:
|
||
|
fhandle.seek(0) # Read 0xff next
|
||
|
size = 2
|
||
|
ftype = 0
|
||
|
while not 0xc0 <= ftype <= 0xcf:
|
||
|
fhandle.seek(size, 1)
|
||
|
byte = fhandle.read(1)
|
||
|
while ord(byte) == 0xff:
|
||
|
byte = fhandle.read(1)
|
||
|
ftype = ord(byte)
|
||
|
size = struct.unpack('>H', fhandle.read(2))[0] - 2
|
||
|
# We are at a SOFn block
|
||
|
fhandle.seek(1, 1) # Skip `precision' byte.
|
||
|
height, width = struct.unpack('>HH', fhandle.read(4))
|
||
|
except struct.error:
|
||
|
raise ValueError("Invalid JPEG file")
|
||
|
# handle JPEG2000s
|
||
|
elif size >= 12 and data.startswith(b'\x00\x00\x00\x0cjP \r\n\x87\n'):
|
||
|
fhandle.seek(48)
|
||
|
try:
|
||
|
height, width = struct.unpack('>LL', fhandle.read(8))
|
||
|
except struct.error:
|
||
|
raise ValueError("Invalid JPEG2000 file")
|
||
|
return width, height
|