basic st3 support. Can't get jpeg dimensions to work.

This commit is contained in:
Liam Cain
2013-05-05 13:19:59 -04:00
parent fc5c97af0c
commit 95e479dd66
2 changed files with 40 additions and 37 deletions

View File

@@ -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 '<img' in view.substr(tag_scope) and path.endswith(('.png','.jpg','.jpeg','.gif')):
with open(full_path,'rb') as r:
read_data = r.read() if path.endswith(('.jpg','.jpeg')) else r.read(24)
con_type, w, h = getImageInfo(read_data)
read_data = r.read() # if path.endswith(('.jpg','.jpeg')) else r.read(24)
con_type, w, h = getImageInfo(read_data.contentBinary())
if self.get_setting('afn_insert_width_first',view):
self.insert_dimension(edit,h,'height',tag_scope)
self.insert_dimension(edit,w,'width',tag_scope)
@@ -98,7 +98,7 @@ class FileNameComplete(sublime_plugin.EventListener):
if fn.endswith(('.png','.jpg','.jpeg','.gif')):
path = os.path.join(sdir, fn)
with open(path,'rb') as r:
read_data = r.read() if path.endswith(('.jpg','.jpeg')) else r.read(24)
read_data = r.read() # if path.endswith(('.jpg','.jpeg')) else r.read(24)
con_type, w, h = getImageInfo(read_data)
return fn+'\t'+'w:'+ str(w) +" h:" + str(h)
return fn
@@ -145,13 +145,12 @@ class FileNameComplete(sublime_plugin.EventListener):
dir_files = os.listdir(this_dir)
for d in dir_files:
n = d.decode('utf-8')
if n.startswith('.'): continue
if not '.' in n: n += '/'
completions.append((self.fix_dir(this_dir,n), n))
if d.startswith('.'): continue
if not '.' in d: d += '/'
completions.append((self.fix_dir(this_dir,d), d))
if completions:
InsertDimensionsCommand.this_dir = this_dir
return completions
except OSError:
print "AutoFileName: could not find " + this_dir
print("AutoFileName: could not find " + this_dir)
return

View File

@@ -1,15 +1,17 @@
import StringIO
# from io import BytesIO
import io
import struct
def getImageInfo(data):
data = str(data)
olddata = str(data)
data = bytes(data)
size = len(data)
height = -1
width = -1
content_type = ''
# handle GIFs
if (size >= 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("<HH", data[6:10])
@@ -19,15 +21,16 @@ def getImageInfo(data):
# See PNG 2. Edition spec (http://www.w3.org/TR/PNG/)
# Bytes 0-7 are below, 4-byte chunk length, then 'IHDR'
# and finally the 4-byte width, height
elif ((size >= 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