image/test/unit/test_encoder_apng.js

Fri, 16 Jan 2015 18:13:44 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Fri, 16 Jan 2015 18:13:44 +0100
branch
TOR_BUG_9701
changeset 14
925c144e1f1f
permissions
-rw-r--r--

Integrate suggestion from review to improve consistency with existing code.

michael@0 1 /*
michael@0 2 * Test for APNG encoding in libpr0n
michael@0 3 *
michael@0 4 */
michael@0 5
michael@0 6
michael@0 7 const Ci = Components.interfaces;
michael@0 8 const Cc = Components.classes;
michael@0 9
michael@0 10 // dispose=[none|background|previous]
michael@0 11 // blend=[source|over]
michael@0 12
michael@0 13 var apng1A = {
michael@0 14 // A 3x3 image with 3 frames, alternating red, green, blue. RGB format.
michael@0 15 width : 3, height : 3, skipFirstFrame : false,
michael@0 16 format : Ci.imgIEncoder.INPUT_FORMAT_RGB,
michael@0 17 transparency : null,
michael@0 18 plays : 0,
michael@0 19
michael@0 20 frames : [
michael@0 21 { // frame #1
michael@0 22 width : 3, height : 3,
michael@0 23 x_offset : 0, y_offset : 0,
michael@0 24 dispose : "none", blend : "source", delay : 500,
michael@0 25
michael@0 26 format : Ci.imgIEncoder.INPUT_FORMAT_RGB, stride : 9,
michael@0 27 transparency : null,
michael@0 28
michael@0 29 pixels : [
michael@0 30 255,0,0, 255,0,0, 255,0,0,
michael@0 31 255,0,0, 255,0,0, 255,0,0,
michael@0 32 255,0,0, 255,0,0, 255,0,0
michael@0 33 ]
michael@0 34 },
michael@0 35
michael@0 36 { // frame #2
michael@0 37 width : 3, height : 3,
michael@0 38 x_offset : 0, y_offset : 0,
michael@0 39 dispose : "none", blend : "source", delay : 500,
michael@0 40
michael@0 41 format : Ci.imgIEncoder.INPUT_FORMAT_RGB, stride : 9,
michael@0 42 transparency : null,
michael@0 43
michael@0 44 pixels : [
michael@0 45 0,255,0, 0,255,0, 0,255,0,
michael@0 46 0,255,0, 0,255,0, 0,255,0,
michael@0 47 0,255,0, 0,255,0, 0,255,0
michael@0 48 ]
michael@0 49 },
michael@0 50
michael@0 51 { // frame #3
michael@0 52 width : 3, height : 3,
michael@0 53 x_offset : 0, y_offset : 0,
michael@0 54 dispose : "none", blend : "source", delay : 500,
michael@0 55
michael@0 56 format : Ci.imgIEncoder.INPUT_FORMAT_RGB, stride : 9,
michael@0 57 transparency : null,
michael@0 58
michael@0 59 pixels : [
michael@0 60 0,0,255, 0,0,255, 0,0,255,
michael@0 61 0,0,255, 0,0,255, 0,0,255,
michael@0 62 0,0,255, 0,0,255, 0,0,255
michael@0 63 ]
michael@0 64 }
michael@0 65
michael@0 66 ],
michael@0 67 expected : "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAMAAAADCAIAAADZSiLoAAAACGFjVEwAAAADAAAAAM7tusAAAAAaZmNUTAAAAAAAAAADAAAAAwAAAAAAAAAAAfQD6AAAdRYgGAAAABBJREFUCJlj+M/AAEEMWFgAj44I+H2CySsAAAAaZmNUTAAAAAEAAAADAAAAAwAAAAAAAAAAAfQD6AAA7mXKzAAAABNmZEFUAAAAAgiZY2D4zwBFWFgAhpcI+I731VcAAAAaZmNUTAAAAAMAAAADAAAAAwAAAAAAAAAAAfQD6AAAA/MZJQAAABNmZEFUAAAABAiZY2Bg+A9DmCwAfaAI+AGmQVoAAAAASUVORK5CYII="
michael@0 68 };
michael@0 69
michael@0 70
michael@0 71 var apng1B = {
michael@0 72 // A 3x3 image with 3 frames, alternating red, green, blue. RGBA format.
michael@0 73 width : 3, height : 3, skipFirstFrame : false,
michael@0 74 format : Ci.imgIEncoder.INPUT_FORMAT_RGBA,
michael@0 75 transparency : null,
michael@0 76 plays : 0,
michael@0 77
michael@0 78 frames : [
michael@0 79 { // frame #1
michael@0 80 width : 3, height : 3,
michael@0 81 x_offset : 0, y_offset : 0,
michael@0 82 dispose : "none", blend : "source", delay : 500,
michael@0 83
michael@0 84 format : Ci.imgIEncoder.INPUT_FORMAT_RGBA, stride : 12,
michael@0 85
michael@0 86 pixels : [
michael@0 87 255,0,0,255, 255,0,0,255, 255,0,0,255,
michael@0 88 255,0,0,255, 255,0,0,255, 255,0,0,255,
michael@0 89 255,0,0,255, 255,0,0,255, 255,0,0,255
michael@0 90 ]
michael@0 91 },
michael@0 92
michael@0 93 { // frame #2
michael@0 94 width : 3, height : 3,
michael@0 95 x_offset : 0, y_offset : 0,
michael@0 96 dispose : "none", blend : "source", delay : 500,
michael@0 97
michael@0 98 format : Ci.imgIEncoder.INPUT_FORMAT_RGBA, stride : 12,
michael@0 99
michael@0 100 pixels : [
michael@0 101 0,255,0,255, 0,255,0,255, 0,255,0,255,
michael@0 102 0,255,0,255, 0,255,0,255, 0,255,0,255,
michael@0 103 0,255,0,255, 0,255,0,255, 0,255,0,255
michael@0 104 ]
michael@0 105 },
michael@0 106
michael@0 107 { // frame #3
michael@0 108 width : 3, height : 3,
michael@0 109 x_offset : 0, y_offset : 0,
michael@0 110 dispose : "none", blend : "source", delay : 500,
michael@0 111
michael@0 112 format : Ci.imgIEncoder.INPUT_FORMAT_RGBA, stride : 12,
michael@0 113
michael@0 114 pixels : [
michael@0 115 0,0,255,255, 0,0,255,255, 0,0,255,255,
michael@0 116 0,0,255,255, 0,0,255,255, 0,0,255,255,
michael@0 117 0,0,255,255, 0,0,255,255, 0,0,255,255
michael@0 118 ]
michael@0 119 }
michael@0 120
michael@0 121 ],
michael@0 122 expected : "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAMAAAADCAYAAABWKLW/AAAACGFjVEwAAAADAAAAAM7tusAAAAAaZmNUTAAAAAAAAAADAAAAAwAAAAAAAAAAAfQD6AAAdRYgGAAAABFJREFUCJlj+M/A8B+GGXByAF3XEe/CoiJ1AAAAGmZjVEwAAAABAAAAAwAAAAMAAAAAAAAAAAH0A+gAAO5lyswAAAASZmRBVAAAAAIImWNg+I8EcXIAVOAR77Vyl9QAAAAaZmNUTAAAAAMAAAADAAAAAwAAAAAAAAAAAfQD6AAAA/MZJQAAABRmZEFUAAAABAiZY2Bg+P8fgXFxAEvpEe8rClxSAAAAAElFTkSuQmCC"
michael@0 123 };
michael@0 124
michael@0 125
michael@0 126 var apng1C = {
michael@0 127 // A 3x3 image with 3 frames, alternating red, green, blue. RGBA format.
michael@0 128 // The first frame is skipped, so it will only flash green/blue (or static red in an APNG-unaware viewer)
michael@0 129 width : 3, height : 3, skipFirstFrame : true,
michael@0 130 format : Ci.imgIEncoder.INPUT_FORMAT_RGBA,
michael@0 131 transparency : null,
michael@0 132 plays : 0,
michael@0 133
michael@0 134 frames : [
michael@0 135 { // frame #1
michael@0 136 width : 3, height : 3,
michael@0 137 x_offset : 0, y_offset : 0,
michael@0 138 dispose : "none", blend : "source", delay : 500,
michael@0 139
michael@0 140 format : Ci.imgIEncoder.INPUT_FORMAT_RGBA, stride : 12,
michael@0 141
michael@0 142 pixels : [
michael@0 143 255,0,0,255, 255,0,0,255, 255,0,0,255,
michael@0 144 255,0,0,255, 255,0,0,255, 255,0,0,255,
michael@0 145 255,0,0,255, 255,0,0,255, 255,0,0,255
michael@0 146 ]
michael@0 147 },
michael@0 148
michael@0 149 { // frame #2
michael@0 150 width : 3, height : 3,
michael@0 151 x_offset : 0, y_offset : 0,
michael@0 152 dispose : "none", blend : "source", delay : 500,
michael@0 153
michael@0 154 format : Ci.imgIEncoder.INPUT_FORMAT_RGBA, stride : 12,
michael@0 155
michael@0 156 pixels : [
michael@0 157 0,255,0,255, 0,255,0,255, 0,255,0,255,
michael@0 158 0,255,0,255, 0,255,0,255, 0,255,0,255,
michael@0 159 0,255,0,255, 0,255,0,255, 0,255,0,255
michael@0 160 ]
michael@0 161 },
michael@0 162
michael@0 163 { // frame #3
michael@0 164 width : 3, height : 3,
michael@0 165 x_offset : 0, y_offset : 0,
michael@0 166 dispose : "none", blend : "source", delay : 500,
michael@0 167
michael@0 168 format : Ci.imgIEncoder.INPUT_FORMAT_RGBA, stride : 12,
michael@0 169
michael@0 170 pixels : [
michael@0 171 0,0,255,255, 0,0,255,255, 0,0,255,255,
michael@0 172 0,0,255,255, 0,0,255,255, 0,0,255,255,
michael@0 173 0,0,255,255, 0,0,255,255, 0,0,255,255
michael@0 174 ]
michael@0 175 }
michael@0 176
michael@0 177 ],
michael@0 178 expected : "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAMAAAADCAYAAABWKLW/AAAACGFjVEwAAAACAAAAAPONk3AAAAARSURBVAiZY/jPwPAfhhlwcgBd1xHvwqIidQAAABpmY1RMAAAAAAAAAAMAAAADAAAAAAAAAAAB9APoAAB1FiAYAAAAEmZkQVQAAAABCJljYPiPBHFyAFTgEe+kD/2tAAAAGmZjVEwAAAACAAAAAwAAAAMAAAAAAAAAAAH0A+gAAJiA8/EAAAAUZmRBVAAAAAMImWNgYPj/H4FxcQBL6RHvC5ggGQAAAABJRU5ErkJggg=="
michael@0 179 };
michael@0 180
michael@0 181
michael@0 182 var apng2A = {
michael@0 183 // A 3x3 image with 3 frames, alternating red, green, blue. RGBA format.
michael@0 184 // blend = over mode
michael@0 185 // (The green frame is a horizontal gradient, and the blue frame is a
michael@0 186 // vertical gradient. They stack as they animate.)
michael@0 187 width : 3, height : 3, skipFirstFrame : false,
michael@0 188 format : Ci.imgIEncoder.INPUT_FORMAT_RGBA,
michael@0 189 transparency : null,
michael@0 190 plays : 0,
michael@0 191
michael@0 192 frames : [
michael@0 193 { // frame #1
michael@0 194 width : 3, height : 3,
michael@0 195 x_offset : 0, y_offset : 0,
michael@0 196 dispose : "none", blend : "source", delay : 500,
michael@0 197
michael@0 198 format : Ci.imgIEncoder.INPUT_FORMAT_RGBA, stride : 12,
michael@0 199
michael@0 200 pixels : [
michael@0 201 255,0,0,255, 255,0,0,255, 255,0,0,255,
michael@0 202 255,0,0,255, 255,0,0,255, 255,0,0,255,
michael@0 203 255,0,0,255, 255,0,0,255, 255,0,0,255
michael@0 204 ]
michael@0 205 },
michael@0 206
michael@0 207 { // frame #2
michael@0 208 width : 3, height : 3,
michael@0 209 x_offset : 0, y_offset : 0,
michael@0 210 dispose : "none", blend : "over", delay : 500,
michael@0 211
michael@0 212 format : Ci.imgIEncoder.INPUT_FORMAT_RGBA, stride : 12,
michael@0 213
michael@0 214 pixels : [
michael@0 215 0,255,0,255, 0,255,0,180, 0,255,0,75,
michael@0 216 0,255,0,255, 0,255,0,180, 0,255,0,75,
michael@0 217 0,255,0,255, 0,255,0,180, 0,255,0,75
michael@0 218 ]
michael@0 219 },
michael@0 220
michael@0 221 { // frame #3
michael@0 222 width : 3, height : 3,
michael@0 223 x_offset : 0, y_offset : 0,
michael@0 224 dispose : "none", blend : "over", delay : 500,
michael@0 225
michael@0 226 format : Ci.imgIEncoder.INPUT_FORMAT_RGBA, stride : 12,
michael@0 227
michael@0 228 pixels : [
michael@0 229 0,0,255,75, 0,0,255,75, 0,0,255,75,
michael@0 230 0,0,255,180, 0,0,255,180, 0,0,255,180,
michael@0 231 0,0,255,255, 0,0,255,255, 0,0,255,255
michael@0 232 ]
michael@0 233 }
michael@0 234
michael@0 235 ],
michael@0 236 expected : "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAMAAAADCAYAAABWKLW/AAAACGFjVEwAAAADAAAAAM7tusAAAAAaZmNUTAAAAAAAAAADAAAAAwAAAAAAAAAAAfQD6AAAdRYgGAAAABFJREFUCJlj+M/A8B+GGXByAF3XEe/CoiJ1AAAAGmZjVEwAAAABAAAAAwAAAAMAAAAAAAAAAAH0A+gAAZli+loAAAAcZmRBVAAAAAIImWNg+M/wn+E/wxaG/wzeDDg5ACeGDvKVa3pyAAAAGmZjVEwAAAADAAAAAwAAAAMAAAAAAAAAAAH0A+gAAXT0KbMAAAAcZmRBVAAAAAQImWNgYPjvjcAM/7cgMMP//zAMAPqkDvLn1m3SAAAAAElFTkSuQmCC"
michael@0 237 };
michael@0 238
michael@0 239
michael@0 240 var apng2B = {
michael@0 241 // A 3x3 image with 3 frames, alternating red, green, blue. RGBA format.
michael@0 242 // blend = over, dispose = background
michael@0 243 // (The green frame is a horizontal gradient, and the blue frame is a
michael@0 244 // vertical gradient. Each frame is displayed individually, blended to
michael@0 245 // whatever the background is.)
michael@0 246 width : 3, height : 3, skipFirstFrame : false,
michael@0 247 format : Ci.imgIEncoder.INPUT_FORMAT_RGBA,
michael@0 248 transparency : null,
michael@0 249 plays : 0,
michael@0 250
michael@0 251 frames : [
michael@0 252 { // frame #1
michael@0 253 width : 3, height : 3,
michael@0 254 x_offset : 0, y_offset : 0,
michael@0 255 dispose : "background", blend : "source", delay : 500,
michael@0 256
michael@0 257 format : Ci.imgIEncoder.INPUT_FORMAT_RGBA, stride : 12,
michael@0 258
michael@0 259 pixels : [
michael@0 260 255,0,0,255, 255,0,0,255, 255,0,0,255,
michael@0 261 255,0,0,255, 255,0,0,255, 255,0,0,255,
michael@0 262 255,0,0,255, 255,0,0,255, 255,0,0,255
michael@0 263 ]
michael@0 264 },
michael@0 265
michael@0 266 { // frame #2
michael@0 267 width : 3, height : 3,
michael@0 268 x_offset : 0, y_offset : 0,
michael@0 269 dispose : "background", blend : "over", delay : 500,
michael@0 270
michael@0 271 format : Ci.imgIEncoder.INPUT_FORMAT_RGBA, stride : 12,
michael@0 272
michael@0 273 pixels : [
michael@0 274 0,255,0,255, 0,255,0,180, 0,255,0,75,
michael@0 275 0,255,0,255, 0,255,0,180, 0,255,0,75,
michael@0 276 0,255,0,255, 0,255,0,180, 0,255,0,75
michael@0 277 ]
michael@0 278 },
michael@0 279
michael@0 280 { // frame #3
michael@0 281 width : 3, height : 3,
michael@0 282 x_offset : 0, y_offset : 0,
michael@0 283 dispose : "background", blend : "over", delay : 500,
michael@0 284
michael@0 285 format : Ci.imgIEncoder.INPUT_FORMAT_RGBA, stride : 12,
michael@0 286
michael@0 287 pixels : [
michael@0 288 0,0,255,75, 0,0,255,75, 0,0,255,75,
michael@0 289 0,0,255,180, 0,0,255,180, 0,0,255,180,
michael@0 290 0,0,255,255, 0,0,255,255, 0,0,255,255
michael@0 291 ]
michael@0 292 }
michael@0 293
michael@0 294 ],
michael@0 295 expected : "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAMAAAADCAYAAABWKLW/AAAACGFjVEwAAAADAAAAAM7tusAAAAAaZmNUTAAAAAAAAAADAAAAAwAAAAAAAAAAAfQD6AEAbA0RWQAAABFJREFUCJlj+M/A8B+GGXByAF3XEe/CoiJ1AAAAGmZjVEwAAAABAAAAAwAAAAMAAAAAAAAAAAH0A+gBAYB5yxsAAAAcZmRBVAAAAAIImWNg+M/wn+E/wxaG/wzeDDg5ACeGDvKVa3pyAAAAGmZjVEwAAAADAAAAAwAAAAMAAAAAAAAAAAH0A+gBAW3vGPIAAAAcZmRBVAAAAAQImWNgYPjvjcAM/7cgMMP//zAMAPqkDvLn1m3SAAAAAElFTkSuQmCC"
michael@0 296 };
michael@0 297
michael@0 298
michael@0 299 var apng3 = {
michael@0 300 // A 3x3 image with 4 frames. First frame is white, then 1x1 frames draw a diagonal line
michael@0 301 width : 3, height : 3, skipFirstFrame : false,
michael@0 302 format : Ci.imgIEncoder.INPUT_FORMAT_RGBA,
michael@0 303 transparency : null,
michael@0 304 plays : 0,
michael@0 305
michael@0 306 frames : [
michael@0 307 { // frame #1
michael@0 308 width : 3, height : 3,
michael@0 309 x_offset : 0, y_offset : 0,
michael@0 310 dispose : "none", blend : "source", delay : 500,
michael@0 311
michael@0 312 format : Ci.imgIEncoder.INPUT_FORMAT_RGBA, stride : 12,
michael@0 313
michael@0 314 pixels : [
michael@0 315
michael@0 316 255,255,255,255, 255,255,255,255, 255,255,255,255,
michael@0 317 255,255,255,255, 255,255,255,255, 255,255,255,255,
michael@0 318 255,255,255,255, 255,255,255,255, 255,255,255,255
michael@0 319 ]
michael@0 320 },
michael@0 321
michael@0 322 { // frame #2
michael@0 323 width : 1, height : 1,
michael@0 324 x_offset : 0, y_offset : 0,
michael@0 325 dispose : "none", blend : "source", delay : 500,
michael@0 326
michael@0 327 format : Ci.imgIEncoder.INPUT_FORMAT_RGBA, stride : 12,
michael@0 328
michael@0 329 pixels : [
michael@0 330 0,0,0,255
michael@0 331 ]
michael@0 332 },
michael@0 333
michael@0 334 { // frame #3
michael@0 335 width : 1, height : 1,
michael@0 336 x_offset : 1, y_offset : 1,
michael@0 337 dispose : "none", blend : "source", delay : 500,
michael@0 338
michael@0 339 format : Ci.imgIEncoder.INPUT_FORMAT_RGBA, stride : 12,
michael@0 340
michael@0 341 pixels : [
michael@0 342 0,0,0,255
michael@0 343 ]
michael@0 344 },
michael@0 345
michael@0 346 { // frame #4
michael@0 347 width : 1, height : 1,
michael@0 348 x_offset : 2, y_offset : 2,
michael@0 349 dispose : "none", blend : "source", delay : 500,
michael@0 350
michael@0 351 format : Ci.imgIEncoder.INPUT_FORMAT_RGBA, stride : 12,
michael@0 352
michael@0 353 pixels : [
michael@0 354 0,0,0,255
michael@0 355 ]
michael@0 356 }
michael@0 357 ],
michael@0 358
michael@0 359 expected : "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAMAAAADCAYAAABWKLW/AAAACGFjVEwAAAAEAAAAAHzNZtAAAAAaZmNUTAAAAAAAAAADAAAAAwAAAAAAAAAAAfQD6AAAdRYgGAAAAA5JREFUCJlj+I8EGHByALuHI91kueRqAAAAGmZjVEwAAAABAAAAAQAAAAEAAAAAAAAAAAH0A+gAADJXfawAAAARZmRBVAAAAAIImWNgYGD4DwABBAEAbr5mpgAAABpmY1RMAAAAAwAAAAEAAAABAAAAAQAAAAEB9APoAAC4OHoxAAAAEWZkQVQAAAAECJljYGBg+A8AAQQBAJZ8LRAAAAAaZmNUTAAAAAUAAAABAAAAAQAAAAIAAAACAfQD6AAA/fh01wAAABFmZEFUAAAABgiZY2BgYPgPAAEEAQB3Eum9AAAAAElFTkSuQmCC"
michael@0 360 };
michael@0 361
michael@0 362 // Main test entry point.
michael@0 363 function run_test() {
michael@0 364 dump("Checking apng1A...\n");
michael@0 365 run_test_for(apng1A);
michael@0 366 dump("Checking apng1B...\n");
michael@0 367 run_test_for(apng1B);
michael@0 368 dump("Checking apng1C...\n");
michael@0 369 run_test_for(apng1C);
michael@0 370
michael@0 371 dump("Checking apng2A...\n");
michael@0 372 run_test_for(apng2A);
michael@0 373 dump("Checking apng2B...\n");
michael@0 374 run_test_for(apng2B);
michael@0 375
michael@0 376 dump("Checking apng3...\n");
michael@0 377 run_test_for(apng3);
michael@0 378 };
michael@0 379
michael@0 380
michael@0 381 function run_test_for(input) {
michael@0 382 var encoder, dataURL;
michael@0 383
michael@0 384 encoder = encodeImage(input);
michael@0 385 dataURL = makeDataURL(encoder, "image/png");
michael@0 386 do_check_eq(dataURL, input.expected);
michael@0 387 };
michael@0 388
michael@0 389
michael@0 390 function encodeImage(input) {
michael@0 391 var encoder = Cc["@mozilla.org/image/encoder;2?type=image/png"].createInstance();
michael@0 392 encoder.QueryInterface(Ci.imgIEncoder);
michael@0 393
michael@0 394 var options = "";
michael@0 395 if (input.transparency) { options += "transparency=" + input.transparency; }
michael@0 396 options += ";frames=" + input.frames.length;
michael@0 397 options += ";skipfirstframe=" + (input.skipFirstFrame ? "yes" : "no");
michael@0 398 options += ";plays=" + input.plays;
michael@0 399 encoder.startImageEncode(input.width, input.height, input.format, options);
michael@0 400
michael@0 401 for (var i = 0; i < input.frames.length; i++) {
michael@0 402 var frame = input.frames[i];
michael@0 403
michael@0 404 options = "";
michael@0 405 if (frame.transparency) { options += "transparency=" + input.transparency; }
michael@0 406 options += ";delay=" + frame.delay;
michael@0 407 options += ";dispose=" + frame.dispose;
michael@0 408 options += ";blend=" + frame.blend;
michael@0 409 if (frame.x_offset > 0) { options += ";xoffset=" + frame.x_offset; }
michael@0 410 if (frame.y_offset > 0) { options += ";yoffset=" + frame.y_offset; }
michael@0 411
michael@0 412 encoder.addImageFrame(frame.pixels, frame.pixels.length,
michael@0 413 frame.width, frame.height, frame.stride, frame.format, options);
michael@0 414 }
michael@0 415
michael@0 416 encoder.endImageEncode();
michael@0 417
michael@0 418 return encoder;
michael@0 419 }
michael@0 420
michael@0 421
michael@0 422 function makeDataURL(encoder, mimetype) {
michael@0 423 var rawStream = encoder.QueryInterface(Ci.nsIInputStream);
michael@0 424
michael@0 425 var stream = Cc["@mozilla.org/binaryinputstream;1"].createInstance();
michael@0 426 stream.QueryInterface(Ci.nsIBinaryInputStream);
michael@0 427
michael@0 428 stream.setInputStream(rawStream);
michael@0 429
michael@0 430 var bytes = stream.readByteArray(stream.available()); // returns int[]
michael@0 431
michael@0 432 var base64String = toBase64(bytes);
michael@0 433
michael@0 434 return "data:" + mimetype + ";base64," + base64String;
michael@0 435 }
michael@0 436
michael@0 437 /* toBase64 copied from extensions/xml-rpc/src/nsXmlRpcClient.js */
michael@0 438
michael@0 439 /* Convert data (an array of integers) to a Base64 string. */
michael@0 440 const toBase64Table = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz' +
michael@0 441 '0123456789+/';
michael@0 442 const base64Pad = '=';
michael@0 443 function toBase64(data) {
michael@0 444 var result = '';
michael@0 445 var length = data.length;
michael@0 446 var i;
michael@0 447 // Convert every three bytes to 4 ascii characters.
michael@0 448 for (i = 0; i < (length - 2); i += 3) {
michael@0 449 result += toBase64Table[data[i] >> 2];
michael@0 450 result += toBase64Table[((data[i] & 0x03) << 4) + (data[i+1] >> 4)];
michael@0 451 result += toBase64Table[((data[i+1] & 0x0f) << 2) + (data[i+2] >> 6)];
michael@0 452 result += toBase64Table[data[i+2] & 0x3f];
michael@0 453 }
michael@0 454
michael@0 455 // Convert the remaining 1 or 2 bytes, pad out to 4 characters.
michael@0 456 if (length%3) {
michael@0 457 i = length - (length%3);
michael@0 458 result += toBase64Table[data[i] >> 2];
michael@0 459 if ((length%3) == 2) {
michael@0 460 result += toBase64Table[((data[i] & 0x03) << 4) + (data[i+1] >> 4)];
michael@0 461 result += toBase64Table[(data[i+1] & 0x0f) << 2];
michael@0 462 result += base64Pad;
michael@0 463 } else {
michael@0 464 result += toBase64Table[(data[i] & 0x03) << 4];
michael@0 465 result += base64Pad + base64Pad;
michael@0 466 }
michael@0 467 }
michael@0 468
michael@0 469 return result;
michael@0 470 }

mercurial