Changeset 29787 for other-projects/nz-flag-design/trunk/similarity-2d
- Timestamp:
- 2015-03-17T00:27:22+13:00 (9 years ago)
- Location:
- other-projects/nz-flag-design/trunk/similarity-2d
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
other-projects/nz-flag-design/trunk/similarity-2d/flag-processing.js
r29757 r29787 5 5 var show_progress = 5; 6 6 7 var start_hist_delay = 4000;7 var start_hist_delay = 1000; // used to be 4000 8 8 var mini_delay = 300; 9 9 10 10 11 var hasLocalStorage = false; 12 var needToComputeHistograms = true; 11 var hasLocalStorage = (typeof(Storage) !== "undefined"); // **** 12 //var hasLocalStorage = false; 13 13 14 var quantHSLHistogramArray; 15 16 //var saveOnServer=true; 17 var saveOnServer=false; 18 var retrieveFromServer=true; 19 20 21 function runlength_encode(int_array) { 22 23 var array_len = int_array.length; 24 25 if (array_len<2) { 26 console.warn("runlength_encode(): unable to run-length encode array of length " + array_len); 27 console.warn("returning null"); 28 return null; 29 } 30 31 var int_array_rle = new Array(); 32 33 34 var lock_val=int_array[0]; 35 var next_val=int_array[1]; 36 var i = 1; 37 38 while (i<array_len) { 39 var c=1; 40 while (next_val==lock_val) { 41 c++; 42 var next_pos = i+c-1; 43 if (next_pos<array_len) { 44 next_val = int_array[next_pos]; 45 } 46 else { 47 // hit end of array 48 break; 49 } 50 } 51 // come out of a run-length sequence of same val 52 // next_val is one past the run-length sequence 53 54 int_array_rle.push(c); 55 int_array_rle.push(lock_val); 56 57 i+=c; 58 59 if (i<array_len) { 60 lock_val = next_val; 61 next_val = int_array[i]; 62 } 63 else { 64 break; 65 } 66 } 67 68 return int_array_rle; 69 } 70 71 72 73 function runlength_decode(int_array_rle) { 74 75 var array_rle_len = int_array_rle.length; 76 77 if (array_rle_len<2) { 78 console.warn("runlength_decode(): unable to run-length decode array of length " + array_rle_len); 79 console.warn("returning null"); 80 return null; 81 } 82 83 var int_array = new Array(); 84 for (var i=0; i<array_rle_len; i+=2){ 85 var rep = int_array_rle[i]; 86 var val = int_array_rle[i+1]; 87 for (var r=0; r<rep; r++) { 88 int_array.push(val); 89 } 90 } 91 92 return int_array; 93 } 94 95 96 97 function arrays_identical(int_array1, int_array2) { 98 99 var array_len1 = int_array1.length; 100 var array_len2 = int_array2.length; 101 102 if (array_len1 != array_len2) { 103 console.warn("arrays_identical(): array lengths differ, array1("+array_len1+") vs array2(" + array_len2 + ")"); 104 return false; 105 } 106 107 var all_ident = true; 108 109 for (var i=0; i<array_len1; i++){ 110 if (int_array1[i] != int_array2[i]) { 111 all_ident = false; 112 break; 113 } 114 } 115 116 return all_ident; 117 } 14 118 15 119 … … 61 165 var y,p; 62 166 for (y=0,p=0; y<imageObj.height; y++) { 63 // setTimeout(function() {64 167 var x; 65 168 for (x=0; x<imageObj.width; x++,p+=4) { … … 87 190 outputArray[result] += count; 88 191 } 89 // },4);90 192 } 91 193 … … 94 196 outputArray[i] = outputArray[i] / total; 95 197 } 198 199 /* 200 if (saveOnServer) { 201 console.log("Saving HSV quantized histogram on server: " + imageObj.title) 202 //console.log("outputArray = " + JSON.stringify(outputArray)); 203 204 var jsonFilename = imageObj.title + ".json"; 205 var data = { jsonFilename: jsonFilename, jsonData: JSON.stringify(outputArray)}; 206 207 console.log("Saving colour histogram data for " + imageObj.title); 208 $.ajax({ 209 type: "POST", 210 url: "../similarity-2d/save-json-data.jsp", 211 data: data 212 }); 213 } 214 */ 96 215 97 216 return outputArray; … … 182 301 */ 183 302 184 185 186 303 imageObj.onclick = function(e) { 187 304 // Store result of HSL quantization in the image object/tag … … 231 348 // Only need to compute this if it is not 232 349 // already stored in the object/tag 233 imageObj.quantHSLHist = createHSLHistogram(imageObj,16); 234 //console.log(imageObj.quantHSLHist); 350 351 if (retrieveFromServer) { 352 // see if it is stored on the server 353 console.log("Retrieving stored colour histogram for " + imageObj.title + " ..."); 354 $.ajax({ 355 async: false, 356 dataType: "json", 357 url: "../similarity-2d/json-data/" + imageObj.title + ".json", 358 success: function(data) { 359 console.log("... success"); 360 // 'data' is the array of integer vals we want 361 imageObj.quantHSLHist = data; 362 }, 363 error: function() { 364 console.log("... not stored on server"); 365 // not stored on the server (currently) 366 // => need to compute it 367 imageObj.quantHSLHist = createHSLHistogram(imageObj,16); 368 //console.log(imageObj.quantHSLHist); 369 370 } 371 }); 372 } 373 else { 374 imageObj.quantHSLHist = createHSLHistogram(imageObj,16); 375 } 376 235 377 } 236 378 … … 294 436 function doneComputingHistograms(img_list_len) 295 437 { 438 quantHSLHistogramArray = new Array(img_list_len); 439 for (var i=0; i<img_list_len; i++) { 440 var flag_id = "flag-img-" + i; 441 var imageObj = document.getElementById(flag_id); 442 quantHSLHistogramArray[i] = imageObj.quantHSLHist; 443 } 444 var json_hist_data = JSON.stringify(quantHSLHistogramArray); 445 296 446 if (hasLocalStorage) { 297 447 // build up quantHSLHistogramArray from all the images 298 448 299 quantHSLHistogramArray = new Array(img_list_len);300 var i;301 for (i=0; i<img_list_len; i++) {302 var flag_id = "flag-img-" + i;303 var imageObj = document.getElementById(flag_id);304 quantHSLHistogramArray[i] = imageObj.quantHSLHist;305 }306 307 449 console.log("Storing computed HSL histograms in localStorage"); 308 localStorage.quantHSLHistogramArray = JSON.stringify(quantHSLHistogramArray); 450 localStorage.quantHSLHistogramArray = json_hist_data; 451 } 452 453 if (saveOnServer) { 454 // run-length encode data, so less to send 455 var quantHSLHistogramArrayRLE = new Array(img_list_len); 456 for (var i=0; i<img_list_len; i++) { 457 //console.log("rle encoding ... i = " + i); 458 quantHSLHistogramArrayRLE[i] = runlength_encode(quantHSLHistogramArray[i]) 459 460 //var test_decode_array = runlength_decode(quantHSLHistogramArrayRLE[i]); 461 //console.log("Checking decoded array: " + arrays_identical(test_decode_array,quantHSLHistogramArray[i])); 462 } 463 464 465 var json_hist_data_rle = JSON.stringify(quantHSLHistogramArrayRLE); 466 467 var json_filename = "all-hsv-histograms-rle.json"; 468 var data = { jsonFilename: json_filename, jsonData: json_hist_data_rle}; 469 470 console.log("Saving colour histogram data for all flags"); 471 $.ajax({ 472 type: "POST", 473 url: "../similarity-2d/save-json-data.jsp", 474 data: data 475 }); 309 476 } 310 477 } … … 312 479 function doneDisplayFlags(img_list,$progressArea,$progressBar) 313 480 { 314 hasLocalStorage = false;315 needToComputeHistograms = true; 316 317 if (typeof(Storage) !== "undefined") { 318 hasLocalStorage = true; 481 console.log("doneDisplayFlags()"); 482 483 var needToComputeHistograms = true; 484 485 if (hasLocalStorage) { 319 486 320 487 if (localStorage.quantHSLHistogramArray) { 321 488 322 console.log("Retrieving quantized HSL histograms ");489 console.log("Retrieving quantized HSL histograms from local storage"); 323 490 324 491 var stored_hist_array = JSON.parse(localStorage["quantHSLHistogramArray"]); … … 336 503 } 337 504 338 339 505 needToComputeHistograms = false; 340 506 } … … 345 511 } 346 512 347 348 513 if (needToComputeHistograms) { 349 computeFlagHistograms(img_list,$progressArea,$progressBar); 514 515 if (retrieveFromServer) { 516 // before computing histograms from scratch, see if resulting data stored on server 517 518 console.log("Retrieving stored colour histgram data for all flags ..."); 519 $.ajax({ 520 async: true, 521 dataType: "json", 522 url: "../similarity-2d/json-data/all-hsv-histograms-rle.json", 523 success: function(rle_data) { 524 console.log("... success"); 525 if (rle_data.length != img_list.length) { 526 console.log("Stored histogram data out of date with flag list."); 527 // what is stored on the sever differed to the number of images displayed 528 computeFlagHistograms(img_list,$progressArea,$progressBar); 529 } 530 else { 531 var img_list_len = img_list.length; 532 quantHSLHistogramArray = new Array(img_list_len); 533 534 // Need to rle decode each entry in this array 535 for (var i=0; i<img_list_len; i++) { 536 quantHSLHistogramArray[i] = runlength_decode(rle_data[i]); 537 var flag_id = "flag-img-" + i; 538 var imageObj = document.getElementById(flag_id); 539 imageObj.quantHSLHist = quantHSLHistogramArray[i]; 540 541 } 542 543 needToComputeHistograms = false; 544 545 $progressArea.slideUp(); 546 doneComputingHistograms(img_list.length); 547 548 } 549 }, 550 error: function() { 551 console.log("... not stored on server"); 552 computeFlagHistograms(img_list,$progressArea,$progressBar); 553 } 554 }); 555 556 557 } 558 else { 559 computeFlagHistograms(img_list,$progressArea,$progressBar); 560 } 350 561 } 351 562 else { -
other-projects/nz-flag-design/trunk/similarity-2d/list-flags.jsp
r29621 r29787 2 2 String docBase = getServletContext().getRealPath("/"); 3 3 File similarityDir = new File(docBase,"similarity-2d"); 4 File imagesDir = new File(similarityDir,"Images"); 4 5 String imagesStr = (request.getParameter("imagesDir")!=null) ? request.getParameter("imagesDir") : "import"; 6 File imagesDir = new File(similarityDir,imagesStr); 5 7 6 8 ArrayList<File> imageFileList = listFilesForFolder(imagesDir,".gif"); 7 9 8 File imageUrlBase = new File( "Images");10 File imageUrlBase = new File(imagesStr); 9 11 10 String action = request.getParameter("action");12 //String action = request.getParameter("action"); 11 13 12 14 out.println("<script>");
Note:
See TracChangeset
for help on using the changeset viewer.