@@ -31,6 +31,8 @@ var exec = child.exec;
3131var spawn = child . spawn ;
3232var path = require ( 'path' ) ;
3333var sof = { 0xc0 : true , 0xc1 : true , 0xc2 : true , 0xc3 : true , 0xc5 : true , 0xc6 : true , 0xc7 : true , 0xc9 : true , 0xca : true , 0xcb : true , 0xcd : true , 0xce : true , 0xcf : true } ;
34+ var middlewares = { } ;
35+ var Fs = require ( 'fs' ) ;
3436
3537function u16 ( buf , o ) {
3638 return buf [ o ] << 8 | buf [ o + 1 ] ;
@@ -244,10 +246,25 @@ Image.prototype.save = function(filename, callback, writer) {
244246 if ( ! callback )
245247 return ;
246248
247- if ( error )
249+ if ( error ) {
248250 callback ( error , false ) ;
249- else
250- callback ( null , true ) ;
251+ return ;
252+ }
253+
254+ var middleware = middlewares [ self . outputType ] ;
255+ if ( ! middleware )
256+ return callback ( null , true ) ;
257+
258+ var reader = Fs . createReadStream ( filename ) ;
259+ var writer = Fs . createWriteStream ( filename + '_' ) ;
260+
261+ reader . pipe ( middleware ( ) ) . pipe ( writer ) ;
262+
263+ writer . on ( 'finish' , function ( ) {
264+ Fs . rename ( filename + '_' , filename , function ( ) {
265+ callback ( null , true ) ;
266+ } ) ;
267+ } ) ;
251268 } ) ;
252269
253270 if ( self . currentStream ) {
@@ -293,7 +310,7 @@ Image.prototype.pipe = function(stream, type, options) {
293310 if ( ! self . builder . length )
294311 return ;
295312
296- if ( type === undefined || type === null )
313+ if ( ! type )
297314 type = self . outputType ;
298315
299316 var cmd = spawn ( self . isIM ? 'convert' : 'gm' , self . arg ( ! self . filename ? '-' : self . filename , ( type ? type + ':' : '' ) + '-' ) ) ;
@@ -302,7 +319,12 @@ Image.prototype.pipe = function(stream, type, options) {
302319 cmd . stdout . on ( 'data' , stream . emit . bind ( stream , 'data' ) ) ;
303320 cmd . stdout . on ( 'end' , stream . emit . bind ( stream , 'end' ) ) ;
304321 cmd . on ( 'error' , stream . emit . bind ( stream , 'error' ) ) ;
305- cmd . stdout . pipe ( stream , options ) ;
322+
323+ var middleware = middlewares [ type ] ;
324+ if ( middleware )
325+ cmd . stdout . pipe ( middleware ( ) ) . pipe ( stream , options ) ;
326+ else
327+ cmd . stdout . pipe ( stream , options ) ;
306328
307329 if ( self . currentStream ) {
308330 if ( self . currentStream instanceof Buffer )
@@ -341,7 +363,11 @@ Image.prototype.stream = function(type, writer) {
341363 if ( writer )
342364 writer ( cmd . stdin ) ;
343365
344- return cmd . stdout ;
366+ var middleware = middlewares [ type ] ;
367+ if ( ! middleware )
368+ return cmd . stdout ;
369+
370+ return cmd . stdout . pipe ( middleware ( ) ) ;
345371} ;
346372
347373/*
@@ -743,9 +769,6 @@ Image.prototype.colors = function(value) {
743769 return this . push ( '-colors' , value , 10 ) ;
744770} ;
745771
746- /*
747- @color {String}
748- */
749772Image . prototype . background = function ( color ) {
750773 return this . push ( '-background' , color , 2 ) ;
751774} ;
@@ -785,4 +808,8 @@ exports.init = function(filename, imageMagick, width, height) {
785808
786809exports . load = function ( filename , imageMagick , width , height ) {
787810 return new Image ( filename , imageMagick , width , height ) ;
788- } ;
811+ } ;
812+
813+ exports . middleware = function ( type , fn ) {
814+ middlewares [ type ] = fn ;
815+ } ;
0 commit comments