reftest-analyzer.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587
  1. /*
  2. Copyright 2012 Mozilla Foundation
  3. Version: MPL 1.1/GPL 2.0/LGPL 2.1
  4. The contents of this file are subject to the Mozilla Public License Version
  5. 1.1 (the "License"); you may not use this file except in compliance with
  6. the License. You may obtain a copy of the License at
  7. http://www.mozilla.org/MPL
  8. Software distributed under the License is distributed on an "AS IS" basis,
  9. WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  10. for the specific language governing rights and limitations under the
  11. License.
  12. Alternatively, the contents of this file may be used under the terms of
  13. either the GNU General Public License Version 2 or later (the "GPL"), or
  14. the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
  15. in which case the provisions of the GPL or the LGPL are applicable instead
  16. of those above. If you wish to allow use of your version of this file only
  17. under the terms of either the GPL or the LGPL, and not to allow others to
  18. use your version of this file under the terms of the MPL, indicate your
  19. decision by deleting the provisions above and replace them with the notice
  20. and other provisions required by the LGPL or the GPL. If you do not delete
  21. the provisions above, a recipient may use your version of this file under
  22. the terms of any one of the MPL, the GPL or the LGPL.
  23. Original author: L. David Baron <dbaron@dbaron.org>
  24. */
  25. // Global variables
  26. const XLINK_NS = "http://www.w3.org/1999/xlink";
  27. const SVG_NS = "http://www.w3.org/2000/svg";
  28. let gPhases = null;
  29. const gMagPixPaths = []; // 2D array of array-of-two <path> objects used in the pixel magnifier
  30. const gMagWidth = 5; // number of zoomed in pixels to show horizontally
  31. const gMagHeight = 5; // number of zoomed in pixels to show vertically
  32. const gMagZoom = 16; // size of the zoomed in pixels
  33. let gImage1Data = null; // ImageData object for the test output image
  34. let gImage2Data = null; // ImageData object for the reference image
  35. const gFlashingPixels = []; // array of <path> objects that should be flashed due to pixel color mismatch
  36. let gPath = ""; // path taken from #web= and prepended to ref/snp urls
  37. let gSelected = null; // currently selected comparison
  38. window.onload = function () {
  39. load();
  40. function ID(id) {
  41. return document.getElementById(id);
  42. }
  43. function hashParameters() {
  44. const query = window.location.hash.substring(1);
  45. const params = new Map();
  46. for (const [key, value] of new URLSearchParams(query)) {
  47. params.set(key.toLowerCase(), value);
  48. }
  49. return params;
  50. }
  51. function load() {
  52. gPhases = [ID("entry"), ID("loading"), ID("viewer")];
  53. buildMag();
  54. const params = hashParameters();
  55. if (params.has("log")) {
  56. ID("logEntry").value = params.get("log");
  57. logPasted();
  58. } else if (params.has("web")) {
  59. loadFromWeb(params.get("web"));
  60. }
  61. ID("logEntry").focus();
  62. }
  63. function buildMag() {
  64. const mag = ID("mag");
  65. const r = document.createElementNS(SVG_NS, "rect");
  66. r.setAttribute("x", (gMagZoom * -gMagWidth) / 2);
  67. r.setAttribute("y", (gMagZoom * -gMagHeight) / 2);
  68. r.setAttribute("width", gMagZoom * gMagWidth);
  69. r.setAttribute("height", gMagZoom * gMagHeight);
  70. mag.append(r);
  71. mag.setAttribute(
  72. "transform",
  73. "translate(" +
  74. (gMagZoom * (gMagWidth / 2) + 1) +
  75. "," +
  76. (gMagZoom * (gMagHeight / 2) + 1) +
  77. ")"
  78. );
  79. for (let x = 0; x < gMagWidth; x++) {
  80. gMagPixPaths[x] = [];
  81. for (let y = 0; y < gMagHeight; y++) {
  82. const p1 = document.createElementNS(SVG_NS, "path");
  83. p1.setAttribute(
  84. "d",
  85. "M" +
  86. (x - gMagWidth / 2 + 1) * gMagZoom +
  87. "," +
  88. (y - gMagHeight / 2) * gMagZoom +
  89. "h" +
  90. -gMagZoom +
  91. "v" +
  92. gMagZoom
  93. );
  94. p1.setAttribute("stroke", "#CCC");
  95. p1.setAttribute("stroke-width", "1px");
  96. p1.setAttribute("fill", "#aaa");
  97. const p2 = document.createElementNS(SVG_NS, "path");
  98. p2.setAttribute(
  99. "d",
  100. "M" +
  101. (x - gMagWidth / 2 + 1) * gMagZoom +
  102. "," +
  103. (y - gMagHeight / 2) * gMagZoom +
  104. "v" +
  105. gMagZoom +
  106. "h" +
  107. -gMagZoom
  108. );
  109. p2.setAttribute("stroke", "#CCC");
  110. p2.setAttribute("stroke-width", "1px");
  111. p2.setAttribute("fill", "#888");
  112. mag.append(p1, p2);
  113. gMagPixPaths[x][y] = [p1, p2];
  114. }
  115. }
  116. let flashedOn = false;
  117. setInterval(function () {
  118. flashedOn = !flashedOn;
  119. flashPixels(flashedOn);
  120. }, 500);
  121. }
  122. function showPhase(phaseId) {
  123. for (const i in gPhases) {
  124. const phase = gPhases[i];
  125. phase.style.display = phase.id === phaseId ? "block" : "none";
  126. }
  127. if (phaseId === "viewer") {
  128. ID("images").style.display = "none";
  129. }
  130. }
  131. async function loadFromWeb(url) {
  132. const lastSlash = url.lastIndexOf("/");
  133. if (lastSlash) {
  134. gPath = url.substring(0, lastSlash + 1);
  135. }
  136. const response = await fetch(url);
  137. if (!response.ok) {
  138. throw new Error(response.statusText);
  139. }
  140. processLog(await response.text());
  141. }
  142. function fileEntryChanged() {
  143. showPhase("loading");
  144. const input = ID("fileEntry");
  145. const files = input.files;
  146. if (files.length > 0) {
  147. // Only handle the first file; don't handle multiple selection.
  148. // The parts of the log we care about are ASCII-only. Since we
  149. // can ignore lines we don't care about, best to read in as
  150. // ISO-8859-1, which guarantees we don't get decoding errors.
  151. const fileReader = new FileReader();
  152. fileReader.onload = function (e) {
  153. const log = e.target.result;
  154. if (log) {
  155. processLog(log);
  156. } else {
  157. showPhase("entry");
  158. }
  159. };
  160. fileReader.readAsText(files[0], "iso-8859-1");
  161. }
  162. // So the user can process the same filename again (after
  163. // overwriting the log), clear the value on the form input so we
  164. // will always get an onchange event.
  165. input.value = "";
  166. }
  167. function logPasted() {
  168. showPhase("loading");
  169. const entry = ID("logEntry");
  170. const log = entry.value;
  171. entry.value = "";
  172. processLog(log);
  173. }
  174. const gTestItems = [];
  175. function processLog(contents) {
  176. const lines = contents.split(/[\r\n]+/);
  177. for (const j in lines) {
  178. let line = lines[j];
  179. let match = line.match(/^(?:NEXT ERROR )?REFTEST (.*)$/);
  180. if (!match) {
  181. continue;
  182. }
  183. line = match[1];
  184. match = line.match(
  185. /^(TEST-PASS|TEST-UNEXPECTED-PASS|TEST-KNOWN-FAIL|TEST-UNEXPECTED-FAIL)(\(EXPECTED RANDOM\)|) \| ([^|]+) \|(.*)/
  186. );
  187. if (match) {
  188. const state = match[1];
  189. const random = match[2];
  190. const url = match[3];
  191. const extra = match[4];
  192. gTestItems.push({
  193. pass: !state.endsWith("FAIL"),
  194. // only one of the following three should ever be true
  195. unexpected: state.startsWith("TEST-UNEXPECTED"),
  196. random: random === "(EXPECTED RANDOM)",
  197. skip: extra === " (SKIP)",
  198. url,
  199. images: [],
  200. });
  201. continue;
  202. }
  203. match = line.match(
  204. /^ {2}IMAGE[^:]*\((\d+\.?\d*)x(\d+\.?\d*)x(\d+\.?\d*)\): (.*)$/
  205. );
  206. if (match) {
  207. const item = gTestItems.at(-1);
  208. item.images.push({
  209. width: parseFloat(match[1]),
  210. height: parseFloat(match[2]),
  211. outputScale: parseFloat(match[3]),
  212. file: match[4],
  213. });
  214. }
  215. }
  216. buildViewer();
  217. }
  218. function buildViewer() {
  219. if (gTestItems.length === 0) {
  220. showPhase("entry");
  221. return;
  222. }
  223. // const cell = ID("itemlist");
  224. const table = document.getElementById("itemtable");
  225. table.textContent = ""; // Remove any table contents from the DOM.
  226. const tbody = document.createElement("tbody");
  227. table.append(tbody);
  228. for (const i in gTestItems) {
  229. const item = gTestItems[i];
  230. if (item.pass && !item.unexpected) {
  231. continue;
  232. }
  233. const tr = document.createElement("tr");
  234. let rowclass = item.pass ? "pass" : "fail";
  235. let td = document.createElement("td");
  236. let text = "";
  237. if (item.unexpected) {
  238. text += "!";
  239. rowclass += " unexpected";
  240. }
  241. if (item.random) {
  242. text += "R";
  243. rowclass += " random";
  244. }
  245. if (item.skip) {
  246. text += "S";
  247. rowclass += " skip";
  248. }
  249. td.append(document.createTextNode(text));
  250. tr.append(td);
  251. td = document.createElement("td");
  252. td.id = "url" + i;
  253. td.className = "url";
  254. const match = item.url.match(/\/mozilla\/(.*)/);
  255. text = document.createTextNode(match ? match[1] : item.url);
  256. if (item.images.length > 0) {
  257. const a = document.createElement("a");
  258. a.id = i;
  259. a.className = "image";
  260. a.href = "#";
  261. a.append(text);
  262. td.append(a);
  263. } else {
  264. td.append(text);
  265. }
  266. tr.append(td);
  267. tr.className = rowclass;
  268. tbody.append(tr);
  269. }
  270. // Bind an event handler to each image link
  271. const images = document.getElementsByClassName("image");
  272. for (const image of images) {
  273. image.addEventListener(
  274. "click",
  275. function (e) {
  276. showImages(e.target.id);
  277. },
  278. false
  279. );
  280. }
  281. showPhase("viewer");
  282. }
  283. function getImageData(src, whenReady) {
  284. const img = new Image();
  285. img.onload = function () {
  286. const canvas = document.createElement("canvas");
  287. canvas.width = img.naturalWidth;
  288. canvas.height = img.naturalHeight;
  289. const ctx = canvas.getContext("2d");
  290. ctx.drawImage(img, 0, 0);
  291. whenReady(ctx.getImageData(0, 0, img.naturalWidth, img.naturalHeight));
  292. };
  293. img.src = gPath + src;
  294. }
  295. function showImages(i) {
  296. if (gSelected !== null) {
  297. ID("url" + gSelected).classList.remove("selected");
  298. }
  299. gSelected = i;
  300. ID("url" + gSelected).classList.add("selected");
  301. ID("url" + gSelected).scrollIntoView();
  302. const item = gTestItems[i];
  303. const cell = ID("images");
  304. ID("image1").style.display = "";
  305. const scale = item.images[0].outputScale / window.devicePixelRatio;
  306. ID("image1").setAttribute("width", item.images[0].width * scale);
  307. ID("image1").setAttribute("height", item.images[0].height * scale);
  308. ID("svg").setAttribute("width", item.images[0].width * scale);
  309. ID("svg").setAttribute("height", item.images[0].height * scale);
  310. ID("image2").style.display = "none";
  311. if (item.images[1]) {
  312. ID("image2").setAttribute("width", item.images[1].width * scale);
  313. ID("image2").setAttribute("height", item.images[1].height * scale);
  314. }
  315. ID("diffrect").style.display = "none";
  316. ID("imgcontrols").reset();
  317. ID("image1").setAttributeNS(
  318. XLINK_NS,
  319. "xlink:href",
  320. gPath + item.images[0].file
  321. );
  322. // Making the href be #image1 doesn't seem to work
  323. ID("feimage1").setAttributeNS(
  324. XLINK_NS,
  325. "xlink:href",
  326. gPath + item.images[0].file
  327. );
  328. if (item.images.length === 1) {
  329. ID("imgcontrols").style.display = "none";
  330. } else {
  331. ID("imgcontrols").style.display = "";
  332. ID("image2").setAttributeNS(
  333. XLINK_NS,
  334. "xlink:href",
  335. gPath + item.images[1].file
  336. );
  337. // Making the href be #image2 doesn't seem to work
  338. ID("feimage2").setAttributeNS(
  339. XLINK_NS,
  340. "xlink:href",
  341. gPath + item.images[1].file
  342. );
  343. }
  344. cell.style.display = "";
  345. getImageData(item.images[0].file, function (data) {
  346. gImage1Data = data;
  347. });
  348. getImageData(item.images[1].file, function (data) {
  349. gImage2Data = data;
  350. });
  351. }
  352. function showImage(i) {
  353. if (i === 1) {
  354. ID("image1").style.display = "";
  355. ID("image2").style.display = "none";
  356. } else {
  357. ID("image1").style.display = "none";
  358. ID("image2").style.display = "";
  359. }
  360. }
  361. function showDifferences(cb) {
  362. ID("diffrect").style.display = cb.checked ? "" : "none";
  363. }
  364. function flashPixels(on) {
  365. const stroke = on ? "#FF0000" : "#CCC";
  366. const strokeWidth = on ? "2px" : "1px";
  367. for (const pixel of gFlashingPixels) {
  368. pixel.setAttribute("stroke", stroke);
  369. pixel.setAttribute("stroke-width", strokeWidth);
  370. }
  371. }
  372. function cursorPoint(evt) {
  373. const m = evt.target.getScreenCTM().inverse();
  374. let p = ID("svg").createSVGPoint();
  375. p.x = evt.clientX;
  376. p.y = evt.clientY;
  377. p = p.matrixTransform(m);
  378. return { x: Math.floor(p.x), y: Math.floor(p.y) };
  379. }
  380. function hex2(i) {
  381. return (i < 16 ? "0" : "") + i.toString(16);
  382. }
  383. function canvasPixelAsHex(data, x, y) {
  384. const offset = (y * data.width + x) * 4 * window.devicePixelRatio;
  385. const r = data.data[offset];
  386. const g = data.data[offset + 1];
  387. const b = data.data[offset + 2];
  388. return "#" + hex2(r) + hex2(g) + hex2(b);
  389. }
  390. function hexAsRgb(hex) {
  391. return (
  392. "rgb(" +
  393. [
  394. parseInt(hex.substring(1, 3), 16),
  395. parseInt(hex.substring(3, 5), 16),
  396. parseInt(hex.substring(5, 7), 16),
  397. ] +
  398. ")"
  399. );
  400. }
  401. function magnify(evt) {
  402. const cursor = cursorPoint(evt);
  403. const x = cursor.x;
  404. const y = cursor.y;
  405. let centerPixelColor1, centerPixelColor2;
  406. const dx_lo = -Math.floor(gMagWidth / 2);
  407. const dx_hi = Math.floor(gMagWidth / 2);
  408. const dy_lo = -Math.floor(gMagHeight / 2);
  409. const dy_hi = Math.floor(gMagHeight / 2);
  410. flashPixels(false);
  411. gFlashingPixels.length = 0;
  412. for (let j = dy_lo; j <= dy_hi; j++) {
  413. for (let i = dx_lo; i <= dx_hi; i++) {
  414. const px = x + i;
  415. const py = y + j;
  416. const p1 = gMagPixPaths[i + dx_hi][j + dy_hi][0];
  417. const p2 = gMagPixPaths[i + dx_hi][j + dy_hi][1];
  418. if (
  419. px < 0 ||
  420. py < 0 ||
  421. px >= gImage1Data.width ||
  422. py >= gImage1Data.height
  423. ) {
  424. p1.setAttribute("fill", "#aaa");
  425. p2.setAttribute("fill", "#888");
  426. } else {
  427. const color1 = canvasPixelAsHex(gImage1Data, x + i, y + j);
  428. const color2 = canvasPixelAsHex(gImage2Data, x + i, y + j);
  429. p1.setAttribute("fill", color1);
  430. p2.setAttribute("fill", color2);
  431. if (color1 !== color2) {
  432. gFlashingPixels.push(p1, p2);
  433. p1.parentNode.append(p1);
  434. p2.parentNode.append(p2);
  435. }
  436. if (i === 0 && j === 0) {
  437. centerPixelColor1 = color1;
  438. centerPixelColor2 = color2;
  439. }
  440. }
  441. }
  442. }
  443. flashPixels(true);
  444. showPixelInfo(
  445. x,
  446. y,
  447. centerPixelColor1,
  448. hexAsRgb(centerPixelColor1),
  449. centerPixelColor2,
  450. hexAsRgb(centerPixelColor2)
  451. );
  452. }
  453. function showPixelInfo(x, y, pix1rgb, pix1hex, pix2rgb, pix2hex) {
  454. // const pixelinfo = ID("pixelinfo");
  455. ID("coords").textContent = [x, y];
  456. ID("pix1hex").textContent = pix1hex;
  457. ID("pix1rgb").textContent = pix1rgb;
  458. ID("pix2hex").textContent = pix2hex;
  459. ID("pix2rgb").textContent = pix2rgb;
  460. }
  461. const logPastedButton = document.getElementById("logPasted");
  462. logPastedButton.addEventListener("click", logPasted, false);
  463. const fileEntryButton = document.getElementById("fileEntry");
  464. fileEntryButton.addEventListener("change", fileEntryChanged, false);
  465. const testImage = document.getElementById("testImage");
  466. testImage.addEventListener(
  467. "click",
  468. function () {
  469. showImage(1);
  470. },
  471. false
  472. );
  473. const referenceImage = document.getElementById("referenceImage");
  474. referenceImage.addEventListener(
  475. "click",
  476. function () {
  477. showImage(2);
  478. },
  479. false
  480. );
  481. const differences = document.getElementById("differences");
  482. differences.addEventListener(
  483. "click",
  484. function (e) {
  485. showDifferences(e.target);
  486. },
  487. false
  488. );
  489. const magnifyElement = document.getElementById("magnify");
  490. magnifyElement.addEventListener(
  491. "mousemove",
  492. function (e) {
  493. magnify(e);
  494. },
  495. false
  496. );
  497. window.addEventListener("keydown", function keydown(event) {
  498. if (event.which === 84) {
  499. // 't' switch test/ref images
  500. let val = 0;
  501. if (document.querySelector('input[name="which"][value="0"]:checked')) {
  502. val = 1;
  503. }
  504. document
  505. .querySelector('input[name="which"][value="' + val + '"]')
  506. .click();
  507. } else if (event.which === 68) {
  508. // 'd' toggle differences
  509. document.getElementById("differences").click();
  510. } else if (event.which === 78 || event.which === 80) {
  511. // 'n' next image, 'p' previous image
  512. let select = gSelected;
  513. if (gSelected === null) {
  514. select = 0;
  515. } else if (event.which === 78) {
  516. select++;
  517. } else {
  518. select--;
  519. }
  520. const length = gTestItems.length;
  521. if (select < 0) {
  522. select = length - 1;
  523. } else if (select >= length) {
  524. select = 0;
  525. }
  526. showImages(select);
  527. }
  528. });
  529. };