Last active
August 29, 2015 14:05
-
-
Save Noitidart/77006d9201d0e1dfe2be to your computer and use it in GitHub Desktop.
_ff-addon-tutorial-ProcessIcon - Shows how to process icon on the different operating systems.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/******************* | |
* Source for all of these examples | |
* MXR :: Mozilla Release - Search "_processIcon" - http://mxr.mozilla.org/mozilla-release/search?string=_processIcon | |
*/ | |
/**** | |
* Windows | |
* Source: (http://mxr.mozilla.org/mozilla-release/source/toolkit/webapps/WinNativeApp.js#445 | |
* Looks like it uses `.ico` | |
*/ | |
///////////// | |
/** | |
* Process the icon from the imageStream as retrieved from | |
* the URL by getIconForApp(). This will save the icon to the | |
* topwindow.ico file. | |
* | |
* @param aMimeType the icon mimetype | |
* @param aImageStream the stream for the image data | |
* @param aDir the directory where the icon should be stored | |
*/ | |
_processIcon: function(aMimeType, aImageStream, aDir) { | |
let deferred = Promise.defer(); | |
let imgTools = Cc["@mozilla.org/image/tools;1"]. | |
createInstance(Ci.imgITools); | |
let imgContainer = imgTools.decodeImage(aImageStream, aMimeType); | |
let iconStream = imgTools.encodeImage(imgContainer, | |
"image/vnd.microsoft.icon", | |
"format=bmp;bpp=32"); | |
let tmpIconFile = getFile(aDir, this.iconPath); | |
let outputStream = FileUtils.openSafeFileOutputStream(tmpIconFile); | |
NetUtil.asyncCopy(iconStream, outputStream, function(aResult) { | |
if (Components.isSuccessCode(aResult)) { | |
deferred.resolve(); | |
} else { | |
deferred.reject("Failure copying icon: " + aResult); | |
} | |
}); | |
return deferred.promise; | |
} | |
///////////// | |
/**** | |
* Mac OS X | |
* Source: http://mxr.mozilla.org/mozilla-release/source/toolkit/webapps/MacNativeApp.js#291 | |
* Looks like it uses `.incs` | |
*/ | |
///////////// | |
/** | |
* Process the icon from the imageStream as retrieved from | |
* the URL by getIconForApp(). This will bundle the icon to the | |
* app package at Contents/Resources/appicon.icns. | |
* | |
* @param aMimeType the icon mimetype | |
* @param aImageStream the stream for the image data | |
* @param aDir the directory where the icon should be stored | |
*/ | |
_processIcon: function(aMimeType, aIcon, aDir) { | |
let deferred = Promise.defer(); | |
let tmpIconPath = OS.Path.join(aDir, this.iconFile); | |
function conversionDone(aSubject, aTopic) { | |
if (aTopic != "process-finished") { | |
deferred.reject("Failure converting icon, exit code: " + aSubject.exitValue); | |
return; | |
} | |
// SIPS silently fails to convert the icon, so we need to verify if the | |
// icon was successfully converted. | |
OS.File.exists(tmpIconPath).then((aExists) => { | |
if (aExists) { | |
deferred.resolve(); | |
} else { | |
deferred.reject("Failure converting icon, unrecognized image format"); | |
} | |
}); | |
} | |
let process = Cc["@mozilla.org/process/util;1"]. | |
createInstance(Ci.nsIProcess); | |
let sipsFile = getFile("/usr/bin/sips"); | |
process.init(sipsFile); | |
process.runAsync(["-s", "format", "icns", | |
aIcon.path, | |
"--out", tmpIconPath, | |
"-z", "128", "128"], | |
9, conversionDone); | |
return deferred.promise; | |
} | |
///////////// | |
/**** | |
* Linux | |
* Source: http://mxr.mozilla.org/mozilla-release/source/toolkit/webapps/LinuxNativeApp.js#317 | |
* Looks like it uses `.png` | |
*/ | |
///////////// | |
/** | |
* Process the icon from the imageStream as retrieved from | |
* the URL by getIconForApp(). | |
* | |
* @param aMimeType ahe icon mimetype | |
* @param aImageStream the stream for the image data | |
* @param aDir the directory where the icon should be stored | |
*/ | |
_processIcon: function(aMimeType, aImageStream, aDir) { | |
let deferred = Promise.defer(); | |
let imgTools = Cc["@mozilla.org/image/tools;1"]. | |
createInstance(Ci.imgITools); | |
let imgContainer = imgTools.decodeImage(aImageStream, aMimeType); | |
let iconStream = imgTools.encodeImage(imgContainer, "image/png"); | |
let iconFile = getFile(aDir, this.iconFile); | |
let outputStream = FileUtils.openSafeFileOutputStream(iconFile); | |
NetUtil.asyncCopy(iconStream, outputStream, function(aResult) { | |
if (Components.isSuccessCode(aResult)) { | |
deferred.resolve(); | |
} else { | |
deferred.reject("Failure copying icon: " + aResult); | |
} | |
}); | |
return deferred.promise; | |
} | |
///////////// | |
/********* | |
* Source: http://mxr.mozilla.org/mozilla-release/source/toolkit/webapps/NativeApp.jsm#186 | |
* Then WebApp Manager does something with these above I think they feed it to `_processIcon` | |
* How to feed file to `_processIcon`. | |
* 1. Edit `_processIcon` to define where to save the image | |
* 2. Example using `WinNativeApp.js` implementation: | |
*/ | |
///////////// | |
Cu.import("resource://gre/modules/NetUtil.jsm"); | |
/* | |
* getFileInputStream() | |
* | |
* Returns an input stream for the specified file. | |
*/ | |
function getFileInputStream(aFile) { | |
var inputStream = Cc["@mozilla.org/network/file-input-stream;1"]. | |
createInstance(Ci.nsIFileInputStream); | |
// init the stream as RD_ONLY, -1 == default permissions. | |
inputStream.init(aFile, 0x01, -1, null); | |
// Blah. The image decoders use ReadSegments, which isn't implemented on | |
// file input streams. Use a buffered stream to make it work. | |
var bis = Cc["@mozilla.org/network/buffered-input-stream;1"]. | |
createInstance(Ci.nsIBufferedInputStream); | |
bis.init(inputStream, 1024); | |
return bis; | |
} | |
var imgName = "image1.png"; | |
var inMimeType = "image/png"; | |
var iconPngNsiFile = FileUtils.getFile('Desk', ['icon.png']); | |
Services.ww.activeWindow.alert(iconPngNsiFile.path) | |
_processIcon(inMimeType, getFileInputStream(iconPngNsiFile), 'Desk'); | |
/* | |
var istream = getFileInputStream(imgFile); | |
var outParam = { | |
value: null | |
}; | |
imgTools.decodeImageData(istream, inMimeType, outParam); | |
var container = outParam.value; | |
*/ | |
/** | |
* Process the icon from the imageStream as retrieved from | |
* the URL by getIconForApp(). This will save the icon to the | |
* topwindow.ico file. | |
* | |
* @param aMimeType the icon mimetype | |
* @param aImageStream the stream for the image data | |
* @param aDir the directory where the icon should be stored | |
*/ | |
function _processIcon(aMimeType, aImageStream, aDir) { | |
let deferred = Promise.defer(); | |
let imgTools = Cc["@mozilla.org/image/tools;1"].createInstance(Ci.imgITools); | |
let imgContainer = imgTools.decodeImage(aImageStream, aMimeType); | |
let iconStream = imgTools.encodeImage(imgContainer, "image/vnd.microsoft.icon", "-moz-parse-options:format=bmp;bpp=32"); | |
let tmpIconFile = FileUtils.getFile(aDir, ['icon.ico']); | |
let outputStream = FileUtils.openSafeFileOutputStream(tmpIconFile); | |
NetUtil.asyncCopy(iconStream, outputStream, function(aResult) { | |
if (Components.isSuccessCode(aResult)) { | |
deferred.resolve(); | |
} else { | |
deferred.reject("Failure copying icon: " + aResult); | |
} | |
}); | |
return deferred.promise; | |
} | |
///////////// |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
README
SEE ALSO GitHubGIST :: Noitidart / _ff-addon-snippet-MakeIcoOfPaths.js
SEE ALSO GitHubGIST :: Noitidart / _ff-addon-snippet-makeIcnsOfPaths.js
SEE ALSO GitHubGIST :: Noitidart / _ff-addon-snippet-installPngsAsLinuxIcon.js
NOTE The methods in the "See Also" Gists are proper methods for using container files for Windows, Mac OSX, and Linux. The method in this Gist is very crude, it doesn't support container, it just does single sized PNG image conversion
Rev1
Rev2
Rev3
_processIcon
_processIcon
a bit, can't just copy paste it because they usegetFile
which is justFileUtils.getFile
Rev4
Rev5
Rev6
Rev7
.md
to.js
for improved searchability