Permalink
Browse files

Jpeg now returns Buffer

  • Loading branch information...
1 parent bdf1b7f commit 7104754541637d7f56dfc20984235433860d7704 @pkrumins committed Aug 18, 2010
Showing with 15 additions and 16 deletions.
  1. +2 −4 examples/jpeg-example-async.js
  2. +2 −4 examples/jpeg-example.js
  3. +3 −3 examples/jpeg-example2-async.js
  4. +1 −1 examples/jpeg-example2.js
  5. +7 −4 src/jpeg.cpp
@@ -3,12 +3,10 @@ var sys = require('sys');
var Jpeg = require('../jpeg').Jpeg;
var Buffer = require('buffer').Buffer;
-// the rgba-terminal.dat file is 1152000 bytes long.
-var rgba = new Buffer(1152000);
-rgba.write(fs.readFileSync('./rgba-terminal.dat', 'binary'), 'binary');
+var rgba = fs.readFileSync('./rgba-terminal.dat');
var jpeg = new Jpeg(rgba, 720, 400, 'rgba');
jpeg.encode(function (image) {
- fs.writeFileSync('./jpeg-async.jpeg', image, 'binary');
+ fs.writeFileSync('./jpeg-async.jpeg', image.toString('binary'), 'binary');
})
@@ -3,12 +3,10 @@ var sys = require('sys');
var Jpeg = require('../jpeg').Jpeg;
var Buffer = require('buffer').Buffer;
-// the rgba-terminal.dat file is 1152000 bytes long.
-var rgba = new Buffer(1152000);
-rgba.write(fs.readFileSync('./rgba-terminal.dat', 'binary'), 'binary');
+var rgba = fs.readFileSync('./rgba-terminal.dat');
var jpeg = new Jpeg(rgba, 720, 400, 'rgba');
-var jpeg_img = jpeg.encodeSync();
+var jpeg_img = jpeg.encodeSync().toString('binary');
fs.writeFileSync('./jpeg.jpeg', jpeg_img, 'binary');
@@ -16,7 +16,7 @@ for (var i=0; i<HEIGHT; i++) {
}
var jpeg = new Jpeg(rgba, WIDTH, HEIGHT, 'rgb');
-var jpeg_img = jpeg.encode();
-
-fs.writeFileSync('./jpeg-gradient.jpeg', jpeg_img, 'binary');
+jpeg.encode(function (jpeg_img) {
+ fs.writeFileSync('./jpeg-gradient-async.jpeg', jpeg_img.toString('binary'), 'binary');
+});
@@ -16,7 +16,7 @@ for (var i=0; i<HEIGHT; i++) {
}
var jpeg = new Jpeg(rgba, WIDTH, HEIGHT, 'rgb');
-var jpeg_img = jpeg.encodeSync();
+var jpeg_img = jpeg.encodeSync().toString('binary');
fs.writeFileSync('./jpeg-gradient.jpeg', jpeg_img, 'binary');
View
@@ -39,9 +39,10 @@ Jpeg::JpegEncodeSync()
return VException(err);
}
- return scope.Close(
- Encode(jpeg_encoder.get_jpeg(), jpeg_encoder.get_jpeg_len(), BINARY)
- );
+ int jpeg_len = jpeg_encoder.get_jpeg_len();
+ Buffer *retbuf = Buffer::New(jpeg_len);
+ memcpy(retbuf->data(), jpeg_encoder.get_jpeg(), jpeg_len);
+ return scope.Close(retbuf->handle_);
}
void
@@ -175,7 +176,9 @@ Jpeg::EIO_JpegEncodeAfter(eio_req *req)
argv[1] = ErrorException(enc_req->error);
}
else {
- argv[0] = Local<Value>::New(Encode(enc_req->jpeg, enc_req->jpeg_len, BINARY));
+ Buffer *buf = Buffer::New(enc_req->jpeg_len);
+ memcpy(buf->data(), enc_req->jpeg, enc_req->jpeg_len);
+ argv[0] = buf->handle_;
argv[1] = Undefined();
}

0 comments on commit 7104754

Please sign in to comment.