OLD | NEW |
(Empty) | |
| 1 <!-- |
| 2 Copyright (c) 2011 The Chromium Authors. All rights reserved. |
| 3 Use of this source code is governed by a BSD-style license that can be |
| 4 found in the LICENSE file. |
| 5 --> |
| 6 <!DOCTYPE html> |
| 7 <html> |
| 8 <head> |
| 9 <meta charset="utf-8"> |
| 10 <link rel="stylesheet" href="../../resources/js-test-style.css"/> |
| 11 <script src="../../resources/js-test-pre.js"></script> |
| 12 <script src="../resources/pnglib.js"></script> |
| 13 <script src="../resources/webgl-test.js"></script> |
| 14 <script src="../resources/webgl-test-utils.js"></script> |
| 15 |
| 16 <script> |
| 17 var wtu = WebGLTestUtils; |
| 18 var gl = null; |
| 19 var textureLoc = null; |
| 20 var successfullyParsed = false; |
| 21 |
| 22 //---------------------------------------------------------------------- |
| 23 // Harness |
| 24 |
| 25 var testCases = []; |
| 26 |
| 27 var DataMode = { |
| 28 IMAGE: 0, |
| 29 IMAGE_DATA: 1, |
| 30 |
| 31 NUM_HTML_MODES: 2, |
| 32 |
| 33 RAW_DATA: 2, |
| 34 |
| 35 // This must remain the last mode. |
| 36 NUM_MODES: 3 |
| 37 }; |
| 38 |
| 39 function init() |
| 40 { |
| 41 if (window.initNonKhronosFramework) { |
| 42 window.initNonKhronosFramework(true); |
| 43 } |
| 44 |
| 45 description('Verify texImage2D and texSubImage2D code paths taking both HTML
and user-specified data with all format/type combinations'); |
| 46 |
| 47 var canvas = document.getElementById("example"); |
| 48 gl = wtu.create3DContext(canvas); |
| 49 gl.disable(gl.DITHER); |
| 50 var program = wtu.setupTexturedQuad(gl); |
| 51 |
| 52 gl.clearColor(0,0,0,1); |
| 53 gl.clearDepth(1); |
| 54 gl.disable(gl.BLEND); |
| 55 |
| 56 textureLoc = gl.getUniformLocation(program, "tex"); |
| 57 |
| 58 initializeTests(); |
| 59 } |
| 60 |
| 61 function initializeTests() |
| 62 { |
| 63 // Verify that uploading to packed pixel formats performs the |
| 64 // required conversion and associated loss of precision. |
| 65 for (var dataMode = 0; dataMode < DataMode.NUM_HTML_MODES; ++dataMode) { |
| 66 for (var useTexSubImage2D = 0; useTexSubImage2D < 2; ++useTexSubImage2D)
{ |
| 67 testCases.push({ |
| 68 dataMode: dataMode, |
| 69 useTexSubImage2D: !!useTexSubImage2D, |
| 70 width: 256, |
| 71 height: 1, |
| 72 generator: generateOpaqueGrayscaleRamp, |
| 73 premultiplyAlpha: false, |
| 74 format: gl.RGBA, |
| 75 type: gl.UNSIGNED_BYTE, |
| 76 verifier: allChannelsIncreaseByNoMoreThan, |
| 77 threshold: 1, |
| 78 numOccurrences: 1, |
| 79 description: "RGBA/UNSIGNED_BYTE should maintain full precision
of data" |
| 80 }); |
| 81 testCases.push({ |
| 82 dataMode: dataMode, |
| 83 useTexSubImage2D: !!useTexSubImage2D, |
| 84 width: 256, |
| 85 height: 1, |
| 86 generator: generateOpaqueGrayscaleRamp, |
| 87 premultiplyAlpha: false, |
| 88 format: gl.RGBA, |
| 89 type: gl.UNSIGNED_SHORT_4_4_4_4, |
| 90 verifier: allChannelsIncreaseByAtLeast, |
| 91 threshold: 15, |
| 92 numOccurrences: 10, |
| 93 description: "RGBA/UNSIGNED_SHORT_4_4_4_4 must drop low four bit
s of precision" |
| 94 }); |
| 95 testCases.push({ |
| 96 dataMode: dataMode, |
| 97 useTexSubImage2D: !!useTexSubImage2D, |
| 98 width: 256, |
| 99 height: 1, |
| 100 generator: generateOpaqueGrayscaleRamp, |
| 101 premultiplyAlpha: false, |
| 102 format: gl.RGBA, |
| 103 type: gl.UNSIGNED_SHORT_5_5_5_1, |
| 104 verifier: allChannelsIncreaseByAtLeast, |
| 105 threshold: 7, |
| 106 numOccurrences: 20, |
| 107 description: "RGBA/UNSIGNED_SHORT_5_5_5_1 must drop low three bi
ts of precision" |
| 108 }); |
| 109 testCases.push({ |
| 110 dataMode: dataMode, |
| 111 useTexSubImage2D: !!useTexSubImage2D, |
| 112 width: 256, |
| 113 height: 1, |
| 114 generator: generateOpaqueGrayscaleRamp, |
| 115 premultiplyAlpha: false, |
| 116 format: gl.RGB, |
| 117 type: gl.UNSIGNED_BYTE, |
| 118 verifier: allChannelsIncreaseByNoMoreThan, |
| 119 threshold: 1, |
| 120 numOccurrences: 1, |
| 121 description: "RGB/UNSIGNED_BYTE should maintain full precision o
f data" |
| 122 }); |
| 123 testCases.push({ |
| 124 dataMode: dataMode, |
| 125 useTexSubImage2D: !!useTexSubImage2D, |
| 126 width: 256, |
| 127 height: 1, |
| 128 generator: generateOpaqueGrayscaleRamp, |
| 129 premultiplyAlpha: false, |
| 130 format: gl.RGB, |
| 131 type: gl.UNSIGNED_SHORT_5_6_5, |
| 132 verifier: allChannelsIncreaseByAtLeast, |
| 133 threshold: 3, |
| 134 numOccurrences: 20, |
| 135 description: "RGB/UNSIGNED_SHORT_5_6_5 must drop low two or thre
e bits of precision" |
| 136 }); |
| 137 testCases.push({ |
| 138 dataMode: dataMode, |
| 139 useTexSubImage2D: !!useTexSubImage2D, |
| 140 width: 256, |
| 141 height: 1, |
| 142 generator: generateTranslucentGrayscaleRamp, |
| 143 premultiplyAlpha: false, |
| 144 format: gl.ALPHA, |
| 145 type: gl.UNSIGNED_BYTE, |
| 146 verifier: alphaChannelIncreasesByNoMoreThan, |
| 147 threshold: 1, |
| 148 numOccurrences: 1, |
| 149 description: "ALPHA/UNSIGNED_BYTE should maintain full precision
of data" |
| 150 }); |
| 151 testCases.push({ |
| 152 dataMode: dataMode, |
| 153 useTexSubImage2D: !!useTexSubImage2D, |
| 154 width: 256, |
| 155 height: 1, |
| 156 generator: generateOpaqueGrayscaleRamp, |
| 157 premultiplyAlpha: false, |
| 158 format: gl.LUMINANCE, |
| 159 type: gl.UNSIGNED_BYTE, |
| 160 verifier: allChannelsIncreaseByNoMoreThan, |
| 161 threshold: 1, |
| 162 numOccurrences: 1, |
| 163 description: "LUMINANCE/UNSIGNED_BYTE should maintain full preci
sion of data" |
| 164 }); |
| 165 testCases.push({ |
| 166 dataMode: dataMode, |
| 167 useTexSubImage2D: !!useTexSubImage2D, |
| 168 width: 256, |
| 169 height: 1, |
| 170 generator: generateOpaqueGrayscaleRamp, |
| 171 premultiplyAlpha: false, |
| 172 format: gl.LUMINANCE_ALPHA, |
| 173 type: gl.UNSIGNED_BYTE, |
| 174 verifier: allChannelsIncreaseByNoMoreThan, |
| 175 threshold: 1, |
| 176 numOccurrences: 1, |
| 177 description: "LUMINANCE_ALPHA/UNSIGNED_BYTE should maintain full
precision of data" |
| 178 }); |
| 179 } |
| 180 } |
| 181 |
| 182 // Verify that setting the UNPACK_PREMULTIPLY_ALPHA_WEBGL pixel |
| 183 // store parameter and sending down a zero alpha causes the color |
| 184 // channels to go to zero. |
| 185 for (var dataMode = 0; dataMode < DataMode.NUM_MODES; ++dataMode) { |
| 186 for (var useTexSubImage2D = 0; useTexSubImage2D < 2; ++useTexSubImage2D)
{ |
| 187 testCases.push({ |
| 188 dataMode: dataMode, |
| 189 useTexSubImage2D: !!useTexSubImage2D, |
| 190 width: 256, |
| 191 height: 1, |
| 192 generator: generateTransparentGrayscaleRamp, |
| 193 premultiplyAlpha: true, |
| 194 format: gl.RGBA, |
| 195 type: gl.UNSIGNED_BYTE, |
| 196 verifier: colorChannelsAreZero, |
| 197 description: "UNPACK_PREMULTIPLY_ALPHA_WEBGL with RGBA/UNSIGNED_
BYTE" |
| 198 }); |
| 199 testCases.push({ |
| 200 dataMode: dataMode, |
| 201 useTexSubImage2D: !!useTexSubImage2D, |
| 202 width: 256, |
| 203 height: 1, |
| 204 generator: generateTransparentGrayscaleRamp, |
| 205 premultiplyAlpha: true, |
| 206 format: gl.RGBA, |
| 207 type: gl.UNSIGNED_SHORT_4_4_4_4, |
| 208 verifier: colorChannelsAreZero, |
| 209 description: "UNPACK_PREMULTIPLY_ALPHA_WEBGL with RGBA/UNSIGNED_
SHORT_4_4_4_4" |
| 210 }); |
| 211 testCases.push({ |
| 212 dataMode: dataMode, |
| 213 useTexSubImage2D: !!useTexSubImage2D, |
| 214 width: 256, |
| 215 height: 1, |
| 216 generator: generateTransparentGrayscaleRamp, |
| 217 premultiplyAlpha: true, |
| 218 format: gl.RGBA, |
| 219 type: gl.UNSIGNED_SHORT_5_5_5_1, |
| 220 verifier: colorChannelsAreZero, |
| 221 description: "UNPACK_PREMULTIPLY_ALPHA_WEBGL with RGBA/UNSIGNED_
SHORT_5_5_5_1" |
| 222 }); |
| 223 // The following few tests are invalid for the raw data |
| 224 // mode because there is either no alpha channel or no |
| 225 // separate alpha channel. |
| 226 if (dataMode != DataMode.RAW_DATA) { |
| 227 testCases.push({ |
| 228 dataMode: dataMode, |
| 229 useTexSubImage2D: !!useTexSubImage2D, |
| 230 width: 256, |
| 231 height: 1, |
| 232 generator: generateTransparentGrayscaleRamp, |
| 233 premultiplyAlpha: true, |
| 234 format: gl.RGB, |
| 235 type: gl.UNSIGNED_BYTE, |
| 236 verifier: colorChannelsAreZero, |
| 237 description: "UNPACK_PREMULTIPLY_ALPHA_WEBGL with RGB/UNSIGN
ED_BYTE" |
| 238 }); |
| 239 testCases.push({ |
| 240 dataMode: dataMode, |
| 241 useTexSubImage2D: !!useTexSubImage2D, |
| 242 width: 256, |
| 243 height: 1, |
| 244 generator: generateTransparentGrayscaleRamp, |
| 245 premultiplyAlpha: true, |
| 246 format: gl.RGB, |
| 247 type: gl.UNSIGNED_SHORT_5_6_5, |
| 248 verifier: colorChannelsAreZero, |
| 249 description: "UNPACK_PREMULTIPLY_ALPHA_WEBGL with RGB/UNSIGN
ED_SHORT_5_6_5" |
| 250 }); |
| 251 testCases.push({ |
| 252 dataMode: dataMode, |
| 253 useTexSubImage2D: !!useTexSubImage2D, |
| 254 width: 256, |
| 255 height: 1, |
| 256 generator: generateTransparentGrayscaleRamp, |
| 257 premultiplyAlpha: true, |
| 258 format: gl.ALPHA, |
| 259 type: gl.UNSIGNED_BYTE, |
| 260 verifier: colorChannelsAreZero, |
| 261 description: "UNPACK_PREMULTIPLY_ALPHA_WEBGL with ALPHA/UNSI
GNED_BYTE" |
| 262 }); |
| 263 testCases.push({ |
| 264 dataMode: dataMode, |
| 265 useTexSubImage2D: !!useTexSubImage2D, |
| 266 width: 256, |
| 267 height: 1, |
| 268 generator: generateTransparentGrayscaleRamp, |
| 269 premultiplyAlpha: true, |
| 270 format: gl.LUMINANCE, |
| 271 type: gl.UNSIGNED_BYTE, |
| 272 verifier: colorChannelsAreZero, |
| 273 description: "UNPACK_PREMULTIPLY_ALPHA_WEBGL with LUMINANCE/
UNSIGNED_BYTE" |
| 274 }); |
| 275 } |
| 276 testCases.push({ |
| 277 dataMode: dataMode, |
| 278 useTexSubImage2D: !!useTexSubImage2D, |
| 279 width: 256, |
| 280 height: 1, |
| 281 generator: generateTransparentGrayscaleRamp, |
| 282 premultiplyAlpha: true, |
| 283 format: gl.LUMINANCE_ALPHA, |
| 284 type: gl.UNSIGNED_BYTE, |
| 285 verifier: colorChannelsAreZero, |
| 286 description: "UNPACK_PREMULTIPLY_ALPHA_WEBGL with LUMINANCE_ALPH
A/UNSIGNED_BYTE" |
| 287 }); |
| 288 } |
| 289 } |
| 290 |
| 291 // Produce data for all testcases. Because we load images, some of |
| 292 // these may generate their data asynchronously. |
| 293 generateTestData(); |
| 294 } |
| 295 |
| 296 function generateTestData() |
| 297 { |
| 298 for (var i = 0; i < testCases.length; i++) { |
| 299 var testCase = testCases[i]; |
| 300 var wrapper = null; |
| 301 switch (testCase.dataMode) { |
| 302 case DataMode.IMAGE: |
| 303 wrapper = new ImageWrapper(testCase.width, testCase.height); |
| 304 break; |
| 305 case DataMode.IMAGE_DATA: |
| 306 wrapper = new ImageDataWrapper(testCase.width, testCase.height); |
| 307 break; |
| 308 case DataMode.RAW_DATA: |
| 309 switch (testCase.type) { |
| 310 case gl.UNSIGNED_BYTE: |
| 311 switch (testCase.format) { |
| 312 case gl.RGBA: |
| 313 wrapper = new RGBA8DataWrapper(testCase.width, testCase.heig
ht); |
| 314 break; |
| 315 case gl.RGB: |
| 316 wrapper = new RGB8DataWrapper(testCase.width, testCase.heigh
t); |
| 317 break; |
| 318 case gl.ALPHA: |
| 319 wrapper = new A8DataWrapper(testCase.width, testCase.height)
; |
| 320 break; |
| 321 case gl.LUMINANCE: |
| 322 wrapper = new L8DataWrapper(testCase.width, testCase.height)
; |
| 323 break; |
| 324 case gl.LUMINANCE_ALPHA: |
| 325 wrapper = new LA8DataWrapper(testCase.width, testCase.height
); |
| 326 break; |
| 327 } |
| 328 break; |
| 329 case gl.UNSIGNED_SHORT_4_4_4_4: |
| 330 wrapper = new RGBA4444DataWrapper(testCase.width, testCase.heigh
t); |
| 331 break; |
| 332 case gl.UNSIGNED_SHORT_5_5_5_1: |
| 333 wrapper = new RGBA5551DataWrapper(testCase.width, testCase.heigh
t); |
| 334 break; |
| 335 case gl.UNSIGNED_SHORT_5_6_5: |
| 336 wrapper = new RGB565DataWrapper(testCase.width, testCase.height)
; |
| 337 break; |
| 338 } |
| 339 } |
| 340 testCase.wrapper = wrapper; |
| 341 testCase.generator(wrapper); |
| 342 testCase.wrapper.generateData(); |
| 343 } |
| 344 |
| 345 // See whether we need to run the tests, in case all of them |
| 346 // generated their results synchronously. |
| 347 maybeRunTests(); |
| 348 } |
| 349 |
| 350 var ranTests = false; |
| 351 |
| 352 function maybeRunTests() |
| 353 { |
| 354 if (!ranTests) |
| 355 for (var i = 0; i < testCases.length; ++i) |
| 356 if (!testCases[i].wrapper || !testCases[i].wrapper.data) |
| 357 return; |
| 358 |
| 359 ranTests = true; |
| 360 |
| 361 for (var i = 0; i < testCases.length; ++i) |
| 362 runOneTest(testCases[i]); |
| 363 |
| 364 finishTest(); |
| 365 } |
| 366 |
| 367 function testCaseToString(testCase) |
| 368 { |
| 369 var mode; |
| 370 switch (testCase.dataMode) { |
| 371 case DataMode.IMAGE: |
| 372 mode = "Image"; |
| 373 break; |
| 374 case DataMode.IMAGE_DATA: |
| 375 mode = "ImageData"; |
| 376 break; |
| 377 case DataMode.RAW_DATA: |
| 378 mode = "raw data"; |
| 379 break; |
| 380 } |
| 381 return (testCase.useTexSubImage2D ? "texSubImage2D" : "texImage2D") + |
| 382 " with " + mode + " at " + testCase.width + "x" + testCase.height; |
| 383 } |
| 384 |
| 385 function runOneTest(testCase) |
| 386 { |
| 387 debug("Testing " + testCaseToString(testCase)); |
| 388 var data = testCase.wrapper.data; |
| 389 gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT); |
| 390 var texture = gl.createTexture(); |
| 391 // Bind the texture to texture unit 0. |
| 392 gl.bindTexture(gl.TEXTURE_2D, texture); |
| 393 // Set up texture parameters. |
| 394 gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST); |
| 395 gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST); |
| 396 // Set up pixel store parameters. |
| 397 gl.pixelStorei(gl.UNPACK_PREMULTIPLY_ALPHA_WEBGL, testCase.premultiplyAlpha)
; |
| 398 // Upload the image into the texture. |
| 399 if (testCase.useTexSubImage2D) { |
| 400 // Initialize the texture to black first. |
| 401 gl.texImage2D(gl.TEXTURE_2D, 0, testCase.format, testCase.width, testCas
e.height, 0, |
| 402 testCase.format, testCase.type, null); |
| 403 } |
| 404 switch (testCase.dataMode) { |
| 405 case DataMode.IMAGE: |
| 406 case DataMode.IMAGE_DATA: |
| 407 if (testCase.useTexSubImage2D) |
| 408 gl.texSubImage2D(gl.TEXTURE_2D, 0, 0, 0, testCase.format, testCase.t
ype, data); |
| 409 else |
| 410 gl.texImage2D(gl.TEXTURE_2D, 0, testCase.format, testCase.format, te
stCase.type, data); |
| 411 break; |
| 412 case DataMode.RAW_DATA: |
| 413 if (testCase.useTexSubImage2D) |
| 414 gl.texSubImage2D(gl.TEXTURE_2D, 0, 0, 0, testCase.width, testCase.he
ight, testCase.format, testCase.type, data); |
| 415 else |
| 416 gl.texImage2D(gl.TEXTURE_2D, 0, testCase.format, testCase.width, tes
tCase.height, 0, testCase.format, testCase.type, data); |
| 417 break; |
| 418 } |
| 419 // Point the uniform sampler to texture unit 0. |
| 420 gl.uniform1i(textureLoc, 0); |
| 421 // Draw the triangles. |
| 422 gl.drawArrays(gl.TRIANGLES, 0, 6); |
| 423 // Clean up the texture. |
| 424 gl.deleteTexture(texture); |
| 425 |
| 426 // Read back the rendering results. |
| 427 buf = new Uint8Array(testCase.width * testCase.height * 4); |
| 428 gl.readPixels(0, 0, testCase.width, testCase.height, gl.RGBA, gl.UNSIGNED_BY
TE, buf); |
| 429 // Run the verification routine. |
| 430 if (testCase.verifier(buf, testCase.threshold, testCase.numOccurrences)) |
| 431 testPassed(testCase.description); |
| 432 else |
| 433 testFailed(testCase.description); |
| 434 } |
| 435 |
| 436 //---------------------------------------------------------------------- |
| 437 // Wrappers for programmatic construction of Image, ImageData and raw texture da
ta |
| 438 // |
| 439 |
| 440 function ImageWrapper(width, height) |
| 441 { |
| 442 this.pngBuilder_ = new PNGlib(width, height, 256); |
| 443 } |
| 444 |
| 445 ImageWrapper.prototype.getWidth = function() { |
| 446 return this.pngBuilder_.width; |
| 447 }; |
| 448 |
| 449 ImageWrapper.prototype.getHeight = function() { |
| 450 return this.pngBuilder_.height; |
| 451 }; |
| 452 |
| 453 ImageWrapper.prototype.setPixel = function(x, y, r, g, b, a) { |
| 454 this.pngBuilder_.buffer[this.pngBuilder_.index(x, y)] = this.pngBuilder_.col
or(r, g, b, a); |
| 455 }; |
| 456 |
| 457 // Generates data into "data" property, possibly asynchronously. |
| 458 ImageWrapper.prototype.generateData = function() { |
| 459 var that = this; |
| 460 var img = new Image(); |
| 461 img.onload = function() { |
| 462 that.data = img; |
| 463 maybeRunTests(); |
| 464 }; |
| 465 img.src = "data:image/png;base64," + this.pngBuilder_.getBase64(); |
| 466 }; |
| 467 |
| 468 function ImageDataWrapper(width, height) |
| 469 { |
| 470 if (!ImageDataWrapper.tempCanvas) { |
| 471 ImageDataWrapper.tempCanvas = document.createElement("canvas"); |
| 472 } |
| 473 this.imageData_ = ImageDataWrapper.tempCanvas.getContext("2d").createImageDa
ta(width, height); |
| 474 } |
| 475 |
| 476 ImageDataWrapper.tempCanvas = null; |
| 477 |
| 478 ImageDataWrapper.prototype.getWidth = function() { |
| 479 return this.imageData_.width; |
| 480 }; |
| 481 |
| 482 ImageDataWrapper.prototype.getHeight = function() { |
| 483 return this.imageData_.height; |
| 484 }; |
| 485 |
| 486 ImageDataWrapper.prototype.setPixel = function(x, y, r, g, b, a) { |
| 487 var index = 4 * (this.imageData_.width * y + x); |
| 488 this.imageData_.data[index] = r; |
| 489 this.imageData_.data[index + 1] = g; |
| 490 this.imageData_.data[index + 2] = b; |
| 491 this.imageData_.data[index + 3] = a; |
| 492 }; |
| 493 |
| 494 ImageDataWrapper.prototype.generateData = function() { |
| 495 this.data = this.imageData_; |
| 496 maybeRunTests(); |
| 497 }; |
| 498 |
| 499 function TextureDataWrapper(width, height) |
| 500 { |
| 501 this.width_ = width; |
| 502 this.height_ = height; |
| 503 } |
| 504 |
| 505 TextureDataWrapper.prototype.getWidth = function() { |
| 506 return this.width_; |
| 507 }; |
| 508 |
| 509 TextureDataWrapper.prototype.getHeight = function() { |
| 510 return this.height_; |
| 511 }; |
| 512 |
| 513 TextureDataWrapper.prototype.generateData = function() { |
| 514 this.data = this.data_; |
| 515 maybeRunTests(); |
| 516 }; |
| 517 |
| 518 function RGBA8DataWrapper(width, height) |
| 519 { |
| 520 TextureDataWrapper.call(this, width, height); |
| 521 this.data_ = new Uint8Array(4 * width * height); |
| 522 } |
| 523 |
| 524 RGBA8DataWrapper.prototype = new TextureDataWrapper; |
| 525 |
| 526 RGBA8DataWrapper.prototype.setPixel = function(x, y, r, g, b, a) { |
| 527 var index = 4 * (this.width_ * y + x); |
| 528 this.data_[index] = r; |
| 529 this.data_[index + 1] = g; |
| 530 this.data_[index + 2] = b; |
| 531 this.data_[index + 3] = a; |
| 532 }; |
| 533 |
| 534 function RGBA5551DataWrapper(width, height) |
| 535 { |
| 536 TextureDataWrapper.call(this, width, height); |
| 537 this.data_ = new Uint16Array(width * height); |
| 538 } |
| 539 |
| 540 RGBA5551DataWrapper.prototype = new TextureDataWrapper; |
| 541 |
| 542 RGBA5551DataWrapper.prototype.setPixel = function(x, y, r, g, b, a) { |
| 543 var value = (((r & 0xF8) << 8) |
| 544 | ((g & 0xF8) << 3) |
| 545 | ((b & 0xF8) >> 2) |
| 546 | (a >> 7)); |
| 547 this.data_[this.width_ * y + x] = value; |
| 548 }; |
| 549 |
| 550 function RGBA4444DataWrapper(width, height) |
| 551 { |
| 552 TextureDataWrapper.call(this, width, height); |
| 553 this.data_ = new Uint16Array(width * height); |
| 554 } |
| 555 |
| 556 RGBA4444DataWrapper.prototype = new TextureDataWrapper; |
| 557 |
| 558 RGBA4444DataWrapper.prototype.setPixel = function(x, y, r, g, b, a) { |
| 559 var value = (((r & 0xF0) << 8) |
| 560 | ((g & 0xF0) << 4) |
| 561 | (b & 0xF0) |
| 562 | (a >> 4)); |
| 563 this.data_[this.width_ * y + x] = value; |
| 564 }; |
| 565 |
| 566 function RGB8DataWrapper(width, height) |
| 567 { |
| 568 TextureDataWrapper.call(this, width, height); |
| 569 this.data_ = new Uint8Array(3 * width * height); |
| 570 } |
| 571 |
| 572 RGB8DataWrapper.prototype = new TextureDataWrapper; |
| 573 |
| 574 RGB8DataWrapper.prototype.setPixel = function(x, y, r, g, b, a) { |
| 575 var index = 3 * (this.width_ * y + x); |
| 576 this.data_[index] = r; |
| 577 this.data_[index + 1] = g; |
| 578 this.data_[index + 2] = b; |
| 579 }; |
| 580 |
| 581 function RGB565DataWrapper(width, height) |
| 582 { |
| 583 TextureDataWrapper.call(this, width, height); |
| 584 this.data_ = new Uint16Array(width * height); |
| 585 } |
| 586 |
| 587 RGB565DataWrapper.prototype = new TextureDataWrapper; |
| 588 |
| 589 RGB565DataWrapper.prototype.setPixel = function(x, y, r, g, b, a) { |
| 590 var value = (((r & 0xF8) << 8) |
| 591 | ((g & 0xFC) << 3) |
| 592 | ((b & 0xF8) >> 3)); |
| 593 this.data_[this.width_ * y + x] = value; |
| 594 }; |
| 595 |
| 596 function A8DataWrapper(width, height) |
| 597 { |
| 598 TextureDataWrapper.call(this, width, height); |
| 599 this.data_ = new Uint8Array(width * height); |
| 600 } |
| 601 |
| 602 A8DataWrapper.prototype = new TextureDataWrapper; |
| 603 |
| 604 A8DataWrapper.prototype.setPixel = function(x, y, r, g, b, a) { |
| 605 this.data_[this.width_ * y + x] = a; |
| 606 }; |
| 607 |
| 608 function L8DataWrapper(width, height) |
| 609 { |
| 610 TextureDataWrapper.call(this, width, height); |
| 611 this.data_ = new Uint8Array(width * height); |
| 612 } |
| 613 |
| 614 L8DataWrapper.prototype = new TextureDataWrapper; |
| 615 |
| 616 L8DataWrapper.prototype.setPixel = function(x, y, r, g, b, a) { |
| 617 this.data_[this.width_ * y + x] = r; |
| 618 }; |
| 619 |
| 620 function LA8DataWrapper(width, height) |
| 621 { |
| 622 TextureDataWrapper.call(this, width, height); |
| 623 this.data_ = new Uint8Array(2 * width * height); |
| 624 } |
| 625 |
| 626 LA8DataWrapper.prototype = new TextureDataWrapper; |
| 627 |
| 628 LA8DataWrapper.prototype.setPixel = function(x, y, r, g, b, a) { |
| 629 var index = 2 * (this.width_ * y + x); |
| 630 this.data_[index] = r; |
| 631 this.data_[index + 1] = a; |
| 632 }; |
| 633 |
| 634 //---------------------------------------------------------------------- |
| 635 // Color ramp generation functions |
| 636 // |
| 637 |
| 638 function generateOpaqueGrayscaleRamp(wrapper) |
| 639 { |
| 640 var width = wrapper.getWidth(); |
| 641 var height = wrapper.getHeight(); |
| 642 for (var x = 0; x < width; ++x) { |
| 643 var value = Math.round(255.0 * x / width); |
| 644 for (var y = 0; y < height; ++y) |
| 645 wrapper.setPixel(x, y, value, value, value, 255); |
| 646 } |
| 647 } |
| 648 |
| 649 function generateTranslucentGrayscaleRamp(wrapper) |
| 650 { |
| 651 var width = wrapper.getWidth(); |
| 652 var height = wrapper.getHeight(); |
| 653 for (var x = 0; x < width; ++x) { |
| 654 var value = Math.round(255.0 * x / width); |
| 655 for (var y = 0; y < height; ++y) |
| 656 wrapper.setPixel(x, y, value, value, value, value); |
| 657 } |
| 658 } |
| 659 |
| 660 function generateTransparentGrayscaleRamp(wrapper) |
| 661 { |
| 662 var width = wrapper.getWidth(); |
| 663 var height = wrapper.getHeight(); |
| 664 for (var x = 0; x < width; ++x) { |
| 665 var value = Math.round(255.0 * x / width); |
| 666 for (var y = 0; y < height; ++y) |
| 667 wrapper.setPixel(x, y, value, value, value, 0); |
| 668 } |
| 669 } |
| 670 |
| 671 //---------------------------------------------------------------------- |
| 672 // Verification routines |
| 673 // |
| 674 |
| 675 function allChannelsIncreaseByNoMoreThan(array, threshold, numOccurrences) { |
| 676 var numFound = 0; |
| 677 for (var i = 4; i < array.length; i += 4) |
| 678 for (var j = 0; j < 4; j++) |
| 679 if (array[i + j] - array[i + j - 4] > threshold) |
| 680 ++numFound; |
| 681 |
| 682 return numFound < numOccurrences; |
| 683 } |
| 684 |
| 685 function alphaChannelIncreasesByNoMoreThan(array, threshold, numOccurrences) { |
| 686 var numFound = 0; |
| 687 for (var i = 7; i < array.length; i += 4) |
| 688 if (array[i] - array[i - 4] > threshold) |
| 689 ++numFound; |
| 690 |
| 691 return numFound < numOccurrences; |
| 692 } |
| 693 |
| 694 function allChannelsIncreaseByAtLeast(array, threshold, numOccurrences) { |
| 695 var numFound = 0; |
| 696 for (var i = 4; i < array.length; i += 4) |
| 697 for (var j = 0; j < 4; ++j) |
| 698 if (array[i + j] - array[i + j - 4] > threshold) |
| 699 ++numFound; |
| 700 |
| 701 return numFound > numOccurrences; |
| 702 } |
| 703 |
| 704 function colorChannelsAreZero(array, threshold, numOccurrences) { |
| 705 var passed = true; |
| 706 var numFailures = 0; |
| 707 |
| 708 for (var i = 4; i < array.length; i += 4) |
| 709 for (var j = 0; j < 3; ++j) |
| 710 if (array[i + j] != 0) { |
| 711 passed = false; |
| 712 if (++numFailures <= 5) |
| 713 debug(" array[" + (i + j) + "] should have been 0, was " +
array[i + j]); |
| 714 } |
| 715 |
| 716 return passed; |
| 717 } |
| 718 |
| 719 </script> |
| 720 </head> |
| 721 <body onload="init()"> |
| 722 <canvas id="example" width="256px" height="1px"></canvas> |
| 723 <div id="description"></div> |
| 724 <div id="console"></div> |
| 725 </body> |
| 726 </html> |
OLD | NEW |