Skip to content

Commit

Permalink
added progressive download option
Browse files Browse the repository at this point in the history
  • Loading branch information
chafey committed Nov 2, 2023
1 parent 769ec97 commit e554c08
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 5 deletions.
69 changes: 68 additions & 1 deletion test/browser/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,14 @@ <h1>HTJ2K/JPH Decoding with WebAssembly (v<span id="version"></span>)</h1>
<option value="../../test/fixtures/j2c/VL1.j2c">VL1</option>
<option value="../../test/fixtures/j2c/38320-4k.j2c">Digital Path 3840x2160</option>
</select>
<label><input id="progressive" type="checkbox" value="">Progressive</label>
<button id="benchmark">Benchmark</button>
<span>Status:</span><span id="status"></span>
</div>
<div class="row">
<label><input id="visualizeDeltas" type="checkbox" value="">Visualize Deltas</label>
</div>

</div>
<div class="row">
<div class="col-md-12">
Expand Down Expand Up @@ -198,6 +200,8 @@ <h1>HTJ2K/JPH Decoding with WebAssembly (v<span id="version"></span>)</h1>
let setTilePartDivisionsAtComponents = false;
let setTilePartDivisionsAtResolutions = false;
let blockDimensions = 64;
let progressive = 0;
let urlPath = '';

var makeCRCTable = function(){
var c;
Expand Down Expand Up @@ -506,8 +510,19 @@ <h1>HTJ2K/JPH Decoding with WebAssembly (v<span id="version"></span>)</h1>
$('#encodeTime').text((timePerFrame).toFixed(2) + ' ms (' + mps.toFixed(2) + " MP/s; " + fps.toFixed(0) + " fps)");
}

$('#progressive').change(function (e) {
progressive = $('#progressive').is(":checked");
reset();
load(urlPath);
})

function load(url) {
fetch(url)
urlPath = url
if(progressive) {
loadProgressive(url)
return;
} else {
fetch(url)
.then((response) => {
return response.arrayBuffer();
})
Expand All @@ -527,8 +542,60 @@ <h1>HTJ2K/JPH Decoding with WebAssembly (v<span id="version"></span>)</h1>
}).catch(function () {
$('#status').text('error loading ' + url);
});
}
}

function concatTypedArrays(a, b) { // a, b TypedArray of same type
var c = new (a.constructor)(a.length + b.length);
c.set(a, 0);
c.set(b, a.length);
return c;
}

async function loadProgressive(url) {

fullEncodedBitStream = new Uint8Array()

try {
const response = await fetch(url)
const reader = response.body.getReader();
while (true) {
const {value, done} = await reader.read();
if (done) break;
fullEncodedBitStream = concatTypedArrays(fullEncodedBitStream, value)
encodedBitStream = new Uint8Array(fullEncodedBitStream.buffer);
$('#encodedBytesRead').text('' + encodedBitStream.length.toLocaleString() + ' bytes');
$('#encodedBytesReadRange').attr('max', encodedBitStream.length - 1);
decode();
uncompressedImageFrame = new Uint8Array(decoder.getDecodedBuffer().length);
uncompressedImageFrame.set(decoder.getDecodedBuffer());
}
}
catch(ex) {
$('#status').text('Exception thrown ' + ex);
}


/*
.then((arrayBuffer) => {
try {
fullEncodedBitStream = new Uint8Array(arrayBuffer);
encodedBitStream = new Uint8Array(arrayBuffer);
$('#encodedBytesRead').text('' + encodedBitStream.length.toLocaleString() + ' bytes');
$('#encodedBytesReadRange').attr('max', encodedBitStream.length - 1);
decode();
uncompressedImageFrame = new Uint8Array(decoder.getDecodedBuffer().length);
uncompressedImageFrame.set(decoder.getDecodedBuffer());
}
catch (ex) {
$('#status').text('Exception thrown while parsing ' + ex);
}
}).catch(function () {
$('#status').text('error loading ' + url);
});*/
}


function reset() {
decodeLevel = 0;
minMax = undefined;
Expand Down
17 changes: 13 additions & 4 deletions test/cpp/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -84,10 +84,11 @@ void decodeFile(const char *path, size_t iterations = 1)

clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &finish);
sub_timespec(start, finish, &delta);
auto frameInfo = decoder.getFrameInfo();

auto ns = delta.tv_sec * 1000000000.0 + delta.tv_nsec;
auto totalTimeMS = ns / 1000000.0;

auto frameInfo = decoder.getFrameInfo();
auto timePerFrameMS = ns / 1000000.0 / (double)iterations;
auto pixels = (frameInfo.width * frameInfo.height);
auto megaPixels = (double)pixels / (1024.0 * 1024.0);
Expand All @@ -110,7 +111,11 @@ void encodeFile(const char *inPath, const FrameInfo frameInfo, const char *outPa

clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &finish);
sub_timespec(start, finish, &delta);
const double ms = (double)(delta.tv_nsec) / 1000000.0;

auto ns = delta.tv_sec * 1000000000.0 + delta.tv_nsec;
auto totalTimeMS = ns / 1000000.0;

const double ms = totalTimeMS;

printf("Encode of %s took %f ms\n", inPath, ms);

Expand All @@ -123,9 +128,11 @@ void encodeFile(const char *inPath, const FrameInfo frameInfo, const char *outPa

int main(int argc, char **argv)
{
const size_t iterations = (argc > 1) ? atoi(argv[1]) : 10;
const size_t iterations = (argc > 1) ? atoi(argv[1]) : 1;
decodeFile("test/fixtures/j2c/CT1.j2c", iterations);
decodeFile("test/fixtures/j2c/MG1.j2c", iterations);
decodeFile("test/fixtures/j2c/38320-4k.j2c", iterations);

// decodeFile("test/fixtures/j2c/CT2.j2c");
// decodeFile("test/fixtures/j2c/MG1.j2c");
/*
Expand All @@ -152,6 +159,8 @@ int main(int argc, char **argv)
*/

//encodeFile("test/fixtures/raw/CT1.RAW", {.width = 512, .height = 512, .bitsPerSample = 16, .componentCount = 1, .isSigned = true}, "test/fixtures/j2c/CT1.j2c");
encodeFile("test/fixtures/raw/38320-4k.RAW", {.width = 3840, .height = 2160, .bitsPerSample = 8, .componentCount = 3, .isSigned = false, .isUsingColorTransform=true}, "test/fixtures/j2c/38320-4k.j2c");
//encodeFile("test/fixtures/raw/38320-4k.RAW", {.width = 3840, .height = 2160, .bitsPerSample = 8, .componentCount = 3, .isSigned = false, .isUsingColorTransform=true}, "test/fixtures/j2c/38320-4k.j2c");
//encodeFile("../tiffextract/38320.RAW", {.width = 17515, .height = 14440, .bitsPerSample = 8, .componentCount = 3, .isSigned = false, .isUsingColorTransform=true}, "test/fixtures/j2c/38320.j2c");

return 0;
}

0 comments on commit e554c08

Please sign in to comment.