blob: 06a864edf4e219ade95c0c322cc14d935e3141b5 [file] [log] [blame]
zachr@google.com6f8e2c52013-08-07 15:43:04 +00001var MAX_SWAP_IMG_SIZE = 400;
djsollen@google.com513a7bf2013-11-07 19:24:06 +00002var MAGNIFIER_WIDTH = 200;
3var MAGNIFIER_HEIGHT = 200;
4var MAGNIFIER_HALF_WIDTH = MAGNIFIER_WIDTH * 0.5;
5var MAGNIFIER_HALF_HEIGHT = MAGNIFIER_HEIGHT * 0.5;
6// TODO add support for a magnified scale factor
7var MAGNIFIER_SCALE_FACTOR = 2.0;
zachr@google.com6f8e2c52013-08-07 15:43:04 +00008
zachr@google.com1bc995e2013-07-12 13:39:53 +00009angular.module('diff_viewer', []).
djsollen@google.com513a7bf2013-11-07 19:24:06 +000010directive('imgCompare', function() {
11 // Custom directive for comparing (3-way) images
12 return {
13 restrict: 'E', // The directive can be used as an element name
14 replace: true, // The directive replaces itself with the template
15 template: '<canvas/>',
16 scope: true,
17 link: function(scope, elm, attrs, ctrl) {
18 var image = new Image();
19 var canvas = elm[0];
20 var ctx = canvas.getContext('2d');
zachr@google.com1bc995e2013-07-12 13:39:53 +000021
djsollen@google.com513a7bf2013-11-07 19:24:06 +000022 var magnifyContent = false;
zachr@google.com1bc995e2013-07-12 13:39:53 +000023
djsollen@google.com513a7bf2013-11-07 19:24:06 +000024 // When the type attribute changes, load the image and then render
25 attrs.$observe('type', function(value) {
26 switch(value) {
27 case "alphaMask":
28 image.src = scope.record.differencePath;
29 break;
30 case "baseline":
31 image.src = scope.record.baselinePath;
32 magnifyContent = true;
33 break;
34 case "test":
35 image.src = scope.record.testPath;
36 magnifyContent = true;
37 break;
38 default:
39 console.log("Unknown type attribute on <img-compare>: " + value);
40 return;
41 }
zachr@google.com1bc995e2013-07-12 13:39:53 +000042
djsollen@google.com513a7bf2013-11-07 19:24:06 +000043 image.onload = function() {
44 // compute the scaled image width/height for image and canvas
45 var divisor = 1;
46 // Make it so the maximum size of an image is MAX_SWAP_IMG_SIZE,
47 // and the images are scaled down in halves.
48 while ((image.width / divisor) > MAX_SWAP_IMG_SIZE) {
49 divisor *= 2;
50 }
51
52 scope.setImgScaleFactor(1 / divisor);
53
54 // Set canvas to correct size
55 canvas.width = image.width * scope.imgScaleFactor;
56 canvas.height = image.height * scope.imgScaleFactor;
57
58 // render the image onto the canvas
59 scope.renderImage();
60 }
61 });
62
63 // When the magnify attribute changes, render the magnified rect at
64 // the default zoom level.
65 scope.$watch('magnifyCenter', function(magCenter) {
66 if (!magnifyContent) {
67 return;
68 }
69
70 scope.renderImage();
71
72 if (!magCenter) {
73 return;
74 }
75
76 var magX = magCenter.x - MAGNIFIER_HALF_WIDTH;
77 var magY = magCenter.y - MAGNIFIER_HALF_HEIGHT;
78
79 var magMaxX = canvas.width - MAGNIFIER_WIDTH;
80 var magMaxY = canvas.height - MAGNIFIER_HEIGHT;
81
82 var magRect = { x: Math.max(0, Math.min(magX, magMaxX)),
83 y: Math.max(0, Math.min(magY, magMaxY)),
84 width: MAGNIFIER_WIDTH,
85 height: MAGNIFIER_HEIGHT
86 };
87
88 var imgRect = { x: (magCenter.x / scope.imgScaleFactor) - MAGNIFIER_HALF_WIDTH,
89 y: (magCenter.y / scope.imgScaleFactor) - MAGNIFIER_HALF_HEIGHT,
90 width: MAGNIFIER_WIDTH,
91 height: MAGNIFIER_HEIGHT
92 };
93
94 // draw the magnified image
95 ctx.clearRect(magRect.x, magRect.y, magRect.width, magRect.height);
96 ctx.drawImage(image, imgRect.x, imgRect.y, imgRect.width, imgRect.height,
97 magRect.x, magRect.y, magRect.width, magRect.height);
98
99 // draw the outline rect
100 ctx.beginPath();
101 ctx.rect(magRect.x, magRect.y, magRect.width, magRect.height);
102 ctx.lineWidth = 2;
103 ctx.strokeStyle = 'red';
104 ctx.stroke();
105
106 });
107
108 // render the image to the canvas. This is often done every frame prior
109 // to any special effects (i.e. magnification).
110 scope.renderImage = function() {
111 ctx.clearRect(0, 0, canvas.width, canvas.height);
112 ctx.drawImage(image, 0, 0, canvas.width, canvas.height);
113 };
114
115 // compute a rect (x,y,width,height) that represents the bounding box for
116 // the magnification effect
117 scope.computeMagnifierOutline = function(event) {
118 var scaledWidth = MAGNIFIER_WIDTH * scope.imgScaleFactor;
119 var scaledHeight = MAGNIFIER_HEIGHT * scope.imgScaleFactor;
120 return {
121 x: event.offsetX - (scaledWidth * 0.5),
122 y: event.offsetY - (scaledHeight * 0.5),
123 width: scaledWidth,
124 height: scaledHeight
zachr@google.com1bc995e2013-07-12 13:39:53 +0000125 };
djsollen@google.com513a7bf2013-11-07 19:24:06 +0000126 };
zachr@google.com1bc995e2013-07-12 13:39:53 +0000127
djsollen@google.com513a7bf2013-11-07 19:24:06 +0000128 // event handler for mouse events that triggers the magnification
129 // effect across the 3 images being compared.
130 scope.MagnifyDraw = function(event, startMagnify) {
131 if (startMagnify) {
132 scope.setMagnifierState(true);
133 } else if (!scope.magnifierOn) {
134 return;
135 }
zachr@google.com1bc995e2013-07-12 13:39:53 +0000136
djsollen@google.com513a7bf2013-11-07 19:24:06 +0000137 scope.renderImage();
zachr@google.com1bc995e2013-07-12 13:39:53 +0000138
djsollen@google.com513a7bf2013-11-07 19:24:06 +0000139 // render the magnifier outline rect
140 var rect = scope.computeMagnifierOutline(event);
141 ctx.save();
142 ctx.beginPath();
143 ctx.rect(rect.x, rect.y, rect.width, rect.height);
144 ctx.lineWidth = 2;
145 ctx.strokeStyle = 'red';
146 ctx.stroke();
147 ctx.restore();
148
149 // update scope on baseline / test that will cause them to render
150 scope.setMagnifyCenter({x: event.offsetX, y: event.offsetY});
151 };
152
153 // event handler that triggers the end of the magnification effect and
154 // resets all the canvases to their original state.
155 scope.MagnifyEnd = function(event) {
156 scope.renderImage();
157 // update scope on baseline / test that will cause them to render
158 scope.setMagnifierState(false);
159 scope.setMagnifyCenter(undefined);
160 };
161 }
162 };
zachr@google.com1bc995e2013-07-12 13:39:53 +0000163});
164
djsollen@google.com513a7bf2013-11-07 19:24:06 +0000165function ImageController($scope, $http, $location, $timeout, $parse) {
166 $scope.imgScaleFactor = 1.0;
167 $scope.magnifierOn = false;
168 $scope.magnifyCenter = undefined;
169
170 $scope.setImgScaleFactor = function(scaleFactor) {
171 $scope.imgScaleFactor = scaleFactor;
172 }
173
174 $scope.setMagnifierState = function(magnifierOn) {
175 $scope.magnifierOn = magnifierOn;
176 }
177
178 $scope.setMagnifyCenter = function(magnifyCenter) {
179 $scope.magnifyCenter = magnifyCenter;
180 }
181}
182
zachr@google.com74c5ab12013-08-07 18:06:39 +0000183function DiffListController($scope, $http, $location, $timeout, $parse) {
184 // Detect if we are running the web server version of the viewer. If so, we set a flag and
185 // enable some extra functionality of the website for rebaselining.
186 $scope.isDynamic = ($location.protocol() == "http" || $location.protocol() == "https");
187
zachr@google.com1bc995e2013-07-12 13:39:53 +0000188 // Label each kind of differ for the sort buttons.
zachr@google.com9432c0c2013-07-09 21:08:28 +0000189 $scope.differs = [
190 {
191 "title": "Different Pixels"
192 },
193 {
194 "title": "Perceptual Difference"
195 }
196 ];
zachr@google.com1bc995e2013-07-12 13:39:53 +0000197
198 // Puts the records within AngularJS scope
zachr@google.com9432c0c2013-07-09 21:08:28 +0000199 $scope.records = SkPDiffRecords.records;
zachr@google.com1bc995e2013-07-12 13:39:53 +0000200
zachr@google.com74c5ab12013-08-07 18:06:39 +0000201 // Keep track of the index of the last record to change so that shift clicking knows what range
202 // of records to apply the action to.
203 $scope.lastSelectedIndex = undefined;
204
zachr@google.com1bc995e2013-07-12 13:39:53 +0000205 // Indicates which diff metric is used for sorting
206 $scope.sortIndex = 1;
207
208 // Called by the sort buttons to adjust the metric used for sorting
209 $scope.setSortIndex = function(idx) {
210 $scope.sortIndex = idx;
zachr@google.com74c5ab12013-08-07 18:06:39 +0000211
212 // Because the index of things has most likely changed, the ranges of shift clicking no
213 // longer make sense from the user's point of view. We reset it to avoid confusion.
214 $scope.lastSelectedIndex = undefined;
zachr@google.com1bc995e2013-07-12 13:39:53 +0000215 };
216
217 // A predicate for pulling out the number used for sorting
218 $scope.sortingDiffer = function(record) {
219 return record.diffs[$scope.sortIndex].result;
220 };
zachr@google.com6f8e2c52013-08-07 15:43:04 +0000221
zachr@google.com74c5ab12013-08-07 18:06:39 +0000222 // Flash status indicator on the page, and then remove it so the style can potentially be
zachr@google.com6f8e2c52013-08-07 15:43:04 +0000223 // reapplied later.
zachr@google.com74c5ab12013-08-07 18:06:39 +0000224 $scope.flashStatus = function(success) {
zachr@google.com6f8e2c52013-08-07 15:43:04 +0000225 var flashStyle = success ? "success-flash" : "failure-flash";
226 var flashDurationMillis = success ? 500 : 800;
227
228 // Store the style in the record. The row will pick up the style this way instead of through
229 // index because index can change with sort order.
zachr@google.com74c5ab12013-08-07 18:06:39 +0000230 $scope.statusClass = flashStyle;
zachr@google.com6f8e2c52013-08-07 15:43:04 +0000231
232 // The animation cannot be repeated unless the class is removed the element.
233 $timeout(function() {
zachr@google.com74c5ab12013-08-07 18:06:39 +0000234 $scope.statusClass = "";
zachr@google.com6f8e2c52013-08-07 15:43:04 +0000235 }, flashDurationMillis);
zachr@google.com74c5ab12013-08-07 18:06:39 +0000236 };
zachr@google.com6f8e2c52013-08-07 15:43:04 +0000237
zachr@google.com74c5ab12013-08-07 18:06:39 +0000238 $scope.selectedRebaseline = function(index, event) {
239 // Retrieve the records in the same order they are displayed.
240 var recordsInOrder = $parse("records | orderBy:sortingDiffer")($scope);
241
242 // If the user is shift clicking, apply the last tick/untick to all elements in between this
243 // record, and the last one they ticked/unticked.
244 if (event.shiftKey && $scope.lastSelectedIndex !== undefined) {
245 var currentAction = recordsInOrder[index].isRebaselined;
246 var smallerIndex = Math.min($scope.lastSelectedIndex, index);
247 var largerIndex = Math.max($scope.lastSelectedIndex, index);
248 for (var recordIndex = smallerIndex; recordIndex <= largerIndex; recordIndex++) {
249 recordsInOrder[recordIndex].isRebaselined = currentAction;
250 }
251 $scope.lastSelectedIndex = index;
252 }
253 else
254 {
255 $scope.lastSelectedIndex = index;
256 }
257
258 };
259
260 $scope.commitRebaselines = function() {
261 // Gather up all records that have the rebaseline set.
262 var rebaselines = [];
263 for (var recordIndex = 0; recordIndex < $scope.records.length; recordIndex++) {
264 if ($scope.records[recordIndex].isRebaselined) {
265 rebaselines.push($scope.records[recordIndex].testPath);
266 }
267 }
268 $http.post("/commit_rebaselines", {
269 "rebaselines": rebaselines
zachr@google.com6f8e2c52013-08-07 15:43:04 +0000270 }).success(function(data) {
zachr@google.com74c5ab12013-08-07 18:06:39 +0000271 $scope.flashStatus(data.success);
zachr@google.com6f8e2c52013-08-07 15:43:04 +0000272 }).error(function() {
zachr@google.com74c5ab12013-08-07 18:06:39 +0000273 $scope.flashStatus(false);
zachr@google.com6f8e2c52013-08-07 15:43:04 +0000274 });
zachr@google.com6f8e2c52013-08-07 15:43:04 +0000275 };
zachr@google.com1bc995e2013-07-12 13:39:53 +0000276}