From 95e479dd665615926c0618b2d44e4990830fdaa4 Mon Sep 17 00:00:00 2001 From: Liam Cain Date: Sun, 5 May 2013 13:19:59 -0400 Subject: [PATCH] basic st3 support. Can't get jpeg dimensions to work. --- autofilename.py | 17 +++++++------- getimageinfo.py | 60 ++++++++++++++++++++++++++----------------------- 2 files changed, 40 insertions(+), 37 deletions(-) diff --git a/autofilename.py b/autofilename.py index 004b929..363f687 100644 --- a/autofilename.py +++ b/autofilename.py @@ -1,7 +1,7 @@ import sublime import sublime_plugin import os -from getimageinfo import getImageInfo +from .getimageinfo import getImageInfo class InsertDimensionsCommand(sublime_plugin.TextCommand): this_dir = '' @@ -39,8 +39,8 @@ class InsertDimensionsCommand(sublime_plugin.TextCommand): if '= 10) and data[:6] in ('GIF87a', 'GIF89a'): + if (size >= 10) and data[:6] == b'GIF89a': # Check to see if content_type is correct content_type = 'image/gif' w, h = struct.unpack("= 24) and data.startswith('\211PNG\r\n\032\n') - and (data[12:16] == 'IHDR')): + elif ((size >= 24) and data[:8] == b'\211PNG\r\n\032\n' + and (data[12:16] == b'IHDR')): content_type = 'image/png' w, h = struct.unpack(">LL", data[16:24]) width = int(w) height = int(h) # Maybe this is for an older PNG version. - elif (size >= 16) and data.startswith('\211PNG\r\n\032\n'): + elif (size >= 16) and data[:8] == b'\211PNG\r\n\032\n': + print("we have a png2") # Check to see if we have the right content type content_type = 'image/png' w, h = struct.unpack(">LL", data[8:16]) @@ -35,27 +38,28 @@ def getImageInfo(data): height = int(h) # handle JPEGs - elif (size >= 2) and data.startswith('\377\330'): - content_type = 'image/jpeg' - jpeg = StringIO.StringIO(data) - jpeg.read(2) - b = jpeg.read(1) - try: - while (b and ord(b) != 0xDA): - while (ord(b) != 0xFF): b = jpeg.read(1) - while (ord(b) == 0xFF): b = jpeg.read(1) - if (ord(b) >= 0xC0 and ord(b) <= 0xC3): - jpeg.read(3) - h, w = struct.unpack(">HH", jpeg.read(4)) - break - else: - jpeg.read(int(struct.unpack(">H", jpeg.read(2))[0])-2) - b = jpeg.read(1) - width = int(w) - height = int(h) - except struct.error: - pass - except ValueError: - pass + # Can't get this method to work. + # elif (size >= 2) and data[:2] == b'\377\330': + # content_type = 'image/jpeg' + # jpeg = io.StringIO(olddata) + # jpeg.read(2) + # b = jpeg.read(1) + # try: + # while (b and ord(b) != 0xDA): + # while (ord(b) != 0xFF): b = jpeg.read(1) + # while (ord(b) == 0xFF): b = jpeg.read(1) + # if (ord(b) >= 0xC0 and ord(b) <= 0xC3): + # jpeg.read(3) + # h, w = struct.unpack(">HH", jpeg.read(4)) + # break + # else: + # jpeg.read(int(struct.unpack(">H", jpeg.read(2))[0])-2) + # b = jpeg.read(1) + # width = int(w) + # height = int(h) + # except struct.error: + # pass + # except ValueError: + # pass return content_type, width, height \ No newline at end of file