-
Notifications
You must be signed in to change notification settings - Fork 99
Feature/14 base64 #26
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
base: master
Are you sure you want to change the base?
Changes from all commits
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 |
---|---|---|
@@ -0,0 +1,12 @@ | ||
var imagediff = require('../js/imagediff.js'), | ||
fs = require('fs'); | ||
|
||
var source = fs.readFileSync('1_normal_a.jpg'); | ||
var target = fs.readFileSync('1_normal_b.jpg'); | ||
|
||
source = "data:image/jpg;base64," + source.toString('base64'); | ||
target = "data:image/jpg;base64," + target.toString('base64'); | ||
|
||
var isEqual = imagediff.equal(source, target, 0); | ||
|
||
console.log("Images are equal: " + isEqual); |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -43,6 +43,16 @@ | |
|
||
|
||
// Type Checking | ||
function isBase64Image (string) { | ||
var | ||
base64Regex = /^([A-Za-z0-9+\/]{4})*([A-Za-z0-9+\/]{4}|[A-Za-z0-9+\/]{3}=|[A-Za-z0-9+\/]{2}==)$/, | ||
imageRegex = /^data:image\/(png|jpg);base64,/; | ||
return ( | ||
typeof string === 'string' && | ||
imageRegex.test(string) && | ||
base64Regex.test(string.replace(imageRegex, "")) | ||
); | ||
} | ||
function isImage (object) { | ||
return isType(object, TYPE_IMAGE); | ||
} | ||
|
@@ -64,7 +74,8 @@ | |
isImage(object) || | ||
isCanvas(object) || | ||
isContext(object) || | ||
isImageData(object) | ||
isImageData(object) || | ||
isBase64Image(object) | ||
); | ||
} | ||
function isType (object, type) { | ||
|
@@ -96,6 +107,25 @@ | |
if (isCanvas(object)) { return toImageDataFromCanvas(object); } | ||
if (isContext(object)) { return toImageDataFromContext(object); } | ||
if (isImageData(object)) { return object; } | ||
if (isBase64Image(object)) { return toImageDataFromBase64.apply(null, arguments); } | ||
} | ||
|
||
function toImageDataFromBase64 (string, callback) { | ||
var | ||
image = new Image; | ||
image.src = string; | ||
image.onload = function () { | ||
var | ||
height = image.height, | ||
width = image.width; | ||
canvas.width = width; | ||
canvas.height = height; | ||
context.clearRect(0, 0, width, height); | ||
context.drawImage(image, 0, 0); | ||
if (callback) { | ||
callback(context.getImageData(0, 0, width, height)); | ||
} | ||
}; | ||
} | ||
function toImageDataFromImage (image) { | ||
var | ||
|
@@ -334,6 +364,7 @@ | |
createCanvas : getCanvas, | ||
createImageData : getImageData, | ||
|
||
isBase64Image : isBase64Image, | ||
isImage : isImage, | ||
isCanvas : isCanvas, | ||
isContext : isContext, | ||
|
@@ -343,16 +374,16 @@ | |
toImageData : function (object) { | ||
checkType(object); | ||
if (isImageData(object)) { return copyImageData(object); } | ||
return toImageData(object); | ||
return toImageData.apply(null, arguments); | ||
}, | ||
|
||
equal : function (a, b, tolerance) { | ||
equal : function (a, b, tolerance, callback) { | ||
checkType(a, b); | ||
a = toImageData(a); | ||
b = toImageData(b); | ||
return equal(a, b, tolerance); | ||
}, | ||
diff : function (a, b) { | ||
diff : function (a, b, callback) { | ||
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. Same question as above, the callbacks are not never called. 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. In some cases the image is not loaded immediately. We must wait for 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. Are you referring to the images being passed into the diff function, or the diff image that is generated? 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 and the following line: We need to load the base64 src to an image before drawing that image to a canvas. 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. I recommend setting the onload function before setting the src to avoid the race condition where the image is loaded before onload is set. |
||
checkType(a, b); | ||
a = toImageData(a); | ||
b = toImageData(b); | ||
|
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.
What's the purpose of the callback in this function?