Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unable to view image (binary) files #16

Open
keith-hall opened this issue Nov 21, 2016 · 4 comments
Open

Unable to view image (binary) files #16

keith-hall opened this issue Nov 21, 2016 · 4 comments

Comments

@keith-hall
Copy link
Contributor

Trying to use PackageResourceViewer to open a image (binary) file currently fails.

failed to load image

Tested in ST 3125 on Windows 7 x64.

Steps to reproduce:

  1. Open Command Palette
  2. Type PRV: O and select Package Resource Viewer: Open Resource
  3. Select Theme - Default
  4. Select a .png file, for example arrow_down.png
  5. See an error instead of the image: Failed to load image
@keith-hall
Copy link
Contributor Author

The error in the console is:

Traceback (most recent call last):
  File "package_resource_viewer in C:\Users\Keith\AppData\Roaming\Sublime Text 3\Installed Packages\PackageResourceViewer.sublime-package", line 108, in package_file_callback
  File "package_resource_viewer in C:\Users\Keith\AppData\Roaming\Sublime Text 3\Installed Packages\PackageResourceViewer.sublime-package", line 137, in open_file
  File "package_resources in C:\Users\Keith\AppData\Roaming\Sublime Text 3\Installed Packages\PackageResourceViewer.sublime-package", line 29, in get_resource
  File "package_resources in C:\Users\Keith\AppData\Roaming\Sublime Text 3\Installed Packages\PackageResourceViewer.sublime-package", line 42, in _get_resource
  File "C:\Program Files\Sublime Text 3\sublime.py", line 190, in load_resource
    s = sublime_api.load_resource(name)
UnicodeDecodeError: 'utf-8' codec can't decode byte 0x89 in position 0: invalid start byte
decode image failed

I believe this is because load_binary_resource must be used instead of load_resource. Maybe it will be necessary to check the value of the binary_file_patterns preference in the Preferences.sublime-settings file, and check each pattern against the resource file name with fnmatch to decide which API call to use.

@skuroda
Copy link
Owner

skuroda commented Nov 22, 2016

Thanks for reporting the issue! I'll try to take a look. I've been pretty busy and haven't been able to keep the plugins updated. As always PR are welcome :)

@keith-hall
Copy link
Contributor Author

I've had a go at this, but it looks like it is not possible to get ST to display an image that does not exist on the filesystem. Specifically, the view object only allows plugins to insert text (i.e. str type), not a bytearray.

I therefore think we will have to use a workaround. I have some ideas:

  1. extract the image and delete it again when the user closes the tab. (I was originally thinking only to delete it if it wasn't manually saved by the user, so that it would stay as an override file in that circumstance, but the save command is actually disabled for image preview in ST3.)
  2. take the idea of https://github.com/bordaigorl/sublime-non-text-files and open the image in the default application, i.e. external to ST. Again it would require extracting it, but now we can't tell when it was closed to know when to delete it, so IMO it's not a good solution.
  3. use ST3's phantom functionality to display the image - ST supports using a datauri in the HTML image src (i.e. base64 encoded), as seen here: https://github.com/SublimeText/LaTeXTools/blob/6c540b005df6f02336d1e347c160dc24c390b474/st_preview/preview_math.py#L312

What do you think? I heard that ST2 doesn't support image preview, so probably we're looking at an ST3 specific implementation anyway.

@skuroda
Copy link
Owner

skuroda commented Nov 24, 2016

Thanks for taking a look at this, I'll take a closer look at the links you've provided. Yes, as far as I know, I'll have to create a branch for ST2 and ST3. No big deal there though, just something I need to do (for multiple of my plugins). It's great to see more functionality being added to ST3 though. Definitely felt like it stalled there for a while.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants