├── .gitignore ├── icon.png ├── icon128.png ├── icon16.png ├── icon48.png ├── manifest.json ├── options ├── options.html └── options.js ├── LICENSE ├── README.md └── background.js /.gitignore: -------------------------------------------------------------------------------- 1 | icon.sketch 2 | public 3 | test.* -------------------------------------------------------------------------------- /icon.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/my-codeworks/tiff-viewer-extension/HEAD/icon.png -------------------------------------------------------------------------------- /icon128.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/my-codeworks/tiff-viewer-extension/HEAD/icon128.png -------------------------------------------------------------------------------- /icon16.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/my-codeworks/tiff-viewer-extension/HEAD/icon16.png -------------------------------------------------------------------------------- /icon48.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/my-codeworks/tiff-viewer-extension/HEAD/icon48.png -------------------------------------------------------------------------------- /manifest.json: -------------------------------------------------------------------------------- 1 | { 2 | "manifest_version": 2, 3 | "name": "TIFF viewer", 4 | "description": "Intercepts any resource requests that end in .tif or .tiff and converts them into data urls using an emscripten port of libtiff.", 5 | "version": "1.1.0", 6 | "homepage_url": "https://github.com/my-codeworks/tiff-viewer-extension", 7 | "incognito": "split", 8 | "author": "my codeworks", 9 | "background": { 10 | "scripts": [ 11 | "lib/tiff.min.js", 12 | "background.js" 13 | ] 14 | }, 15 | 16 | "permissions": [ 17 | "storage", 18 | "webRequest", 19 | "webRequestBlocking", 20 | "tabs", 21 | "" 22 | ], 23 | 24 | "options_ui": { 25 | "page": "options/options.html" 26 | }, 27 | 28 | "icons": { 29 | "128": "icon128.png", 30 | "48": "icon48.png", 31 | "16": "icon16.png", 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /options/options.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | TIFF Viewer Options 5 | 6 | 7 | 8 | 21 | 26 |

27 |
28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2015 my codeworks 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | 23 | -------------------------------------------------------------------------------- /options/options.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | const DecoderMemoryLimitInMegabytesDefault = 32; 3 | const ShowDebugOutputDefault = false; 4 | 5 | // Saves options to chrome.storage.local. 6 | function save_options() { 7 | var SaveDecoderMemoryLimitInMegabytes = parseInt(document.getElementById('DecoderMemoryLimitInMegabytes').value); 8 | var SaveDebugOutput = document.getElementById('ShowDebugOutput').checked; 9 | 10 | // Before saving verify if SaveDebugOutput was updated 11 | chrome.storage.local.get('ShowDebugOutput', function(options) { 12 | var CheckShowDebugOutput = options.ShowDebugOutput; 13 | chrome.storage.local.set({ 14 | DecoderMemoryLimitInMegabytes: SaveDecoderMemoryLimitInMegabytes, 15 | ShowDebugOutput: SaveDebugOutput 16 | }, function() { 17 | // Update status to let user know options were saved. 18 | var status = document.getElementById('status'); 19 | status.textContent = 'Options saved.'; 20 | setTimeout(function() { 21 | status.textContent = ''; 22 | }, 1050); 23 | }); 24 | if(CheckShowDebugOutput !== undefined && CheckShowDebugOutput !== SaveDebugOutput) { 25 | chrome.runtime.reload(); 26 | } 27 | }); 28 | } 29 | 30 | // Restores option state using the preferences stored in chrome.storage. 31 | function restore_options() { 32 | chrome.storage.local.get(['DecoderMemoryLimitInMegabytes', 'ShowDebugOutput'], function(options) { 33 | if(options.DecoderMemoryLimitInMegabytes !== undefined) { 34 | document.getElementById('DecoderMemoryLimitInMegabytes').value = options.DecoderMemoryLimitInMegabytes; 35 | } else { 36 | document.getElementById('DecoderMemoryLimitInMegabytes').value = DecoderMemoryLimitInMegabytesDefault; 37 | } 38 | 39 | if(options.ShowDebugOutput !== undefined) { 40 | document.getElementById('ShowDebugOutput').checked = options.ShowDebugOutput; 41 | } else { 42 | document.getElementById('ShowDebugOutput').checked = ShowDebugOutputDefault; 43 | } 44 | }); 45 | } 46 | // Reset select box and checkbox state using the default 47 | // add-on values. 48 | function reset_options() { 49 | document.getElementById('DecoderMemoryLimitInMegabytes').value = DecoderMemoryLimitInMegabytesDefault; 50 | document.getElementById('ShowDebugOutput').checked = ShowDebugOutputDefault; 51 | 52 | // Update status to let user know options were saved. 53 | var status = document.getElementById('status'); 54 | status.textContent = 'Options restored, need to save.'; 55 | setTimeout(function() { 56 | status.textContent = ''; 57 | }, 1550); 58 | } 59 | document.addEventListener('DOMContentLoaded', restore_options); 60 | document.getElementById('save').addEventListener('click', save_options); 61 | document.getElementById('reset').addEventListener('click', reset_options); -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Tiff viewer extension 2 | A Chrome/Firefox extension that enables TIFF images inline in your browser, as if they were supported for real! 3 | 4 | ## Download 5 | Chrome: https://chrome.google.com/webstore/detail/tiff-viewer/fciggfkkblggmebjbekbebbcffeacknj 6 | 7 | Firefox: https://addons.mozilla.org/en-US/firefox/addon/tiff-viewer/ 8 | 9 | ## What does it do? 10 | The extension works by intercepting any attempt by the browser to load a URL that ends in .tiff (or variants) and handling that request instead. The handling consists of using a Javascript, LLVM/Emscripten, port of libtiff to render the TIFF image onto an in memory canvas. The canvas has a method for getting its content as a `data:image` URL and that is used to get a redirect URL for the original browser request. 11 | 12 | So: 13 | 14 | 1. Block original request 15 | 2. Load image data from requested URL using XHR 16 | 3. Render received image data into a canvas 17 | 4. Get `data:image` URI from canvas and redirect to it 18 | 19 | The effect is that the TIFF images appear on page as if the browser had native support for TIFF images. There is a small delay though, due to the fact that we have to download and decode the image separately. But it's small enough that you won't notice it unless you look for it. 20 | 21 | # Known limitations 22 | The extension has a few limitations that might be worth knowing about. If you find an image it cant decode or if it decodes it incorrectly etc open an issue, include the URL to the image and I'll have a look. 23 | 24 | ## Memory 25 | The libtiff port has a limited amount of memory available for decoding the image. Currently it's set to 32MB, 33554432 bytes. This is pretty high and that is because the port doesn't really use memory efficiently. It seems to be loading the entire image into memory before manipulating it. In my tests this was enough to decode a 1200x1600 pixel image and should be enough for most uses. 26 | 27 | If you run into problems with it you can change the amount in the options page. 28 | 29 | ## libtiff.js 30 | I'm using Seikichi's port of libtiff, [seikichi/tiff.js](https://github.com/seikichi/tiff.js/tree/master), and the extension has the same limitations as his port. Mainly it's the memory issue discussed above. But it also doesn't support JPEG compressed Tiffs since Seikichi didn't link libjpeg when he compiled libtiff :) 31 | 32 | # Plans 33 | It would be nice to have a real Javascript decoder for tiffs, instead of the LLVM port. G.P. Hemsley has that going over at Github as well, [GPHemsley/tiff-js](https://github.com/GPHemsley/tiff-js), so I might get into that if I have some time over (so probably not tbh). 34 | 35 | Replace images with a `blob` instead of `data:image`. 36 | 37 | Another approach for intercepting the image and decoding would also be nice since it has to be synchronous now and that means I can't use the nice XHR bytearray return type but had to hack my own conversion that eats time as well... 38 | 39 | # Thanks to 40 | [Paul Heil](https://github.com/Pheil) for his pull requests updating the tiff library and adding the options page. 41 | 42 | [Seikichi](https://github.com/seikichi) for his port of libtiff. That saved me some serious time and effort. 43 | 44 | The Google Chrome team for decent documentation that made it relatively simple to implement this. 45 | 46 | Mozilla for [MDN](https://developer.mozilla.org), THE best resource when you want to quickly look up some Javascript API :) 47 | 48 | # License 49 | I'm using the MIT license for this. See the LICENSE document for more info. 50 | -------------------------------------------------------------------------------- /background.js: -------------------------------------------------------------------------------- 1 | chrome.storage.local.get(['DecoderMemoryLimitInMegabytes','ShowDebugOutput'], function (options) { 2 | var DecoderMemoryLimitInbytes; 3 | if (options.DecoderMemoryLimitInMegabytes === undefined) { 4 | DecoderMemoryLimitInbytes = 32*1E6; 5 | } else { 6 | DecoderMemoryLimitInbytes = options.DecoderMemoryLimitInMegabytes*1E6 ; 7 | } 8 | Tiff.initialize({ TOTAL_MEMORY: DecoderMemoryLimitInbytes }); 9 | 10 | // Disable console.log if ShowDebugOutput is false 11 | if (options.ShowDebugOutput !== true) { 12 | console.log = function() {}; 13 | } 14 | 15 | function convert_binary_string_to_buffer( data ){ 16 | var buffer, view, a_byte; 17 | 18 | buffer = new ArrayBuffer( data.length ); 19 | view = new DataView( buffer ); 20 | 21 | data.split( '' ).forEach( function( c, i ){ 22 | a_byte = c.charCodeAt(); 23 | view.setUint8( i, a_byte & 0xff ); 24 | }); 25 | 26 | return view.buffer; 27 | } 28 | 29 | function create_request_for_url( url ){ 30 | var request; 31 | 32 | console.log( 'Starting XHR request for', url ); 33 | request = new XMLHttpRequest(); 34 | request.open( 'GET', url, false ); 35 | request.overrideMimeType( 'text\/plain; charset=x-user-defined' ); 36 | request.send( ); 37 | console.log( 'Finished XHR request' ); 38 | 39 | return request; 40 | } 41 | 42 | function get_buffer_from_url( url ){ 43 | var request; 44 | 45 | request = create_request_for_url( url ); 46 | 47 | if( request.status != 200 ){ 48 | console.log( 'Exception', 'request for image failed', request ); 49 | return url; 50 | } 51 | 52 | return convert_binary_string_to_buffer( request.responseText ); 53 | } 54 | 55 | convert_buffer_to_dataurl = function( buffer ){ 56 | var tiff; 57 | 58 | try { 59 | console.log( 'Opening tiff' ); 60 | tiff = new Tiff({ buffer: buffer }); 61 | console.log( 'Opened', 'width: ', tiff.width(), 'height: ', tiff.height(), tiff ); 62 | } catch( e ){ 63 | console.log( 'Exception', e ); 64 | return; 65 | } 66 | 67 | return tiff.toDataURL(); 68 | }; 69 | 70 | function redirect_request_to_dataurl( details ){ 71 | var url, buffer, dataURI; 72 | 73 | console.log( 'Entering', details ); 74 | url = details.url; 75 | console.log( 'Loading', details.url ); 76 | buffer = get_buffer_from_url( url ); 77 | console.log( 'Loaded', buffer.byteLength, 'bytes of data' ); 78 | dataURI = convert_buffer_to_dataurl( buffer ); 79 | console.log( 'Converted', dataURI ); 80 | 81 | return { redirectUrl: dataURI }; 82 | } 83 | 84 | function content_type_tiff_header_filter( header ){ 85 | var is_content_type_header, is_of_type_tiff; 86 | 87 | if( !header.name || !header.value ){ return false; } 88 | 89 | is_content_type_header = header.name.toLowerCase() === 'content-type'; 90 | is_of_type_tiff = header.value.toLowerCase().indexOf( 'image/tiff' ) > -1; 91 | 92 | return is_content_type_header && is_of_type_tiff; 93 | } 94 | 95 | function content_type_header_is_tiff( headers ){ 96 | var content_type_tiff_headers; 97 | 98 | content_type_tiff_headers = headers.filter( content_type_tiff_header_filter ); 99 | 100 | return content_type_tiff_headers.length > 0; 101 | } 102 | 103 | function redirect_request_to_dataurl_if_response_content_type_is_tiff( details ){ 104 | if( !content_type_header_is_tiff( details.responseHeaders ) ){ return; } 105 | 106 | details.responseHeaders.push({ name: 'Content-Type', value: 'image/png' }); 107 | 108 | response = redirect_request_to_dataurl( details ); 109 | response.responseHeaders = details.responseHeaders; 110 | 111 | return response; 112 | } 113 | 114 | chrome.webRequest.onBeforeRequest.addListener( 115 | redirect_request_to_dataurl, 116 | { 117 | urls: [ "*://*/*.tiff", "*://*/*.tif", "*://*/*.Tiff", "*://*/*.Tif", "*://*/*.TIFF", "*://*/*.TIF" ], 118 | types: [ 'main_frame', 'sub_frame', 'image' ] 119 | }, 120 | [ 'blocking' ] 121 | ); 122 | 123 | chrome.webRequest.onHeadersReceived.addListener( 124 | redirect_request_to_dataurl_if_response_content_type_is_tiff, 125 | { 126 | urls: [ '' ], 127 | types: [ 'main_frame', 'sub_frame', 'image' ] 128 | }, 129 | [ 'blocking', 'responseHeaders' ] 130 | ); 131 | }); --------------------------------------------------------------------------------