Bug 405982: Improve error handling and consistency with image encoders.

r=dolse, r+sr=pavlov, a=beltzner
This commit is contained in:
dtownsend%oxymoronical.com 2007-12-21 09:19:27 +00:00
Родитель 661614290b
Коммит 971fb9f936
4 изменённых файлов: 49 добавлений и 8 удалений

Просмотреть файл

@ -736,6 +736,7 @@ nsCanvasRenderingContext2D::GetInputStream(const char *aMimeType,
cairo_surface_status(mSurface) != CAIRO_STATUS_SUCCESS)
return NS_ERROR_FAILURE;
nsresult rv;
const char encoderPrefix[] = "@mozilla.org/image/encoder;2?type=";
nsAutoArrayPtr<char> conid(new (std::nothrow) char[strlen(encoderPrefix) + strlen(aMimeType) + 1]);
strcpy(conid, encoderPrefix);
@ -746,10 +747,11 @@ nsCanvasRenderingContext2D::GetInputStream(const char *aMimeType,
return NS_ERROR_FAILURE;
if (mImageSurfaceData) {
encoder->InitFromData(mImageSurfaceData,
mWidth * mHeight * 4, mWidth, mHeight, mWidth * 4,
imgIEncoder::INPUT_FORMAT_HOSTARGB,
nsDependentString(aEncoderOptions));
rv = encoder->InitFromData(mImageSurfaceData,
mWidth * mHeight * 4, mWidth, mHeight, mWidth * 4,
imgIEncoder::INPUT_FORMAT_HOSTARGB,
nsDependentString(aEncoderOptions));
NS_ENSURE_SUCCESS(rv, rv);
} else {
nsAutoArrayPtr<PRUint8> imageBuffer(new (std::nothrow) PRUint8[mWidth * mHeight * 4]);
if (!imageBuffer)
@ -769,10 +771,11 @@ nsCanvasRenderingContext2D::GetInputStream(const char *aMimeType,
cairo_paint (cr);
cairo_destroy (cr);
encoder->InitFromData(imageBuffer.get(),
mWidth * mHeight * 4, mWidth, mHeight, mWidth * 4,
imgIEncoder::INPUT_FORMAT_HOSTARGB,
nsDependentString(aEncoderOptions));
rv = encoder->InitFromData(imageBuffer.get(),
mWidth * mHeight * 4, mWidth, mHeight, mWidth * 4,
imgIEncoder::INPUT_FORMAT_HOSTARGB,
nsDependentString(aEncoderOptions));
NS_ENSURE_SUCCESS(rv, rv);
}
return CallQueryInterface(encoder, aStream);

Просмотреть файл

@ -488,6 +488,7 @@ _TEST_FILES_E = \
test_2d.imageData.put.unchanged.html \
test_2d.imageData.put.unaffected.html \
test_2d.imageData.put.path.html \
test_bug405982.html \
image_transparent50.png \
image_redtransparent.png \
image_yellow.png \

Просмотреть файл

@ -0,0 +1,34 @@
<!DOCTYPE HTML>
<title>Canvas test: toDataURL.png</title>
<script src="/MochiKit/MochiKit.js"></script>
<script src="/tests/SimpleTest/SimpleTest.js"></script>
<link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css">
<body>
<canvas id="c" width="100" height="50"><p class="fallback">FAIL (fallback content)</p></canvas>
<script>
SimpleTest.waitForExplicitFinish();
MochiKit.DOM.addLoadEvent(function () {
var canvas = document.getElementById('c');
var ctx = canvas.getContext('2d');
netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
try {
var data = canvas.toDataURL('image/png', 'quality=100');
ok(false, "Should have thrown an exception for invalid args to png encoder");
}
catch (e) {
is(e.result, Components.results.NS_ERROR_INVALID_ARG, "Exception was wrong for png encoder");
}
try {
var data = canvas.toDataURL('image/jpeg', 'foobar=true');
ok(false, "Should have thrown an exception for invalid args to jpeg encoder");
}
catch (e) {
is(e.result, Components.results.NS_ERROR_INVALID_ARG, "Exception was wrong for jpeg encoder");
}
SimpleTest.finish();
});
</script>

Просмотреть файл

@ -126,6 +126,9 @@ NS_IMETHODIMP nsJPEGEncoder::InitFromData(const PRUint8* aData,
NS_WARNING("Quality value invalid, should be integer 0-100, using default");
}
}
else {
return NS_ERROR_INVALID_ARG;
}
}
jpeg_compress_struct cinfo;