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
Support canvas.getBuffer('raw') #819
Changes from 4 commits
3ab222e
fe88c85
bb50641
239d3c3
f7cab45
94ae2a8
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,7 +5,8 @@ | |
var Canvas = require('../') | ||
, assert = require('assert') | ||
, parseFont = Canvas.Context2d.parseFont | ||
, fs = require('fs'); | ||
, fs = require('fs') | ||
, os = require('os'); | ||
|
||
console.log(); | ||
console.log(' canvas: %s', Canvas.version); | ||
|
@@ -246,6 +247,12 @@ describe('Canvas', function () { | |
assert.equal(50, canvas.height); | ||
}); | ||
|
||
it('Canvas#stride', function() { | ||
var canvas = new Canvas(24, 10); | ||
assert.ok(canvas.stride >= 24, 'canvas.stride is too short'); | ||
assert.ok(canvas.stride < 1024, 'canvas.stride seems too long'); | ||
}); | ||
|
||
it('Canvas#getContext("invalid")', function () { | ||
assert.equal(null, new Canvas(200, 300).getContext('invalid')); | ||
}); | ||
|
@@ -377,6 +384,103 @@ describe('Canvas', function () { | |
}); | ||
}); | ||
|
||
describe('#toBuffer("raw")', function() { | ||
var canvas = new Canvas(10, 10) | ||
, ctx = canvas.getContext('2d'); | ||
|
||
ctx.clearRect(0, 0, 10, 10); | ||
|
||
ctx.fillStyle = 'rgba(200, 200, 200, 0.505)'; | ||
ctx.fillRect(0, 0, 5, 5); | ||
|
||
ctx.fillStyle = 'red'; | ||
ctx.fillRect(5, 0, 5, 5); | ||
|
||
ctx.fillStyle = '#00ff00'; | ||
ctx.fillRect(0, 5, 5, 5); | ||
|
||
ctx.fillStyle = 'black'; | ||
ctx.fillRect(5, 5, 4, 5); | ||
|
||
/** Output: | ||
* *****RRRRR | ||
* *****RRRRR | ||
* *****RRRRR | ||
* *****RRRRR | ||
* *****RRRRR | ||
* GGGGGBBBB- | ||
* GGGGGBBBB- | ||
* GGGGGBBBB- | ||
* GGGGGBBBB- | ||
* GGGGGBBBB- | ||
*/ | ||
|
||
var buf = canvas.toBuffer('raw'); | ||
var stride = canvas.stride; | ||
|
||
var isBE = (function() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This can be replaced with There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good catch |
||
var b = new ArrayBuffer(4); | ||
var u32 = new Uint32Array(b); | ||
var u8 = new Uint8Array(b); | ||
u32[0] = 1; | ||
return u8[0] ? 'LE' : 'BE'; | ||
}()); | ||
|
||
function assertPixel(u32, x, y, message) { | ||
var expected = '0x' + u32.toString(16); | ||
|
||
// Buffer doesn't have readUInt32(): it only has readUInt32LE() and | ||
// readUInt32BE(). | ||
var px = buf.readUInt32BE(y * stride + x * 4); | ||
if (isBE) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. How about something like this? var postfix = os.endianness()
var px = buf['readUInt32' + postfix](y * stride + x * 4) |
||
px = (((px & 0xff) << 24) | ||
| ((px & 0xff00) << 8) | ||
| ((px & 0xff0000) >> 8) | ||
| ((px & 0xff000000) >> 24)) | ||
>>> 0; // -1 => 0xffffffff | ||
} | ||
var actual = '0x' + px.toString(16); | ||
|
||
assert.equal(actual, expected, message); | ||
} | ||
|
||
it('should have the correct size', function() { | ||
assert.equal(buf.length, stride * 10); | ||
}); | ||
|
||
it('does not premultiply alpha', function() { | ||
assertPixel(0x80646464, 0, 0, 'first semitransparent pixel'); | ||
assertPixel(0x80646464, 4, 4, 'last semitransparent pixel'); | ||
}); | ||
|
||
it('draws red', function() { | ||
assertPixel(0xffff0000, 5, 0, 'first red pixel'); | ||
assertPixel(0xffff0000, 9, 4, 'last red pixel'); | ||
}); | ||
|
||
it('draws green', function() { | ||
assertPixel(0xff00ff00, 0, 5, 'first green pixel'); | ||
assertPixel(0xff00ff00, 4, 9, 'last green pixel'); | ||
}); | ||
|
||
it('draws black', function() { | ||
assertPixel(0xff000000, 5, 5, 'first black pixel'); | ||
assertPixel(0xff000000, 8, 9, 'last black pixel'); | ||
}); | ||
|
||
it('leaves undrawn pixels black, transparent', function() { | ||
assertPixel(0x0, 9, 5, 'first undrawn pixel'); | ||
assertPixel(0x0, 9, 9, 'last undrawn pixel'); | ||
}); | ||
|
||
it('is immutable', function() { | ||
ctx.fillStyle = 'white'; | ||
ctx.fillRect(0, 0, 10, 10); | ||
canvas.toBuffer('raw'); // (side-effect: flushes canvas) | ||
assertPixel(0xffff0000, 5, 0, 'first red pixel'); | ||
}); | ||
}); | ||
|
||
describe('#toDataURL()', function () { | ||
var canvas = new Canvas(200, 200) | ||
, ctx = canvas.getContext('2d'); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How about
info.Length() >= 1
to support.toBuffer('raw', undefined, undefined)
? (might be used if someone wraps.toBuffer
)